Lesson 17: Ship Hits + Lives + Game Over (Matthes Ch 13 ยง6)
Learning Intentions
- Add real stakes โ the ship can be hit, has 3 lives, game ends when they run out.
- Aliens reaching the bottom also counts as a hit.
Success Criteria
- When an alien touches my ship, I lose a life.
- I have 3 lives total; game ends when all are gone.
- Aliens reaching the bottom also counts as a hit.
๐ Do Now โ Error of the Day
Look at your L16 game. Right now, nothing bad happens if an alien reaches the bottom. Is that fair? What should happen?
๐ I Do โ Teacher Demo
Live-code the ship-alien collision check with spritecollideany. Build _ship_hit() with sleep(0.5) + reset. Add _check_aliens_bottom() (reuses _ship_hit). Track ships_left as a simple counter (GameStats class comes next lesson).
๐ค We Do โ Build Together
Class adds stakes together. Run into an alien โ ship dies, lives drop, ship re-centres, 0.5s pause. Lose 3 lives โ game exits (for now).
๐ You Do โ Your Turn
Tune ship_limit. Try 1 (brutal), 3 (book), 5 (easy). Arcade quests cover spritecollideany + counter tracking.
- Add self.ships_left = 3 to Game.__init__
- Add pygame.sprite.spritecollideany(self.ship, self.aliens) check each frame
- Build _ship_hit(): sleep(0.5), empty groups, respawn fleet + ship
- Add _check_aliens_bottom() โ treat bottom-reach the same as ship-hit
๐ฎ Practice Quests โ work through these
Loading practice quests for this lesson...
Three tiers โ pick yours
Support
_ship_hit pre-written
Core
build from scratch
Extension
flash + brief invincibility
Extensions
- Flash the ship briefly after respawn for a visual indicator.
- Add a 'ships_left' printout to the top-left corner (preview of L18 HUD).
๐ Exit Ticket โ Coding Journal
Journal: why does _ship_hit call sleep(0.5)? What would happen if we didn't?
Evidence of Learning
Lives decrement on hit; game ends cleanly after 3 deaths.