Mini project: the Pong game in Processing Python Mode
Environment:
the examples in this lesson are written for Processing Python Mode. If you work in the recommended environment Thonny + py5, most of the code stays the same, and names with a capital letter in the middle are written in lower case with an underscore:
the examples in this lesson are written for Processing Python Mode. If you work in the recommended environment Thonny + py5, most of the code stays the same, and names with a capital letter in the middle are written in lower case with an underscore:
mouseX → mouse_x, noStroke() → no_stroke(). See the table of differences.Lesson 9 of 20 · Python and Processing
What are we making?
We are making a simple version of the Pong game:
- The ball moves around the screen
- The player controls the paddle with the mouse
- The ball bounces off the walls and the paddle
Note:
This is excellent practice in combining everything we have learned so far: animation, variables, collision detection and interaction.
This is excellent practice in combining everything we have learned so far: animation, variables, collision detection and interaction.
Step 1 — Basic variables
# Ball
ball_x = 300
ball_y = 200
dx = 4
dy = 3
ball_size = 20
# Paddle
paddle_width = 100
paddle_height = 15
# Points
score = 0
Tip: You can add more paddles or change the size of the ball as an experiment.
Step 2 — The setup function
def setup():
size(600, 400)
textSize(20)
Result
A game window of 600×400 is created and the font for the points is set.
Step 3 — Drawing and moving the ball and the paddle
def draw():
global ball_x, ball_y, dx, dy, score
background(30)
# Ball
fill(255)
ellipse(ball_x, ball_y, ball_size, ball_size)
# The paddle follows the mouse
rect(mouseX - paddle_width/2, height - 40,
paddle_width, paddle_height)
# Points
text("Score: " + str(score), 20, 30)
# Moving the ball
ball_x += dx
ball_y += dy
# Bouncing off the left and right walls
if ball_x < ball_size/2 or ball_x > width - ball_size/2:
dx = -dx
# Bouncing off the top wall
if ball_y < ball_size/2:
dy = -dy
# Collision with the paddle
if (ball_y > height - 50 and
mouseX - paddle_width/2 < ball_x < mouseX + paddle_width/2):
dy = -dy
score += 1 # Increases the points
Result
The ball moves and bounces off the walls and the paddle. The paddle follows the mouse. The points increase when the ball hits the paddle.
Tip: Try changing the speed of the ball and the width of the paddle for different difficulty levels.
Step 4 — Game Over
Add a check for when the ball falls below the paddle:
if ball_y > height:
text("GAME OVER", width/2 - 70, height/2)
noLoop() # Stops draw()
Result
The game stops when the ball falls below the paddle and shows GAME OVER.
You can use
loop() to restart the game.
Step 5 — Advanced additions (bonus)
- Add more balls (multi-ball)
- Add a paddle colour that changes when the ball hits it
- Add sound or visual effects on a collision
- Add a restart button with
mousePressed()
Mini project: a challenge
Make a variant of Pong where the ball speeds up every time it hits the paddle.
if (ball_y > height - 50 and
mouseX - paddle_width/2 < ball_x < mouseX + paddle_width/2):
dy = -dy * 1.1
dx = dx * 1.1
score += 1
Tip: This adds "difficulty" and dynamics to the game.
Comparison with the Java version
The same game can also be made in Java Processing:
Processing with Java — projectsThe logic is identical, only the syntax is different.
What have we learned?
- Animating objects
- Detecting collisions with the paddle
- Interaction with the mouse
- Showing the result (score)
- The basic logic of the Pong mini game
Tip: Experiment with more balls, different speeds, or add an AI opponent for the next level of challenge.
Related topics
Check your knowledge
Open the quiz
Mini quiz: Pong
1. What does dx = -dx do?
2. What stops the repetition of draw()?
