Scratch Game
Normal
60 mins
470 points
What you need:
  • Computer/laptop

Attack of the Dots

In this project we create a game where you control a coloured disc and you must spin it to match the coloroured dots that are attacking!

This message will be visible to everyone. Send

    This code along has finished! ⌚

    You can watch it back by playing the video!

    The code along is open for everyone to join in but if you would like to earn the points and badges please join our club!

    1 - Remix the starter project

    We've created a starter project that has some custom sprites for this game.

    Go to the starter project at the below link and click on the Remix button to create a copy of the project.

    https://scratch.mit.edu/projects/281652790/editor/


    2 - Make the disc spin

    In this game coloured balls will move towards the disc in the middle. You need to spin the wheel to match the colour of each ball as it hits the wheel. So we'll program the left and right arrow keys on our keyboard to spin the wheel in each direction.

    Add the following code to the 'wheel' sprite:

    when green flag clicked go to x[0] y [0] // place it in the center set size to [30]% // resize the wheel to 30% forever if < key (left arrow v) pressed? > then turn ccw (3) degrees // turn anti clockwise end if < key (right arrow v) pressed? > then turn cw (3) degrees // turn clockwise end end

    Once you've added the code, click the green flag and test if your arrow keys spin the ball!


    3 - Clone the ball

    In the game, the balls should appear every few seconds, in a random position and start moving towards the wheel.

    Add the following code to the green ball called 'Sprite1':

    when green flag clicked hide forever wait (pick random (4) to (8)) seconds // wait a random number of seconds create clone of (myself v) // create the clone end when I start as a clone go to (random position v) // start in a random position point towards (wheel v) // point in the direction of the wheel show


    4 - Not too close!

    We don't want the balls appearing too close to the wheel as it won't give us much time to react. So to prevent this add the following code to 'Sprite1' which detects where the ball is when it's cloned and deletes it if it's too close.

    when I start as a clone go to (random position v) point towards (wheel v) show // add new code under here if < ( distance to (wheel v)) < (100) > then delete this clone end


    5 - Make them move

    Now that they're appearing randomly every few seconds we need to make them start moving towards the wheel. We've already used a block to point them at the wheel so now we just need to add this code to 'Sprite1':

    when I start as a clone go to (random position v) point towards (wheel v) show if < ( distance to (wheel v)) < (100) > then delete this clone end forever // add this code move (2) steps end


    6 - Detect the colour

    You'll notice that the balls are just moving through the wheel. Next we will add code to detect what colour of the wheel they first touch and play different sounds depending on whether they match or not.

    Add the following new code to 'Sprite1'. Pay close attention to where this code is being added, it needs to go inside the forever block, but above the move 2 steps block.

    when I start as a clone go to (random position v) point towards (wheel v) show if < ( distance to (wheel v)) < (100) > then delete this clone end forever if < touching (wheel v) > then move (1) steps // this makes sure it touches the wheel if < touching color (#21610B) > then // select the green of the wheel start sound (pop v) delete this clone else start sound (Zoop v) // add this sound from the sounds library delete this clone end end move (2) steps // add new code above here end


    7 - Create purple and orange balls

    The starter project comes with one green ball called 'Sprite1' already added. Now that we've added all the code to this sprite, we can duplicate it to create the purple and orange balls and all the code is copied too.

    Right click on 'Sprite1' in the sprite list and click on the 'duplicate' option. Do this twice so that you've got three ball sprites.

    Next use the costume editor to change the colour of the balls to purple and orange. Use the eye dropper tool on the wheel sprite to select the exact purple and orange colours.



    8 - Change the code for the purple and orange balls

    Lastly we need to change the touching color   ? blocks in 'Sprite2' and 'Sprite3' to the following so that they will work properly.

    Change this block in the purple dot sprite (Sprite2).

    < touching color (#5F04B4) > // select the purple of the wheel

    Change this block in the orange dot sprite (Sprite3).

    < touching color (#DF7401) > // select the orange of the wheel

    Once you've updated these blocks click on the green flag and play the game.


    9 - Extra challenges

    That's it you're done, you've completed all the steps. Have fun playing the game!

    Have you any ideas on how to improve the game? How about trying to add in code to do one of these:

    • Create a 'score' variable and add to it each time you match a colour, subtract from it each time you get a colour wrong.
    • Create a 'lives' variable and set it to 5 at the beginning. Lose a life each time you get a colour wrong.
    • Make the balls move faster my changing the move 2 steps block.

    Join our club 😃

    Create your profile, earn points & badges and learn how to code!

    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
    🍪 Our website uses cookies to make your browsing experience better. By using our website you agree to our use of cookies. Learn more