- Learnt27 min · arrow keys, change x and y, forever-if, glide
- Basic25 min · build Cat vs Mouse
- Challenge 1–327 min · the chase, your own cast, your own game
- Extrabonus · now it chases you
Review — where we got to 5 min
Last lesson you built a story with scenes. You learnt three things:
- Start a character outside the Stage and walk it in. That is an entrance.
- A sprite cannot walk off the Stage.
hideis what takes it out of the picture — and every hide needs ashowat the top. - The Stage has its own scripts. That is where
switch backdrop to [Hall v]and the music go.
Quick-fire
Why does this story work the first time and play to an empty Stage the second?
when flag clicked
go to x: (-200) y: (-40)
say [Bye!] for (2) seconds
repeat (20)
move (10) steps
end
hide
Reveal the answer
There is no show. The cat hid at the end of the first run and is still hidden. Add show straight under the hat block.
Today’s Topic 3 min
- An interactive story — one you control with the keyboard
change x by (10)andchange y by (10), and how they differ frommove (10) steps- The forever-if loop, for sensing
glide (1) secs to x: (0) y: (0)
Learning outcome
By the end of this lesson you will be able to:
- Drive a sprite around the Stage with the four arrow keys.
- Say what
change x by (10)does thatmove (10) stepsdoes not. - Make one sprite react to being touched, using
foreverwrapped round anif <> then.
Learnt — driving, and sensing 27 min
A key is a hat block
when [space v] key pressed lives in Events, right under when flag clicked. It is a hat block, so it starts its own stack.
- Drag
when [space v] key pressedinto the Script Area. - Click the dropdown and choose right arrow.
- Snap
change x by (10)underneath. - Press the right arrow key. You do not touch the green flag at all.
That last part surprises everybody. A key-pressed hat is listening the whole time the project is open — the green flag is not involved.
All four directions
Four keys, four stacks. Two change x, two change y.
when [right arrow v] key pressed
change x by (10)
when [left arrow v] key pressed
change x by (-10)
when [up arrow v] key pressed
change y by (10)
when [down arrow v] key pressed
change y by (-10)
You still want a when flag clicked stack, but only to put the cat back where it started:
when flag clicked
go to x: (-190) y: (-60)
point in direction (90 v)
How to use this: Click the green flag, then click the stage once and use the arrow keys to move the Cat.
change x by (10) or move (10) steps?
On a cat facing right, these two blocks do exactly the same thing. Turn the cat, and they stop agreeing.
move (10) stepsgoes where the sprite’s nose points.change x by (10)goes right along the Stage, whichever way the sprite is facing.
How to use this: Click the green flag to start it. Nothing else to press — watch it through, then use ↺ Reset to run it again.
Sensing — the forever-if loop
Now the other character. It should react when the cat touches it.
if <> then lives in Control, and the condition to drop into it — touching [Cat v]? — lives in Sensing.
when flag clicked
go to x: (150) y: (-60)
wait (0.5) seconds
forever
if <touching [Cat v]?> then
glide (1) secs to x: (150) y: (90)
say [Too close!] for (1) seconds
end
end
How to use this: Click the green flag to start it. Nothing else to press — watch it through, then use ↺ Reset to run it again.
Why the wait 0.5 seconds?
Press the flag a second time and, for a moment, the cat is still standing where you left it — possibly right on top of the mouse. The mouse senses a touch before you have done anything and bolts immediately.
wait (0.5) seconds before the forever gives every sprite time to get back to its starting place first. Half a second, and the bug is gone.
Glide, not go to
| Block | What it does |
|---|---|
go to x: (0) y: (0) | Teleports. The sprite is simply somewhere else, instantly. |
glide (1) secs to x: (0) y: (0) | Slides there smoothly, taking the number of seconds you give it. |
The seconds are the time, not the speed. Give it a faraway place and one second, and it travels fast to get there on time.
glide (1) secs to [random position v] sends it somewhere different every time — which is exactly what a mouse being chased should do.
Basic — build Cat vs Mouse 25 min
A cat you drive, and a mouse that runs away when you catch it.
How to use this: Click the green flag, then click the stage once and use the arrow keys to move the Cat.
Step 1 — the cast
New project. Add a backdrop, and add Mouse1 from the sprite library. Rename them Cat and Mouse.
Step 2 — where they start
Give the Cat a when flag clicked stack with go to x: (-190) y: (-60) and point in direction (90 v). Give the Mouse one with go to x: (150) y: (-60).
Step 3 — the controls
On the Cat, build the four key-pressed stacks. Test each key as you add it.
Step 4 — the mouse notices
On the Mouse, add a forever holding if <touching [Cat v]?> then, with a glide (1) secs to [random position v] inside.
Step 5 — the buffer
Put wait (0.5) seconds between the go to x: () y: () and the forever. Now press the flag twice and check the mouse stays put until you actually reach it.
Step 6 — check your work against this
when flag clicked
go to x: (150) y: (-60)
wait (0.5) seconds
forever
if <touching [Cat v]?> then
glide (1) secs to [random position v]
end
end
The mouse runs away before I touch it — why?
Either the wait (0.5) seconds is missing, or it is inside the forever instead of above it. It belongs between the go to x: () y: () and the forever, where it runs once.
The arrow keys do nothing.
Click once on the Stage first — the keyboard goes to whatever you last clicked. If it still does nothing, check the key-pressed hats are on the Cat and not on the Mouse.
Cat and mouse
Make the chase into a game you can actually play.
- The arrow keys drive the cat in all four directions.
- Touching the mouse sends it gliding to a random place.
- It works the same on the second and third green flag.
Teacher note
Two failures are worth catching early. A pupil who used move (10) steps instead of change x by (10) has a cat that steers oddly once anything turns it. And a missing wait (0.5) seconds shows up only on the second run, so ask everyone to press the flag twice before they call it finished.
Your cast, and it watches you
Swap both characters for sprites you pick yourself. Then add one new rule:
The character you are not driving must always turn to face the one you are.
Done when: whichever way you drive, the other character is looking straight at you — and it still runs away when you catch it.Teacher reveal — pointing at something
point towards [Cat v] is the block, and it belongs inside the same forever as the touching check, above the if <> then. Facing has to be re-decided constantly, exactly like sensing.
It will stand on its head as soon as the player goes left, which is Topic 1.3’s set rotation style [left-right v] — worth letting them rediscover rather than telling them.
Design your own game
One character you drive, and two or three others that each do something different when you touch them.
One might run away. One might say something. One might hide, or change size, or glide back to where it started. Your choice — but no two the same.
Done when: your game has one sprite under keyboard control and at least two others that react to being touched, each in its own way, and one run makes all three reactions happen.Teacher note
Every reacting sprite needs its own forever with its own if <> then. That is the moment parallel scripting stops being a trick from Topic 1.3 and becomes how a game is built: four sprites all watching at once, none of them waiting for the others.
Now it chases you
Turn the game round. The other character stops running away and starts coming after you, slowly and without ever stopping. You have to keep away from it.
When it catches you, the game says so and stops.
Summary 5 min
when [right arrow v] key pressedis a hat block. It runs the moment you press the key — no green flag needed.change x by (10)moves right along the Stage.move (10) stepsmoves along the sprite’s nose. For keyboard controls, use change x and change y.- An
if <> thenasks its question once. Put it inside aforeverand the sprite is always watching. wait (0.5) secondsbefore a sensing loop gives everything time to get to its starting place first.glide (1) secs to x: () y: ()slides;go to x: () y: ()teleports. The number is the time, not the speed.
- Interactive
- A project that answers back — it does something because of what you do.
- Sensing
- A sprite checking something about the world: am I touching, is a key down.
- Condition
- The pointed-bracket question inside an if. It is either true or false.
- Forever-if
- An if inside a forever — the shape you use whenever a sprite must keep watching.
Hand in
Share your project and send the link to your teacher. Missions you finish at home count too — bring the link to the next class and claim your Adcoins.