- 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 move, one game rule at a time.
- Write the rules down first. Then build them in order. Test 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. When they let go, it is the No Spray one. That is the else part doing its job.
Today’s Topic 3 min
- Coding the second half of the game: the sprites nobody moves
- Using
if / elseon an NPC - Sensing the player, with a second script running at the same time
- Duplicating a finished sprite
- Random movement with glide
Learning outcome
By the end of this lesson you will be able to:
- Make an NPC do different things, based on what the player does.
- Duplicate a sprite when it is finished. Say 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?
An NPC is a character that nobody moves. Rules 7 and 8 say one bump can end two ways. It depends on if the player is spraying. So the NPC must find that out.
It cannot see the player’s costume. But it can read the keyboard. The player picks 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. It must only run when the two sprites touch. So it goes inside a forever-if, just like in Lesson 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 lots of these. Do not make them all first and then code them one by one.
- Build one NPC completely.
- Test it. Test both endings.
- Right-click the sprite in the Sprite panel. Choose duplicate.
- Duplicate twice more. All the code comes with each copy.
Find a bug after you make four copies, and you must fix it four times. Find it before, and you fix it once.
Making them move — and why it needs a second stack
Now make them drift about. Put glide (3) secs to [random position v] in a forever.
But it cannot go in the sensing loop. A three-second glide stops that script for three seconds. While it glides, the sprite does not check if it is 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] | Jumps. It is just somewhere else, straight away. |
glide (3) secs to [random position v] | Slides there in three seconds. You can watch it move, and catch it. |
Use go to x: () y: () to place things at the start. Use glide () secs to x: () y: () for things the player should see move.
Basic — finish the game 25 min
Open your project from Lesson 7. The player is done. Today you make 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. Watch it land in a new place 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. Put say and hide in the top mouth. Put only say in the bottom one.
Step 4 — test both
Move into it without pressing space. It should talk back and stay. Move into it while you hold space. It should shout and hide. Do not go on until both work.
Step 5 — duplicate
Right-click the sprite and duplicate it, three times. Click the flag. There are four of them, all in different places.
Step 6 — make them drift
Add the second stack to each of them. It is when flag clicked, then a forever with glide (3) secs to [random position v].
Did you duplicate before adding this? Then you must add it four times. Now you know why the order matters.
They hide even when I am not pressing space.
Your hide is outside the if / else. So it runs, whatever the answer is. Put it inside the top mouth only.
I can walk straight through them sometimes.
The glide is inside the sensing loop. Move it to 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 way it is going.
- Take
if on edge, bounceoff your character. It should be able to stay next to 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 that works the same way. The rules:
- Arrow keys move the cat. It starts in the middle.
- Space not pressed: flying. Space pressed: punching.
- When it goes right, it faces right. When it goes left, it faces left.
- Six bats at 40% size. They start in random places and drift about.
- You hit a bat while you are flying: nothing happens.
- You hit a bat while you are punching: it hides.
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. It does not go on the first hit. Spray it once. It says something and gets smaller. 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 as the player. So both agree.
if / elselets one bump end two ways.- Finish one sprite, then duplicate it. If you do it the other way round, you fix each bug four times.
- A glide stops its script while it runs. A sprite that must keep sensing needs a second stack.
go to x: () y: ()jumps.glide () secs to x: () y: ()slides.
- NPC
- Non-playing character. A sprite nobody moves.
- Duplicate
- A copy of a sprite, with all its scripts and costumes.
- Blocking
- One slow block makes every block under it in the stack wait.
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.