Scratch exercise 004

Follow the mouse pointer

In this exercise you will try out 4 ways to make the Cat sprite follow the mouse pointer. We will start with a simple method and then try out more advanced ones.

 

NOTE: You can use any version of Scratch.

 

Complete these steps ..

 

1. Open Scratch, OR create a new game (from the top menu choose "File>New").

 

 

2. Re-name the cat sprite "Cat" ..

 

Click the "only face left right" button ..

If you are using Scratch desktop add this code ..

 

 

3. Add these scripts/code to the Cat sprite to make it smaller, and so that you can easily end your game by pressing the "E" key ..

 

Press the "B" key on your keyboard to shrink the cat.

 

 

4. This following script/code makes the Cat sprite stick to the mouse-pointer. Add it to the Cat ..

 

Note: Because the Cat sprite has two costumes, the "next costume" script block makes the Cat look like it's walking.

 

Press the "1" key and then move your mouse-pointer on the stage to try it out.

Press the "E" to end the game when you're ready to continue working on it.

 

 

5. Use the File menu to save your game. Name it "Follow the mouse v1".

 

 

6. Let's add the next script/code. This time we'll create a better script/code which makes the Cat move (walk) towards the mouse pointer ..

 

Press the "2" key and then move your mouse-pointer on the stage to try it out.

Press the "E" to end the game when you're ready to continue working on it.

 

NOTE: If you are a beginner you DO NOT have to complete the next steps of this tutorial, but you can if you like.

 

 

7. Our "2" script/code works better than the "1" script because the Cat chases after the mouse-pointer when we move the mouse-pointer quickly. However, the Cat sprite glitches when it catches up with it. Let's fix this by making the Cat sprite follow the mouse-pointer but only when it is more than 30 pixels away.

Add this script/code ..

 

Press the "3" key and then move your mouse-pointer on the stage to try it out. Press "E" to end the game when you're finished.

 

 

8. In our final script/code we are going to make the Cat sprite walk faster if it is far away from the mouse-pointer and slower when it gets close.

Add this script/code ..

Notice how the number of steps moved depends on how far the Cat sprite is from the mouse-pointer ("distance to mouse-pointer"). The further away it is the faster it walks.

Also notice how we have divided "distance to mouse-pointer" by 10 to make sure the Cat sprite doesn't move too fast. If the Cat sprite is 100 pixels away it will move by 10 steps (100 divided by 10 = 10).

 

Press the "4" key and then move your mouse-pointer on the stage to try it out.

Press the "E" to end the game when you're ready to continue working on it.

 

 

9. Use the File menu to save your game.

 

 

10. Well done. You've finished. Can you use these script/code in a game you are making?