Key point — persisting state
Persisting state means saving data so it survives beyond a single session. When you close a game and your high score is still there the next day — that is persisted data. Scratch cloud variables (☁) do exactly this by storing a number on MIT's servers.
Key point — state machines
A state machine controls which "screen" a game is on at any moment. The game is always in exactly one state: menu, playing, or game-over. A variable called gameState holds the current state; broadcasts and backdrop switches move between them.
Learning objectives
- Explain what "persisting state" means and give a real-world example
- Implement a simple state machine using a
gameState variable
- Use cloud variables (☁) to share a high score between players
- Add level progression: increase difficulty using a
level variable
- Plan a project on paper before coding (state diagram)
Lesson structure — 60 minutes
- Discussion (8 min) — "Have you ever lost your game progress? Why does that happen?"
- State machine demo (15 min) — Build the 3-screen game flow together
- Cloud variable demo (7 min) — Show the ☁ symbol; sync across browsers live
- Final project build (25 min) — Add menu, game over screen, and high score to Week 5 game
- Showcase (5 min) — Share links; class votes on favourite feature
Key Scratch blocks this week
☁ [high score]
set [gameState] to [menu]
switch backdrop to [Menu]
when backdrop switches to [Menu]
set [level] to (1)
change [level] by (1)
stop [all]
Code pattern — State machine game flow
when 🚩 clicked
set [gameState] to "menu"
switch backdrop to [Menu]
when backdrop switches to [Menu]
wait until <key [space] pressed?>
set [score] to 0
set [lives] to 3
set [gameState] to "playing"
switch backdrop to [Game]
when I receive [game-over]
set [gameState] to "game-over"
if (score) > (☁ high score) then
set [☁ high score] to (score)
end
switch backdrop to [Game Over]
when I receive [level-complete]
change [level] by 1
set [enemy speed] to (5 + level)
Cloud variables — safety note
- Cloud variables are stored publicly on MIT's servers and are visible to anyone who runs the project.
- They only store numbers — strings are not supported.
- Scratch requires users to be aged 13+ for cloud variables — demonstrate this feature using your teacher account and have pupils observe rather than use it themselves.
- Never store names, ages, or school information in cloud variables.
🎮 Final Project — My Complete Game
Pupils submit a complete game demonstrating all six weeks of learning:
- Menu screen — backdrop switch, press space to start
- Keyboard-controlled player — events and motion
- Collision detection — sensing and conditionals
- Score and lives — variables correctly initialised and updated
- Enemies or collectibles — cloning pattern
- Game over screen — state machine with backdrop switch
- Extension: Level progression — level variable increases difficulty
- Extension: Cloud high score — demonstrated via teacher account
Design before you code
Have pupils sketch their game on paper first: what screens exist? What variables are needed? Draw arrows between screens showing what triggers each transition. This state diagram maps directly to the code they will write and builds the habit of planning that professional software engineers use every day.