- 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 by itself with
repeat until <(lives) = (0)>. A guard stops lives from going below zero.
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. This whole arc is about making that possible.
Today’s Topic 3 min
- A
statevariable that holds a word:title,play,over. - Every script asks the state before it acts.
broadcast [start v]to move from one state to the next.- One key that does 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.
- Spot 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 with the green flag. So both run straight away:
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 start.play: the game is happening.over: it has finished, and the score is on screen.
Make one variable called state, for all sprites. 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]: makes the change.broadcast [start v]: tells everybody it happened.
You could do it with the variable alone. Small games often do. A broadcast is better when more than one sprite needs to react. A when I receive [start v] runs as soon as the message is sent. It does not wait for a 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 people forget. A game with four sprites has a dozen scripts. Each one needs to know if it should be doing anything. A script that does not ask keeps running in every state. It runs under the title screen and after the game is over. In two lessons’ time, it will also run while the game is paused.
Basic — the title screen 24 min
Start a new project. Add 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.
- After the repeat:
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 instead of when I receive [start v].
Pressing space does nothing.
The broadcast is never sent. Or the script that receives it listens for a different message. Check that both dropdowns say the same word.
The parrot flies off the edge and I cannot get it back.
Add four edge checks. For example: if <(x position) > (210)> then set x to (210). Do the same for the other three sides.
The state box shows 0.
You dragged the variable into a number slot somewhere. Or you 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 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 the game is not being played.
- 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. It 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. In one, state holds 1, 2 and 3. In the other, it holds title, play and over. They behave the same. Pick one and say why it is better.
- 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. It is almost always
title,play,over. stateis shared, because there is one game. It holds a word.- Every script asks the state before it acts. The one that forgets is the most common 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 a way to lose. You will also find out why one lightning bolt can cost three lives in a fifth of a second.