- Learnt27 min · collecting, hiding, and a locked door
- Basic25 min · fill the maze
- Challenge 1–327 min · crystals, a key, and all three
- Extrabonus · the one that is not a crystal
Review — where we got to 5 min
Last lesson the walls became solid.
- The maze is one sprite, at (0, 0), the size of the Stage.
- Move, ask
touching [Maze v]?, step back by the same amount. - The exit is a sprite, and touching it ends the game.
Quick-fire
Your maze has one route from start to exit. Why does nobody play it twice?
Reveal the answer
There is nothing to decide. You learn the way once and after that you are just holding keys. Today puts things in the maze that make you choose which way to go.
Today’s Topic 3 min
- Collectibles: a sprite that hides when you touch it
change [gems v] by (1)— counting what you picked up- A hidden sprite touches nothing, which is how a door opens
broadcast [got key v]— the key tells the door
Learning outcome
By the end of this lesson you will be able to:
- Put collectibles in a maze and count them.
- Make a door that is solid until something opens it.
- Reset everything so the second game is the same as the first.
Learnt — collecting, hiding, opening 27 min
1 · A collectible hides
A crystal you pick up is a sprite that hides itself and adds one to a number. It is not deleted — you need it back next game.
when flag clicked
go to x: (60) y: (-120)
show
wait until <touching [Beetle v]?>
change [gems v] by (1)
hide
wait until <> from the Muffin Catcher does the watching, so there is no forever loop to write.- Make a variable called
gemsand tick it so it shows. - On the player, add
set [gems v] to (0)under its flag hat. - Add a Crystal sprite and give it the stack above.
- Play. Drive into the crystal: it goes, and the number goes up.
- Press the flag again. The crystal is back and the number is 0.
How to use this: Click the green flag, then click the stage once and use the arrow keys to move the Beetle.
2 · A hidden sprite touches nothing
This is the fact the whole lesson turns on, and it is worth saying out loud: a hidden sprite is not touching anything. Not the player, not a wall, nothing.
So a door that is solid can become a door you can walk through, by hiding.
if <key [left arrow v] pressed?> then
change x by (-4)
if <touching [Maze v]?> then
change x by (4)
end
if <touching [Door v]?> then
change x by (4)
end
end
3 · The key sends a message
The key does not open anything. It hides itself and shouts, and the door opens itself. You have seen this shape before — it is the music video’s cues.
when flag clicked
show
wait until <touching [Beetle v]?>
broadcast [got key v]
hide
when flag clicked
show
when I receive [got key v]
hide
Two sprites that know nothing about each other, joined by a message. That is why you can add a second door later and it needs no new code — it just listens too.
How to use this: Click the green flag, then click the stage once and use the arrow keys to move the Beetle.
Basic — fill the maze 25 min
Open your maze from last lesson. Add one thing at a time and play after each.
How to use this: Click the green flag, then click the stage once and use the arrow keys to move the Beetle.
Step 1 — one crystal
- Make the variable
gemsand show it. - Add
set [gems v] to (0)to the player’s flag stack. - Add a Crystal, put it in a corridor, and give it the five-block stack.
- Play, collect it, and press the flag to check it comes back.
Step 2 — three crystals
- Right-click the crystal and duplicate it twice.
- Change each copy’s
go to x: () y: ()to a different corridor. - Play. All three should count into the same number.
Step 3 — the door
- Add a sprite for the door and put it across a corridor near the exit.
- Give it a flag stack with
show. - On the player, add a
touching [Door v]?step back inside each of the four directions. - Play. You should not be able to get past it at all.
Step 4 — the key
- Add the Key sprite, far away from the door.
- Give it the wait-and-broadcast stack. Make the message
got key. - On the door, add
when I receive [got key v]andhide. - Play the whole maze: key first, then door, then star.
Step 5 — play it twice
- Finish it, then press the flag.
- Crystals back, key back, door shut, gems at 0.
- Anything still missing needs a
show.
The door never opens.
The message names do not match. A message made twice with slightly different spelling gives you two messages, and the door is listening to the wrong one.
I can walk through the door.
Its step back is missing from one of the four directions. Check all four, not just the one you tried.
My crystals never come back.
They have hide but no show. Every sprite that hides during a game must show at the start of one.
Three things worth having
Put three collectibles in your maze, in places that are out of the way.
- Each one hides when collected and adds to the same number.
- None of them is on the direct route to the exit.
- All three come back when the flag is pressed.
A locked way out
Put a door between the player and the exit, and hide the key somewhere they have to go and find.
- The door is as solid as a wall until the key is taken.
- The key is a long way from the door.
- After the key, the door is gone and the way out is clear.
Teacher note — why the key does not open the door
Pupils always want the key to reach over and hide the door. It cannot: a sprite only controls itself. The message is not a workaround, it is how sprites talk, and this is the second time the arc has needed it. Worth naming.
All of them, or nothing
Take the key away. Now the door opens only when every crystal in the maze has been collected.
- Two crystals is not enough. Three is.
- The player can see how many they have.
- The door opens the moment the last one is taken, not a second later.
How to use this: Click the green flag, then click the stage once and use the arrow keys to move the Beetle.
Teacher reveal — the door watches a number
when flag clicked
show
wait until <(gems) = (3)>
hide
Four blocks, and no broadcast at all. Both shapes are correct: a message is right when one thing causes another, and watching a number is right when a condition does. Pupils who use <(gems) > (2)> have made the safer version again, for the same reason as the Muffin Catcher.
The one that is not a crystal
Somewhere in your maze is something that looks exactly like the others and is not. Touch it and you are back at the start with nothing.
How to use this: Click the green flag, then click the stage once and use the arrow keys to move the Beetle.
- The decoy looks like a real collectible.
- Touching it sends the player back to the start.
- It does not disappear, so it is still there next time round.
- Somebody who has played once can learn where it is.
Teacher notes — sending the player home
when flag clicked
show
forever
wait until <touching [Beetle v]?>
broadcast [caught v]
wait (1) seconds
end
The decoy, and on the player when I receive [caught v] with go to x: (180) y: (-120). The wait (1) seconds matters: without it the decoy fires again the moment the player is home, if home happens to be near it.
Going further: a decoy that only becomes a trap once you have two crystals would need the decoy to look at (gems) — a number one sprite sets and another reads. That is exactly what a shared variable is for.
Summary 4 min
The maze now has decisions in it, and they all come from three small facts.
- A collectible hides and counts.
showat the start brings it back. - A hidden sprite touches nothing, so hiding is how a door opens.
- A key does not open a door. It broadcasts, and the door opens itself.
- Watching a number is the other way to open a door, and it is just as correct.
- Collectible
- A sprite that hides when touched and adds to a count.
- Locked door
- A sprite that is solid like a wall until something tells it to hide.
- wait until
- Holds a script until a question is true. Neater than a forever with an if in it.
- Reset
- The show and the set-to-0 that make the second game the same as the first.
Next lesson: something in the maze starts moving, and it is not on your side.