Introduction:
In this article, we are going to be using Python and the Turtle library to create a simple Pong game. The game is similar to an actual game of table tennis, where two players will use keys to move two paddles up and down on either side of the game screen. Users will use these to deflect a ball and prevent it from hitting the edge of the screen on their side. The game is lots of fun to play, and its’ simple code makes it the perfect project for kids or those who are new to Python to create. Regardless of your proficiency with Python, this project is a great way to test your knowledge and learn more about making a functional program.
Who is this Project For?
This is a beginner-level project for those who are new to Python. Before starting this project, you should already have experience with functions, if-statements, and the Python Turtle library. The project will use these concepts and help you strengthen your programming skills.
What Will We Learn?
This program focuses on creating simple graphics using Turtle, using vectors to control the positioning of game objects, and using functions to organize code and make it more efficient. These concepts are essential to know and can be applied to any Python project.
Features to Consider:
- The ball will move randomly around the game screen
- Players will be able to move their paddles to deflect the ball
- If the ball hits the paddle, it will bounce off in the opposite direction.
- Once the ball gets too close to the edge of the screen on a player’s side, the game ends
Pseudo Code:
Below is the pseudocode for the snake program:
Main Steps:
This project can be broken down into 4 main steps:
- Import libraries and set up variables
- Make the player paddles
- Create the draw function
- Finish program setup
Step 1: Import Libraries and Set Up Variables
The first thing we’ll need to do when creating this game is import all of the required libraries. For this program, we will be using the Turtle library to run the program, the random module to create random coordinates and directions for the ball, and the FreeGames library to control the locations of the ball and player paddles. Importing these libraries is necessary for the program to run. We can import these libraries using this code:
Next, we need to set up some of the main variables for the program. To start, we will create a function called ‘value’, which will return a random value between (-5, -3) or (3, 5). This will be used to determine where the ball goes first. We will create vectors for the ball, the ball’s direction (called aim), and will also create some player game states. The code for this section will look like this:
Note that in this example, we used the value function to create some initial starting vectors for the ball’s aim. This will be seen when the game first begins and the ball starts traveling in a random direction.
Step 2: Make the Player Paddles
Now it’s time to create the player’s paddles as well as the function that will allow users to move them. We will start by creating a ‘move’ function. This will take the player’s name and a value to change their location as input, and then will change the player’s position by that value. For this function, we will type:
Now we need to create the rectangles. This will be done using a for-loop that takes 4 parameters: the rectangle’s X and Y-coordinates, and its’ width and height. Then, we will move the pen to the rectangle’s location and then draw the shape. To do this we will use a while loop and use it to create a 10x50 rectangle Once this code is complete, it should look like this:
Step 3: Create the Draw Function
Next, we need to create the main function that will draw the game’s graphics. This is the function that controls the majority of the game’s functionality. Start by defining the function, then we’ll clear the game screen and draw the two rectangles. Next, we need to make the ball move in the direction that it’s aimed in, using the X and Y coordinates from the aim vector. After this is done we will lift the turtle pen from the screen, go to the x and y-position, draw a dot, and update the game screen. At this point the code should look like this:
Now it’s time to add restrictions to the ball’s coordinates so that it stays on the game screen at all times. We will use if-statements to make sure that if the ball’s y-coordinates are greater than 200 or less than -200, the ball changes direction. We will do the same thing with the x-coordinates, but will also adjust the ball’s state. Once this is done we will also add an ontimer() command, which is used to update the game screen every 50 milliseconds by calling the draw function. Once this is done, the code will be:
Step 4: Finish Program Setup
Finally, we’re on to the last step, which will include setting up the window, as well as creating some small functions that will allow the player to control the snake using the keyboard. We’ll start by setting up the window, by entering the coordinates (in this case, we’re using 420, 420, 370, 0). Then, we will hide the turtle cursor as well as the tracer, which will make sure the turtle doesn’t leave a line behind it. Then, we will call the listen() command, which will make the program listen for user input. The next step will involve creating four lambda functions to allow the user to move their paddle by pressing the correct keys on the keyboard. IN this project, we will be using the ‘W’ and ‘S’ keys to control the left player, and the ‘I’ and ‘K’ keys to control the right player.
Hint: A lambda function is a function that can be created without a name, which makes it perfect for this purpose.
Finally, we need to call the draw function and add in a done() command and the program is finished! Once all of this is complete, the code will look like this:
Project Complete!
Now the project is complete! We hope you’ve enjoyed this simple Pong game using Python and hopefully, you’ve earned some new skills or gained a deeper understanding of the concepts covered in this project. Feel free to test your code now and see how it works. If you’re stuck or have any issues with your code, try reviewing it again either in your text editor or by looking at the code included in the article as a reference.
Check our articles about python projects for kids:
- Python AI Project for Kids: Face Detection: Computer vision and object detection programs are becoming more and more common in our lives, but often seem complicated. Here's a simple computer vision project that will help you start learning more about this interesting topic.
- Python Game for Kids: Python Snake Game: If you've ever wanted to create your own fun and simple video games, You're in luck! Keep reading to find out how you can create your own Snake game using Python and the Turtle library!
- Python Project for kids: Python Alarm Clock: It's the time of year where students go back to school, so what better time to practice coding by creating an alarm clock that can wake you up in the morning!