Game tutorial · Maze

How to make a maze game in Scratch (step by step, with an AI helper)

A maze game is the perfect first project: the rules are easy to explain and the code teaches a useful trick – detecting walls by their colour. In this guide you draw a maze as a backdrop, move a small player with the arrow keys, stop it at the walls, reach a goal, then add more levels and a timer. Each step has real Scratch 3 blocks, plus notes on how Bee, the AI buddy in Enimist's Maker Studio, can help.

Draw the maze as a backdrop

Open the Stage, go to Backdrops and draw the maze with the rectangle or line tool. Use ONE colour for every wall – a dark blue, for example – and don't use that colour anywhere else. The game will recognise walls only by that colour, so a stray dark-blue flower would also act as a wall.

Leave corridors at least 40 pixels wide so the player fits. Mark a start in one corner and leave space for the goal in another.

Tip: Bee's “Draw for me” chip is great for the player and the goal (“a small happy bee”, “a jar of honey”). For the maze itself, draw the walls yourself so the corridors are exactly the width you want.

Move with the arrow keys

Make a small player sprite (about 24 pixels) and move it 3 steps at a time while an arrow key is held. Using “key pressed?” inside forever (instead of “when key pressed” hats) gives smooth movement and lets the player move diagonally.

Blocks
when green flag clicked
go to x: (-200) y: (150)
forever
  if <key (right arrow) pressed?> then
    change x by (3)
  if <key (left arrow) pressed?> then
    change x by (-3)
  if <key (up arrow) pressed?> then
    change y by (3)
  if <key (down arrow) pressed?> then
    change y by (-3)

Stop at the walls with “touching color”

After each move, ask: am I touching the wall colour? If yes, undo that exact move. Click the colour square in “touching color?” and use the eyedropper to pick the wall colour straight from the stage, so it matches exactly.

Checking right after each direction (not once at the end) matters: if you move right and up in the same frame and hit a wall going up, you only undo the up move and can still slide along the wall.

Common mistakes: walls drawn with a soft brush have blurry edges in slightly different colours, so the check misses them – use solid rectangles or lines. If the player still squeezes through thin walls, make the walls at least 6 pixels thick or lower the step size from 3 to 2. And if the player gets stuck the moment the game starts, its start position is inside a wall – move the “go to” point into an open corridor.

Blocks
if <key (right arrow) pressed?> then
  change x by (3)
  if <touching color [dark blue]?> then
    change x by (-3)

Reach the goal

Put the goal sprite at the end of the maze. In the player's loop, check if it touches the goal. When it does, say “You did it!”, wait, and move to the next level.

Using “next backdrop” for levels means each new maze is just a new backdrop drawn with the same wall colour – the code doesn't change at all.

Blocks
if <touching (Goal)?> then
  say (You did it!) for (1) seconds
  next backdrop
  go to x: (-200) y: (150)

Add a timer and a best time

Scratch has a built-in timer. Reset it when the game starts, and when the player finishes the last level, save the time in a variable “best” if it is better than the old one. A timer turns a maze into a challenge friends want to beat.

For an extra twist, make a guard sprite that glides back and forth across a corridor, and send the player back to the start when they touch it.

Blocks
when green flag clicked
reset timer
switch backdrop to (Level 1)

Stuck? How the AI buddy can help

If the player walks through walls, the usual cause is a colour that doesn't match exactly. Ask Bee in the Maker Studio: “My player goes through the walls of my maze” – Bee can see your blocks and will point at the colour check.

Want Bee to build it? Type what you want – “add a guard that walks back and forth” – and Bee reads your project, prepares the change as a draft card (“Bee will: add 1 script(s), draw 1 picture(s)”) and waits. Tap Apply to put it in your project, or Discard; after applying, Undo puts everything back. Then tap “Done” to see how much of the game you made yourself.

Bee built a few blocks from a plan, with Keep, Remove and Try it buttons (Vietnamese interface shown)
Bee built a few blocks from a plan, with Keep, Remove and Try it buttons (Vietnamese interface shown)

Scratch is a project of the Scratch Foundation, in collaboration with the Lifelong Kindergarten Group at the MIT Media Lab. Enimist Studio is not affiliated with Scratch or the Scratch Foundation.

Ready to make a first game?

Open the Maker Studio on a computer – Bee will be there from the very first block.

More guides