Lab: game design patterns
Apply the observer pattern, component architecture, and spatial hashing to realistic game design decisions.
- Choose between observer, polling, and direct coupling for event handling
- Decide when to use component architecture over inheritance
- Recognise when spatial hashing is the right optimisation for collision
Optional scenario lab. Design patterns are judgment calls — knowing the pattern is only half the job. Practice recognising which one fits a situation and why.
Scenarios: game design patterns
- 1.A player picks up a coin. The score UI, the sound system, and the achievement system all need to react. You don't want the player object to know about any of them directly. Best approach?
- 2.You have Player, Enemy, and NPC entities that all need health, but only Player needs input handling and only Enemy needs AI. Using inheritance,
GameObject→LivingThing→Playerquickly becomes tangled. Better approach? - 3.Spatial hashing is worth implementing for any game that has collision detection, regardless of entity count.
- 4.In a spatial hash, you set the cell size much smaller than your largest entity. What problem does this cause?
- 5.Your game's UI needs to update every time the player's HP changes. HP changes happen in the physics update, in the damage system, and via power-ups. Which pattern fits best?
The pattern-selection habit: observer when many systems react to one event; components when entities need mix-and-match capabilities; spatial hashing when brute-force collision becomes the bottleneck. A pattern applied where it doesn't fit costs more than it saves.
Spatial hashing
Checking all pairs of entities for collision is O(n²). A grid-based spatial hash reduces lookup to O(1) by assigning entities to cells and only testing neighbours.
Packaging concepts
Distributing a .py file requires the player to have Python and the right packages installed. Learn the options for bundling your game into something anyone can run.