Lesson 7: Refactoring โ Settings module + check_events (Matthes Ch 12 ยง5)
Learning Intentions
- Split alien_invasion.
- py into cleaner pieces โ a Settings class in its own file, an _check_events helper method โ so the main loop is readable.
Success Criteria
- My settings.py file contains a Settings class with screen_width/height/bg_color.
- My main file imports Settings and uses self.settings.screen_width.
- My game loop calls self._check_events() instead of a big inline for-loop.
๐ Do Now โ Error of the Day
Open your alien_invasion.py from L06. Count: how many hard-coded numbers can you find? (1200? 800? 230? Every one is a 'magic number' we'll tidy up today.)
๐ I Do โ Teacher Demo
Live-code the refactor: create settings.py with class Settings holding screen_width/height/bg_color. Import it in main. Replace every hard-coded number with self.settings.*. Then extract the event-handling for-loop into a helper method _check_events.
๐ค We Do โ Build Together
Class does the refactor together, file by file. Before/after diff on the projector. Run the game โ same behaviour, cleaner code.
๐ You Do โ Your Turn
Finish the Settings split on your own game. Add ship_speed = 1.5 to Settings now (you'll use it in L08). Add docstrings to each file. Arcade quests cover module imports + refactoring.
- Create settings.py with class Settings + __init__ holding 3 values
- In main: from settings import Settings + self.settings = Settings()
- Replace hard-coded 1200, 800, (230,230,230) with self.settings.* references
- Write _check_events(self) method + replace inline event loop with self._check_events()
๐ฎ Practice Quests โ work through these
Loading practice quests for this lesson...
Three tiers โ pick yours
Support
settings.py skeleton provided
Core
build from scratch
Extension
pre-add ship_speed in preparation for L08
Extensions
- Add ship_speed and bullet_speed to Settings now (L08 will use them).
- Add a docstring at the top of each file explaining what it does.
๐ Exit Ticket โ Coding Journal
Journal: 1 sentence โ why did we make the code MORE files, not fewer? What did we gain?
Evidence of Learning
Main game file is ~30 lines cleaner. Settings all in one place.