Making a Maze Game in Scratch
A step-by-step primary lesson on making a maze game in Scratch: draw a maze, move a sprite with arrow keys, stop at walls using touching colour, and reach a goal. With a worked example and quiz.
Key takeaways
- A maze game needs a player sprite, walls drawn in one colour, and a goal to reach
- You move the player by changing its x and y position when arrow keys are pressed
- The 'touching colour' block stops the player from walking through walls
- Reaching the goal colour can show a win message and stop the game
What is a maze game?
A maze game is one of the most satisfying first projects you can build. You guide a small character through twisting corridors, you must not bump into the walls, and you race to reach a goal at the other end. Almost every maze game has three parts:
- A player sprite you steer with the arrow keys.
- Walls the player is not allowed to walk through.
- A goal to reach, which ends the level when you touch it.
In this lesson we will build a complete maze game in Scratch. Scratch is a free coding tool where you snap coloured blocks together to make characters move. If you have never used it before, work through getting started with Scratch first, then come back here. It also helps to understand the stage grid, which is explained in coordinates and movement in Scratch.
Step 1: Draw the maze
In Scratch, the background picture is called the stage backdrop. We will paint the maze onto it.
- Click the Stage thumbnail in the bottom-right corner.
- Open the Backdrops tab and choose the paintbrush to start a blank backdrop.
- Pick the line tool and a thick line width, then draw your maze walls. Make every wall the same colour — let's say dark blue. This matters a lot, as you will see in Step 3.
- With a different colour, paint a small square in the far corner. This is the goal (for example, bright green).
Leave clear paths between the walls so the player has room to move. Keep your first maze simple; you can always make it trickier later.
Step 2: Add and place the player
Now we need a sprite to be the player. Click the sprite library and pick a small one, such as the Ball or Beetle, or draw a tiny dot of your own. A small sprite fits through narrow corridors more easily.
We want the player to always start at the entrance. Click the player sprite and add this script:
when green flag clicked
go to x: -200 y: -150
The numbers x and y are positions on the stage. x: -200 is near the left edge and y: -150 is near the bottom, which is a good starting corner. Adjust the numbers so your sprite begins right at the start of your maze.
Step 3: Move with the arrow keys and stop at walls
Here is the clever part. We let the player move with the arrow keys, but after each move we check: did I just bump into a wall? If so, we move straight back, so the player never actually enters the wall.
Add this to the player sprite, under the blocks from Step 2:
when green flag clicked
go to x: -200 y: -150
forever
if key [up arrow] pressed? then
change y by 5
if touching color [dark blue]? then
change y by -5
if key [down arrow] pressed? then
change y by -5
if touching color [dark blue]? then
change y by 5
if key [right arrow] pressed? then
change x by 5
if touching color [dark blue]? then
change x by -5
if key [left arrow] pressed? then
change x by -5
if touching color [dark blue]? then
change x by 5
Read one block group carefully, because they all follow the same pattern:
if key [right arrow] pressed?checks whether you are holding right.change x by 5moves the player 5 steps to the right.if touching color [dark blue]?asks: am I now overlapping a wall?- If yes,
change x by -5moves the player back exactly 5 steps, cancelling the move. The player looks like it simply stopped at the wall.
To set the colour, click the colour swatch inside the touching color block, choose the dropper tool, and click one of your walls so it captures the exact wall colour. The whole thing sits inside a forever loop, so Scratch keeps checking the keyboard the entire time you play.
Step 4: Win when you reach the goal
We painted the goal a different colour (bright green). Now we react when the player touches it. Add this inside the same forever loop, after the movement checks:
if touching color [bright green]? then
say [You made it!] for 2 seconds
stop [all]
When the player overlaps the green goal, the sprite shows "You made it!" and stop [all] ends the game. Click the green flag and play your maze from start to finish!
A worked example
Imagine your player is in a corridor and you hold the right arrow while a wall sits just to the right:
- The forever loop reaches the right-arrow check and sees the key is pressed.
change x by 5nudges the player into the wall.if touching color [dark blue]?is now true, sochange x by -5pulls it straight back out.- The net movement is zero, so the player appears stuck at the wall — exactly what we want.
- The moment you release right and press up into an open path, that branch runs instead and the player slides through.
This "move, then undo if blocked" trick is how most simple maze games handle walls.
Try it: make it your own
Your maze works! Now make it yours with one of these challenges:
- Add a timer: show the
timervalue and try to beat your best time. - Add a danger colour: paint some red spikes. If the player touches red, send it back to the start with
go to x: -200 y: -150. - Build levels: when the player wins,
switch backdrop toa harder maze instead of stopping. - Slow it down: change every
5to3for careful, precise movement.
If something acts strangely, read your blocks in order and check the wall colour is set correctly. Fixing problems like that is called debugging, and every coder does it. Have fun exploring your maze! 🧩
Quick quiz
Test yourself and earn XP
Why should all the maze walls be drawn in the same colour?
If every wall is the same colour, a single 'touching colour' block can detect all of them, instead of needing one check per wall.
What does 'change x by 5' do to the player sprite?
Larger x means further right on the stage, so 'change x by 5' moves the sprite 5 steps to the right.
After the player moves and finds it is touching a wall, the best fix is to...
Moving back by the opposite amount cancels the move that hit the wall, so the player appears to stop at the wall.
How can the game tell the player has reached the goal?
Painting the goal a special colour lets an 'if touching colour?' block detect when the player arrives there.
Why do we put the movement checks inside a 'forever' loop?
A forever loop repeats its blocks endlessly, so the game keeps checking the arrow keys the whole time you play.
FAQ
No. Scratch runs free in a web browser at the Scratch website. You only need an account if you want to save and share your maze.
Usually the walls are not all exactly the same colour, or the 'touching colour' block is set to a different colour than the walls. Click the colour in the block, then use the dropper tool to pick the exact wall colour, and make sure every wall uses that one colour.
Keep exploring
More in Coding