Session 3: Coach notes
Goal of this session
After the Basic checkpoint a ninja can:
- Create a bullet when SPACE is pressed and store it in a list.
- Make bullets move up and remove them when they go off the screen.
- Understand why you use
bullets[:]and notbulletswhen removing inside a loop.
Preparation
- Starter ZIP ready on USB and the site.
- Run the solution once yourself so you know the timing of the steps.
- Dojo Defender: today ninjas add a shooting mechanism to their project.
Schedule (3 hours)
| Time | Activity |
|---|---|
| 0:00 to 0:10 | Looking back at session 2 + intro |
| 0:10 to 0:20 | Step 1: moving the ship (review) |
| 0:20 to 1:00 | Step 2: shooting + moving the bullets |
| 1:00 to 1:45 | Step 3: collision with enemies |
| 1:45 to 2:15 | Step 4: moving enemies + game over |
| 2:15 to 2:45 | Dojo Defender: adding the shooting mechanism |
| 2:45 to 3:00 | Showcase + cliffhanger |
Common questions
"bullets gives a RuntimeError while removing"
The ninja is looping over the original list while removing. Get them to use bullets[:] (a copy).
“My bullets don’t show up”
Check whether kogel.draw() is in draw() (in the loop over bullets). Also check whether bullets is empty at the start.
"on_key_down isn’t recognised"
This is a Pgzero callback: the function has to be called exactly that. Check for typos.
Hints for Stretch and Expert
Stretch: collision:
The nested loop (bullets × enemies) is the core of it. Explain why you need break after removing, otherwise Python crashes because you have already changed the bullets list while the inner loop keeps running.
Expert: moving enemies:
Use the existing timer variable. Explain that % 90 triggers every 90 frames, which at 60 fps comes down to 1.5 seconds per step.
Cliffhanger script
“Next time: no aliens, but a classic challenger: PONG. Two players, one ball, and you learn the real pygame way of programming.”
Dojo Defender today
Ninjas who join in with Dojo Defender add shooting and enemies to their S1 project today.
Time split: spend ~45 min on Dojo Defender after the regular S3 content.
Steps:
- Open the S2 starter (a copy of the S1 solution).
- Step 1-2: shooting with SPACE and cooldown.
- Step 3: spawning enemies.
- Step 4: collision + waves.
Starter: dojo-defender-s3-starter.zip
Solution: dojo-defender-s3-solution.zip
Worksheet: /dojo-defender/session-2/
Note: Ninjas who missed S1 need the S1 solution as a starting point.