- 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 made sprites that move on their own.
- An NPC checks the same key as the player. Then they both agree on what is happening.
- Finish one sprite first. Then duplicate it.
- A glide makes its stack wait. So sensing needs a second stack.
Quick-fire
Where does this sprite end up?
when flag clicked
go to [random position v]
Reveal the answer
Nobody knows! It goes somewhere new every time you click. Today you 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 move in random ways at a steady 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) is in Operators. Its shape is new to you. It is an oval.
Oval blocks are not instructions. They are values. A value is a number, like 7. You drop ovals 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. The sprite goes a different distance each time.
pick random (1) to (10) gives you any whole number from 1 to 10. Each number has the same chance. The two numbers are the range. Change them and you change the range. pick random (-10) to (10) can give a number below zero too.
Pointing anywhere
Directions go from −180 to 180. So point in direction (pick random (-180) to (180)) points a sprite any way at all.
Flying at a steady speed
In Topic 1.8, NPCs moved with glide (3) secs to [random position v]. It works, but it looks odd. Here is why.
A glide always takes the time you give it. A long way in 3 seconds is fast. A short way in 3 seconds is slow. So the sprite keeps changing speed.
Pointing and moving does not do that. 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) steps long. Say you put if on edge, bounce after it. The bat would get stuck on the wall until the twenty steps ran out.
So the bounce gets its own stack. It checks again and again, very fast:
when flag clicked
forever
if on edge, bounce
end
This is parallel scripting again: two stacks running at the same time. The rule is always the same. If one thing is slow and one must be quick, put them in different stacks.
Basic — build Cat vs Bat 25 min
The cat uses the script from Topic 1.7, with new costume names. 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
Start a new project with the Woods backdrop. Add Cat Flying and a Bat. Rename the cat’s two costumes Flying and Punching.
Step 2 — the cat
Build the cat’s controls, the same as before. Use four movement ifs and an if / else for space. Set the rotation style to left–right. Set the facing 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]. The last one stops it flying upside down.
Step 4 — the bat flies
Add the forever loop 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
Make a third stack: a forever with if <touching [Cat v]?> then. Inside it go if <key [space v] pressed?> then and hide.
Put show on one of the flag stacks. If you forget, the next game starts with no bats.
Step 7 — duplicate
Test everything on one bat. Then duplicate it five times, so you have six bats.
My bat gets stuck on the wall for a moment.
The bounce is inside the flying loop. Move it to a new stack with its own when flag clicked.
The bat spins upside down.
set rotation style [left-right v] is missing. A random direction can be an upside-down one.
Six bats, and only a punch counts
Finish the game.
- Six bats at 40% size. Each one starts in a random place and moves at random.
- A bat you fly through: nothing happens.
- A bat you punch: it disappears.
Teacher note
This is the deck’s Mission 1. 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 the same. Use pick random (1) to (10) to make each one different. You should not need six different scripts.
- A random size, between about 25% and 60%.
- A random speed. This is the number of steps it moves.
- A random wait before it starts, so they do not all move together.
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 about anything you like. In it, at least three things are picked by pick random (1) to (10) when the flag is clicked.
Play it three times. Can you guess the third game from the first two? Then add more random things.
Done when: you play twice and write down three things that were different the second time. You did not change any of them 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 each bat’s speed match its size. Small bats are quick. Big bats are slow.
Then a player can look at the Stage and know which bat to leave until last. That makes random feel fair.
Summary 5 min
pick random (1) to (10)is an oval block. Oval blocks are values. They go in the holes of other blocks.- Every number in the range has the same chance. Numbers below zero work too.
point in direction (pick random (-180) to (180))points a sprite any way at all.- A glide always takes the same time, so its speed changes. Pointing and moving keeps a steady speed.
- When the movement loop is long, put
if on edge, bounceon its own stack.
- Random
- Picked by chance. You cannot know it before it happens.
- Range
- The two numbers that pick random chooses between, and all the numbers in the middle.
- 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 to get your Adcoins.