Scratch 06 · Build a maze with a finish line

A fox follows a dotted route through a maze to a flag.

Scratch lesson 6 of 10 · Beginner to intermediate · About 45 minutes

Draw a winding maze, steer a small runner with the arrow keys and use colour sensing to detect walls and the finish. Learn to test collisions before adding more obstacles.

What you’ll make Build a repeatable maze game with a fixed start, a wall reset and a one-time finish.

Get ready

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

  • Start a new project. Choose a small sprite and rename it Runner. Make it small enough to fit comfortably through the maze gaps. For a wide character, try a size of 18% and adjust after testing.
  • Select the Stage, open Backdrops and paint a new backdrop. Draw two thick, solid rectangles in the same dark wall colour. Draw the lower wall from the left edge towards the right, leaving a wide gap on the right. Draw the upper wall from the right edge towards the left, leaving a wide gap on the left.
  • Put the start in open space near the bottom left. Place a solid green finish rectangle near the top right, well away from the wall and the edge. Leave the rest of the backdrop a plain light colour. The route weaves right, then left, then right again.
  • Use one solid fill colour for both walls. Keep outlines the same colour or remove them. In the sensing blocks, use the eyedropper to sample the solid middle of a wall and the solid middle of the finish rectangle.

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

Try a move. Use an arrow key. → Check for a wall. Send the sprite to the start. → Reach the finish. Show the ending.
Project flow · Try a move → Check for a wall → Reach the finish

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. Choose a safe start

Click Runner and put it at x = -185, y = -125 with the green flag. Use the Looks say block with its text box empty to clear the previous win bubble. If the position touches your wall or edge, drag Runner to clear space beside your start and use the x and y shown in its sprite information instead. Use those same coordinates in the wall reset.

Block stack for Runner
when green flag clicked
set size to (18) %
go to x: (-185) y: (-125)
say []

2. Read all four arrows in one loop

Attach a forever loop underneath the starting stack. Put four separate if then blocks inside it. Place a Sensing key pressed? block in each condition and choose its arrow in the menu. Each trip round the loop checks all four keys. Small steps help the runner detect walls reliably.

Block stack for Runner
forever
if <key [right arrow] pressed?> then
change x by (4)
↳ End of block
if <key [left arrow] pressed?> then
change x by (-4)
↳ End of block
if <key [up arrow] pressed?> then
change y by (4)
↳ End of block
if <key [down arrow] pressed?> then
change y by (-4)
↳ End of block
↳ End of block

3. Send Runner back after a wall hit

Put this check inside the same forever loop, below all four arrow checks. Combine touching color? and touching [edge]? in the green or block. Use the eyedropper to choose the exact wall fill in the first sensing block. If either test is true, the runner goes straight back to its start.

Block stack for Runner
if <<touching color [wall colour]?> or <touching [edge]?>> then
go to x: (-185) y: (-125)
↳ End of block

4. Detect the finish and end the run

Add this condition inside the same loop, immediately after the wall check. Sample the finish fill with the second touching color? block. A one-time message tells the player they won, then stop [this script] ends the movement loop. To play again, click the green flag.

Block stack for Runner
if <touching color [finish colour]?> then
say [You made it!]
stop [this script]
↳ End of block

Test your project

  1. Start the game and touch a wall. Runner should appear in the bottom-left start area. Try bumping both walls and one Stage edge.
  2. Follow the open route to the green finish. The speech bubble should appear once and the arrow keys should stop moving Runner.
  3. Click the green flag. Check that the bubble clears and all four keys work again.
  4. Ask someone to try your maze. Watch where they stop or bump into a wall so you can widen any gap that feels too tight.

If something goes wrong

Runner passes through walls.
Use the eyedropper on the solid wall fill, make both walls the same colour and check the wall condition sits inside the loop. Keep the movement steps small and walls thick.
Runner keeps resetting on the starting spot.
Move the starting coordinates farther from the wall and outer edge, then update both go to blocks to match.
The finish does not work.
Resample the green fill from the finish, keep the finish clear of the wall and edge, and place its condition inside the same loop.

Make it your own

  • Can two players both finish the maze without resetting? Ask each to plan their route aloud before touching a key.
  • Try one extra wall with an open route around it. Re-test from the green flag and check that the finish is still reachable.

What you’ve learned You paired a movement loop with conditions for touching a wall, the edge or the finish. The order of those tests gives the maze reliable rules.