Session 4: Pong
Two paddles, one ball: the classic game of Pong. But there’s a surprise: this session you write the game in real pygame, without pgzrun. You learn to set up the game loop yourself. That’s how most real games work.
What you’ll learn today
- The pygame game loop: events, update, draw
pygame.Rectfor rectangles and collisions- Making the ball bounce (reversing the speed)
- Keeping score for two players
Step 0: Setup
- Open Thonny.
- Download the starter via this link, unpack the ZIP and open
main.py. - Click Run. You see two paddles and a yellow ball in the middle, but the ball doesn’t move yet.
Difference with pgzrun: There are no draw() and update() functions any more. Everything sits in one while running: loop.
Step 1: Make the ball move
Find the lines:
# bal.x += ball_dx # turn this on when you're done
# ball.y += ball_dyRemove the # in front of both lines. But first set a starting speed. Find ball_dx, ball_dy = 0, 0 and change it to:
ball_dx, ball_dy = 4, 4Click Run. The ball moves now, but it disappears off the edge. We’ll fix that in step 2.
Step 2: ✅ Basic: the ball bounces off the walls and the paddles
Add the following after ball.x += ball_dx and ball.y += ball_dy:
# Bounce off the top and bottom edge
if ball.top <= 0 or ball.bottom >= HEIGHT:
ball_dy = -ball_dy
# Bounce off the paddles
if ball.colliderect(left_paddle) or ball.colliderect(right_paddle):
ball_dx = -ball_dxThe ball now bounces forever. Also add limits for the paddles, so they don’t go off the screen. Have a look at the code that’s already in the starter.
Step 3: ⭐ Stretch: keeping score
If the ball leaves the screen on the left, the right-hand player scores. If it goes out on the right, the left-hand player scores:
# Ball out on the left → right scores
if ball.left <= 0:
score_right += 1
ball.center = (WIDTH // 2, HEIGHT // 2)
ball_dx = 4
# Ball out on the right → left scores
if ball.right >= WIDTH:
score_left += 1
ball.center = (WIDTH // 2, HEIGHT // 2)
ball_dx = -4The score is already drawn in draw(). Check that the variables are right.
Step 4: 🔥 Expert: win condition and speeding up
Win at 5 points: Add a variable won. When a player reaches 5, stop the game:
if score_left >= 5:
won = "Left wins!"
if score_right >= 5:
won = "Right wins!"Show the winner on the screen and stop the movement.
Speeding up: After every rally the ball gets a bit faster:
if ball.colliderect(left_paddle) or ball.colliderect(right_paddle):
ball_dx = -ball_dx * 1.05 # 5% faster
ball_dy = ball_dy * 1.05Build in a maximum: ball_dx = max(-12, min(12, ball_dx)).
Showcase
Play a match against a buddy. First to 5 points wins. Show it to a coach.
See you next time!
“Next time: Breakout: the ball smashes bricks to pieces. And there’s SOUND.”
Take it home
- Easy: change the colour of the ball or the paddles.
- Medium: make one paddle bigger as that player falls behind (handicap).
- Hard: add an AI opponent: the right paddle follows the ball automatically.
- Very hard: add power-ups: an item that doubles the ball or makes the paddle bigger.
Dojo Defender
Today Dojo Defender moves to real Pygame! Download the starter. Were you not there? Download the solution from session 2. Worksheet: Dojo Defender Session 3.