Making Pong with PyGame
PyGame is a very versatile tool used for game developement with Python. There are many amazing things you can do with pygame, but first it is important to know the basics.
In order to do that, we are going to create a basic pong game, which teaches a lot of the fundamentals of PyGame.
As with anything in programming, the best way to learn is by doing, so let’s dive straight into it
Since PyGame isn’t installed with Python, we will need to install it ourselves. To do that, simply run this command:
>>> pip install pygame
Now we can get into the real meat of the code. In a new python file, start by importing pygame:
After that, we need to initialize the game, with a set height and width. We do that by creating variables height and width, and then passing them as a tuple into a pygame function:
If you were to run this now, you would see a blank screen with a caption of “pygame window”, and a very pixelated pygame logo, which would then instantly disappear. That’s because the script ends almost as soon as it starts. To fix this, create a game_on
variable and a while True: pass
loop. We will edit these later. If you run this, you may notice that you can’t actually close the window. First, open up task manager and kill the program ASAP, as it can cause your computer to crash. To fix this issue (and also make the screen a different color), use the following:
What this does is it loops through each event in the current events, and checks if one of the events is a QUIT event (ie the user somehow tries to close the window). If so, it stops the while loop. If the event is not QUIT, then it updates the display screen.
Before the loop or the game_on
is where the real meat of the game is. However, it is key to understand how the game works. There are a couple main mechanics of the game:
- The player must be able to move their paddle
- The CPU must automatically track the location of the ball and follow it
- The ball must bounce of the sides of the walls or the paddle (and if the ball touches the player side, log that the player lost, same for the CPU side)
In order to accomplish this, we need to keep track of the player paddle location, ball location, and cpu paddle location. For the player, I implement the location as the distance from the original position to where it is now. For the rest, I just implement them as the real position.
We also need the following functions:
- player(): displays the player paddle on the screen
- cpu(): displays the cpu paddle on the screen
- ball(): displays the ball on the screen
- calc_cpu(): calculates where the cpu should go
- calc_ball(): calculates where the ball should go
- move_up(): move the player paddle up
- move_down(): move the player paddle down
- game_over(): reset the game and display who won
We also need the icons, and images for each object in the game
Icons/Images
Here are the icons and images I used for the game
Loading Variables
I used pygame.display.set_caption to set the caption, and I also loaded in the images. After this, the window should have:
at the top. See if you can figure out how the rest of the code works!
Functions — my oh my
There are 8 functions we need to implement, but the first 3 are really similar. We just use screen.blit to load the image onto the screen.
Next, we need to implement the calc_cpu() and calc_ball() functions. For the calc_cpu(), I checked if the center of the cpu paddle needed to move up or down to match the center of the ball, and then moved it accordingly.
For the calc_ball, I just moved the ball, and then checked if the ball hit a side, and calculated its x and y momentum respectively. It was a bit more complex because it required also checking if the ball had hit the paddle.
Then, the game_over function was relatively simple:
After that, the move up and move down worked similarly to the calc_cpu, but the player controlled the directions
And that’s it! All the functions we need are made
If you were to try running the code now, nothing would show. That is because we need to update the while loop. I’ll put the code first, and then explain it:
First, we check if the player had exited the program, similar to before. After that, we get all the keys pressed. Then, we use a big if chain to see if the player had pressed the w or s keys, or if the player had pressed the up/down arrow keys.
Then, we check if the user pressed 5, in which case the game closes.
Finally, we run most of the functions in sequence, and then update the screen.
This is what the final code looks like:
If you try running the code, you should have a fully functional pong game working.
That’s all for this tutorial. If you learned a thing or two, don’t forget to clap!