- Learnt27 min · how an NPC knows, two stacks at once, random drift
- Basic25 min · finish the game
- Challenge 1–327 min · eight of them, two players, Cat vs Bat
- Extrabonus · one of them takes two hits
Review — where we got to 5 min
Last lesson you coded the character you drive, one game rule at a time.
- Write the rules down first, then build them in order, testing after each one.
if / elsehas two mouths. One of them always runs.if on edge, bouncegoes last in the forever, outside every if.
Quick-fire
The player is holding space. What is on screen?
if <key [space v] pressed?> then
switch costume to [Spray v]
else
switch costume to [No Spray v]
end
Reveal the answer
The Spray costume. And the moment they let go, the No Spray one — that is the else half doing its job.
Today’s Topic 3 min
- Coding the second half of the game — the sprites nobody drives
- Using
if / elseon an NPC - Sensing the player with a second script running alongside
- Duplicating a finished sprite
- Random movement with glide
Learning outcome
By the end of this lesson you will be able to:
- Make an NPC react differently depending on what the player is doing.
- Duplicate a sprite once it is finished, and know why that order matters.
- Give a sprite two scripts so it can move and sense at the same time.
Learnt — building the other side 27 min
How does the NPC know?
Rules 7 and 8 say the same bump has two different outcomes, depending on whether the player is spraying. So the NPC has to find that out.
It cannot see the player’s costume. But it can read the keyboard — and the player decided its costume from the keyboard too. So both sprites ask the same question:
if <key [space v] pressed?> then
say [No!] for (1) seconds
hide
else
say [I'm not afraid of you!] for (1) seconds
end
Only when it is touched
That if / else must not run all the time — only when the two sprites are actually touching. So it goes inside a forever-if, exactly like Topic 1.5.
when flag clicked
show
go to [random position v]
wait (0.5) seconds
forever
if <touching [Cleaner v]?> then
if <key [space v] pressed?> then
say [No!] for (1) seconds
hide
else
say [I'm not afraid of you!] for (1) seconds
end
end
end
Finish one, then duplicate
You need several of these. Do not make them first and code them one by one.
- Build one NPC completely.
- Test it. Both outcomes.
- Right-click the sprite in the Sprite panel and choose duplicate.
- Duplicate twice more. All the code comes with each copy.
Finding a bug after you have made four copies means fixing it four times. Finding it before means fixing it once.
Making them move — and why it needs a second stack
Now make them drift about. glide (3) secs to [random position v] in a forever does it.
But it cannot go in the sensing loop. A three-second glide holds that script for three seconds — and while it is gliding, the sprite is not checking whether it has been touched. You would walk right through it.
So it gets its own stack, with its own green-flag hat:
when flag clicked
forever
glide (3) secs to [random position v]
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.
glide or go to?
| Block | What it does |
|---|---|
go to [random position v] | Teleports. It is simply somewhere else, instantly. |
glide (3) secs to [random position v] | Slides there over three seconds. You can watch it travel — and catch it. |
Use go to x: () y: () to place things at the start. Use glide () secs to x: () y: () for anything the player is supposed to see move.
Basic — finish the game 25 min
Open your Topic 1.7 project. The player is already done; today is everything else.
How to use this: Click the green flag, then click the stage once and use the arrow keys. The space bar does something as well — find out what.
Step 1 — one NPC, placed at random
On your virus sprite: when flag clicked, show, go to [random position v]. Click the flag a few times and watch it land somewhere different each time.
Step 2 — it notices you
Add wait (0.5) seconds, then a forever with if <touching [Cleaner v]?> then inside.
Step 3 — the two outcomes
Put the if / else on key [space v] pressed? inside that if. Say and hide in the top mouth; say only in the bottom one.
Step 4 — test both
Drive into it without pressing space. It should tell you off and stay. Drive into it holding space. It should shout and disappear. Do not go on until both work.
Step 5 — duplicate
Right-click the sprite, duplicate, three times. Click the flag: four of them, all in different places.
Step 6 — make them drift
Add the second stack — when flag clicked then a forever with glide (3) secs to [random position v] — to each of them.
If you duplicated before adding this, you have to add it four times. That is the lesson about doing it in the right order, learnt the slow way.
They vanish even when I am not pressing space.
Your hide is outside the if / else, so it runs whichever way the question is answered. It belongs inside the top mouth only.
I can walk straight through them sometimes.
The glide is inside the sensing loop. Split it into its own stack with its own when flag clicked.
Eight of them, and no bouncing
Finish the game properly.
- At least eight NPCs.
- Your character faces the right way whichever direction it is going.
- Take
if on edge, bounceoff your character. It should be able to sit against a wall.
Teacher note
Removing the bounce is worth a sentence in class: with it, a character pinned in a corner keeps flipping direction and becomes unsteerable. It was the right block for Topic 1.6’s free-roaming cat and it is the wrong one here. Blocks are not good or bad on their own.
Two players, twelve to clear
- Player 1 on W A S D, player 2 on the arrow keys.
- At least twelve NPCs.
- Each player’s spray looks different from the other’s.
Teacher reveal — the hard part is the NPC
Two players means the NPC has two ways to die, and each one has its own spray key. That is two touching-checks in the same forever:
if <touching [Player1 v]?> then
if <key [space v] pressed?> then
hide
end
end
if <touching [Player2 v]?> then
if <key [m v] pressed?> then
hide
end
end
Pupils who try to write one check for “either player” usually reach for <> or <> and then discover they cannot tell which spray key to look at. That dead end is a good one — let them find it.
Cat vs Bat
A new game with the same machinery. The rules:
- Arrow keys drive the cat. It starts in the middle.
- Space not pressed: flying. Space pressed: punching.
- Going right it faces right, going left it faces left.
- Six bats at 40% size, spawning randomly and drifting randomly.
- A bat hit while you are flying: nothing happens.
- A bat hit while you are punching: it disappears.
Teacher note
This is the same game with different pictures, and saying so is the point of setting it. A pupil who realises they can copy their whole virus script onto a bat and change two words has understood something worth more than the game.
One of them takes two hits
Add one big NPC that does not go down on the first hit. Spray it once and it says something and shrinks. Spray it again and it goes.
The others still take one hit. Only this one is different.
Summary 5 min
- An NPC cannot see the player’s costume. It reads the same key the player read, so both agree.
if / elsegives one bump two outcomes.- Finish one sprite completely, then duplicate. Fixing a bug four times is the price of doing it the other way round.
- A glide holds its script while it runs. Anything that must keep sensing needs a second stack.
go to x: () y: ()teleports;glide () secs to x: () y: ()travels.
- NPC
- Non-playing character — a sprite nobody drives.
- Duplicate
- A copy of a sprite, with all its scripts and costumes.
- Blocking
- When one slow block holds up everything below it in the same stack.
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.