예제 #1
0
class BallTestCase(ModelTestCase):
    def setUp(self):
        super(BallTestCase, self).setUp()
        self.ball = Ball(surf=self.display_surface,
                         pos=self.default_pos,
                         speed=settings.DEFAULT_SPEED)

    def test_hits_top_edge_returns_true(self):
        self.ball.top = settings.TOP_EDGE
        self.assertTrue(self.ball.hits_top_edge())

    def test_hits_top_edge_returns_false(self):
        self.ball.top = settings.TOP_EDGE + 1
        self.assertFalse(self.ball.hits_top_edge())

    def test_hits_bottom_edge_returns_true(self):
        self.ball.bottom = settings.BOTTOM_EDGE
        self.assertTrue(self.ball.hits_bottom_edge())

    def test_hits_bottom_edge_returns_false(self):
        self.ball.bottom = settings.BOTTOM_EDGE - 1
        self.assertFalse(self.ball.hits_bottom_edge())

    def test_reset_velocity_sets_to_default_speed(self):
        self.ball.velocity = (200, 200)
        self.ball.reset_velocity()
        default_velocity = [settings.DEFAULT_SPEED, settings.DEFAULT_SPEED]
        self.assertEqual(self.ball.velocity, default_velocity)

    def test_is_moving_left_when_velocity_x_less_than_zero(self):
        self.ball.velocity[0] = -1
        self.assertTrue(self.ball.is_moving_left())

    def test_not_moving_left_when_velocity_x_zero_or_greater(self):
        self.ball.velocity[0] = 0
        self.assertFalse(self.ball.is_moving_left())

        self.ball.velocity[0] = 1
        self.assertFalse(self.ball.is_moving_left())

    def test_is_moving_left_after_velocity_reset(self):
        self.ball.reset_velocity()
        self.assertTrue(self.ball.is_moving_left())

    def test_is_moving_right_when_velocity_x_greater_than_zero(self):
        self.ball.velocity[0] = 1
        self.assertTrue(self.ball.is_moving_right())

    def test_not_moving_right_when_velocity_x_less_than_zero(self):
        self.ball.velocity[0] = 0
        self.assertFalse(self.ball.is_moving_right())

        self.ball.velocity[0] = -1
        self.assertFalse(self.ball.is_moving_right())

    @mock.patch('pygame.draw.rect')
    def test_render_draws_rectangle(self, mock_draw):
        self.ball.render()
        mock_draw.assert_called_with(self.ball.surf, self.ball.color,
                                     self.ball.rect)
예제 #2
0
파일: main.py 프로젝트: okayjeff/pong
def main():
    arena = Arena(surf=DISPLAY_SURF)

    player_1 = Player(name=settings.PLAYER_ONE,
                      surf=DISPLAY_SURF,
                      pos=get_player_default_pos(num=settings.PLAYER_ONE))

    player_2 = Player(name=settings.PLAYER_TWO,
                      surf=DISPLAY_SURF,
                      pos=get_player_default_pos(num=settings.PLAYER_TWO))

    ball = Ball(surf=DISPLAY_SURF,
                pos=get_ball_default_pos(),
                speed=settings.EASY)

    clock = Clock(surf=DISPLAY_SURF)
    clock.start()

    # Stack used to maintain render order
    game_objects = [
        arena,
        player_1,
        player_2,
        ball,
        clock,
    ]

    # Main loop
    while True:
        # Events
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                exit_game()

            elif event.type == pygame.MOUSEMOTION:
                player_1.move((0, event.pos[1] - player_1.y))

        # Logic
        game_over = check_point_scored(ball)
        if game_over:
            SoundController.play_game_over()
            clock.stop()
            elapsed = clock.get_elapsed_seconds()
            show_game_over_screen(DISPLAY_SURF, FPS_CLOCK, elapsed)
            clock.reset()
            clock.start()

            ball.reset_velocity()
            ball.reposition(get_ball_default_pos())

        handle_ball_movement(ball, player_1, player_2)
        handle_player_movement(player_2, ball)

        # Rendering
        render_game_objects(game_objects)
        pygame.display.update()
        FPS_CLOCK.tick(settings.FPS)