Session 7: Coach notes
Goal of this session
At the end of the Basic checkpoint a ninja can:
- Write a class with
__init__, attributes and anupdate()method. - Instantiate bugs and keep them in a list.
- Apply collision detection between bullets and bugs.
What is not expected after session 7: subclasses, math.sin movement, DuikBug. Those are Stretch/Expert material.
Preparation (30 min before the session)
- Explain classes in 5 minutes on the projector. This is the most important new concept. Use a simple example:
class Hond:withself.naamand ablaf()method. Ask ninjas for analogies. import mathis already in the starter, so ninjas don’t have to look that up themselves for Step 3.- Check the spawn timer in the starter (by default every 90 frames). Change it if the difficulty isn’t right for your group.
Schedule (3 hours)
| Time | Activity |
|---|---|
| 0:00 to 0:10 | Welcome, looking back at session 6 |
| 0:10 to 0:25 | Projector: explaining classes and instances |
| 0:25 to 1:30 | Working on your own: steps 1 and 2 |
| 1:30 to 2:15 | Stretch (SlingerBug) |
| 2:15 to 2:45 | Expert (DuikBug) + buffer |
| 2:45 to 3:00 | Showcase + cliffhanger |
Common questions
“AttributeError: ‘Bug’ object has no attribute ‘alive’”
self.alive = True is missing from __init__. Have the ninja check __init__: is the indentation right? Does alive have self. in front of it?
“SlingerBug doesn’t move; it just goes straight down”
super().__init__ calls Bug.__init__, but self.timer isn’t created yet if Bug.update() gets called instead of SlingerBug.update(). Ask: “Which update() are you calling?” Python looks up update in the most specific class first (the subclass).
“DuikBug flies off the screen”
The player_x_ref lambda capture sometimes works in surprising ways. Check that the lambda uses lambda: player_x (a function), not player_x (the value at the moment you create it). If player_x is a global variable, the lambda works correctly.
“Bullets disappear but the bugs don’t”
The collision loop removes the bullet but forgets bug.alive = False or bugs.remove(bug). Have the ninja compare the step-2 code with their own version, line by line.
Hints for Stretch and Expert
Stretch: SlingerBug:
math.sin(timer) gives a value between -1 and 1. Multiply it by a number (for example 60) for the amplitude of the swing. timer has to go up by a small number every frame (for example 0.05); bigger means swinging faster.
Expert: DuikBug:
The lambda lambda: player_x only works if player_x is a variable that is in scope. If the player is a pygame.Rect, use lambda: player_rect.centerx instead. Emphasise that this is a reference to a function, not a value; it gives the current position every time.
Dojo Defender today
In this session ninjas also build the boss for Dojo Defender (session 6 of the DD track). See the DD session 6 coach notes for the full guidance.
Dojo Defender schedule
| DD part | Time | When? |
|---|---|---|
| DD Step 1: Boss variables | 15 min | After Bug Zapper Step 2 |
| DD Step 2: Spawning the boss + HP bar | 20 min | After Bug Zapper Step 3 |
| DD Step 3: Boss AI (3 phases) | 30 min | After Bug Zapper Step 4 |
| DD Step 4: Defeating the boss | 10 min | At the end |
Starter ZIP
Build the starter ZIP from the DD S5 solution before the session starts:
code/sessions/python/06-platformer/dojo-defender-solution/ → zip as dojo-defender-s7-starter.zip.
Make sure boss.png, boss_minion.png and boss_roar.wav are in the ZIP.
Cliffhanger script
Read this out to the group:
“Today you wrote your own classes: bugs that think, move and dive at you. Next time you build an endless highway in the sky that SCROLLS past, with obstacles that come faster and faster. Can you survive it?”