- 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. 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 aforeverlets a sprite keep 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, stops, then keeps going. Your computer always waits a little before it repeats a key you hold. This is the key repeat pause. Hold down a letter key and you see “a…aaaaaa”. 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 one to use, and when
if on edge, bounce
Learning outcome
By the end of this lesson you will be able to:
- Build smooth keyboard control. Let two keys work at once.
- Say why a forever-if is better than key hats for a game.
- Build Cat & Mouse again with the better controls.
Learnt — controls that feel right 27 min
Two blocks with almost the same name
Scratch has two ways to ask about a key. They are not the same block.
| Block | Category | What it is |
|---|---|---|
when [right arrow v] key pressed | Events | A hat block. It starts a stack when you press the key. |
key [right arrow v] pressed? | Sensing | A question. It is true while you hold the key. It is false when you do not. |
Look at the pointed ends of the second one. That shape fits into an if <> then. 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. 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 really 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 difference. The loop never waits to be told again.
Feel the difference
Two cats, same two keys. The top one is built the Lesson 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. They are all checked every time round. So when you hold two keys, both work. The cat goes on a slant.
The event version cannot do this. Each hat is its own stack. Your computer only repeats the last key you pressed.
Bouncing off the edge
if on edge, bounce goes in the forever but outside all the ifs. Then it is checked every time round, whatever key is down.
Add set rotation style [left-right v] at the top too. If not, the bounce 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
The same game as last week, with better controls. The mouse gets one new idea too.
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 controls from the Learnt part on your cat. Test all four keys. Test two keys at once too. Then 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. It is the 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 when you catch it. Then it runs.
Step 4 — glide away
Then glide (1) secs to [random position v].
Step 5 — say Ouch at the same time
You could put say [Ouch!] for (1) seconds above the glide. But then the mouse stands still for a second first. That looks wrong.
So split it out. Add broadcast [ouch v] inside the if. 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. If not, it will often turn upside down when it points at you.
My cat moves, stops, then moves again.
You still have the old when [right arrow v] key pressed hats somewhere. Delete them. The loop does their job now. With both, the cat jumps two different distances.
Holding two keys only moves the cat one way.
Your ifs are inside each other. They should be one under the other. Use four separate ifs in the forever, not an if inside an if.
The clean-up game
You move one sprite. You must clear four others off the Stage. Your teacher will give you the two pictures. Or you can pick any two sprites from the library.
The rules:
- Arrow keys move your character. Use rotation style left–right.
- It starts in the middle. It bounces off the edges.
- It faces the way it moves. When it goes right, it looks right.
- When you hold space, it changes to a second costume. When you let go, it changes back.
- Add four of the other sprite. Each one starts in a random place.
- Touch one while you hold space. It shouts “No!” and hides. Touch one 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, with one hand on each set of keys. Then you never have to wait for anybody to test it.
- Player 1 moves with W A S D.
- Player 2 moves with the arrow keys.
- Add more of the other sprite, so there are enough for both players 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 kind of game, your idea. Make two players with smooth control. Add at least one character that nobody moves. It does something when either player touches it.
Pick your own sprites. The game does not have to be a chase.
Done when: you move both characters yourself, with one hand on each set of keys. Both move smoothly. The sprite nobody moves does something when one of them 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. It says so. Then the game stops for both of them.
Nobody can win twice. A second green flag starts a new race.
Summary 5 min
key [right arrow v] pressed?is a Sensing question, not an Events hat. It goes inside an if.- Key hats stop for a moment. This is because your computer waits before it repeats a key you hold. A forever-if never waits.
- With four ifs in one forever, two keys can work at the same time. The sprite goes on a slant.
if on edge, bouncegoes in the forever, outside the ifs. So it is checked every time 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. Use it for every control and every sensing script.
- Key repeat
- The short pause before your computer repeats a key you hold down.
- Smooth control
- A sprite moves the whole time you hold a key. There is no gap at the start.
- NPC
- A character nobody moves. It plays by itself.
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.