Session 5: Breakout
A ball bounces back and forth and smashes rows of coloured bricks to pieces. You steer the paddle to keep the ball in the air. And this time you hear it too: there are sound effects on every collision.
What you’ll learn today
- Loading and playing sound with
pygame.mixer - Collisions with several objects (a list of bricks)
- Making the ball bounce in the right direction
- Building a win and lose screen
Step 0: Setup
- Open Thonny.
- Download the starter via this link, unpack the ZIP and open
main.py. - Click Run. You see the paddle, the ball and the bricks. The ball already moves horizontally but not vertically. You’re going to change that.
Step 1: Make the ball move vertically
Find this line in the starter:
ball_dx, ball_dy = 4, 0
# ball_dy = -4 ← remove the # to make the ball move!Remove the # in front of ball_dy = -4. Now the ball has a vertical speed. Add movement in the game loop (look for the update section):
ball.x += ball_dx
ball.y += ball_dyClick Run. The ball moves diagonally but disappears off the edges.
Step 2: ✅ Basic: ball bounces off walls and paddle
Add bouncing:
# Top, left and right edge
if ball.left <= 0 or ball.right >= WIDTH:
ball_dx = -ball_dx
if ball.top <= 0:
ball_dy = -ball_dy
# Paddle
if ball.colliderect(paddle):
ball_dy = -ball_dy
if snd_hit:
snd_hit.play()Lives: If the ball reaches the bottom, you lose a life:
if ball.top > HEIGHT:
levens -= 1
# Reset the ball on top of the paddle
ball.center = (paddle.centerx, paddle.top - BALL_SIZE)
ball_dy = -4Step 3: ⭐ Stretch: breaking bricks
Check the collision with every brick in the loop:
for steen in stenen[:]:
if ball.colliderect(steen['rect']):
stenen.remove(steen)
ball_dy = -ball_dy
score += 10
if snd_hit:
snd_hit.play()
break # break only one brick per frameWin condition: If all the bricks are gone, the ninja has won. Show a message:
if not stenen:
# show win screenStep 4: 🔥 Expert: lives, explosion sound and speed
Explosion sound when a brick breaks (use snd_explode from the starter):
if snd_explode:
snd_explode.play()Game over at 0 lives: show a screen and stop the ball.
Speeding up as fewer bricks are left:
snelheid_factor = 1 + (1 - len(stenen) / totaal_stenen) * 0.5
ball_dx_huidig = ball_dx * snelheid_factorA new row of bricks when everything is gone: call stenen = maak_stenen() again and raise the difficulty.
Showcase
Who clears the most bricks? Compare scores and show your coach the longest streak.
See you next time!
“Next time: gravity. Your character falls, and you have to make it jump on platforms.”
Take it home
- Easy: change the colours of the bricks to your favourite colours.
- Medium: make some bricks twice as strong (they have to be hit twice).
- Hard: add a power-up that makes the paddle wider for a while.
- Very hard: add a second ball that spawns when score > 50.
View the cheatsheet for this session
Dojo Defender
Dojo Defender gets a menu and new enemy types! Download the starter. Were you not there? Download the solution from session 3. Worksheet: Dojo Defender Session 4.