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

Lesson 12: Bullet Cleanup + Fire Limits (Matthes Ch 12 ยง8 pt 3)

Learning Intentions

Success Criteria

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

Run your L11 game. Hold spacebar for 10 seconds without moving. Then print(len(self.bullets)) in the loop. What number do you see climbing?

๐Ÿ‘ I Do โ€” Teacher Demo

Show the problem: bullets off-screen still live in the Group. List grows forever, game slows down. FIX: loop for bullet in self.bullets.copy(): and remove offscreen ones. Add bullets_allowed to Settings.

๐Ÿค We Do โ€” Build Together

Class adds cleanup + limit together. Hold spacebar โ€” len(bullets) stays at 3. Explain why .copy() matters when modifying a collection during iteration.

๐Ÿš€ You Do โ€” Your Turn

Tune bullets_allowed. Try 1 (limited), 3 (book default), 10 (overwhelming). Which feels right? Arcade quests cover cleanup + len() checks.

  • Add self.bullets_allowed = 3 to Settings
  • In _fire_bullet, wrap body in: if len(self.bullets) < self.settings.bullets_allowed:
  • Add cleanup loop: for bullet in self.bullets.copy(): if bullet.rect.bottom <= 0: self.bullets.remove(bullet)
  • Print len(self.bullets) each frame โ€” confirm it stays โ‰ค 3

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

Loading practice quests for this lesson...

Three tiers โ€” pick yours

Support

cleanup loop pre-written

Core

implement from scratch

Extension

cooldown timer

Extensions

๐Ÿ“ Exit Ticket โ€” Coding Journal

Journal: why does our cleanup loop iterate over bullets.copy() and not bullets?

Evidence of Learning

Bullets disappear off the top. At most N on screen at once.

โ† PreviousLesson L11โ†‘ HubAll LessonsNext โ†’Lesson L13