Lesson 18: Play Button + GameStats class (Matthes Ch 14 ยง1)
Learning Intentions
- Refactor the scattered ship_left counter into a proper GameStats class.
- Add a Play button so the game starts on click, not on launch.
Success Criteria
- My game_stats.py has a GameStats class with reset_stats() and game_active flag.
- Clicking 'Play' starts the game; losing shows the Play button again.
- I can explain why we hide the mouse during play.
๐ Do Now โ Error of the Day
Right now your game ends and the window freezes. A real game shows a 'Play' button so the player can start again. Today we add one.
๐ I Do โ Teacher Demo
Live-code GameStats class (extracts ships_left + adds game_active flag). Then the Button class โ a Rect with rendered text. Hook MOUSEBUTTONDOWN โ _check_play_button.
๐ค We Do โ Build Together
Class wires up click-to-start together. Play button shows on launch + after losing. Click โ game resets, mouse hides, play begins.
๐ You Do โ Your Turn
Customise button colour + text. Extension: show a second button like 'HIGH SCORE'. Arcade quests cover MOUSEBUTTONDOWN + collidepoint.
- Create game_stats.py with class GameStats โ reset_stats() sets ships_left + game_active
- Create button.py with class Button โ a Rect + rendered text
- Add self.play_button = Button(self, 'Play') to Game.__init__
- Add MOUSEBUTTONDOWN handler โ _check_play_button
- On play click: reset stats, empty groups, create new fleet, hide mouse
๐ฎ Practice Quests โ work through these
Loading practice quests for this lesson...
Three tiers โ pick yours
Support
GameStats + Button skeletons pre-written
Core
build from scratch
Extension
animated button
Extensions
- Add a 'HIGH SCORE' title above the Play button.
- Show a flashing 'CLICK TO PLAY' hint.
๐ Exit Ticket โ Coding Journal
Journal: explain the game_active flag โ why do we need it separate from ships_left?
Evidence of Learning
Game starts with Play button visible. Clicking it starts play. Dying shows it again.