class GameOverScene(Scene): def __init__(self): Scene.__init__(self) self.main_menu_button = Button("Main menu", (0, height / 2 + 110)) self.main_menu_button.x = width / 2 - self.main_menu_button.w / 2 def main_menu_action(): self.go_to(game.MAIN_MENU) self.main_menu_button.mouse_up = main_menu_action self.play_again_button = Button("Play again", (0, height / 2 + 50)) self.play_again_button.x = width / 2 - self.play_again_button.w / 2 def play_again_action(): game.reset() self.go_to(game.GAMEINTRO) game.scene.part = 7 self.play_again_button.mouse_up = play_again_action def begin(self): pygame.mixer.music.load(GAMEOVER_SONG) pygame.mixer.music.play(1) def render(self): screen.fill((0, 0, 0)) for creature in game.creatures: # display when alive or dying if creature.alive: creature.display() game.player.display(intro_mode=True) # show game over text with border img = HEADER_FONT.render("GAME OVER", False, (255, 125, 125)) border = HEADER_FONT.render("GAME OVER", False, (255, 255, 255)) screen.blit(border, (width / 2 - img.get_rect().width / 2 - 1, height / 2 - img.get_rect().height / 2 - 61)) screen.blit(border, (width / 2 - img.get_rect().width / 2 + 1, height / 2 - img.get_rect().height / 2 - 59)) screen.blit(img, (width / 2 - img.get_rect().width / 2, height / 2 - img.get_rect().height / 2 - 60)) # show generations survived img = NORMAL_FONT.render( "You survived %s %s" % (game.generation, "generation" if game.generation == 1 else "generations"), False, (255, 255, 255)) screen.blit(img, (width / 2 - img.get_rect().width / 2, height / 2 - img.get_rect().height / 2)) self.main_menu_button.add(screen) self.play_again_button.add(screen)
class MainMenuScene(Scene): def __init__(self): Scene.__init__(self) self.play_button = Button("Play", (width / 2 - 100, height - 200), 200, 80) self.play_button.font = pygame.font.SysFont("Consolas", 30) def play_action(): game.reset() self.go_to(game.GAMEINTRO) self.play_button.mouse_up = play_action self.header_image = HEADER_FONT.render("Dr. Darwin", False, (255, 255, 255)) def begin(self): pygame.mixer.music.load(MAINMENU_SONG) pygame.mixer.music.play(-1) # stores mouse click events for previous 3 frame self.prev_clicked = [0, 0, 0] # creature for demo self.creatures = [Creature(0, demo=True)] def render(self): screen.fill((0, 0, 0)) self.play_button.add(screen) screen.blit(self.header_image, (width / 2 - self.header_image.get_rect().width / 2, 40)) for creature in self.creatures: creature.update() creature.display() creature.demo_player.update() # shifts mouse click events self.prev_clicked[1:3] = self.prev_clicked[:2] self.prev_clicked[0] = pygame.mouse.get_pressed()[0] # if mouse up, append a new demo creature to the main menu scene if not self.prev_clicked[0] and self.prev_clicked[1]: self.creatures.append(Creature(0, demo=True))
class GameIntroScene(Scene): ''' Parts of this scene: 1. Darwin is hovering, tank is at bottom "Skip intro" button at bottom right (go to part 4) 2. 1st how-to image 3. 2nd how-to image 4. 3rd how-to image 5. 4th how-to image 6. 5th how-to image 7. Creatures move directly down from Darwin, Darwin dropping sprite 8. Creatures move to their positions 9. Darwin moves up out of screen ''' def __init__(self): Scene.__init__(self) self.creature_init_pos = None self.darwin = Darwin() # darwin positions without hovering effect self.darwin_x = self.darwin.x self.darwin_y = self.darwin.y # time since part started self.part_start = 0 self.skip_button = Button("Skip intro >", (width - 210, height - 60)) def skip_button_action(): self.part = 7 self.skip_button.mouse_up = skip_button_action self.press_space = NORMAL_FONT.render("Press ENTER to continue", False, (230, 230, 230)) # loaded images of the how-tos self.howtos = [ pygame.image.load(image).convert_alpha() for image in [ "resources/howto1.png", "resources/howto2.png", "resources/howto3.png", "resources/howto4.png", "resources/howto5.png" ] ] def begin(self): # if first time playing game, go to instruction page, otherwise go straight to gameplay if first_time: self.part = 1 else: self.part = 7 # initial positions of creatures self.creature_init_pos = [(creature.x, creature.y) for creature in game.creatures] # time that part started self.part_start = pygame.time.get_ticks() # play music pygame.mixer.music.load(GAMEINTRO_SONG) pygame.mixer.music.play(-1) def render(self): # time since part started part_time = pygame.time.get_ticks() - self.part_start screen.fill((0, 0, 0)) # parts are in reverse order so 2 parts arent rendered on the same render call if self.part == 9: duration = 800 for creature in game.creatures: creature.display(intro_mode=True) self.darwin.y = self.darwin_y - 140 * part_time / duration if part_time > duration: self.go_to(game.GAMEPLAY) if self.part == 8: duration = 900 # creatures move from darwin to positions for index, creature in enumerate(game.creatures): creature.x = self.darwin_x + ( self.creature_init_pos[index][0] - self.darwin_x) * (part_time) / duration creature.y = self.darwin_y + 90 + ( self.creature_init_pos[index][1] - self.darwin_y - 90) * (part_time) / duration creature.display(intro_mode=True) if part_time > duration: # set creature positions to their initial positions for index, creature in enumerate(game.creatures): creature.x, creature.y = self.creature_init_pos[index] self.part += 1 self.part_start = pygame.time.get_ticks() if self.part == 7: duration = 800 # play evil laugh track if self.frame == 1: pygame.mixer.music.stop() pygame.mixer.music.load(LAUGH_SOUND) pygame.mixer.music.play(1) # creatures move below darwin for index, creature in enumerate(game.creatures): creature.x = self.darwin_x creature.y = self.darwin_y + 20 + 70 * (part_time) / duration creature.display(intro_mode=True) if part_time > duration: self.part += 1 self.part_start = pygame.time.get_ticks() if self.part == 6: screen.blit(self.howtos[4], (0, 0)) screen.blit(self.press_space, (width / 2 - self.press_space.get_rect().width / 2, height - 40)) # avoid double press if game.key_enter and part_time > 500: self.part += 1 self.part_start = pygame.time.get_ticks() pygame.mixer.music.stop() pygame.mixer.music.load(LAUGH_SOUND) pygame.mixer.music.play(1) if self.part == 5: screen.blit(self.howtos[3], (0, 0)) screen.blit(self.press_space, (width / 2 - self.press_space.get_rect().width / 2, height - 40)) # avoid double press if game.key_enter and part_time > 500: self.part += 1 self.part_start = pygame.time.get_ticks() self.skip_button.add(screen) if self.part == 4: screen.blit(self.howtos[2], (0, 0)) screen.blit(self.press_space, (width / 2 - self.press_space.get_rect().width / 2, height - 40)) # avoid double press if game.key_enter and part_time > 500: self.part += 1 self.part_start = pygame.time.get_ticks() self.skip_button.add(screen) if self.part == 3: screen.blit(self.howtos[1], (0, 0)) screen.blit(self.press_space, (width / 2 - self.press_space.get_rect().width / 2, height - 40)) # let the user control the tank if game.key_right: game.player.direction -= 0.16 * frame_time if game.key_left: game.player.direction += 0.16 * frame_time if game.key_up: game.player.vel = game.player.max_vel elif game.key_down: game.player.vel = -game.player.max_vel else: game.player.vel = 0 if game.key_space: game.player.shoot() # so that player doesn't die game.player.health = 100 # avoid double press if game.key_enter and part_time > 500: self.part += 1 self.part_start = pygame.time.get_ticks() game.player.reset() self.skip_button.add(screen) if self.part == 2: screen.blit(self.howtos[0], (0, 0)) screen.blit(self.press_space, (width / 2 - self.press_space.get_rect().width / 2, height - 40)) if game.key_enter: self.part += 1 self.part_start = pygame.time.get_ticks() if self.part == 1: duration = 1000 if part_time > duration: self.part += 1 self.part_start = pygame.time.get_ticks() if self.part in [1, 2, 7, 8]: self.darwin.display(hover=True) elif self.part == 9: self.darwin.display() if self.part in [1, 2, 3, 7, 8, 9]: game.player.display(intro_mode=True) if self.part == 3: game.player.update()