예제 #1
0
    def setUp(self):
        self.plane_mock = Mock()
        self.plane_mock.alive.return_value = True
        self.plane_factory_mock = Mock()
        self.plane_factory_mock.plane.side_effect = lambda x, y: self.plane_mock
        self.plane_factory_mock.get_plane_cost.return_value = 123
        self.player_input_mock = Mock()
        self.game_notification = GameNotification("press key", " until spawn")
        self.spawn_timer_mock = Mock()
        self.spawn_timer_mock.time_left.return_value = 10
        self.player_recorder_mock = Mock()
        self.spawn_timer_mock.expired.return_value = False
        self.user_mock = Mock()

        self.player = Player(self.plane_factory_mock, self.player_input_mock,
                             self.game_notification, self.player_recorder_mock,
                             self.user_mock, self.spawn_timer_mock)
예제 #2
0
    def game(self):
        """Creates a Game based on the state of the `self`"""
        level_config = self._level_config_selector.get_selected()
        game_input = GameInput(self._event_handler,
                               self._config.game_input_config)

        game_notifications = []
        plane_factories = []

        start_positions = level_config.starting_locations()

        for i in range(self._n_players):
            game_notifications.append(
                GameNotification(self._config.press_key_to_start_message,
                                 self._config.until_spawn_message))
            plane_factories.append(PlaneFactory(self._config.plane_config))
            plane_factories[i].start_position = start_positions[i]

        players = []
        game_views = []
        for i in range(self._n_players):
            player_input_config = self._config.player_input_configs[i]
            player_input = PlayerInput(game_input, player_input_config)
            user = self.user_selectors[i].get_current()
            spawn_timer = Timer(self._config.player_spawn_time)
            players.append(
                Player(plane_factories[i], player_input, game_notifications[i],
                       PlayerRecorder(user, Timer()), user, spawn_timer))
            camera = Camera(self._config.game_camera_height)
            font_color = self._config.game_view_font_color
            game_views.append(GameView(players[-1], camera, font_color))

        game_length = self._config.game_length
        game_state = GameState(level_config.game_objects(), players,
                               level_config.name(), Timer(game_length))

        background = GameBackground.from_config(self._config.background_config)

        pause_overlay = PauseOverlay(self._config.pause_message,
                                     self._config.pause_font_color,
                                     self._config.pause_blur_radius)
        info_bar = InfoBar(self._config.info_bar_level_message,
                           self._config.info_bar_time_left_message,
                           self._config.info_bar_font_color,
                           self._config.info_bar_background_color)
        renderer = GameRenderer(self._screen, game_views, background,
                                pause_overlay, info_bar)

        game_clock = Clock(self._config.game_fps, busy_wait, True)
        game = Game(game_input, game_state, renderer, game_clock)
        return game
예제 #3
0
class TestPlayer(unittest.TestCase):
    def setUp(self):
        self.plane_mock = Mock()
        self.plane_mock.alive.return_value = True
        self.plane_factory_mock = Mock()
        self.plane_factory_mock.plane.side_effect = lambda x, y: self.plane_mock
        self.plane_factory_mock.get_plane_cost.return_value = 123
        self.player_input_mock = Mock()
        self.game_notification = GameNotification("press key", " until spawn")
        self.spawn_timer_mock = Mock()
        self.spawn_timer_mock.time_left.return_value = 10
        self.player_recorder_mock = Mock()
        self.spawn_timer_mock.expired.return_value = False
        self.user_mock = Mock()

        self.player = Player(self.plane_factory_mock, self.player_input_mock,
                             self.game_notification, self.player_recorder_mock,
                             self.user_mock, self.spawn_timer_mock)

    def test_view_location_without_plane(self):
        self.plane_factory_mock.start_position = Vector2(1, 2)
        assert self.player.view_location() == Vector2(1, 2)

    def test_user_recoder_updates(self):
        self.player.update(10)
        self.player_recorder_mock.update.assert_called_with(10)

    def test_spawn_timer_updates(self):
        self.player.update(10)
        self.spawn_timer_mock.update.assert_called_with(10)

    def _create_plane_by_shoot_key_with_timer_expired(self):
        self.spawn_timer_mock.expired.return_value = True
        self.plane_mock.graphic.location = Vector2(2, 1)

        self.player.update(1)

        assert self.player_input_mock.bind_shoot.call_args[0][0] is not None
        self.player_input_mock.bind_shoot.call_args[0][0]()

    def test_shoot_key_creates_new_plane_when_spawn_timer_expired(self):
        self._create_plane_by_shoot_key_with_timer_expired()

        new_objects = self.player.new_objects()
        assert len(new_objects) == 1
        assert new_objects[0] is self.plane_mock

    def test_view_location_with_plane(self):
        self._create_plane_by_shoot_key_with_timer_expired()
        self.plane_mock.graphic.location = Vector2(3, 3)
        assert self.player.view_location() == Vector2(3, 3)

    def test_notification_when_no_plane_and_timer_not_expired(self):
        self.player.update(1)
        assert self.game_notification.get_message() == "10.0 until spawn"

    def test_notification_after_timer_expired(self):
        self.spawn_timer_mock.expired.return_value = True
        self.player.update(1)
        assert self.game_notification.get_message() == "press key"

    def test_notification_stays_clear_when_plane_exists(self):
        self._create_plane_by_shoot_key_with_timer_expired()
        self.player.update(1)
        self.player.update(1)
        assert self.game_notification.get_message() == ""

    def test_dead_plane_starts_spawn_timer(self):
        self._create_plane_by_shoot_key_with_timer_expired()
        self.plane_mock.alive.return_value = False
        previous_call_count = self.spawn_timer_mock.start.call_count
        self.player.update(10)
        # shouldn't have an effect
        self.player.update(10)
        assert self.spawn_timer_mock.start.call_count == previous_call_count + 1

    def test_dead_plane_recorded_correctly(self):
        self._create_plane_by_shoot_key_with_timer_expired()
        self.plane_mock.alive.return_value = False
        self.player.update(10)
        # shouldn't have an effect
        self.player.update(10)
        self.player_recorder_mock.add_death.assert_called_once()
        self.player_recorder_mock.add_score.assert_called_with(-123)

    def test_process_reward_self_ignored(self):
        self.player.process_reward(10, self.player)
        self.player_recorder_mock.add_score.assert_not_called()

    def test_process_reward_other_accepted(self):
        self.player.process_reward(10, Mock())
        self.player_recorder_mock.add_score.assert_called_with(10)

    def test_add_kill_self_ignored(self):
        self.player.add_kill(self.player)
        self.player_recorder_mock.add_kill.assert_not_called()

    def test_add_kill_other_not_ignored(self):
        self.player.add_kill(Mock())
        self.player_recorder_mock.add_kill.assert_called_once()

    def test_add_shot_fired(self):
        self.player.add_shot_fired()
        self.player_recorder_mock.add_shot.assert_called_once()
예제 #4
0
 def test_get_column(self):
     p = Player()
     with pytest.raises(NotImplementedError):
         c = p.get_column()
예제 #5
0
 def to_play(self) -> Player:
     """Return the current player."""
     return Player()
예제 #6
0
 def to_play(self) -> Player:
     return Player()
예제 #7
0
 def to_play(self) -> Player:
     return Player(self.env.current_player)
예제 #8
0
파일: bots.py 프로젝트: idanmel/connect4
 def __init__(self, number_of_games=4000):
     self.number_of_games = number_of_games
     Player.__init__(self)