Lesson 12: Bullet Cleanup + Fire Limits (Matthes Ch 12 ยง8 pt 3)
Learning Intentions
- Remove bullets once they leave the top of the screen.
- cap the number of bullets on screen at once.
Success Criteria
- Old bullets get removed when they reach the top of the screen.
- I can only have N bullets (say 3) on screen at once.
- I can explain why we loop over bullets.copy() not bullets.
๐ 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
- Add a fire cooldown (200 ms between shots) so holding Space doesn't spam.
- Draw a small counter showing bullets_remaining (preview of scoreboard).
๐ 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.