# Python Game for Kids: Python Pong Game

## 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:

```Import the Turtle, random, and FreeGames libraries
Function value
Return random value between (-5, -3) or (3, 5)
Setup variables for the ball, ball aim, and game states
Function move(player, change)
State[player] += change
Function rectangle(x, y, width, height)
Pen up
Go to(x, y)
Pen down
Begin fill
For i in range(2)
forward(width)
left(90)
forward(height)
left(90)
End fill
Function draw
Clear screen
Rectangle(-200, state[1], 10, 50)
Rectangle (190, state[2], 10, 50)
Ball.move(aim)
X = ball.x
Y = ball.y
Pen up
Go to(x, y)
dot(10)
update()
If y < -200 or y > 200:
Aim.y = -aim.y
If x < -185:
Low = state[1]
High = state[1] + 50
If low <= y <= high:
Aim.x = -aim.x
Else:
Return
If x > 185:
Low = state[2]
High = state[2] + 50
If low <= y <= high:
Aim.x = -aim.x
Else:
Return
wait(50)
draw()
Setup window
Set up turtle
Lambda function(magnitude, Up)
Lambda function(magnitude, Down)
Lambda function(magnitude, Up)
Lambda function(magnitude, Down)
draw()
done()
```

## Main Steps:

This project can be broken down into 4 main steps:

1. Import libraries and set up variables
3. Create the draw function
4. 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:

```from random import choice, random
from turtle import *
from freegames import vector
```

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:

```def value():
return (3 + random() * 2) * choice([1, -1])
ball = vector(0, 0)
aim = vector(value(), value())
state = {1: 0, 2: 0}
```

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:

```def move(player, change):
state[player] += change
```

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:

```def rectangle(x, y, width, height):
up()
goto(x, y)
down()
begin_fill()
for count in range(2):
forward(width)
left(90)
forward(height)
left(90)
end_fill()
```

### 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:

```def draw():
clear()
rectangle(-200, state[1], 10, 50)
rectangle(190, state[2], 10, 50)
ball.move(aim)
x = ball.x
y = ball.y
up()
goto(x, y)
dot(10)
update()
```

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:

``` if y < -200 or y > 200:
aim.y = -aim.y
if x < -185:
low = state[1]
high = state[1] + 50
if low <= y <= high:
aim.x = -aim.x
else:
return
if x > 185:
low = state[2]
high = state[2] + 50
if low <= y <= high:
aim.x = -aim.x
else:
return
ontimer(draw, 50)
```

### 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:

```setup(420, 420, 370, 0)
hideturtle()
tracer(False)
listen()
onkey(lambda: move(1, 20), 'w')
onkey(lambda: move(1, -20), 's')
onkey(lambda: move(2, 20), 'i')
onkey(lambda: move(2, -20), 'k')
draw()
done()
```

## 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!

Geekedu is an expert in Coding and Math learning. Our goal is to inspire and empower youth to use their knowledge of technology to become the influencers, inventors and innovators of the future.