- Learnt28 min · a patrol, and being caught
- Basic24 min · the maze game
- Challenge 1–327 min · walls for guards, a chaser, two guards
- Extrabonus · ship it
Review — the arc so far 5 min
- Move, ask, undo. Never step further than your thinnest wall.
- Check x and y separately, and the mouse slides along walls.
- A thing you collect deletes itself. A locked goal asks about more than one thing.
Quick-fire
Your maze has a goal, a lock and three keys. It is still not very exciting. What is missing?
Reveal the answer
Pressure. Nothing in the maze is trying to stop you. With a guard, the puzzle is not just which way to go. It is also when to move. That is a different game.
Today’s Topic 3 min
- A patrol: a route walked for ever.
- Being caught, and what that costs.
distance to (Mouse v), and a guard that changes its mind.- Two guards from one sprite.
Learning outcome
By the end of this lesson you will be able to:
- Make a sprite patrol a fixed route.
- Give it two behaviours and a rule for switching between them.
- Say why a guard needs the same wall check the player has.
Learnt — a guard 28 min
1 · A patrol is just two glides
The simplest enemy in games is a sprite going back and forth. It makes no decisions at all. It still feels alive, because it is somewhere different every time you look:
when flag clicked
go to x: (-100) y: (-40)
forever
next costume
glide (0.9) secs to x: (-100) y: (40)
glide (0.9) secs to x: (-100) y: (-40)
end
How to use this: Click the green flag and watch the ghost walk its corridor four times.
2 · Being caught
The mouse asks about the ghost in the same loop it uses for the cheese. The cost is the lives shape from the Storm Flight arc:
if <touching (Ghost v)?> then
if <(lives) > (0)> then
change [lives v] by (-1)
end
set [ghost v] effect to (50)
go to x: (-180) y: (120)
wait (0.5) seconds
clear graphic effects
end
lives is the one from Lesson 3-20.How to use this: Click the green flag and watch the mouse walk into the guard's corridor three times.
3 · A guard that decides
distance to (Mouse v) reports how far apart two sprites are. One if / else around it turns a patrol into a hunter:
forever
if <(distance to (Mouse v)) < (120)> then
point towards (Mouse v)
move (3) steps
point in direction (90)
else
glide (0.5) secs to x: (20) y: (-40)
glide (0.5) secs to x: (60) y: (-40)
end
end
Basic — the maze game 24 min
Open last lesson’s maze. You will add the thing that makes it a game.
Step 1 — the guard
- Add the
Ghostsprite. Set its size to about 45 and its rotation style todon't rotate. - Choose a corridor the player has to cross. Note its two ends.
- Give it the two-glide patrol.
Step 2 — lives
- Make
livesfor all sprites. Set it to 3 at the start of the round. - Show it on the Stage next to the keys.
Step 3 — being caught
- In the mouse’s checking loop, add
if <touching (Ghost v)?> then. - Lose a life, go see-through, go home, wait, then clear the effect.
- If lives reach 0, set the state to
over.
Step 4 — the guard obeys the state
- Put
wait until <(state) = [play]>at the top of the ghost’s loop. - If not, it keeps patrolling after you have won. That looks broken.
Step 5 — test the whole thing
- Collect all three keys, dodge the guard, and reach the cheese.
- Then lose on purpose three times. Check that the game ends properly.
How to use this: Click the green flag and steer with the arrow keys. Get three keys, then the cheese. Avoid the ghost.
The ghost catches me the instant I start.
Its patrol crosses your starting corner. Move the route, or move the start.
I lose all three lives from one touch.
The half-second wait and the go-home are missing. So the touch is counted every frame. It is the same bug as Lesson 3-22, with the same fix.
The ghost keeps moving after I win.
Its loop is not asking the state. Add the wait-until at the top.
The ghost drifts out of its corridor and sits in a wall.
Its two glide points are not both in the same corridor. Check both. Remember that a glide goes in a straight line between them.
Sending the mouse home does not reset the keys, and that feels unfair.
That is a design choice, not a bug. It is the kinder choice. If the keys reset too, a three-life game would be almost impossible to win. Try both and pick.
Guards get walls too
Give a guard a straight run across the maze with no wall check. Then add the mouse’s wall check and compare.
- Count the frames it spends standing inside a wall.
- Then add the check and count how often it is blocked.
- Say which version you would rather play against. Say why.
How to use this: Click the green flag and watch two runs: one that ignores the maze, one that does not.
Teacher note
Worth letting them argue this one. A ghost that walks through walls is a perfectly good enemy — it is what ghosts do — but it must be deliberate and the player must be able to tell. An enemy that sometimes clips a corner by accident is the worst of both. The rule is: pick one and make it obvious.
A guard that notices you
Two behaviours, and a distance that decides between them.
- Far away: it patrols its corridor as before.
- Within about 120 steps: it points at the mouse and moves.
- A
ghost moodreadout so you can see it switch. - It still obeys the walls while chasing.
How to use this: Click the green flag. Watch the mood box flip to chase as the mouse comes near, and back again.
Teacher reveal — the block that stops it spinning
point towards (Mouse v)
move (3) steps
point in direction (90)
point towards rotates the sprite, which is how move knows where to go — but it leaves the ghost tipped over at whatever angle that was. Pointing back to 90 straight afterwards moves it correctly and leaves it upright. Setting the rotation style to don't rotate does the same job visually.
Two guards, one sprite
Clone the ghost so two corridors are watched. Each guard has its own route.
- Sprite-only variables for the lane and the two ends.
- Two clones, set up before they are made.
- They patrol different corridors and never end up in step.
How to use this: Click the green flag and watch two ghosts patrol two different corridors.
Teacher reveal — this is Bubble Popper again
If lane x is made for all sprites, both ghosts patrol the second corridor and the first is left unguarded — because the second clone overwrote the box before the first one read it. That is exactly the shared-variable bug from Lesson 3-18, in a completely different game. Ask them to predict it before running it; by now most of a class will.
Ship it
Put the whole arc together into one maze game. Make it good enough to give to somebody else.
- A title, and a round that starts from it.
- Walls that work, and a mouse that fits and slides.
- Three keys hidden off the quick route, and a cheese that stays locked.
- Two guards, three lives, and going home when caught.
- A clock, and a best time that lasts after the round.
How to use this: Click the green flag, wait for the title, then steer. Get three keys, dodge the ghosts, reach the cheese.
Teacher note
Worth noting how little new code this lesson added. The guards are two glides; the catching is the lives shape from 3-22; the clock is the one from 3-23; the best time is from 3-27. The arc finishes by assembling things they already had, which is what a project week should feel like.
Summary 5 min
- A patrol is two glides in a forever loop. It does not need to think to work.
- Being caught should cost a life and a position. Going home is the real punishment.
distance to (Mouse v)plus oneif / elseturns a patrol into a chaser.point towardsrotates the sprite, so point back to 90 afterwards.- A guard needs the same wall check the player has. Or you decide on purpose that it does not.
- Two guards from one sprite means sprite-only variables, again.
- Patrol
- A fixed route an enemy walks over and over.
- Aggro range
- How close you have to get before an enemy notices you.
- Respawn
- Going back to the start after being caught.
- Behaviour
- One of the things an enemy can be doing. The rule picks between them.
Hand in
Save your maze game. You have finished the Cheese Maze arc. A sprite that cannot see finds its way around a world made of one colour.