def test_player(self): pygame.init() test_render_group = pygame.sprite.LayeredUpdates() actual = Player(test_render_group) test_timer = Timer(20) nest = FrogNest(1) actual.rect.x = 101 actual.rect.y = 415 actual.index = 1 actual.direction = "right" expected = Player(test_render_group) actual.return_home() self.assertEqual(actual.rect.x, expected.rect.x) self.assertEqual(actual.rect.y, expected.rect.y) self.assertEqual(actual.index, expected.index) self.assertEqual(actual.direction, expected.direction) actual.farthest_distance = 265 expected.score += 50 + 2 * test_timer.get_time() actual.nest(test_timer, nest) self.assertEqual(actual.farthest_distance, expected.farthest_distance) self.assertEqual(actual.score, expected.score) self.assertTrue(actual.disabled_nests.has(nest)) actual.win_game() expected.score += 2000 self.assertEqual(actual.score, expected.score) actual.kill() expected.lives_left -= 1 self.assertEqual(actual.lives_left, expected.lives_left)
def move_player(player: Player, key_depressed, movement_distance_x, movement_distance_y, sound_effect): """ Handles player movement when the AI is not being trained. This should only be called when the game is being played by a human. - :param player: A Player object. - :param key_depressed: An int representing the key that was pressed to call the function. 119 for moving up, 97 for left, 100 for right, 115 for down. These correspond to WASD. -:param movement_distance_x: An int representing the distance a player will move in the horizontal directions :param movement_distance_y: An int representing the distance a player will move in the vertical directions - :param sound_effect: A sound effect asset to be played when the player moves - :return: None """ x_change = 0 y_change = 0 up = 119 # 'w' key ascii left = 97 # 'a' key ascii right = 100 # 'd' key ascii down = 115 # 's' key ascii if key_depressed == up: # if up key is pressed if player.rect.y > 60: # not at top sound_effect.play() y_change -= movement_distance_y # move up up_image = AssetDictionary.get_asset("frog") up_image2 = AssetDictionary.get_asset("frog_jumping") player.images = [up_image, up_image2] new_y = player.rect.y if player.farthest_distance > new_y > 110: player.farthest_distance = new_y player.score += 10 elif key_depressed == left: # if left key is pressed if player.rect.x > 20: # not at leftmost border sound_effect.play() x_change -= movement_distance_x left_image = pygame.transform.rotate( AssetDictionary.get_asset("frog"), 90) left_image2 = pygame.transform.rotate( AssetDictionary.get_asset("frog_jumping"), 90) player.images = [left_image, left_image2] elif key_depressed == right: # if right key is pressed if player.rect.x < 750: # not at rightmost border sound_effect.play() x_change += movement_distance_x right_image = pygame.transform.rotate( AssetDictionary.get_asset("frog"), -90) right_image2 = pygame.transform.rotate( AssetDictionary.get_asset("frog_jumping"), -90) player.images = [right_image, right_image2] elif key_depressed == down: # if down key is pressed if player.rect.y < 800: # not at bottom sound_effect.play() y_change += movement_distance_y down_image = pygame.transform.rotate( AssetDictionary.get_asset("frog"), 180) down_image2 = pygame.transform.rotate( AssetDictionary.get_asset("frog_jumping"), 180) player.images = [down_image, down_image2] player.rect.x += x_change player.rect.y += y_change