Different screens in a game
A game often has several screens: a title screen, the game itself, a winning screen, and a losing screen. These do not have to be separate programs. One program can remember which screen is active.
This article builds on state. We will model the changes with text before adding drawings or buttons.
Choose the next room
Think of the screens as rooms connected by doors. Pressing Start takes you from the title to playing. Winning takes you from playing to the winning room. Some doors should only work from particular rooms.
A change from one state to another is called a transition. A transition table helps you decide the rules before you draw anything.
| Current screen | Action | Next screen |
|---|---|---|
| Title, won, or lost | Start | Playing |
| Playing | Win | Won |
| Playing | Lose | Lost |
Our prototype also allows Start during play to restart a round.
Rehearse a game
The action list is pretend input. It lets us test a whole sequence without clicking buttons.
# language: en
let screen = "title"
say screen
for action in ["start", "win", "start", "lose"]:
if action == "start":
set screen = "playing"
else:
if screen == "playing":
if action == "win":
set screen = "won"
if action == "lose":
set screen = "lost"
say screen
Predict, then run
Can the second Start bring us back from the winning screen?
title
playing
won
playing
lost
Yes. The current screen is one text value. This prevents combinations such as “won and lost at the same time,” which are easy to create accidentally with several unrelated true/false flags.
Your challenge
Put "win" at the beginning of the action list, before Start. It should leave the title screen unchanged. Explain which condition prevents a player winning before playing.
A common mistake
Changing a picture without changing the remembered state can make buttons react as if the previous screen were still active. In a graphical version, draw the scene that matches screen and handle only the actions allowed there. The strings here are your program’s own labels, not special Pliro keywords.
