- Learnt27 min · pick random, steady speed, bouncing on its own stack
- Basic25 min · build Cat vs Bat
- Challenge 1–327 min · six bats, all different, your own game
- Extrabonus · one of them is the fast one
Review — where we got to 5 min
Last lesson you built the sprites nobody drives.
- An NPC reads the same key the player reads, so both agree about what is happening.
- Finish one sprite, then duplicate. Not the other way round.
- A glide holds up its own stack, so sensing needs a second one.
Quick-fire
Where does this sprite end up?
when flag clicked
go to [random position v]
Reveal the answer
Nobody knows — and that is the point. Somewhere different every time you click. Today you get to choose the range yourself.
Today’s Topic 3 min
- The Cat vs Bat game
- The
pick random (1) to (10)block - Random movement at a steady speed
Learning outcome
By the end of this lesson you will be able to:
- Drop
pick random (1) to (10)into another block’s number slot. - Make a sprite wander unpredictably without changing speed.
- Animate an NPC while it moves.
Learnt — random, and how fast 27 min
A block that goes inside another block
pick random (1) to (10) lives in Operators, and it is a different shape from everything you have used so far. It is an oval.
Oval blocks are not instructions. They are values — a number, dressed up as a block. You drop them into the white holes of other blocks.
- Drag
move (10) stepsinto the Script Area. - Drag
pick random (1) to (10)from Operators. - Drop it straight onto the white 10. It fills the hole.
- Click the block a few times and watch the sprite go a different distance each time.
pick random (1) to (10) gives you any whole number from 1 to 10, and each is as likely as the others. Change the numbers and you change the range — pick random (-10) to (10) can give you a negative one too.
Pointing anywhere
Directions run from −180 to 180. So point in direction (pick random (-180) to (180)) aims a sprite absolutely anywhere.
Flying at a steady speed
Topic 1.8 made NPCs drift with glide (3) secs to [random position v]. It works, but it looks odd, and here is why.
A glide always takes the seconds you gave it. A long way in 3 seconds means racing; a short way in 3 seconds means crawling. The sprite keeps changing pace for no reason.
Pointing and moving does not have that problem. Every step is the same size.
when flag clicked
set rotation style [left-right v]
go to [random position v]
forever
point in direction (pick random (-180) to (180))
repeat (pick random (10) to (20))
move (5) steps
next costume
wait (0.05) seconds
end
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.
Bouncing needs its own stack
The flight is repeat (20) long. If if on edge, bounce sat after it, the bat would bury its nose in the wall and stay there until the twenty steps ran out.
So it gets a stack of its own, checking every single frame:
when flag clicked
forever
if on edge, bounce
end
That is parallel scripting for the third lesson running. The pattern is always the same: if one thing is slow and another must be quick, they go in different stacks.
Basic — build Cat vs Bat 25 min
The cat is Topic 1.7’s script with the costumes renamed. Everything new is on the bats.
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 — the cast
New project, Woods backdrop. Add Cat Flying and a Bat. Rename the cat’s two costumes Flying and Punching.
Step 2 — the cat
Build the controller you already know: four movement ifs, the if / else on space, rotation style left–right, facing set inside the left and right ifs.
Step 3 — the bat starts small and somewhere random
set size to (40) %, go to [random position v], and set rotation style [left-right v] so it never flies upside down.
Step 4 — the bat flies
Add the forever with the two pick-randoms in it. Watch one bat for a while.
Step 5 — the bounce stack
Add the second stack with if on edge, bounce in a forever. Now it never gets stuck.
Step 6 — getting knocked out
A third stack: a forever with if <touching [Cat v]?> then, and inside it if <key [space v] pressed?> then and hide.
Do not forget show on one of the flag stacks, or the second game starts empty.
Step 7 — duplicate
Test everything on one bat, then duplicate it five times. Six bats.
My bat freezes against the wall for a moment.
The bounce is inside the flying loop instead of on its own stack. Split it out with its own when flag clicked.
The bat spins upside down.
set rotation style [left-right v] is missing. A random direction includes all the upside-down ones.
Six bats, and only a punch counts
Finish the game.
- Six bats at 40% size, each starting somewhere random and moving randomly.
- A bat you fly through: nothing happens.
- A bat you punch: it disappears.
Teacher note
This is the deck’s Mission 1 and carries its 50 Adcoins. The interesting bug to watch for is a bat that flies away mid-punch: the flight stack is still running while the hit stack hides it, so on the next run it reappears somewhere unexpected. stop [other scripts in sprite v] after the hide is the tidy fix, and it is worth showing to anyone who notices.
No two bats the same
Right now all six bats are identical twins. Use pick random (1) to (10) to make each one its own creature — without writing six different scripts.
- A random size, between about 25% and 60%.
- A random speed — the number of steps it takes.
- A random pause before it sets off, so they do not all move in step.
Teacher note
The idea worth landing: one script, six different sprites, because the script asks for a random number rather than being told one. That is the first step towards Topic 1.10’s clones, where six copies stop being six sprites at all.
A game that is different every time
Make your own game — anything you like — where at least three things are decided by pick random (1) to (10) when the flag is clicked.
Play it three times. If you can predict the third round from the first two, add more randomness.
Done when: you play twice, write down three things that were different the second time, and none of them is a thing you changed by hand.Teacher note
Push for randomness that changes how the game plays, not just how it looks. A random backdrop is decoration; a random number of enemies, a random speed, a random starting position all change what the player has to do.
One of them is the fast one
Random is more fun when the player can read it. Make your bats’ speed match their size: the small ones are quick, the big ones are slow.
Then a player can look at the Stage and know which one to leave until last. That is the difference between random and unfair.
Summary 5 min
pick random (1) to (10)is an oval block. Oval blocks are values, and they go inside other blocks’ holes.- Every number in the range is equally likely, and negatives are allowed.
point in direction (pick random (-180) to (180))aims a sprite anywhere at all.- A glide always takes the same time, so its speed changes. Pointing and moving keeps a steady speed.
if on edge, bouncebelongs on its own stack when the movement loop is long.
- Random
- Picked by chance, so you cannot know it in advance.
- Range
- The two numbers a pick random chooses between, and everything in between.
- Reporter
- An oval block. It is not an instruction — it is a value you drop into a hole.
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.