- Learnt27 min · create clone, when I start as a clone, delete this clone
- Basic25 min · rebuild Cat vs Bat with clones
- Challenge 1–327 min · clone them, randomise the count, start the Shark Game
- Extrabonus · they keep coming
Review — where we got to 5 min
Last lesson you made things random.
pick random (1) to (10)is an oval block. Ovals go in the holes of other blocks.- Pointing and moving keeps a steady speed. Gliding does not.
- When the movement loop is long,
if on edge, bouncegets its own stack.
Quick-fire
You have six bats. You find a bug in the flying script. How many times do you fix it?
Reveal the answer
Six. Every duplicate has its own copy of the script. Today we learn a better way.
Today’s Topic 3 min
- Creating clones
- What really happens when a clone is made
- Choosing how many there are
Learning outcome
By the end of this lesson you will be able to:
- Make many copies of a sprite from one script.
- Say what the real sprite does and what its clones do.
- Change the number of copies by changing one number.
Learnt — one sprite, many copies 27 min
Three new blocks
All three are in Control, at the bottom. A clone is a copy of a sprite, made while the game runs.
| Block | What it does |
|---|---|
create clone of [myself v] | Makes one copy straight away. The copy is in the same place and looks the same. |
when I start as a clone | A hat block. Every new copy runs the blocks under it. |
delete this clone | The copy is gone. It is not hidden. It is gone. |
The real one and the copies
This is the big idea. Read it slowly.
The sprite in your Sprite panel is the real one. It runs the when flag clicked stacks. Its job today is to hide and make copies. That is all it does.
The clones run the when I start as a clone stacks. They do all the flying and sensing. They are the ones that get caught.
when flag clicked
hide
repeat (8)
create clone of [myself v]
end
when I start as a clone
show
go to [random position v]
forever
point in direction (pick random (-180) to (180))
repeat (pick random (10) to (20))
move (4) 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.
Every clone gets its own everything
Each copy has its own position, direction, costume and size. They all run the same script. But the script asks for random numbers. So no two copies do the same thing.
Duplicating cannot do this so well. Six duplicated sprites are six things to keep the same. Six clones are one script.
How many? Change one number.
The number is in the repeat (8). Do you want twelve, not eight? Change the 8. Do you want a surprise? Put a pick random in it.
when flag clicked
hide
repeat (pick random (5) to (14))
create clone of [myself 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.
delete this clone, not hide
When a clone is caught, use delete this clone, not hide.
- A hidden clone is still there. It still runs its scripts and makes the computer work.
- A deleted clone is finished. The Stage really is empty.
There is a bonus too. You never need a show at the top again. Clones are made new on every green flag. Nothing is left over from last time.
Basic — Cat vs Bat, with clones 25 min
Open your Topic 1.9 project. You will delete five sprites, and you will not lose anything.
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 — delete the duplicates
Right-click and delete five of your six bats. Keep one. It feels wrong, but it is right.
Step 2 — change the hats
On the bat that is left, change every when flag clicked to when I start as a clone.
Step 3 — the maker stack
Add one new stack: when flag clicked, hide, then repeat (6) with this inside: create clone of [myself v].
Step 4 — test
Press the flag. You see six bats, all flying and bouncing. There is one sprite in the panel.
Step 5 — delete instead of hide
In the getting-caught stack, replace hide with delete this clone. Then take out the show. Clones do not need it.
Step 6 — change the number
Change the 6 to a 20 and press the flag. You get twenty bats, but you did not add a sprite. Then change it back to a number you can play with.
There is one extra bat that never moves.
That is the real sprite. Its flag stack is missing the hide.
Nothing appears at all.
Your flying script is still on a when flag clicked hat. So the clones have nothing to run. Every stack the clones need starts with when I start as a clone.
Clone your game
Pick a game you built with duplicated sprites, like the viruses or the bats. Build it again with clones.
The game should play the same. The Sprite panel should look very different.
Done when: your game plays as it did before. Your Sprite panel has one enemy sprite, not six. Changing one number changes how many there are.Nobody knows how many
Make the number of enemies random, between about 8 and 20.
Then make each clone different from the others too. Give each one a random size and a random speed. It is the same script, but every clone is different.
Done when: three games in a row have three different numbers of enemies. No two enemies in a game look the same. It is all one script.Teacher note
The randomness has to be inside the when I start as a clone stack, not the maker stack. Put it in the maker and every clone gets the same “random” value, because it was picked once before any of them existed. That is a genuinely good bug to have to think about.
Start the Shark Game
This is a new game. You will finish it in the next two lessons. Your teacher will give you the shark and the sea. The fish come from the Scratch library.
- Shark at 60%, fish at 30%, and the backdrop full screen.
- Arrow keys drive the shark. It starts in the middle.
- Space pressed: shark-bite. Not pressed: shark-swim.
- The left arrow turns it left. The right arrow turns it right.
- 12 to 20 fish. They appear in random places and move at random, at a steady speed.
- A fish disappears only when it meets an open mouth.
- At least four kinds of fish, with about the same number of each.
- Fish bounce off the edges.
Teacher note
This is the deck’s Mission 1, and it is deliberately most of a game. Everything in it has already been taught — this lesson’s clones, Topic 1.9’s random flight, Topic 1.7’s controller. Nobody should need a new block.
Four kinds of fish means four sprites, each cloning three to five times. That is the honest answer at this stage, and it is worth pointing out that the four scripts are identical except for the costume.
They keep coming
So far all your clones are made at the start. Now make them arrive while you play. Make a new one every few seconds, for as long as the game runs.
Now catching them all is not the end of the game. The game is about staying alive.
Summary 5 min
create clone of [myself v]makes a copy. The copy runs the blocks underwhen I start as a clone.- The real sprite hides and makes copies. The clones do everything else.
- Every clone has its own position, size and direction. One script makes many different clones.
- The number of clones is one number in a
repeat (8). Put a pick random in it, and no two games are the same. delete this clone, nothide. A deleted clone is finished. A hidden one is still working.
- Clone
- A copy of a sprite made while the project is running.
- The real sprite
- The one in the Sprite panel. It makes the clones. It usually hides.
- Spawning
- Making new things appear in a game while you play.
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.