- Learnt27 min · walls, the step back, and the way out
- Basic25 min · build the maze and escape it
- Challenge 1–327 min · draw one, seal it, swap it
- Extrabonus · a wall that will not stay still
Review — where we got to 5 min
The Music Video is finished and shared. We need two things from Level 1 today.
- The controller: a
foreverwith oneif <> thenfor each key, from Topic 1.6. touching [Bowl v]?asks if two sprites are on top of each other. We used it in the Muffin Catcher.
Quick-fire
In every game so far, a sprite could go anywhere. What has to change to make a wall?
Reveal the answer
Nothing stops a sprite moving in Scratch. So a wall cannot block you. You check for it after you move. Then you undo the move.
Today’s Topic 3 min
- A maze drawn as one sprite that covers the whole Stage
- Move, check, step back — the rule that makes a wall solid
touching [Maze v]?inside the movement loop- An exit sprite that ends the game when you reach it
Learning outcome
By the end of this lesson you will be able to:
- Draw or add a maze and make its walls solid.
- Steer a sprite down the paths. It cannot pass through walls.
- End the game when the player reaches the way out.
Learnt — walls, undo, and the way out 27 min
1 · A maze is one sprite
You may want to make every wall its own sprite. Do not. Use one sprite with one costume. Make it the size of the whole Stage. Put it at (0, 0).
Then you only ever ask about one thing, however many paths your maze has.
- Add a new sprite and choose Paint.
- Pick the rectangle tool and a dark colour.
- Draw your walls. Leave the floor empty. You can walk where it is empty.
- Name the sprite
Mazeand put it atx: 0 y: 0.
2 · Move, check, step back
Here is the big idea. It surprises everyone. Your sprite does walk into the wall. But it is put back before the screen is drawn. So nobody ever sees it.
if <key [right arrow v] pressed?> then
change x by (4)
if <touching [Maze v]?> then
change x by (-4)
end
end
What the computer actually does
- Is the right arrow down? Yes.
- Add 4 to x. The beetle is now inside the wall.
- Am I touching the maze? Yes.
- Take 4 off x again. The beetle is back where it was.
- Draw the screen. Nobody saw a thing.
The two numbers must match: 4 and -4. What if you move 4 and step back 5? Your player slowly walks backwards through the maze.
How to use this: Click the green flag, then click the stage once and use the arrow keys to move the Beetle.
3 · The way out
The exit is another sprite. To find out if you reached it, ask the same touching question.
when flag clicked
go to x: (180) y: (-120)
forever
if <touching [Star v]?> then
say [Way out!]
stop [all v]
end
end
How to use this: Click the green flag, then click the stage once and use the arrow keys to move the Beetle.
Basic — build the maze 25 min
Build it in this order. Test after every direction. Do not build all four and hope.
Step 1 — the room
- Start a new project. Delete the cat. Pick a stone backdrop, like Castle 2.
- Paint a new sprite called
Maze. Draw a simple maze in it. - Keep the paths wide. Two fingers wide on the screen is about right.
Step 2 — the player
- Add a small sprite. A Beetle or a Ball.
- Use
set size to (40) %so it fits a path easily. - Send it to the start with
go to x: (180) y: (-120).
Step 3 — one direction
- Build the forever loop. Put only the right-arrow if inside it.
- Put the touching if and the step back inside that.
- Test. Drive right into a wall and hold the key. You should not get through.
Step 4 — the other three
- Copy the whole if block three times. Right-click it and choose Duplicate.
- Change the key in each one. Change its move blocks too:
change x by (-4),change y by (4),change y by (-4). - Check that every step back matches its own step.
- Test all four against walls.
Step 5 — the way out
- Add a Star at the far end of your maze.
- On the player, add the second stack that watches for it.
- Play the whole maze from start to star.
Step 6 — play it properly
- Solve your own maze twice.
- Try to break it. Drive into every corner and hold the keys.
- Did you slip through a wall? Make that wall thicker.
My player goes straight through the walls.
The touching if is checking the wrong sprite. Or the maze sprite has a different name. Check that the dropdown says Maze.
My player gets stuck and cannot move at all.
It started inside a wall. Every step it takes is undone straight away. Move the start into an open path.
My player crawls backwards when I hold a key.
The step and the step back are different sizes. They must be the same number. One has a minus and one does not.
Draw your own maze
Paint a maze that nobody else has. Make it solid.
- Your own walls, drawn in the paint editor.
- Walls block all four directions.
- A start and an exit that are a long way apart.
Seal it
A maze with a gap round the outside is not a maze. The exit must be the only way out.
- A wall all the way round the edge of the Stage.
- No path that leads off the side.
- Holding two keys at once does not squeeze the player through a corner.
Teacher note — the diagonal squeeze
Two keys held together move the player twice in one pass of the loop, and each move is checked separately. That is usually fine, but at an inside corner it can put a player somewhere neither move alone would. Thicker walls fix it, and it is worth showing the class the corner where it happens.
Swap and solve
Leave your maze. Do something else for five minutes. Then come back and solve it fresh. Write down what was wrong with it.
- You can solve theirs without being told the way.
- You find at least one problem. It might be a tight corner, a wall you slip through, or a start inside a wall.
- You fix the things they found in yours.
Teacher note — the fastest way to find a bad maze
Nobody finds the tight corner in their own maze, because they know where the corridors are and steer neatly. A stranger drives into everything. This mission is worth the ten minutes for that reason alone.
The wall that will not stay still
Add one more sprite. It is a gate that slides back and forth across a path. When it is in the way, it is as solid as the maze.
How to use this: Click the green flag, then click the stage once and use the arrow keys to move the Beetle.
- The gate moves by itself, for ever.
- The player cannot walk through it.
- You can still solve the maze. The gate never fully blocks the only way through.
- The player has to get past the gate. It is not hidden in a corner.
Teacher notes — one more sprite in the same question
when flag clicked
go to x: (-40) y: (10)
forever
glide (2) secs to x: (60) y: (10)
glide (2) secs to x: (-40) y: (10)
end
That is the gate. The clever part is on the player, and it is one word:
if <touching [Gate v]?> then
change x by (-4)
end
A second touching-if beside the maze one, in every direction. Pupils who spot that they could ask <<touching [Maze v]?> or <touching [Gate v]?>> instead have found an Operators block from Level 2 by themselves. Accept it and say so.
Going further: a gate that opens when you have found a key is next lesson.
Summary 4 min
Nothing in Scratch stops a sprite. You check for walls and undo the move.
- The whole maze is one sprite, at (0, 0), the size of the Stage.
- Move. Then ask
touching [Maze v]?. Then step back by the same amount. - Every direction needs its own step back, in its own block.
- The exit is a sprite. You ask the same question, and the answer is good news.
- Collision
- Two sprites on top of each other. In Scratch you find out by asking. Nothing stops you.
- Step back
- Undoing a move that went into a wall. Every maze game needs it.
- Maze sprite
- All the walls in one sprite. So you only ever ask about one thing.
- Exit
- A sprite that ends the game when the player touches it.
Next lesson: things to collect, a key, and a door that will not open without it.