How to Make a Maze Game in Scratch (with Levels)
Draw your own maze, stop the player walking through walls with colour detection, add a goal, a timer and multiple levels โ a complete Scratch 3 tutorial for beginners.
In this guide
Maze games are a brilliant second Scratch project. You get to design your own level, and you learn a powerful trick game programmers use all the time: collision detection โ knowing when the player bumps into something.
In this guide you will draw a maze, move a character through it with the arrow keys, stop it walking through walls, finish the level by reaching a goal, and then add a timer and more levels.
What you need: the free Scratch editor at scratch.mit.edu (click Create). If you have never used Scratch, try our catch game tutorial first โ it is a gentle start.
The plan
- The stage backdrop is the maze. All walls are one colour (we will use dark blue).
- The player moves with the arrow keys.
- If the player touches the wall colour, it bounces back, so it can never go through a wall.
- A goal sprite sits at the end. Touching it moves you to the next level.
Step 1: Draw the maze
- Click the Stage (bottom right), then the Backdrops tab.
- Choose Paint to start a blank backdrop and name it
Level 1. - Pick the Rectangle tool, set Fill to a dark blue, and draw thick walls. Leave paths at least as wide as your player.
- Leave a gap on the left for the start and on the right for the finish.
Tip: use one exact colour for every wall. The game will check for that colour, so a slightly different shade would let the player walk straight through.
Step 2: Add and shrink the player
- Click Choose a Sprite and pick a small character โ Ball, Beetle or the cat all work.
- Set its Size so it fits easily through your paths (try
25to35). - Drag it to the start of the maze. In the sprite panel, note its x and y numbers.
Step 3: Move with the arrow keys
Select the player and build:
when green flag clicked
go to x: (-210) y: (0)
forever
if <key (up arrow) pressed?> then
change y by (3)
end
if <key (down arrow) pressed?> then
change y by (-3)
end
if <key (left arrow) pressed?> then
change x by (-3)
end
if <key (right arrow) pressed?> then
change x by (3)
end
endReplace -210 and 0 with the start position you noted. Small steps (3) make the movement smooth and precise.
Step 4: Stop the player going through walls
Here is the clever part. After each movement, we check whether the player is touching the wall colour. If it is, we undo the move.
Change each if block so it has an "undo" check inside. For the up arrow:
if <key (up arrow) pressed?> then
change y by (3)
if <touching color [dark blue]?> then
change y by (-3)
end
endTo set the colour, click the colour square in the touching color block, choose the dropper tool at the bottom, and click a wall on the stage.
Do the same for the other three directions, always undoing with the opposite number:
| Arrow | Move | Undo if touching the wall |
|---|---|---|
| Up | change y by 3 |
change y by -3 |
| Down | change y by -3 |
change y by 3 |
| Left | change x by -3 |
change x by 3 |
| Right | change x by 3 |
change x by -3 |
Why this works: the player takes a tiny step, Scratch checks for a collision, and if there is one the step is cancelled before the screen redraws. To the player, it looks like a solid wall.
Step 5: Add the goal
- Add a sprite to be the goal โ Star, Gift or a flag you paint yourself.
- Place it at the end of the maze.
- Add this script to the player, inside the
foreverloop, after the four arrow checks:
if <touching (Star)?> then
say [I made it!] for (2) seconds
stop [all]
endPlay your maze from start to finish. Congratulations โ it is a real game!
Step 6: Add a timer
Scratch has a built-in timer that counts seconds since it was last reset.
- At the top of the player's script, after
go to x: y:, addreset timer. - Make a variable called
time. - Inside the
foreverloop, addset [time] to (round (timer)).
Now the stage shows how many seconds you have taken. Challenge your friends to beat your time!
Step 7: Add more levels
Levels make players keep going. We will use backdrops for levels and a broadcast message to change between them.
- Paint a second backdrop called
Level 2with a harder maze (use the same wall colour). - Make a variable
level. - Change the player's goal check to:
if <touching (Star)?> then
change [level] by (1)
broadcast (next level)
wait (0.5) seconds
end- On the Stage, add:
when green flag clicked
set [level] to (1)
switch backdrop to (Level 1)
when I receive (next level)
if <(level) > (2)> then
switch backdrop to (You Win)
stop [all]
else
switch backdrop to (join [Level ] (level))
end- Paint a
You Winbackdrop. - Move the player back to the start for each level: on the player, add
when I receive (next level)โgo to x: (-210) y: (0). If the start or goal is in a different place on Level 2, move the Star in its ownwhen backdrop switches to (Level 2)script.
join [Level ] (level) builds the backdrop name from the level number, so the same code works for 2, 3, 10 or 100 levels โ you only need to paint them.
Common problems
- The player walks through walls. The wall colour in the block does not exactly match the walls. Use the dropper tool again, and make sure every wall was drawn in the same colour.
- The player gets stuck in a wall. The player sprite is too big or the steps are too large. Make the sprite smaller or use
2instead of3. - The game finishes straight away. The goal is touching the start area. Move the Star further away.
Challenges to try next
- Keys and doors: add a key sprite. Doors (a different colour) only stop the player until the key is collected.
- Moving enemies: add a ghost that glides between two points; touching it sends the player back to the start.
- Lives: give the player 3 lives and lose one each time they touch an enemy.
- Collect coins: coins add to the score and disappear (
hide) when touched.
What you learned
- Collision detection with
touching color, and the "move, check, undo" pattern. - Broadcasts to send messages between sprites and the stage.
- Backdrops as levels, and building names with
join. - Using the built-in timer.
The "move, check, undo" pattern is used in professional 2D games too. When you move on to Python or JavaScript, you will meet exactly the same idea. Read Scratch vs Python to see what comes next.
Frequently asked questions
Can I use a sprite as the maze instead of the backdrop? Yes. Draw the maze as a sprite and use touching (Maze)? instead of touching color. This lets you use any colours you like.
Why not just use move 10 steps and point in direction? You can, but arrow keys with change x and change y give simpler, tighter control for mazes.
How many levels should a maze game have? Three to five levels that get gradually harder is perfect for a first version.
New to StemNest?
Book a free trial class and learn coding and AI live with our friendly tutors.
Book a free trial