def clicked(self, game, sprite): if type(sprite) == TextSprite: if sprite.text == dropper_locale.MENU_PLAY: util.display_screen(game, game.game_screen) elif sprite.text == dropper_locale.MENU_HOW_TO_PLAY: util.display_screen(game, game.how_to_play_screen) elif sprite.text == dropper_locale.MENU_QUIT: util.exit_program()
def test_high_score_updated_on_end_shown(self): """ Checks that the high score is updated when the end game screen is displayed. """ game = Game() game.score = 10 util.display_screen(game, game.end_game_screen) self.assertEqual(game.high_score, 10)
def test_how_to_play_click_opens_how_to_play_screen(self): """ Checks that the how to play screen is opened when the 'How to Play' text is clicked on the menu screen. """ game = Game() util.display_screen(game, game.menu_screen) game.active_screen.clicked( game, get_sprite(game.menu_screen, dropper_locale.MENU_HOW_TO_PLAY)) self.assertEqual(game.active_screen, game.how_to_play_screen)
def test_play_click_opens_game_screen(self): """ Checks that the game screen is opened when the 'Play' text is clicked on the menu screen. """ game = Game() # Load sprites by display the screen util.display_screen(game, game.menu_screen) # Fake a click event game.active_screen.clicked( game, get_sprite(game.menu_screen, dropper_locale.MENU_PLAY)) self.assertEqual(game.active_screen, game.game_screen)
def test_game_ends_on_collision_with_bar(self): """ Checks to see if the end game screen is displayed when a bar hits a player. """ game = Game() util.display_screen(game, game.game_screen) # Fill screen with bar util.render_bar(game.active_screen, dropper_locale.ID_DROPPING_OBJECT, util.colour_red, game.setting_width, 0) # Move bar on top of player util.move_rect_from_id(game.active_screen, dropper_locale.ID_DROPPING_OBJECT, (0, game.game_screen.player_y), util.colour_cyan, util.colour_black) # Run as if the game is ticking game.active_screen.show(game) self.assertEqual(game.active_screen, game.end_game_screen)
def show(self, game): super().show(game) # Update the player's position. util.move_rect_from_id(self, dropper_locale.ID_PLAYER, (pygame.mouse.get_pos()[0], self.player_y), util.colour_black, util.colour_cyan) # Iterate through each bar. for sprite in self.sprites: if sprite.identifier == dropper_locale.ID_DROPPING_OBJECT: # If either part of the bar collides with the player, display the end game screen. if sprite.part1.colliderect( self.sprite_player.rect) or sprite.part2.colliderect( self.sprite_player.rect): util.display_screen(game, game.end_game_screen) return # If the bar has reached the bottom, remove it. elif sprite.rect.y >= self.bar_hit_y: self.sprites.remove(sprite) pygame.draw.rect(self.surface, util.colour_cyan, sprite.part1) pygame.draw.rect(self.surface, util.colour_cyan, sprite.part2) game.score += 1 # Otherwise, move the bar down by 2. else: util.move_bar(self.surface, sprite, 2, util.colour_red, util.colour_cyan) # Increment game iteration. game.iteration += 1 if game.iteration >= 100: # Increment the difficulty every time the score reaches a multiple of 5, # with a maximum difficulty of 3. if game.score != 0 and game.score % 5 == 0 and game.difficulty > 3: game.difficulty -= 1 # Reset the iteration and calculate the gap size. game.iteration = 0 gap = (100 - (game.score * 4)) # Check that the player can actually fit in the gap. if gap < 5: gap = 5 # Render the bar. util.render_bar(self, dropper_locale.ID_DROPPING_OBJECT, util.colour_red, randint(0, (self.width - 100)), gap) # Re-render the score in the top left. pygame.draw.rect(self.surface, util.colour_cyan, self.sprite_score.rect) self.sprite_score = util.render_text(self, dropper_locale.GAME_SCORE.format( game.score), (70, 20), center=False)
def clicked(self, game, sprite): if sprite.identifier == dropper_locale.HOW_TO_PLAY_GO_BACK: util.display_screen(game, game.menu_screen)
def clicked(self, game, sprite): if sprite.identifier == dropper_locale.END_GAME_PLAY_AGAIN: util.display_screen(game, game.game_screen) elif sprite.identifier == dropper_locale.END_GAME_RETURN_TO_MENU: util.display_screen(game, game.menu_screen)