Scratch 17 · Build a game with menus and pause

Three linked cards show play, pause and end states.

Scratch lesson 17 of 20 · Advanced · About 55–70 minutes

Turn a sprint into a complete game with a title screen, a pause that stops the clock, and win and retry screens. Use a state machine to decide what the game can do at each moment.

What you’ll make Build a timed race in which Space starts a round, P pauses and resumes, and the runner must reach the finish within eight seconds of active play.

Get ready

Open the Scratch editor (opens in a new tab) and start a new project.

  • Paint one small sprite named Runner, with the costume centred on its crosshair. The game starts at x −170, y −25. Draw a finish line near x 160 on the Track backdrop.
  • Paint five backdrops called Menu, Track, Pause screen, You win and Try again. Put Space to start on Menu, P to resume on Pause screen, and Space to play again on both ending screens.
  • Create State, Active time, Last tick, Now, Space held and Pause held for all sprites. Show State and Active time while testing. Use the exact capitalisation in the scripts.
  • Create the messages Show menu, Start controls and Reset runner. The Stage will control the states. Runner will read State and move only while the game is Playing.

To keep your project, choose File → Save to your computer in Scratch.

Choose a game state. Start from the menu. → Run the game. Update only while playing. → Pause or restart. Switch state and test.
Project flow · Choose a game state → Run the game → Pause or restart

Build it in Scratch

Choose the sprite named above each stack. The colours help you find the block categories. Indented rows sit inside the block above. Follow the end marker to see where each loop or condition closes.

1. Start on the menu

On the Stage, build one green flag script that runs the game. Keep the forever loop at the bottom. Make the four custom Stage blocks shown in the next steps. The Start controls broadcast starts a long-running receiver, so use plain broadcast. Show menu is finite, so the Stage can wait for it.

Block stack for Stage
when green flag clicked
set [State] to [Menu]
set [Active time] to (0)
set [Space held] to (0)
set [Pause held] to (0)
switch backdrop to [Menu]
broadcast [Show menu] and wait
set [Last tick] to (timer)
broadcast [Start controls]
forever
update clock
check ending
read controls
↳ End of block

2. Count playing time only

Make the Stage block update clock. Scratch’s timer reporter keeps running even during a pause. Store two readings and add the difference only when the previous state was Playing. Always update Last tick so the next reading cannot accidentally include the whole pause.

Block stack for Stage
define update clock
set [Now] to (timer)
if <(State) = [Playing]> then
change [Active time] by ((Now) - (Last tick))
↳ End of block
set [Last tick] to (Now)

3. Start each round with a clean reset

Make start round on the Stage. The temporary Starting state holds movement while the finite Reset runner receiver places the sprite. Take a new clock reading after that reset and only then change to Playing. Another Space press during play should do nothing.

Block stack for Stage
define start round
set [State] to [Starting]
set [Active time] to (0)
switch backdrop to [Track]
broadcast [Reset runner] and wait
set [Last tick] to (timer)
set [State] to [Playing]

4. Read a new press of Space or P

Make read controls on the Stage. Each held variable is a latch. Set it the first time you see that key down, then clear it when the key is released. This makes one press produce one change, even if you hold the key. Start a new round only from Menu, Won or Lost. P only changes Playing to Paused or back again.

Block stack for Stage
define read controls
if <key [space] pressed?> then
if <(Space held) = (0)> then
set [Space held] to (1)
if <<(State) = [Menu]> or <<(State) = [Won]> or <(State) = [Lost]>>> then
start round
↳ End of block
↳ End of block
else
set [Space held] to (0)
↳ End of block
if <key [p] pressed?> then
if <(Pause held) = (0)> then
set [Pause held] to (1)
if <(State) = [Playing]> then
set [State] to [Paused]
switch backdrop to [Pause screen]
else
if <(State) = [Paused]> then
switch backdrop to [Track]
set [Last tick] to (timer)
set [State] to [Playing]
↳ End of block
↳ End of block
↳ End of block
else
set [Pause held] to (0)
↳ End of block

5. Give the finish and the clock clear rules

Make check ending on the Stage. Build the sensing reporter by setting its right dropdown to Runner and its left dropdown to x position. The round ends once time reaches eight seconds or Runner moves to x 160 or beyond. If both checks would pass on the same frame, time takes priority.

Block stack for Stage
define check ending
if <(State) = [Playing]> then
if <not <(Active time) < (8)>> then
set [State] to [Lost]
switch backdrop to [Try again]
else
if <([x position] of [Runner]) > (159)> then
set [State] to [Won]
switch backdrop to [You win]
↳ End of block
↳ End of block
↳ End of block

6. Move Runner only during the race

Select Runner and build these three separate stacks. Menu hides Runner. Starting a round resets x and y and shows Runner. There is exactly one movement loop. It starts from Start controls and checks State before reading the arrows. Leave the Runner where it stops on the pause or ending screen, so you can see the last position.

Block stack for Runner
when I receive [Show menu]
hide
when I receive [Reset runner]
go to x: (-170) y: (-25)
show
when I receive [Start controls]
forever
if <(State) = [Playing]> then
if <key [right arrow] pressed?> then
change x by (5)
↳ End of block
if <key [left arrow] pressed?> then
change x by (-5)
↳ End of block
↳ End of block
↳ End of block

Test your project

  1. Click the flag. Arrow keys should have no effect on Menu. Press Space, hold Right, and confirm You win appears. Press Space again and confirm Runner returns to x −170 and Active time starts near zero.
  2. Start again. Press and hold P for several seconds. The State should switch to Paused only once. Confirm the runner’s x-coordinate and Active time stay fixed, even if you press the arrows. Release P, press it again, and confirm the round resumes from the same place.
  3. Start another round and leave Runner at the start. Confirm Try again appears after eight seconds of active time. Click the green flag during play and confirm you return to Menu.

If something goes wrong

The clock jumps when I resume.
Check that update clock sets Last tick every pass, even when Paused, and that the resume branch also resets Last tick before changing State to Playing.
Holding P makes the game flicker between screens.
Change Pause held to 1 before the toggle. Its reset to zero belongs in the outside else branch, which runs after you release P.
Returning to the menu freezes the game.
Use broadcast without and wait for Start controls because its receiver runs forever. Reserve and wait for the small, finite reset scripts.

Make it your own

  • Add a second difficulty. Set a Time limit variable to 6 or 8 on the menu and compare the clock against it.
  • Add a best-time variable for winning rounds. Keep it when Space starts a new round, then decide whether the green flag should clear it.

What you’ve learned A state machine gives each phase its own rules. One controller can coordinate controls, timing and screens while the other sprites respond to the current state.