L11 ยท T2 Week 6 ๐ŸŽฏ CT5-DPM-01 ยท CT5-THI-01 ๐Ÿ“‚ Phase 2 โ€” Alien Invasion ยท Ch 12 Ship + Bullets

Lesson 11: Firing Bullets + Sprite Groups (Matthes Ch 12 ยง8 pt 2)

Learning Intentions

Success Criteria

๐Ÿ“ Do Now โ€” Error of the Day

What would go wrong if we used a plain Python list for bullets instead of pygame.sprite.Group? (Hint: think about draw + update calls.)

๐Ÿ‘ I Do โ€” Teacher Demo

Live-code: self.bullets = pygame.sprite.Group(). Spacebar KEYDOWN โ†’ self._fire_bullet(). Call self.bullets.update() and loop draws in _update_screen.

๐Ÿค We Do โ€” Build Together

Class wires up the spacebar event together. Holding spacebar โ†’ stream of bullets flies up the screen. Check: raise hand when yours works.

๐Ÿš€ You Do โ€” Your Turn

Tune bullet_speed + bullet_color. Watch what happens when bullets fly off the top (next lesson will fix this). Arcade quests cover Sprite Groups.

  • In Game.__init__: self.bullets = pygame.sprite.Group()
  • Extend _check_keydown_events: on K_SPACE, call self._fire_bullet()
  • Write _fire_bullet(self) that creates a Bullet and adds it to self.bullets
  • In _update_screen: self.bullets.update() + for bullet in self.bullets.sprites(): bullet.draw_bullet()

๐ŸŽฎ Practice Quests โ€” work through these

Loading practice quests for this lesson...

Three tiers โ€” pick yours

Support

_fire_bullet pre-written

Core

implement from scratch

Extension

custom bullet visuals

Extensions

๐Ÿ“ Exit Ticket โ€” Coding Journal

Journal: what's one thing pygame.sprite.Group gives you for free that a plain list wouldn't?

Evidence of Learning

Student can fire multiple bullets on spacebar โ€” they all fly up the screen.

โ† PreviousLesson L10โ†‘ HubAll LessonsNext โ†’Lesson L12