Difference between revisions of "Pong"

From WLCS
Line 13: Line 13:
 
'''Pong Directions:'''
 
'''Pong Directions:'''
 
# Copy catch.py to pong.py and change the ball into a paddle by using Box instead of the Circle. You can look at [http://openbookproject.net/thinkCSpy/app_b.html Appendix B] for more information on Box. Make the adjustments needed to keep the paddle on the screen.
 
# Copy catch.py to pong.py and change the ball into a paddle by using Box instead of the Circle. You can look at [http://openbookproject.net/thinkCSpy/app_b.html Appendix B] for more information on Box. Make the adjustments needed to keep the paddle on the screen.
# Replace the distance function with a boolean function hit(bx, by, r, px, py, h) that returns True when the vertical coordinate of the ball (by) is between the bottom and top of the paddle, and the horizontal location of the ball (bx) is less than or equal to the radius (r) away from the front of the paddle. 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:
+
# 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.
 +
# Notice that the ball's collision with the paddle is not exact.  We need to fix this...
 +
## Replace the distance function with a boolean function hit(bx, by, r, px, py, h) that returns True  
 +
 
 +
when the vertical coordinate of the ball (by) is between the bottom and top of the paddle, and the horizontal location of the ball (bx) is less than or equal to the radius (r) away from the front of the paddle. 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:
  
 
<source lang="python">
 
<source lang="python">

Revision as of 09:19, 26 January 2010

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

Pong Directions:

  1. Copy catch.py to pong.py and change the ball 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) that returns True

when the vertical coordinate of the ball (by) is between the bottom and top of the paddle, and the horizontal location of the ball (bx) is less than or equal to the radius (r) away from the front of the paddle. 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
    """