diff --git a/avocado.py b/avocado.py index 2cfa4d5..c7eb467 100644 --- a/avocado.py +++ b/avocado.py @@ -12,7 +12,7 @@ class Avocado: # I'd rather just not return an instance, # but I don't know how to do that :( if random.randint(0,40) != 1: - self.is_falling = False + self.is_still_falling = False self.has_been_pinned = False return None @@ -34,9 +34,13 @@ class Avocado: self.vx = 2 self.vy = 4 - self.is_falling = True + # Avocado state + self.is_still_falling = True self.has_been_pinned = False + # Avocado sounds + self.click = self.loadClick() + def setTargetColor(self, targetColor): self.target = targetColor @@ -62,6 +66,8 @@ class Avocado: if self.rect.left < mousex and self.rect.right > mousex and \ self.rect.top < mousey and self.rect.bottom > mousey: + self.click.play() + if self.color == self.target: self.has_been_pinned = True return True @@ -70,7 +76,12 @@ class Avocado: def exists(self): - return not self.has_been_pinned and self.is_falling + # return not self.has_been_pinned and self.is_still_falling + return self.is_still_falling + + + def isPinned(self): + return self.has_been_pinned def move(self): @@ -88,10 +99,16 @@ class Avocado: def hasLanded(self): if self.rect.bottom > self.screen_height or self.rect.top < 0: - self.is_falling = False + self.is_still_falling = False print('DEBUG :: splatsh!') return True + def loadClick(self, sound=True): + if not sound: + return + return pygame.mixer.Sound("audio/click.wav") + + def destroy(self): del(self) diff --git a/game.py b/game.py index 1c7dd40..29726ec 100755 --- a/game.py +++ b/game.py @@ -30,13 +30,12 @@ class TheGame: self.size = (self.WIDTH, self.HEIGHT) self.screen = pygame.display.set_mode(self.size) self.colors = [BLUE, GREEN, RED, YELLOW] + self.bg = pygame.image.load(os.path.join('img', 'lawyerCrystalBall.png')) # fonts - self.big = pygame.font.Font(None, 90) + self.bigFont = pygame.font.Font(None, 90) - self.pinned = [] - - # Set splashscreen + # Set splashscreen splashScreen = pygame.image.load("img/splashScreen.png") self.screen.blit(pygame.transform.scale(splashScreen, self.size), (0, 0)) pygame.display.flip() @@ -72,27 +71,24 @@ class TheGame: pygame.mixer.music.play() - def fade(self, sound=True): + def fadeSound(self, sound=True): if not sound: return pygame.mixer.music.fadeout(3000) - - def loadClick(self, sound=True): - if not sound: - return - self.click = pygame.mixer.Sound("audio/click.wav") - return self.click + def chooseRandomColor(self): + selected = random.randint(0, 3) + return self.colors[selected] def gameOver(self): screen_width, screen_height = self.size gameOverImage = pygame.image.load("img/gameOver.png") - gameOverText = self.big.render('GAME OVER', 0, YELLOW) - gameOverImage.blit(gameOverText, (screen_width/8, screen_height/6)) + gameOverText = self.bigFont.render('GAME OVER', 0, YELLOW) + gameOverImage.blit(gameOverText, (screen_width/8, screen_height/7)) self.screen.blit(pygame.transform.scale(gameOverImage, self.size), (0, 0)) pygame.display.flip() - self.fade() + self.fadeSound() pygame.time.wait(3000) pygame.quit() sys.exit() @@ -102,26 +98,33 @@ class TheGame: self.pinned.append(avocado) + def drawBackground(self): + if type(self.bg) is tuple: + self.screen.fill(self.bg) + else: + self.screen.blit(pygame.transform.scale(self.bg, self.size), (0, 0)) + + def main(self): clock = pygame.time.Clock() - bg = pygame.image.load("img/lawyerCrystalBall.png") desired_fps = 60 multiplier = 3 - score = 0 time = timeleft = 30 level = 1 levelChange = 0 - reachScore = 200 - avoClick = self.loadClick() + score = 0 + targetScore = 400 - # initialize the HUD class and the lawyers crystal ball + # initialize the HUD class and the lawyer the_hud = hud.Hud(self.screen) crystalBall = crystal.Crystal(self.screen) # Initial color indication color = self.chooseRandomColor() - avocados = [] # We could use this for redrawing only this part + # We could use this list for redrawing only this part + # of the screen install of all of it + avocados = [] running = True while running: @@ -129,24 +132,21 @@ class TheGame: fps = clock.get_fps() screen_width, screen_height = self.size - if type(bg) is tuple: - self.screen.fill(bg) - else: - self.screen.blit(pygame.transform.scale(bg, self.size), (0, 0)) + # Redraw the background and put our lawyer back on top + self.drawBackground() # Next level? - if score >= reachScore: - score = 0 + if score >= targetScore: + self.score = 0 level += 1 levelChange = 35 timeleft = time avocados = [] - self.pinned = [] print('DEBUG :: Level ' + str(level)) - game.playLevel(level) + self.playLevel(level) if levelChange > 0: - levelText = self.big.render('Level ' + str(level), 0, WHITE) + levelText = self.bigFont.render('Level ' + str(level), 0, WHITE) self.screen.blit(levelText, (screen_width / 3, screen_height / 2)) levelChange -= 1 @@ -159,10 +159,10 @@ class TheGame: # Redraw the HUD the_hud.draw_hud(score, displaytime, round(fps, 2)) - crystalBall.setColor(color) - # Initialize a number of avocados, depending on the level + # If we're not currently in between levels… if levelChange == 0: + # Initialize a number of avocados, depending on the level avocados_in_game = len(avocados) avocadosWanted = level * multiplier if avocados_in_game < avocadosWanted: @@ -174,22 +174,23 @@ class TheGame: # Remove avocados from the list if they no longer exist # E.g. have been pinned or fallen down - self.pinned += [avo for avo in avocados if avo.has_been_pinned] avocados[:] = [ x for x in avocados if x.exists() ] for a in avocados: a.setTargetColor(color) - a.move() - a.blitme() - for a in self.pinned: + if not a.isPinned(): + a.move() a.blitme() # Catch events for event in pygame.event.get(): # Collision detection if event.type == MOUSEBUTTONDOWN: - avoClick.play() + mousepos = pygame.mouse.get_pos() + # Throw a pin here + # pin.throwAt(mousepos) + # Yep, above here for avo in avocados: - hit = avo.isHit(pygame.mouse.get_pos()) + hit = avo.isHit(mousepos) if hit: score += 100 color = self.chooseRandomColor() @@ -206,11 +207,6 @@ class TheGame: pygame.display.flip() - def chooseRandomColor(self): - selected = random.randint(0, 3) - return self.colors[selected] - - if __name__ == '__main__': game = TheGame() game.playLevel()