Lesson 21: High Scores + Level + Ships Remaining (Matthes Ch 14 ยง5-7)
Learning Intentions
- Add the final HUD pieces โ high score (saved across runs), current level, ship-icon lives display.
Success Criteria
- My high score persists after I close + reopen the game.
- Current level displays under the score.
- Remaining lives show as ship icons in the top-left.
๐ Do Now โ Error of the Day
Your Alien Invasion is feature-complete except for one thing: when you close the game, your top score VANISHES. Who remembers what the highest score was last lesson?
๐ I Do โ Teacher Demo
Live-code high score persistence. json.dump(self.stats.high_score, open('high_score.json', 'w')). Load with try/except FileNotFoundError. Extend Scoreboard with prep_high_score, prep_level, and prep_ships (reuses Ship as life icons).
๐ค We Do โ Build Together
Class adds all 3 HUD elements. Full HUD: score top-right, high score top-centre, level under score, lives top-left as ship icons.
๐ You Do โ Your Turn
Beat your own high score. Close the game โ reopen โ check that high_score.json persists. Open it in VS Code to see the number. Arcade quests cover json file I/O.
- Add self.high_score = 0 + load from high_score.json in GameStats
- Add _save_high_score() using json.dump
- Extend Scoreboard: prep_high_score, prep_level, prep_ships (reuses Ship class)
- Update high score on every score change
- In show_score: blit all four (score, high_score, level, ships)
๐ฎ Practice Quests โ work through these
Loading practice quests for this lesson...
Three tiers โ pick yours
Support
prep_* methods scaffolded; json helpers pre-written
Core
build from scratch
Extension
top 5 leaderboard
Extensions
- Store TOP 5 high scores in a list instead of just one.
- Flash 'NEW HIGH SCORE!' briefly when it's beaten.
๐ Exit Ticket โ Coding Journal
Journal: describe what's in your high_score.json file. Why is it a text file, not binary? What else could we save there?
Evidence of Learning
Full HUD: score + high score + level + ship icons. high_score.json persists across runs.