This code is a partially completed version of the classic game "pong". Your mission is to create a fully working one-player version that:
- Has the ball move up/down and left/right
- Has the ball bounce off the top, bottom, and right walls
- Has the ball bounce off the paddle
- Has a "score" variable that shows how many hits in a row the user has had
- Resets the game and the "score" if the player misses the ball and it touches the left wall
The only thing that you should need to do to get this project up and running is:
Open vscode
and select File
-> Open Workspace from File
. Then, select the
pong.code-workspace
file within this project folder.
On the left-most side of the vscode
window, select the "Extensions" tab (four
small squares making a larger square) and install the following extensions:
Python
Pylance
(auto installed withPython
extension)Black Formatter
isort
Note: be careful to select only the "Microsoft approved" extensions.
Open the terminal window by selecting Terminal
-> New Terminal
. Then,
install all the required libraries by typing:
pip3 install pytest pygame
You now should be able to run the program and see a white ball and paddle set
against a black background. To do this, open the src/pong/main.py
file and
click the "play button" at the top right. Assuming you see the ball and the
paddle, move onto the next task.
Now that everything is setup, you can start with developing the game. The file
you are editing is src/pong/main.py
.
Look through the code and try and understand it. Pay particular attention to:
- The difference between
ball_x
vs.ball_x_speed
- What happens during a "collision"
- Why the paddle can only move up (but not down)
Now that you understand the code a little bit, work to accomplish each of the following tasks in order:
- Have the ball move up/down and left/right
- Hint: examine the
ball_x_speed
andball_y_speed
variables
- Hint: examine the
- Change the ball from white to blue
- Hint: change the
get_rgb_color()
function
- Hint: change the
- Have the ball bounce off the top, bottom, and right walls
- Hint: examine how the ball detects/reacts to the right wall and copy the logic
- Have the ball bounce off the paddle
- Hint: look at the
ball_y_speed
- Hint: look at the
- Have a "score" variable that shows how many hits in a row the user has had
- Hint: research
pygame.font.Font
on the internet
- Hint: research
- Reset the game and the "score" if the player misses the ball and it touches the left wall
The hardest part of developing this game is to detect when the ball hits the
paddle. The way I think about it is the following: if the ball is to the left of
the right side of the paddle AND to the right of the left side of the paddle
AND to below the top of the paddle AND above the bottom of the paddle,
then it's hitting the paddle. You can represent this with a complex if
statement that uses three and
connectors.
- Two-player pong with a left AND right paddle
- Have the ball become a new, "random" color every time it hits a paddle
- Have the ball bounce off the paddle at an angle dependent on where it hits the paddle