Pong
One of the first successful video games was Pong. Back in the 1970's a game of Pong was so complicated that devices were sold that played only Pong. Today, you know enough programming to create your own Pong game in an afternoon.
Details
Pong was designed to be like a game of table tennis (get it? Pong, like ping-pong). Create a Python program that uses the graphics and sprite module to play a one-player version of Pong, like a person playing ping-pong against a wall. For example:
The movement of the ball is just like the bouncing image from the screen saver lab activity. Begin this program by copying that program and modifying it to include the paddle at the bottom of the screen. Pressing the left or right arrow keys should move the paddle left and right.
Once the program has a paddle, finish the program by replacing the code that bounces the ball off the bottom of the window to be code that bounces the ball only if it hits the paddle.
The program should run while the ball is on the screen. If the ball goes off the screen, the program should end.
To save time, instead of searching for images, you can use these images:


Paddle Movement
- Create variables to represent the paddle's x and y coordinates. The variables should be defined before the animation loop.
-
Use the
key_down
function in conjunction with an if statement to change the paddle's x coordinate variable when an arrow key is being pressed.
Paddle Bounce
-
Part of the power of writing code using functions is that it
makes it easy to move it from one program to another.
Testing if the ball hits the paddle is simple if you use one
of the functions you have previously written.
Either the
rectangle_overlaps
function or theis_point_in_rectangle
function could be used to to test if the ball hits the paddle. - If the ball does hit the paddle its speed should change just like it hit the bottom of the screen.
- Note: the ball will bounce inside of the paddle if the paddle hits the side of the ball. To prevent this, only consider the ball hitting the paddle if the ball is also moving downward.
Game Over
- Use a while loop instead of a for loop for the main animation loop. The condition of the loop should be while the player has not lost. The player has lost when the ball goes off the screen.
Challenge 1
The original Pong game was meant to be like ping-pong and you need two players to play ping-pong. Modify the program so that it is a two player game. Add a second user controlled image to the opposite side of the screen.
Challenge 2
Modify the program so that it keeps score. Every time the ball goes off the window, the player on the other side gets a point and the ball's position resets. Draw each player's score in the window.
Challenge 3
The game is kind of easy because the path of the ball is completely deterministic. Make the game more interesting by speeding up the ball every time it bounces and give players the ability to add spin to the ball. Players can spin the ball, or change its direction, by moving the paddle when it hits the ball. If the paddle is moving in the same direction as the ball, it will make the bounce angle more obtuse. If the paddle is moving in the opposite direction as the ball, it will make the bounce angle more acute. If the paddle is not moving, it should bounce like normal.