- 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. Two things from Level 1 come back today.
- The controller: a
foreverwith anif <> thenper key, from Topic 1.6. touching [Bowl v]?— asking whether two sprites are on top of each other, from the Muffin Catcher.
Quick-fire
In every game so far, a sprite could go wherever it liked. What has to change for a wall to exist?
Reveal the answer
Nothing stops a sprite moving in Scratch. So a wall cannot block you — it can only be something you check for after you have moved, and then undo.
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 through corridors without it passing through anything.
- 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
It is tempting to make every wall its own sprite. Do not. One sprite, one costume, the size of the whole Stage, sitting at (0, 0).
Then there is only ever one thing to ask about, however many corridors 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 — empty means you can walk there.
- Name the sprite
Mazeand put it atx: 0 y: 0.
2 · Move, check, step back
Here is the whole idea, and it surprises everybody: your sprite does walk into the wall. It is just put back before the screen is drawn, so nobody ever sees it happen.
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. A four-step move undone by a five-step step-back walks your player slowly 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, and reaching it is the same question you have been asking all along.
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 and test after every direction. Do not build all four and hope.
Step 1 — the room
- New project, cat deleted, a stone-ish backdrop such as Castle 2.
- Add a painted sprite called
Mazeand draw a simple maze in it. - Keep the corridors wide. Two finger-widths on screen is about right.
Step 2 — the player
- Add a small sprite. A Beetle or a Ball.
set size to (40) %until it fits a corridor easily.- Send it to the start with
go to x: (180) y: (-120).
Step 3 — one direction
- Build the forever loop with just 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. Nothing should give.
Step 4 — the other three
- Duplicate the whole if block three times (right-click, Duplicate).
- Change each one’s key, and its move blocks:
change x by (-4),change y by (4),change y by (-4). - Check 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.
- Anywhere you slip through is a wall that needs to be thicker.
My player goes straight through the walls.
The touching-if is checking the wrong sprite, or the maze sprite is called something else. Check 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 position into an open corridor.
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 positive and one negative.
Draw your own maze
Paint a maze nobody else has, and make it solid.
- Your own walls, drawn in the paint editor.
- All four directions blocked properly.
- 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. Make yours impossible to escape except through the exit.
- A wall all the way round the edge of the Stage.
- No corridor 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 cold. Then write down what was wrong with it.
- You can solve theirs without being told the way.
- You find at least one thing to report: a tight corner, a wall you slip through, 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: a gate that slides back and forth across a corridor. 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, without being told.
- The player cannot walk through it.
- The maze is still solvable — the gate never closes the only route completely.
- The gate is somewhere the player has to deal with, not tucked 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. Walls are something you check for and undo.
- 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, and reaching it is the same question with a better answer.
- Collision
- Two sprites overlapping. In Scratch you find out by asking, not by being stopped.
- Step back
- Undoing a move that ended up somewhere it should not. The heart of a maze game.
- Maze sprite
- All the walls in one sprite, so there is only ever one thing to ask about.
- Exit
- A sprite that ends the game when the player touches it.
Next lesson: things worth collecting, a key, and a door that will not open without it.