Pong Level 3 — Bounce physics and 2 players
Environment:
the examples in this lesson are written for Processing Python Mode. If you work in the recommended Thonny + py5 environment, 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 Thonny + py5 environment, 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 11 of 20 · Python and Processing
In the previous lesson, Pong Level 2, we added the score and the game over logic.
Now we improve the game and make a more serious version:
- two players
- a more realistic bounce of the ball
- a gradual increase in speed
- a better feeling of control
1. New variables
# Ball
ball_x = 350
ball_y = 200
dx = 5
dy = 4
ball_size = 20
max_speed = 10
# Paddles
paddle_width = 15
paddle_height = 80
left_y = 160
right_y = 160
left_speed = 0
right_speed = 0
# Score
score_left = 0
score_right = 0
What is new:
We added
We added
max_speed to limit the speed of the ball and stop the game from becoming unplayable.
2. Setup
def setup():
size(700, 400)
textSize(24)
3. The main game logic
def draw():
global ball_x, ball_y, dx, dy
global left_y, right_y
global score_left, score_right
background(20)
# Centre line (a visual detail)
for y in range(0, height, 20):
rect(width/2 - 2, y, 4, 10)
# Ball
ellipse(ball_x, ball_y, ball_size, ball_size)
# Paddles
rect(30, left_y, paddle_width, paddle_height)
rect(width-45, right_y, paddle_width, paddle_height)
# Moving the ball
ball_x += dx
ball_y += dy
# Bouncing off the top and bottom
if ball_y < ball_size/2 or ball_y > height-ball_size/2:
dy = -dy
# Collision with the left paddle
if (ball_x < 45 and
left_y < ball_y < left_y + paddle_height):
dx = abs(dx)
# Angle effect (depends on where the ball hits the paddle)
offset = (ball_y - (left_y + paddle_height/2)) / (paddle_height/2)
dy += offset * 2
# Collision with the right paddle
if (ball_x > width-45 and
right_y < ball_y < right_y + paddle_height):
dx = -abs(dx)
offset = (ball_y - (right_y + paddle_height/2)) / (paddle_height/2)
dy += offset * 2
# Gradual speed-up (but limited)
dx *= 1.001
dy *= 1.001
dx = constrain(dx, -max_speed, max_speed)
dy = constrain(dy, -max_speed, max_speed)
# Points
if ball_x < 0:
score_right += 1
resetBall()
if ball_x > width:
score_left += 1
resetBall()
# Moving the paddles
left_y += left_speed
right_y += right_speed
left_y = constrain(left_y, 0, height-paddle_height)
right_y = constrain(right_y, 0, height-paddle_height)
# Score
text(score_left, width/4, 40)
text(score_right, width*3/4, 40)
The key new feature — bounce physics
The ball no longer always bounces at the same angle.
- hitting the centre → a straight bounce
- hitting the edge → a bigger angle
We do this with the offset value.
4. Resetting the ball
def resetBall():
global ball_x, ball_y, dx, dy
ball_x = width/2
ball_y = height/2
# direction left or right, at random (random(1) gives a number from 0 to 1)
dx = 5 if random(1) < 0.5 else -5
dy = random(-3, 3)
Tip: A smaller starting
dy gives better control of the game at the start.
5. Keyboard controls
- Player 1: W / S
- Player 2: ↑ / ↓
def keyPressed():
global left_speed, right_speed
if key == 'w':
left_speed = -6
if key == 's':
left_speed = 6
if keyCode == UP:
right_speed = -6
if keyCode == DOWN:
right_speed = 6
def keyReleased():
global left_speed, right_speed
if key in ['w','s']:
left_speed = 0
if keyCode in [UP, DOWN]:
right_speed = 0
Why is this important?
It allows smooth movement — the paddle moves only while you hold the key down.
It allows smooth movement — the paddle moves only while you hold the key down.
What did we improve?
- a more realistic bounce of the ball
- control of the angle of movement
- gradual speed-up of the game
- playing with two players
- visual improvements (the centre line of the court)
Challenges (advanced)
- Add a maximum score (e.g. 10) and a winner
- Add an AI opponent
- Add a sound when the ball collides
- Add a menu (start / restart)
Related lessons
Check your knowledge
Open the quiz
Mini quiz: bounce physics
1. What is offset used for when the ball hits the paddle?
2. Why do we use constrain() for the speed?
