- Learnt27 min · key pressed?, forever-if control, bounce on edge
- Basic25 min · rebuild Cat & Mouse with smooth controls
- Challenge 1–327 min · the clean-up game, two players, your own
- Extrabonus · make it a race
Review — where we got to 5 min
Last lesson you made a game you could play. You learnt three things:
when [right arrow v] key pressedis a hat block, and it runs without the green flag.change x by (10)goes right along the Stage;move (10) stepsfollows the sprite’s nose.- An
if <> theninside aforeveris how a sprite keeps watching.
Quick-fire
Hold the right arrow key down. What does the cat do?
when [right arrow v] key pressed
change x by (3)
Reveal the answer
It moves 3, pauses, then keeps going. That pause is your computer’s key-repeat delay — the same one that turns “aaa” into “a…aaaaaa” when you hold a letter down. Today you get rid of it.
Today’s Topic 3 min
- Smooth control with the forever-if loop
key [space v] pressed?— the Sensing block, not the hat block- Events or forever-if: which to use, and when
if on edge, bounce
Learning outcome
By the end of this lesson you will be able to:
- Build keyboard control that is smooth and lets two keys work at once.
- Say why a forever-if is better than key-pressed hats for a game.
- Rebuild Cat & Mouse with the better controls under it.
Learnt — controls that feel right 27 min
Two blocks with almost the same name
Scratch has two ways to ask about a key, and they are not the same block.
| Block | Category | What it is |
|---|---|---|
when [right arrow v] key pressed | Events | A hat block. Starts a stack when the key goes down. |
key [right arrow v] pressed? | Sensing | A question. True while the key is held, false when it is not. |
The pointed shape of the second one is the clue: it drops into an if <> then, and today it goes inside a forever.
Build the loop
- Start with the usual setup: go to a position, point in direction 90.
- Snap a
foreverunderneath. - Drop an
if <> theninside the forever. - Drag
key [space v] pressed?into the pointed hole and change it to right arrow. - Put
change x by (3)inside the if.
when flag clicked
go to x: (-190) y: (-60)
point in direction (90 v)
forever
if <key [right arrow v] pressed?> then
change x by (3)
end
end
What the computer actually does
- Go to (−190, −60).
- Point in direction 90.
- Go into the forever loop.
- Is the right arrow down?
- If yes, change x by 3.
- Back to the top of the loop.
- Is the right arrow down? …and on, and on.
There is no pause anywhere in that list. That is the whole difference — the loop never waits to be told again.
Feel the difference
Two cats, same two keys. The top one is wired the Topic 1.5 way; the bottom one uses the loop.
How to use this: Click the green flag, click the stage once, then hold ← or →. Watch the two cats: one jumps in steps, the other slides smoothly.
All four keys, and two at once
Add three more ifs inside the same forever. Because they are all checked every round, two keys held together both happen — and the cat goes diagonally.
The event version cannot do this at all. Each hat is its own stack, and your computer only repeats the key you pressed most recently.
Bouncing off the edge
if on edge, bounce goes in the forever but outside all the ifs. That way it is checked every round, whichever key is down.
Add set rotation style [left-right v] at the top too, or bouncing turns the cat upside down.
when flag clicked
set rotation style [left-right v]
go to x: (0) y: (0)
point in direction (90 v)
forever
if <key [right arrow v] pressed?> then
point in direction (90 v)
change x by (4)
end
if <key [left arrow v] pressed?> then
point in direction (-90 v)
change x by (-4)
end
if <key [up arrow v] pressed?> then
change y by (4)
end
if <key [down arrow v] pressed?> then
change y by (-4)
end
if on edge, bounce
end
How to use this: Click the green flag, then click the stage once and use the arrow keys to move the Cat.
Basic — Cat & Mouse, done properly 25 min
Same game as last week, better controls — and one new idea on the mouse’s side.
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 controller
Build the full forever-if controller from the Learnt section on your cat. Test all four keys and both diagonals before you go on.
Step 2 — the mouse senses you
Give the Mouse a forever with if <touching [Cat v]?> then inside, and wait (0.5) seconds above the forever. Same as last week.
Step 3 — it looks at you first
Put point towards [Cat v] as the first block inside the if. Now the mouse turns to face you at the moment you catch it, before it runs.
Step 4 — glide away
Then glide (1) secs to [random position v].
Step 5 — say Ouch at the same time
Putting say [Ouch!] for (1) seconds above the glide makes the mouse stand still for a second first, which looks wrong.
Instead, split it out. Add broadcast [ouch v] inside the if, and give the Mouse a second stack:
when I receive [ouch v]
say [Ouch!] for (1) seconds
Step 6 — tidy the mouse up
Give the Mouse set rotation style [left-right v] too. Pointing towards you will otherwise flip it upside down half the time.
My cat moves in fits and starts.
You still have the old when [right arrow v] key pressed hats somewhere. Delete them — the loop replaces them completely, and having both makes the cat jump two different distances.
Holding two keys only moves the cat one way.
Your ifs are nested inside each other instead of stacked one under the other. They should be four separate ifs sitting in the forever, not an if inside an if.
The clean-up game
One sprite you drive, four you have to clear off the Stage. Your teacher will give you the two pictures — or pick any two sprites from the library.
The rules:
- Arrow keys drive your character, with rotation style left–right.
- It starts in the middle, and bounces off the edges.
- Facing follows the movement: going right, it looks right.
- Holding space switches it to a second costume. Letting go switches it back.
- Four of the other sprite, each starting somewhere random.
- Touched while you hold space, one shouts “No!” and disappears. Touched without space, it says “I’m not afraid of you!” and stays.
Teacher reveal — the space-bar costume
This is another forever-if, and it belongs on the player sprite beside the movement one:
when flag clicked
forever
if <key [space v] pressed?> then
switch costume to [spraying v]
else
switch costume to [normal v]
end
end
The else half is what makes it snap back when the key is let go. Pupils who use two separate ifs get the same result and it is worth accepting — the if / else block is Topic 1.8’s job.
The “disappear” is hide, and every virus therefore needs show and a go to [random position v] on its flag stack. That is Topic 1.4 coming back round.
Two players, one keyboard
Turn Challenge 1 into a two-player game. You can play both sides yourself — one hand on each set of keys — so you never have to wait for anybody to test it.
- Player 1 drives with W A S D.
- Player 2 drives with the arrow keys.
- Add more of the other sprite, so there is enough for both of them to chase.
Teacher note
This is where the forever-if pays for itself, and it is worth saying out loud. Two players on key-pressed hats fight over the key-repeat, so one of them freezes whenever the other is holding a key. Two forever-if loops on two sprites do not notice each other at all.
Each player’s sprite gets its own loop. There is no shared script and no coordination — they simply both run.
Your own two-player game
Same shape, your idea. Two players with smooth control, and at least one character that neither of them drives but that reacts to both.
Pick your own sprites. The game does not have to be a chase.
Done when: you drive both characters yourself, one hand on each set of keys, both move smoothly, and the sprite nobody drives responds to whichever one reaches it first.Teacher note
The interesting design question is how the NPC tells the two players apart. Two separate ifs — touching [Player1 v]? and touching [Player2 v]? — in the same forever is the answer, and it sets up scoring in Topic 1.11 nicely.
Make it a race
Two players, one finish line. Both start on the left edge. The first one to touch the right edge wins, says so, and the game stops for both of them.
Nobody can win twice — a second green flag starts a clean race.
Summary 5 min
key [right arrow v] pressed?is a Sensing question, not an Events hat. It goes inside an if.- Key-pressed hats stutter, because your computer waits before repeating a held key. A forever-if never waits.
- Four ifs in one forever means two keys can work at the same time — diagonals.
if on edge, bouncegoes in the forever, outside the ifs, so it is checked every round.- Use the forever-if for a game. Use the event hat for a story where a key is pressed now and then.
- Forever-if loop
- An if inside a forever. The shape of every control and every sensing script.
- Key repeat
- Your computer’s pause before it starts repeating a key you are holding down.
- Smooth control
- A sprite that moves the whole time a key is held, with no gap at the start.
- NPC
- A character nobody drives — it plays itself.
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.