Dojo Defender Session 2: Coach notes
Goal of this session
After this session a ninja can:
- Create bullets with
on_key_downand a cooldown mechanism - Spawn enemy objects and make them move with a sine oscillation
- Collision detection between bullets and enemies (nested loop)
- Set up a wave system with increasing difficulty
Preparation (30 min before the session)
- Starter ZIPs ready on USB and on the site:
dojo-defender-s3-starter.en.zipanddojo-defender-s3-solution.en.zip. - Run the starter yourself once on the dojo room laptop.
- Ninjas who missed S1: point them to the S1 solution.
Schedule (3 hours)
This session is an integral part of the normal session 3 (Space Blaster). Use the time as follows:
| Time | Activity |
|---|---|
| 0:00 to 1:00 | Session 3: Space Blaster (step 1-4) |
| 1:00 to 1:15 | Dojo Defender look back + downloading the starter |
| 1:15 to 1:45 | Step 1-2: shooting + cooldown |
| 1:45 to 2:15 | Step 3: spawning + moving enemies |
| 2:15 to 2:45 | Step 4: collision + waves |
| 2:45 to 3:00 | Showcase + cliffhanger |
Common questions
"‘on_key_down’ isn’t recognised"
This is a Pgzero callback — the function has to be called exactly on_key_down with key as its parameter. No typos.
“My bullets disappear immediately”
Bullets do y -= 8 but start at ship.y - 20. Check that bullet.y isn’t immediately below -10. If the ship is too high up, that can happen.
“RuntimeError: list changed during iteration”
The ninja uses for enemy in enemies (without [:]) while calling enemies.remove(). Explain that [:] makes a copy.
“The enemies don’t move in a sine”
The sine oscillation needs start_x, strafe_amplitude and strafe_speed as fields on the Actor. Newer ninjas can do a simple ping-pong movement first.
“The wave counter is wrong”
enemies_destroyed % 10 == 0 triggers every 10 kills. If the ninja doesn’t declare enemies_destroyed as global in the function, the counter doesn’t work.
Hints for Stretch and Expert
Basic: shooting cooldown Explain why a cooldown is needed: without a cooldown the computer fires thousands of bullets per second while SPACE is held down.
Stretch: enemies
spawn_interval = 120 is 2 seconds at 60 fps. As the score rises, the interval drops to 60 (1 second).
Expert: collision + waves
The nested loop (bullets × enemies) is the hardest concept. Draw it on the board: for every bullet you check all the enemies. break is needed to stop after a hit.
Expert: sine movement
math.sin(timer * speed + offset) * amplitude is a wave. timer makes the wave keep going. Without start_x the enemy drifts away from its starting position.
Cliffhanger script
“Today you shot down enemies and survived waves. Next time: POWER-UPS. Shields that protect you, speed boosts that make you faster, and spread shots that fire three bullets at once. Your ship gets stronger the longer you survive.”