- Learnt28 min · what a state is, and every script asking it
- Basic24 min · a title screen that hands over to the game
- Challenge 1–327 min · the script that forgot, the fix, and no green flag
- Extrabonus · words, not numbers
Review — the Bubble Popper arc 5 min
- Clones are copies that run their own script, and every one needs a delete.
- For this sprite only gives each clone its own speed, size and worth.
- A round can end itself with
repeat until <(lives) = (0)>, and a guard stops lives going negative.
Quick-fire
Bubble Popper ended with a game-over card. But to play again you had to click the green flag. Why is that not good enough?
Reveal the answer
Because the green flag is the programmer’s button. A player should never have to reach for it — and making that possible is the whole of this arc.
Today’s Topic 3 min
- A
statevariable, holding a word:title,play,over - Every script asking the state before it acts
broadcast [start v]to move from one state to the next- One key that means different things depending on where you are
Learning outcome
By the end of this lesson you will be able to:
- Say what situation your game is in, out loud, at any moment.
- Build a title screen that hands over to the game.
- Recognise the bug where one script never heard the news.
Learnt — a game is a set of situations 28 min
1 · Everything at once
Here is a game with a title screen and no state. Both scripts start under the green flag, so both run immediately:
when flag clicked
show
go to x: (-150) y: (0)
repeat (26)
next costume
change x by (9)
end
when flag clicked
wait (0.1) seconds
say [STORM FLIGHT - press the arrows to fly] for (2) seconds
How to use this: Click the green flag and try to read the instructions before the parrot has gone.
2 · One variable names the situation
A state is the name of the situation your game is in right now. Almost every game has the same three:
title— waiting for the player to startplay— the game is happeningover— it has finished, and the score is on screen
Make one variable called state, for all sprites, and put a word in it. Then every script begins by asking what it holds.
How to use this: Click the green flag and watch the state box change from title to play to over.
3 · Moving between states
Two blocks do all the work:
set [state v] to [play]— the change itselfbroadcast [start v]— telling everybody it happened
You could do it with the variable alone, and small games often do. A broadcast is better once more than one sprite needs to react, because a when I receive [start v] runs the moment it is sent, instead of waiting for some loop to come round again.
when flag clicked
set [state v] to [title]
go to x: (0) y: (-30)
show
say [STORM FLIGHT - press space] for (1.6) seconds
wait until <key (space v) pressed?>
broadcast [start v]
wait until costs nothing while it waits.4 · Every script asks
This is the part that gets forgotten. A game with four sprites has a dozen scripts, and each one needs to know whether it should be doing anything. A script that does not ask keeps running in every state — under the title screen, after the game is over, and (next lesson but one) while the game is paused.
Basic — the title screen 24 min
Start a new project. A stormy backdrop and a parrot to fly.
Step 1 — the sprites
- Add the
Parrotsprite and set its size to about 55. It has two costumes, sonext costumeflaps its wings. - Choose a stormy or cloudy backdrop.
Step 2 — the state
- Make a variable
state, for all sprites, and show it. - Under the green flag:
set [state v] to [title].
Step 3 — the title
- Move the parrot to the middle and say the game’s name.
wait until <key (space v) pressed?>.- Then
broadcast [start v].
Step 4 — the game
- Add
when I receive [start v]. - Set the state to
play, move the parrot to the left of the Stage. - Add the flying: four
ifblocks, one per arrow key.
if <key (up arrow v) pressed?> then
change y by (7)
end
if <key (down arrow v) pressed?> then
change y by (-7)
end
if <key (right arrow v) pressed?> then
change x by (7)
end
if <key (left arrow v) pressed?> then
change x by (-7)
end
Step 5 — the ending
- Wrap the flying in a repeat, so the round has a length.
- Afterwards:
set [state v] to [over]and say so.
How to use this: Click the green flag, read the title, press space, then fly with the arrow keys.
The game starts before I press space.
The flying is under when flag clicked rather than when I receive [start v].
Pressing space does nothing.
The broadcast is never sent, or the receiving script listens for a different message. Check both dropdowns say the same word.
The parrot flies off the edge and I cannot get it back.
Add the four bounds checks — if <(x position) > (210)> then set x to (210), and the same for the other three sides.
The state box shows 0.
You dragged the variable into a number slot somewhere, or never set it. It should hold a word.
The script that forgot to ask
Add a lightning bolt whose script says nothing about the state, and watch it misbehave.
- A bolt that flashes somewhere random, over and over, in a forever loop.
- A counter of how many times it has struck.
- Read the counter at the moment the game actually begins.
How to use this: Click the green flag and watch the strikes box during the title screen.
play.Teacher note
Worth naming the three faces of this one bug, because pupils meet them as three separate mysteries: enemies that attack the title screen, a score that rises after “game over”, and (lesson 3-23) a game that carries on behind the pause sign. All of them are one script that never asked.
One if better
Fix it. The bolt should only strike while the game is being played.
- The bolt’s loop opens with
if <(state) = [play]> then. - It hides when it is not playing.
- The strikes counter reads 0 on the title screen, every run.
How to use this: Click the green flag. The sky is empty until the game starts, then the bolts arrive.
Teacher reveal — the shape to repeat everywhere
when flag clicked
hide
forever
if <(state) = [play]> then
go to x: (pick random (-160) to (190)) y: (pick random (-90) to (120))
show
change [strikes v] by (1)
wait (0.25) seconds
hide
end
wait (0.2) seconds
end
Every sprite in every game from here on has this shape. Make pupils write it twice by hand before letting them copy it.
Never touch the green flag again
One key, three meanings. Space starts a round from the title, and takes you back to the title when the round is over.
- A single
when [space v] key pressedscript. - Inside it, an
iffor each state it cares about. - A rounds counter, to prove you played more than once without the flag.
How to use this: Click the green flag once, then use only the space bar to play round after round.
Teacher reveal — the key asks where it is
when [space v] key pressed
if <(state) = [title]> then
broadcast [start v]
end
if <(state) = [over]> then
broadcast [to title v]
end
The same key, two jobs, and no confusion — because the script asks the state first. This is the shape of every menu, pause key and confirm button they will ever write.
Words, not numbers
Build the same machine twice: once with state holding 1, 2 and 3, and once holding title, play and over. They behave identically. Argue for one of them.
- Both versions on the Stage at the same time, as two readouts.
- They change together, so you can see they are the same machine.
- Write one sentence saying which you would rather find in a broken project.
How to use this: Click the green flag and watch both boxes step through the game together.
Teacher note
This is the only place in Level 3 that argues for readable data rather than working data, and it repays the ten minutes. The “going further” answer most classes reach is two variables — a word for people and a number for comparing — which is a real engineering trade-off and a good place to leave it.
Summary 5 min
- A state is the name of the situation the game is in. Almost always
title,play,over. stateis shared — there is one game — and holds a word.- Every script asks the state before it acts. The one that forgets is the commonest bug in this arc.
broadcastannounces a change; the variable records it.- The green flag is the programmer’s button. Players get their own key.
- State
- The named situation a game is in right now.
- State machine
- The set of states plus the rules for moving between them.
- Transition
- Moving from one state to the next, usually on a broadcast.
- Title screen
- The state a game sits in before the player starts it.
Hand in
Save your Storm Flight. Next lesson you add the way to lose it — and find out why one lightning bolt can cost three lives in a fifth of a second.