Alex & Jordan in Computer Science

Alex - Alex & Jordan in Computer Science

Scratch Tutorial – Using Clones in Scratch Games

& Linear Search

The tutorial below looks at how to create clones in Scratch and detect whether they have collided with each other using a linear search algorithm.

This tutorial is a step by step guide for students learning independently. A lesson pack for teachers is available here.

To follow more tutorials from my Alex & Jordan in Computer Science series, please click here.

Creating a CloneMoving and Tracking ClonesChecking if the Clones Collide

Creating a Clone of a Sprite

A clone of a sprite is a copy of a sprite that you’ve created. Extra code can be added to give the clones code that runs when they’re created.

This tutorial shows how to create an example of simple clones that move around the screen and recognise when they collide with each other.

In more complex games like the Bug Spawner Game, you may need to have clones of two different sprites looking out for each other. But let’s start slowly!

Start by creating a new sprite that is a simple square on the stage.

when green flag clicked
set [clone id v] to [1]
repeat (6)
    create clone of [myself v]
    change [clone id v] by (1)

In the code of the square, create a starting block for when the green flag is clicked.

We hide the original square sprite because it’s the spawns that we want to add the “real” code to.

Before we can create this code, we need to create a “local” variable called clone id. We do this by checking the “For this sprite only” option when we create the new variable.

We start by setting the value of clone id to one because we want it to start counting once we start creating our clones.

By adding a repeat block we can decide how many squares we want to add to the stage. I’ve used 6 here, but you can choose any number (I’d recommend less than 10 though!).

We’re going to create 6 clones of the square and each time increase the clone id variable by one – this will give each clone it’s own number and let us check whether that clone has collided with something.

Moving The Clones

Once a clone is created in our loop, we want it to move around the stage and bounce off the edges.

when I start as a clone
go to [random position v]
point in direction (pick random (1) to (360))
    move (4) steps
    if on edge, bounce

The “When I start as a clone” starter block will only run if a new clone is created.

Because we hid the original square sprite, we need to make sure that the clone is visible when it starts.

To make the random movement easier, we can send each new clone to a random position, then point in a random direction so that when it starts moving, they’re all bouncing in different random directions.

There’s no real end to our simulation (apart from clicking stop), so a forever loop lets the movement & bouncing off the walls just go on forever.

At this point you will be able to run your code and see your clones squares bouncing around the screen.

Tracking The Clones

Before we can start detecting whether they’ve collided, we need to store some important information about each clone: the x and y coordinates.

Because we have more than one clone, we need to track each one. We can do this with a list.

In the Variables menu, create a new “global” list called “square x”. We make this global by selecting “For all sprites” when the list is created.

Repeat this, and create another list called “square y”.

When you’ve created the lists, make sure that you have the visible on the screen checkbox selected.

When you can see the lists on your screen, click the + icon at the bottom to add six items to each list. Each item will match a clone, so if you have chosen a different number of clones, the number of items in your list must match the number of clones you chose to create.

The final part for tracking is to add two extra blocks of code into the clone’s forever loop which saves the clone’s x and y coordinates into it’s place in the list (clone 1 will store it’s x & y coordinates in place 1, clone 2 will store it’s x & y coordinates in place 2… etc.).

We use the replace block because we want to constantly track each clone’s position as it moves.

The round block makes sure that we’re storing whole numbers so to make checking the position easier.

If you keep your lists displayed on the stage and run your code, you should see the numbers in the lists constantly updating as the circles move around the stage.

If everything is working, uncheck the boxes next to each list so that your lists are hidden on the stage (this makes the shapes easier to see and you can always make them visible again if you want to test the code).

Note: if the numbers don’t update when you run the code, you may not have set the clone id to “for this sprite only” (local). The easiest way to fix this at this point is to delete the variable and recreate it making sure it is local.

Checking if the Clones Collide

The collision block uses some big IF statements, so we’re going to break down building this section into smaller stages!

when I start as a clone
    set [color v] effect to (0)
    set [check for square v] to [1]
    repeat (length of [square x v])

Add another starting “When I start as a clone” block. You may want to put this underneath your other code blocks as some of the lines end up quite long!

We want this to be checking for collisions all the time that the program runs, so a forever loop will go around all of the code.

Setting the colour effect to 0 keeps the square at the original colour we designed (remember “color” because Scratch is built in the US).

We’re going to check all of the cloned squares in our list, so we need to create a new variable called “check for square”. This will start at 1, then count through each clone’s id number.

Then add a repeat block which will loop as many times as the items in your lists. Because the square x & square y lists are the same size, it doesn’t matter which one you add here.

when I start as a clone
    set [color v] effect to (0)
    set [check for square v] to [1]
    repeat (length of [square x v])
    if <not <(clone id) = (item (check for square) of [square x v])>> then


<div class=

Inside the repeat block, we’re going to add an IF block.

This checks to see if the clone we’re looking at is the same as the one we’re testing for collision with (we don’t need to check if it collides with itself!).

So this will only run if the block to check isn’t the current clone.

Inside the IF block we just placed, we’re going to add the first of our really long IF conditions. This will check if the x coordinate of the current clone is in the same range (about 20 x 20 pixels in size) as the x coordinates in the list of the clone to check.

In the example below, the lower square clone would be a collision as its x co-ordinate is 115 which is between the 105 plus 20 (for the width).

Checking for x coordinate collisions

Inside this long IF block is another which is very similar, but checks for the Y coordinates of each clone. If we only had the first long IF block, any time the square clones were at the same point left to right, a collision would be detected (even if they were at the top & bottom of the stage).

So if the IF block for the IF finds a collision, we need to go on and check the y coordinate too.

when I start as a clone
    set [color v] effect to (0)
    set [check for square v] to [1]
    repeat (length of [square x v])
    if <not <(clone id) = (item (check for square) of [square x v])>> then
    if <<(x position) < ((item (check for square) of [square x v]) + (20))> and <(x position) > ((item (check for square) of [square x v]) – (20))>> then
	if <<(y position) < ((item (check for square) of [square y v]) + (20))> and <(y position) > ((item (check for square) of [square y v]) – (20))>> then


<p>Finally, we need to decide what to do if two clones collide. For this experiment, we’re going to change the colour, but you could make the clone disappear by deleting the clone, playing a sound, or updating a score…</p>

<p><img decoding=

Single clone collision program

What did you do with your clone program? Let me know below!

Did you find this helpful?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this.

I'm sorry that this wasn't very useful for you!

Help me to improve this!

Tell me how I can improve this?