Learning Goals 3 min
By the end of this lesson you will be able to:
- Drag
when [space v] key pressedfrom Events and choose a key from its drop-down. - Build several scripts on one sprite, each waiting for a different key.
- Steer the cat around the stage with the arrow keys, with no green flag involved at all.
Warm-Up 7 min
Your show has an opening cue. But once the flag is clicked, the audience can only sit and watch — there is nothing for them to do. Today that changes.
Quick-fire puzzle
Faiz built this script and clicked the green flag. Nothing happened. Then he pressed the space bar and the cat moved. Why?
when [space v] key pressed
move (20) steps
Reveal the answer
This script never asked to hear the flag. It is waiting for the space bar, so the flag click sailed straight past it.
That is the whole idea: different hats listen for different events. One sprite can hold a script that waits for the flag and another that waits for a key, and neither interferes with the other.
New Concept — keys are events too 15 min
Imagine a house with two doorbells — one at the front, one at the back, each with its own chime. Same house, two bells, two different responses. Your keyboard works the same way: every key is its own bell, and scripts wait behind whichever one you choose.
The new block
| Block | Category | What it does |
|---|---|---|
when [space v] key pressed | Events | Waits for one particular key, then runs the blocks below. The drop-down offers space, the four arrows, every letter and every digit. |
Choosing the key
Click the word space inside the block and a long list drops down. Pick anything from it and the block relabels itself. The rest of the script does not change at all — only the cue does.
when [right arrow v] key pressed
move (20) steps
One sprite, many scripts
You saw last lesson that a sprite can hold more than one script. Now that becomes genuinely useful: one cat can answer the flag, the space bar, the right arrow and the letter m, each with its own separate stack.
To add a second script, drag another hat in and drop it well clear of the first. Leave a visible gap so the two do not snap together — a stack that accidentally joins on will run at the wrong moment.
Why it matters
Key scripts are how every game hands control to the player. Arrows walk a character, space makes it jump, letters pick weapons or open doors. Without them a sprite is a film; with them it is something you can play.
Worked Example — steer the cat with the arrows 15 min
We build two scripts on the same sprite, each answering a different arrow key.
Step 1 — Drag the first key hat
From Events, drop when [space v] key pressed into the Script Area, then click its drop-down and choose right arrow.
Step 2 — Give it an action
Snap move (10) steps underneath and change the number to 20. Click the Stage, then press the right arrow. The cat shifts.
when [right arrow v] key pressed
move (20) steps
Step 3 — Add the second hat, well clear of the first
Drag in another key hat and drop it with a clear gap below the first stack. Set its drop-down to left arrow. The gap matters — snap it on by accident and the left-arrow script becomes part of the right-arrow one.
Step 4 — Move the other way
Snap on a move block and type -20. A negative move sends the cat backwards without turning it, which you met back in Lesson 7.
when [right arrow v] key pressed
move (20) steps
when [left arrow v] key pressed
move (-20) steps
Step 5 — Drive it
Click the Stage so the keyboard is heard, then tap both arrows. You are steering a sprite — the first genuinely interactive thing you have built.
Step 6 — Add a third cue for fun
Drag in one more hat, set it to the space bar, and give it say [Meow!] for (1) seconds. Now the cat walks on the arrows and speaks on space, with three scripts sitting happily on one sprite.
What changed: until today every project ran because you clicked the flag. Now it runs because somebody is playing with it.
The full assembled scripts (your reference)
when [right arrow v] key pressed
move (20) steps
when [left arrow v] key pressed
move (-20) steps
Try It Yourself — three small builds 12 min
Goal: Make the space bar produce a meow — with no movement at all. Press it five times and count the bubbles.
when [space v] key pressed
say [Meow!] for (1) seconds
Think: the green flag plays no part here. A script does not need the flag to exist — it needs a cue, and space is as good as any.
Goal: Add up and down arrows that turn the cat rather than moving it, so all four arrows do something different.
when [up arrow v] key pressed
point in direction (0)
when [down arrow v] key pressed
point in direction (180)
Think: now the sideways keys move and the vertical keys steer — so pressing up then right sends the cat somewhere quite different from right on its own.
Mini-mission: the two-key dance step. Give the cat a dance move that only works when the audience presses two different keys in the right order — one key sets it up, the other sets it off.
It works if: pressing key 2 on its own does something visibly different from pressing key 1 first and then key 2 — and you can explain to a partner why. Keep each stack under 8 blocks.
Think: what could the first key change that the second key would then depend on? Direction is one answer; there are others.
Mini-Challenge — the right-arrow road trip 5 min
“Drive Aiman’s cat across the stage”
Build a cat the audience can drive from one side of the stage to the other using a single key — and small enough steps that it takes real effort.
It works if:
- The cat starts at the far left of the stage.
- One key drives it, and each press moves it a small, steady amount.
- Enough presses get it all the way to the right edge.
- You can say roughly how many presses the trip takes, and why.
Reveal one valid solution (teacher)
Set the cat’s x to -200 in Sprite Properties, then build:
when [right arrow v] key pressed
move (10) steps
From −200 to the right edge at 240 is 440 pixels, so at ten per press that is about 44 presses. Working that out is the real task — the script itself is two blocks.
Recap 2 min
You met a second hat block today. when [space v] key pressed waits for one key, chosen from its drop-down, and ignores everything else — including the green flag. Because a sprite can carry as many scripts as you like, one cat can now answer several keys at once, each with its own independent stack.
- when [key v] key pressed
- An Events hat that waits for one keyboard key, picked from its drop-down.
- Drop-down menu
- A small list inside a block, marked with a tiny arrow. Click it to choose from preset options.
- Key event
- The moment a key goes down. Any script with a matching key hat wakes up.
- Multiple scripts
- One sprite can hold many scripts, each headed by its own hat and running independently.
Homework 1 min
The keyboard cat. Build four scripts on one sprite so all four arrow keys do something.
- Drag four key hats into the Script Area, well apart from each other.
- Set their drop-downs to right arrow, left arrow, up arrow and down arrow.
- Under each, snap one motion block:
move (20) steps,move (-20) steps,change y by (20)andchange y by (-20). - Click the Stage, then test all four keys. Screenshot your Script Area.
when [up arrow v] key pressed
change y by (20)
when [down arrow v] key pressed
change y by (-20)
change y by (20) is a Motion block that shifts a sprite up or down without turning it.Bring back next class:
- A screenshot showing all four scripts.
- Your answer to: “Why does each arrow need its own hat block? Could you put all four under one hat instead?”
Heads up for next class: SCR-L01-17 brings the third hat — when this sprite clicked. The audience will be able to poke the cat directly.