Session 4: Coach notes
Goal of this session
After the Basic checkpoint a ninja can:
- Explain what the pygame game loop is and how it differs from pgzrun.
- Make the ball move and bounce off the top and bottom edge and off both paddles.
- Understand what
pygame.Rectis and howcolliderectworks.
This is the first session with real pygame; expect confusion about the game loop. That’s normal.
Preparation
- Starter ZIP ready. Test on the dojo laptop whether pygame is installed (not pgzero).
- Prepare a short explanation: “pgzrun did the loop for us. Now we do it ourselves.”
- Dojo Defender: ninjas migrate their project to pygame as well (the starter contains the basics).
Schedule (3 hours)
| Time | Activity |
|---|---|
| 0:00 to 0:15 | Looking back + explaining pygame vs pgzrun |
| 0:15 to 0:45 | Step 1 to 2: moving the ball + bouncing |
| 0:45 to 1:30 | Step 3: keeping score |
| 1:30 to 2:00 | Step 4: win condition + speed |
| 2:00 to 2:30 | Dojo Defender: pygame migration |
| 2:30 to 2:50 | Showcase: 1-vs-1 matches |
| 2:50 to 3:00 | Cliffhanger |
Common questions
“The ball moves but disappears straight away”
ball_dx and ball_dy are 0. The ninja hasn’t changed the starting values. Point at the line ball_dx, ball_dy = 0, 0.
“The paddle goes through the edge”
Add limits: if paddle.top > 0: for going up, if paddle.bottom < HEIGHT: for going down.
“The screen isn’t refreshed”
pygame.display.flip() is missing or sits outside the loop. Check the indentation.
Hints for Stretch and Expert
Stretch: score:
Reset the ball to the middle after every point: ball.center = (WIDTH // 2, HEIGHT // 2). Swap the direction of ball_dx so the losing player gets the ball back.
Expert: AI paddle:
A simple AI: if right_paddle.centery < ball.centery: right_paddle.y += 4 and the other way round. Limit the speed so the AI isn’t perfect.
Dojo Defender today
DD Session 3 — The starter is a complete port of DD S2 (pgzero) to raw pygame. Ninjas explore the code, add a restart (R), and after that sound. See the coach notes for the full schedule.
If a ninja wants to do both the Pong starter and the DD starter: Get them to focus on one of the two. DD is the harder option — suitable for ninjas who pick up Pong quickly or started earlier.
Sound in DD: The sounds are in _shared/dojo-defender-assets/sounds/. Ninjas can copy them to their own sounds/ folder. Or download the solution.
Cliffhanger script
“Next time the ball smashes bricks to pieces, and you hear it too. Breakout, with real sound.”