Python - Pong

From WLCS

Objective:

  • You will use the GASP Python library to create Pong
  • The Pong game will require that you use math, variables, functions, and loops!

Resources:

Catch Directions:

  1. Read and walk through the entire HTTLACS: Ch 8. Be sure to analyze and run the code given to you.
  2. Complete the following Ch 8 exercises: #1-5, 8-10
    • For exercises #8, you should comment most lines of code, explaining what each line does
  3. Submit the exercises on-line by following the these instructions:
    1. Name your file ch8_FIRSTNAME_LASTNAME.txt or ch8_FIRSTNAME_LASTNAME.odt
    2. Submit the file online via SchoolWebLockers

Pong Directions:

  1. Copy catch.py from Section 8.10 in Case Study: Catch to pong.py and change the mitt into a paddle by using Box instead of the Circle. You can look at Appendix B for more information on Box. Make the adjustments needed to keep the paddle on the screen.
  2. When you play the Catch game, the ball collides and is caught. In Pong, we want the ball to bounce back if there is a collision. If the ball collides, you should make it bounce back along the x-axis. Hint: We have already made the ball bounce in the y-axes when it hits the top or bottom of the screen.
  3. Notice that the ball's collision with the paddle is not exact. We need to fix this...
    1. Replace the distance function with a boolean function hit(bx, by, r, px, py, h), which will return True if the ball hits the paddle and False otherwise. The ball hits the paddle when all the following are True:
      • The vertical coordinate of the ball (by) is between the bottom (py) and top (py+h) of the paddle
      • The horizontal location of the ball (bx) + its radius (r) is greater than the front of the paddle (px)
    2. Use hit to determine when the ball hits the paddle, and make the ball bounce back in the opposite horizontal direction when hit returns True. Your completed function should pass these doctests:
def hit(bx, by, r, px, py, h):
    """
      >>> hit(760, 100, 10, 780, 100, 100)
      False
      >>> hit(770, 100, 10, 780, 100, 100)
      True
      >>> hit(770, 200, 10, 780, 100, 100)
      True
      >>> hit(770, 210, 10, 780, 100, 100)
      False
    """
  1. Change the scoring logic to give the player a point when the ball goes off the screen on the left.
  2. Add a new paddle on the left side of the screen which moves up when 'a' is pressed and down when 's' is pressed.
  3. Create a second hit function for the left paddle, and use it in the loop by checking if the ball ever hits the left paddle
  4. Change the starting point for the ball to the center of the screen, (400, 300), and make it randomly move to the left or right at the start of each round.
  5. Be sure to change the scoring logic so that both players receives points properly.

Grading Rubric:

  • 2 pts - right paddle moves up and down with keyboard
  • 2 pts - ball bounces properly off the right paddle
  • 2 pts - left paddle moves up and down with keyboard
  • 2 pts - ball bounces properly off the left paddle
  • 2 pts - Player 1 and Player 2 points are tracked and displayed properly