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.

 

1. Open Scratch to create a new game, or if Scratch is already open use File>New.

 

 

2. Re-name the cat sprite "Cat" the click the button to make sure he only faces left and right ..

 

 

3. Add these scripts to the Cat sprite to make it smaller, and so that you can easily end your game by pressing the "E" key when you want to work on it some more ..

 

 

4. Let's create our first simple script. This script 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 "E" to end the game when you're finished.

 

 

5. Use "File>Save as" to save your game. Name it "Follow the mouse".

 

 

6. Let's add the next script. This time we'll change the script a little to make 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 "E" to end the game when you're finished.

 

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 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 ..

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 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 ..

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 "E" to end the game when you're finished.

 

 

9. Use "File>Save" to re-save your game.

 

 

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