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.

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.
when green flag clickedset [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 waitset [Last tick] to (timer)broadcast [Start controls]foreverupdate clockcheck endingread controls2. 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.
define update clockset [Now] to (timer)if <(State) = [Playing]> thenchange [Active time] by ((Now) - (Last tick))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.
define start roundset [State] to [Starting]set [Active time] to (0)switch backdrop to [Track]broadcast [Reset runner] and waitset [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.
define read controlsif <key [space] pressed?> thenif <(Space held) = (0)> thenset [Space held] to (1)if <<(State) = [Menu]> or <<(State) = [Won]> or <(State) = [Lost]>>> thenstart roundelseset [Space held] to (0)if <key [p] pressed?> thenif <(Pause held) = (0)> thenset [Pause held] to (1)if <(State) = [Playing]> thenset [State] to [Paused]switch backdrop to [Pause screen]elseif <(State) = [Paused]> thenswitch backdrop to [Track]set [Last tick] to (timer)set [State] to [Playing]elseset [Pause held] to (0)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.
define check endingif <(State) = [Playing]> thenif <not <(Active time) < (8)>> thenset [State] to [Lost]switch backdrop to [Try again]elseif <([x position] of [Runner]) > (159)> thenset [State] to [Won]switch backdrop to [You win]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.
when I receive [Show menu]hidewhen I receive [Reset runner]go to x: (-170) y: (-25)showwhen I receive [Start controls]foreverif <(State) = [Playing]> thenif <key [right arrow] pressed?> thenchange x by (5)if <key [left arrow] pressed?> thenchange x by (-5)Test your project
- 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.
- 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.
- 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.