Dojo Defender Session 3: Coach notes

Goal of this session

After this session a ninja can:

  • Explain the difference between pgzero and raw pygame
  • Write the game loop themselves (while running: clock.tick(60))
  • Convert actors to GameObject with Surface and Rect
  • Handle input with pygame.key.get_pressed() and KEYDOWN
  • Load and play sound with pygame.mixer

Preparation (30 min before the session)

  • Starter ZIPs ready on USB and on the site: dojo-defender-s4-starter.en.zip and dojo-defender-s4-solution.en.zip.
  • Run the starter once on the dojo room laptop.
  • Make sure the sounds are ready in dojo-defender-assets/sounds/, or point ninjas to the solution.
  • Explain why we switch: “pgzero is a set of training wheels. Now you become real Pygame developers.”

Schedule (3 hours)

This session is an integral part of the normal session 4 (Pong). Use the time as follows:

TimeActivity
0:00 to 1:15Session 4: Pong (step 1-4)
1:15 to 1:30Dojo Defender intro: pgzero vs pygame explained
1:30 to 2:00Explore the code + restart (Basic)
2:00 to 2:30Adding sound (Stretch)
2:30 to 2:50Expert: mute button, volume, particles
2:50 to 3:00Showcase + cliffhanger

Common questions

“The images aren’t loaded” The .png extension is probably missing. In pgzero that happened automatically, in pygame it doesn’t. Check: GameObject('player_ship.png', ...).

"pygame.image.load gives an error" The path has to be right. The starter uses _DIR to determine the script path. Add print(_DIR) to debug.

“The sound doesn’t work” Check that pygame.mixer.init() has been called. Without that line pygame.mixer.Sound() does nothing.

“I hear nothing with bg_music” pygame.mixer.music.play(-1) is needed — without -1 it plays only once.

“Why is everything in one while loop?” Because pygame has no draw() and update() functions. Everything happens in the loop. This is how real games work: every frame, everything again.


Hints for Stretch and Expert

Stretch: sound The simplest approach: copy the sounds from the solution folder. Or download the dojo-defender-assets separately.

Expert: mute Use one muted boolean. pygame.mixer.music.set_volume(0) mutes the music. For SFX you have to wrap every play() in if not muted:.

Expert: particle effect A simple particle: store (x, y, dx, dy, lifetime) in a dict. Move and fade them every frame. Remove them when the lifetime is 0.


Cliffhanger script

“You moved Dojo Defender to real pygame and added sound. Next time: POWER-UPS. Shields, speed boosts, spread shots. And a real boss at the end of the waves.”