Sub topic 1
What Win, Lose, and Fail States Are and Why They Matter
Prerequisites
You should understand basic game mechanics and the core gameplay loop. You should also be comfortable reading Python conditionals and functions.
Win, lose, and fail states
A win state ends a run with success. A lose state ends a run with defeat, and the run cannot continue. A fail state is a setback: the player loses something, but the run goes on.
Players read outcomes as feedback on their skill. Vague rules make players blame the game instead of their play, so each state needs a precise definition.
Designing clear win conditions
A win condition should be a test on values the game already tracks, such as relics collected or boss health. "Reach the castle" can be tested every frame. "Feel like a hero" cannot.
Designing lose conditions
A lose condition is also a test on state. When a win and a lose condition are both true on the same frame, the order of the checks decides the result. The code below checks the lose condition first.
from enum import Enum, auto
class Outcome(Enum):
PLAYING = auto()
WON = auto()
LOST = auto()
def evaluate(health, relics, relics_needed):
# The lose check runs first, so a simultaneous win and loss resolves as a loss.
if health <= 0:
return Outcome.LOST
if relics >= relics_needed:
return Outcome.WON
return Outcome.PLAYING
print(evaluate(0, 5, 5))
print(evaluate(10, 5, 5))
print(evaluate(10, 2, 5))Exercise
In the evaluate function, what does the game return if health is 0 and relics meet the goal on the same frame?
Show answer
It returns Outcome.LOST, because the lose check runs before the win check.