Red v Green v Blue

ScratchEasy

In this project a hundred dots, consisting of red, green and blue, will randomly appear and float around the screen. Red dots infect green, green dots infect blue and blue dots infect red. They battle it out until one colour wins out!

1

Create a new Scratch project

Create a new Scratch project and delete the cat sprite

2

Create a red dot sprite

Create a new sprite by clicking on the paintbrush, this will open up the sprite editor. To draw draw a red circle, follow these steps:

  1. Choose a red colour in the Fill tool
  2. Turn off the Outline tool
  3. Select the Circle tool
  4. Draw a small circle in the middle of the editor (hold down the shift key while you draw it to make it a perfect circle)
  5. Name this costume 'red'

3

Create the green and blue costumes

We now need to create two more costumes for this sprite, a green circle and a blue circle.

Duplicate the red costume twice and then use the paint bucket tool to change the colour to blue and green.

Don't forget to name the costumes 'blue' and 'green'!


4

Create a 'count' variable

Now that we've created our sprite, let's start coding!

We're going to code 100 dots to appear so we need a variable to let us count to 100. Using the Variables toolbox create a 'count' variable.

Add the following code to your sprite to start 'count' at 0.

when green flag clicked set [count v] to [0]

5

Create 100 clones of the dot

Next we'll code the dot sprite to be cloned 100 times. We'll use a repeat until block and also the 'count' variable to do this. Each time we clone the sprite we'll add 1 to the 'count' variable. When the 'count' variable is greater than 99 then we'll have created 100 clones.

Add the following code underneath the code from the previous step:

when green flag clicked set [count v] to [0] // insert the new code below this hide repeat until <(count) > [99]> wait [0.1] secs create clone of (myself v) change [count v] by [1]

6

Appear randomly

We hid the original sprite and created 100 clones of it in the previous step. Now for each of those clones we will:

  1. Choose a random costume (red, green or blue)
  2. Go to a random position
  3. Appear on the screen

Add the following code to the sprite:

when I start as a clone repeat (pick random (1) to (3)) // there are only 3 costumes next costume end go to (random position v) show

Click on the green flag once you've added this code, 100 dots should randomly appear on the screen in random positions.


7

Make them move

Next we will make the dots point in a random direction and then start moving, if they touch the edge of the screen they will bounce.

Add the following code underneath the show block from the previous step:

when I start as a clone repeat (pick random (1) to (3)) // there are only 3 costumes next costume end go to (random position v) show // insert the new code below this point in direction (pick random (0) to (359)) forever move (3) steps if on edge, bounce


8

Make them infect each other!

For the last step we will code the dots to sense when they touch another dot and to infect them depending on the colour.

Red infects green (so green changes to red)
Green infects blue (so blue changes to green)
Blue infects red (so red changes to blue)

Add the following code underneath the if on edge, bounce block, pay careful attention to how the if then blocks are 'nested' inside each other.

when I start as a clone repeat (pick random (1) to (3)) // there are only 3 costumes next costume end go to (random position v) show point in direction (pick random (0) to (359)) forever move (3) steps if on edge, bounce // insert the new code below this if < color [#ff6666] is touching [#4d86c1] ?> then // use the eye dropper tool to select the colours switch costume to (blue v) else if < color [#49b655] is touching [#ff6666] ?> then switch costume to (red v) else if < color [#4d86c1] is touching [#49b655] ?> then switch costume to (green v) end end end end

Once you've added this code click the green flag to test that it works. The 100 dots should appear and move around infecting each other until one colour wins!!


9

Improve the project!

Have you any ideas on how you could improve the project?

End of lesson
Copyright Notice
This lesson is copyright of Coding Ireland. Unauthorised use, copying or distribution is not allowed.

Scratch is developed by the Lifelong Kindergarten Group at the MIT Media Lab. It is available for free at https://scratch.mit.edu