L07 ยท T2 Week 4 ๐ŸŽฏ CT5-DPM-01 ยท CT5-THI-01 ๐Ÿ“‚ Phase 2 โ€” Alien Invasion ยท Ch 12 Ship + Bullets

Lesson 7: Refactoring โ€” Settings module + check_events (Matthes Ch 12 ยง5)

Learning Intentions

Success Criteria

๐Ÿ“ 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

๐Ÿ“ 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.

โ† PreviousLesson L06โ†‘ HubAll LessonsNext โ†’Lesson L08