Sub topic 5
Modeling Outcomes as a State Machine
Assume you understand the core gameplay loop, common game mechanics, and basic Python conditionals and functions. This chapter uses a state machine to make win, lose, and recoverable failure rules explicit.
Why use a state machine?
A state is the condition the game is currently in. For outcome design, useful states might include:
PLAYING: The player can continue the current attempt.WON: The player has achieved the objective.LOST: The player has reached a final defeat.RESPAWNING: The game is processing a death and preparing a retry.
A state machine is a model that lists valid states and the events that move the game between them. Instead of scattering outcome checks throughout the code, you define which transitions are allowed and what each event means.
For example, collecting the final relic might move the game from PLAYING to WON. Losing a life might move it briefly to RESPAWNING, then back to PLAYING. Losing the last life moves it to LOST.
Define states and transitions explicitly
The state machine should answer three questions:
- What states can exist?
- What event causes each transition?
- What happens when several conditions are true at once?
An outcome function can express the last question clearly. This example gives defeat priority over victory when health reaches zero on the same update that the player collects the final relic.
from enum import Enum, auto
class Outcome(Enum):
PLAYING = auto()
WON = auto()
LOST = auto()
def evaluate(health, relics, relics_needed):
# Defeat has priority during a simultaneous win and loss.
if health <= 0:
return Outcome.LOST
if relics >= relics_needed:
return Outcome.WON
return Outcome.PLAYING
if __name__ == "__main__":
examples = [
(10, 2, 3),
(10, 3, 3),
(0, 3, 3),
]
for health, relics, needed in examples:
print(evaluate(health, relics, needed).name)The output is:
PLAYING
WON
LOSTThe order of the checks is part of the game design. Reversing the first two checks would make the simultaneous case a win. Neither choice is universally correct, but the choice must be intentional and consistent.
Separate fail states from final states
A fail state is a setback from which the player can recover. A lose state ends the current run, match, or level. A player death is often a fail state while retries remain, but it becomes a lose state when the final retry is used.
from enum import Enum, auto
class Outcome(Enum):
PLAYING = auto()
LOST = auto()
class Run:
def __init__(self, lives, checkpoint):
self.lives = lives
self.checkpoint = checkpoint
self.position = checkpoint
def on_player_death(self):
self.lives -= 1
if self.lives <= 0:
return Outcome.LOST
self.position = self.checkpoint
return Outcome.PLAYING
if __name__ == "__main__":
run = Run(lives=2, checkpoint="bridge")
print(run.on_player_death().name, run.lives, run.position)
print(run.on_player_death().name, run.lives, run.position)The first death returns the player to the checkpoint. The second death ends the run. Notice that the recovery action is part of the transition, not an unrelated side effect hidden in the user interface.
Choose transition precedence
Games often evaluate several events in one update. Define a priority policy for cases such as:
- The player reaches the goal while taking fatal damage.
- A timer expires while an objective is completed.
- Two players satisfy a victory condition at the same time.
- A scripted event occurs during a respawn.
A common policy is to evaluate terminal outcomes first, then recoverable failures, then ordinary play. Another game may favor the player by checking victory first. Document the policy in code and test each boundary case.
Test the state machine
Playtests should verify transitions, not only whether the normal path feels good. Test values just below, exactly at, and just above every threshold. Confirm that a death with one life remaining differs from a death with several lives remaining. Also test simultaneous events and repeated events after a terminal result.
A useful transition table might look like this:
| Current state | Event | Next state |
|---|---|---|
| PLAYING | Final relic collected | WON |
| PLAYING | Death with lives remaining | PLAYING after checkpoint recovery |
| PLAYING | Death with no lives remaining | LOST |
| WON | Ordinary input | WON |
| LOST | Ordinary input | LOST |
Exercise
A game checks relics >= needed before health <= 0. The player has zero health and collects the final relic in the same update. What outcome occurs, and what design decision does this code express?
Show answer
The outcome is WON. The check order gives victory priority over defeat for simultaneous events. If that is not intended, check the lose condition first.