- 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. Touching it ends the game.
Quick-fire
Your maze has one way from start to exit. Why does nobody play it twice?
Reveal the answer
There is nothing to choose. You learn the way once. After that you just hold keys. Today we add things that make you choose which way to go.
Today’s Topic 3 min
- Collectibles: sprites that hide when you touch them
change [gems v] by (1)to count what you pick up- A hidden sprite touches nothing. That 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 things to collect 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 collectible is a thing you pick up. Here it is a crystal. When you touch it, it hides 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 you do not need a forever loop.- Make a variable called
gems. 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 key fact today. Say it out loud: a hidden sprite is not touching anything. Not the player, not a wall, nothing.
So a solid door can hide. Then you can walk through it.
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 and sends a message. Then the door opens itself. You have seen this before. It is like the cues in the music video.
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
The two sprites know nothing about each other. A message joins them. So you can add a second door later with 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. Play after each one.
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 path. Give it the five-block stack.
- Play and collect it. 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 path. - Play. All three should add to the same number.
Step 3 — the door
- Add a sprite for the door. Put it across a path 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 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.
- Check: crystals back, key back, door shut, gems at 0.
- Is something still missing? It needs a
show.
The door never opens.
The message names do not match. Maybe you made the message twice, spelt a bit differently. Then you have two messages. 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. If a sprite hides in a game, it must show at the start of the next one.
Three things worth having
Put three collectibles in your maze. Put them in places that are out of the way.
- Each one hides when you collect it. Each adds to the same number.
- None of them is on the shortest way to the exit.
- All three come back when you press the flag.
A locked way out
Put a door between the player and the exit. Hide the key somewhere. The player has to go and find it.
- 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. 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 you collect every crystal in the maze.
- Two crystals is not enough. Three is.
- The player can see how many they have.
- The door opens as soon as 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
Something in your maze looks just like the other crystals. But it is not one. 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 is the fake one. It looks like a real collectible.
- Touching it sends the player back to the start.
- It does not vanish, so it is still there next time.
- Someone 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 choices in it. They all come from three small facts.
- A collectible hides and counts.
showat the start brings it back. - A hidden sprite touches nothing. So a door opens by hiding.
- A key does not open a door. It broadcasts, and the door opens itself.
- A door can also open by watching a number. That works just as well.
- 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
- Makes a script wait until a question is true. Neater than a forever with an if in it.
- Reset
- The show and the set to 0. They make the second game the same as the first.
Next lesson: something in the maze starts moving. It is not on your side.