- 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. Use
hideto take it away. 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
This story works the first time. Why is the Stage empty the second time?
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. It is still hidden. Add show right 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 are different 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:
- Move a sprite around the Stage with the four arrow keys.
- Say what
change x by (10)does thatmove (10) stepsdoes not. - Make one sprite do something when it is touched. Use an
if <> theninside aforever.
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 small arrow on the block. 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 hat listens all the time while the project is open. It does not need the green flag.
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 need a when flag clicked stack. It puts 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?
When the cat faces right, these two blocks do the same thing. Turn the cat, and they do different things.
move (10) stepsgoes where the sprite’s nose points.change x by (10)goes right along the Stage. It does not matter which way the sprite faces.
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 do something when the cat touches it.
if <> then lives in Control. It needs a question inside it, called a condition. The condition 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. For a moment, the cat is still where you left it. It may be right on top of the mouse. The mouse feels a touch before you do anything. It runs away straight away.
Put wait (0.5) seconds before the forever. It gives every sprite time to get back to its start first. Wait half a second, and the bug is gone.
Glide, not go to
| Block | What it does |
|---|---|
go to x: (0) y: (0) | Jumps. The sprite is just somewhere else, straight away. |
glide (1) secs to x: (0) y: (0) | Slides there smoothly. It takes the number of seconds you give it. |
The number is the time, not the speed. Give it a far place and one second. It goes fast to get there in time.
glide (1) secs to [random position v] sends it somewhere new every time. Random means you cannot guess where. That is just right for a mouse that is being chased.
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
Start a new project. Add a backdrop. Add Mouse1 from the sprite library. Rename the sprites 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 stacks. Test each key when 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. Check that the mouse stays still until you 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
Why does the mouse run away before I touch it?
Maybe the wait (0.5) seconds is missing. Or it is inside the forever, not above it. It goes 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 keys go to the last thing you clicked. Still nothing? Check that the key hats are on the Cat, not on the Mouse.
Cat and mouse
Make the chase into a game you can really play.
- The arrow keys move 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 do not move must always turn to face the one you move.
Done when: you can go any way, and the other character always looks right at you. 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
You move one character. Add two or three others. Each one does something different when you touch it.
One might run away. One might say something. One might hide, change size, or glide back to where it started. You choose. But no two can be the same.
Done when: you move one sprite with the keyboard. At least two others do something when you touch them, each in its own way. In one run, you can make all three things 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. It starts coming after you. It is slow, but it never stops. You must 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 when you press the key. You do not need the green flag.change x by (10)moves right along the Stage.move (10) stepsmoves the way the sprite’s nose points. For keyboard games, use change x and change y.- An
if <> thenasks its question once. Put it inside aforever, and the sprite is always watching. wait (0.5) secondsbefore a sensing loop gives every sprite time to get to its start first.glide (1) secs to x: () y: ()slides.go to x: () y: ()jumps. 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 checks something. Am I touching it? Is a key pressed?
- Condition
- The question with pointed ends inside an if. Its answer is true or false.
- Forever-if
- An if inside a forever. Use it when a sprite must keep watching.
Hand in
Share your project. Send the link to your teacher. Missions you finish at home count too. Bring the link to the next class and claim your Adcoins.