Example #1
0
    def test_bounce_ball_against_bottom_wall(self):
        hit_wall = pygame.mixer.Sound('../sounds/pong_wall.wav')
        hit_paddle = pygame.mixer.Sound('../sounds/pong_wall.wav')
        pong = Pong(hit_wall, hit_paddle)

        pong.ball = Ball(Config.ball_left_start_x, 500 - 20, 5, 5)
        pong.move_ball()
        self.assertEqual(pong.ball.vx, 5)
        self.assertEqual(pong.ball.vy, -5)
Example #2
0
    def test_bounce_ball_against_right_paddle(self):
        hit_wall = pygame.mixer.Sound('../sounds/pong_wall.wav')
        hit_paddle = pygame.mixer.Sound('../sounds/pong_wall.wav')
        pong = Pong(hit_wall, hit_paddle)

        pong.ball = Ball(960, 250, 5, 5)
        pong.move_ball()
        self.assertEqual(pong.ball.vx, -5)
        self.assertEqual(pong.ball.vy, 5)
Example #3
0
    def test_can_move_right_paddle_down(self):
        hit_wall = pygame.mixer.Sound('../sounds/pong_wall.wav')
        hit_paddle = pygame.mixer.Sound('../sounds/pong_wall.wav')
        pong = Pong(hit_wall, hit_paddle)

        pong.move_right_down()
        self.assertEqual(
            pong.rightPaddle.y,
            Config.paddle_right_start_y + pong.rightPaddle.step_size)
Example #4
0
    def test_can_not_move_paddle_below_lower_limit(self):
        hit_wall = pygame.mixer.Sound('../sounds/pong_wall.wav')
        hit_paddle = pygame.mixer.Sound('../sounds/pong_wall.wav')
        pong = Pong(hit_wall, hit_paddle)

        paddle = Paddle(20, Config.paddle_lower_limit)
        pong.leftPaddle = paddle
        pong.move_left_down()
        self.assertEqual(pong.leftPaddle.y, Config.paddle_lower_limit)
Example #5
0
    def test_move_ball(self):
        hit_wall = pygame.mixer.Sound('../sounds/pong_wall.wav')
        hit_paddle = pygame.mixer.Sound('../sounds/pong_wall.wav')
        pong = Pong(hit_wall, hit_paddle)

        pong.move_ball()
        self.assertEqual(pong.ball.x,
                         Config.ball_left_start_x + Config.ball_serve_left_vx)
        self.assertEqual(pong.ball.y,
                         Config.ball_left_start_y + Config.ball_serve_left_vy)
Example #6
0
    def test_ball_serve_right(self):
        hit_wall = pygame.mixer.Sound('../sounds/pong_wall.wav')
        hit_paddle = pygame.mixer.Sound('../sounds/pong_wall.wav')
        pong = Pong(hit_wall, hit_paddle)

        pong.serve_right()
        self.assertEqual(pong.ball.x, Config.ball_right_start_x)
        self.assertEqual(pong.ball.y, Config.ball_right_start_y)
        self.assertEqual(pong.ball.vx, Config.ball_serve_right_vx)
        self.assertEqual(pong.ball.vy, Config.ball_serve_right_vy)
Example #7
0
    def test_ball_missed_on_left(self):
        hit_wall = pygame.mixer.Sound('../sounds/pong_wall.wav')
        hit_paddle = pygame.mixer.Sound('../sounds/pong_wall.wav')
        pong = Pong(hit_wall, hit_paddle)

        pong.ball = Ball(-5, 250, -5, -5)
        pong.move_ball()
        self.assertEqual(pong.ball.x, Config.ball_left_start_x)
        self.assertEqual(pong.ball.y, Config.ball_left_start_y)
        self.assertEqual(pong.ball.vx, 0)
        self.assertEqual(pong.ball.vy, 0)
Example #8
0
    def test_right_paddle_start_position(self):
        hit_wall = pygame.mixer.Sound('../sounds/pong_wall.wav')
        hit_paddle = pygame.mixer.Sound('../sounds/pong_wall.wav')
        pong = Pong(hit_wall, hit_paddle)

        self.assertEqual(pong.rightPaddle.x, Config.paddle_right_start_x)
        self.assertEqual(pong.rightPaddle.y, Config.paddle_right_start_y)
pygame.init()
hit_wall = pygame.mixer.Sound('sounds/pong_wall.wav')
hit_paddle = pygame.mixer.Sound('sounds/pong_wall.wav')
screen = pygame.display.set_mode((Config.width, Config.height))
clock = pygame.time.Clock()

# Set up Models
leftPaddle = Paddle(Config.paddle_left_start_x, Config.paddle_left_start_y,
                    Config.paddle_upper_limit, Config.paddle_lower_limit)
rightPaddle = Paddle(Config.paddle_right_start_x, Config.paddle_right_start_y,
                     Config.paddle_upper_limit, Config.paddle_lower_limit)

ball = Ball(Config.ball_left_start_x, Config.ball_left_start_y,
            Config.ball_size)
score = Score()
pong = Pong(leftPaddle, rightPaddle, ball, score, hit_wall, hit_paddle,
            Config.color)

# Game Loop
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

    keys = pygame.key.get_pressed()
    if keys[pygame.K_q]:
        pong.move_left_up()

    if keys[pygame.K_a]:
        pong.move_left_down()

    if keys[pygame.K_p]: