Scratch exercise tutorial 140

Distance to exercise - intermediate

In this tutorial you will learn how to use the "distance-to" sensing script block to control a sprite's speed and create AI effects.

 

Complete these steps ..

 

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

 

 

2. Select the cat sprite under the stage, and then right-click to delete it.

 

 

3. You need to paint the stage black. Select the stage and switch to the Backgrounds tab ..

 

Click on the "Edit" button and then use the paint bucket tool to fill your stage with black ..

Click "OK" when you are done.

 

 

4. Select the stage script tab and add these scripts to begin and end your game ..

 

 

5. Now, you need to create a sprite which you will control with the mouse-pointer.

Click the "paint new sprite" button under the stage ..

 

Select any colour and then choose the paint brush tool's 4th largest size (bottom row far left) ..

Click once in the middle of the paint window to paint a single circle ..

 

Click "OK" when you are done and then re-name your new sprite "Me".

 

 

6. Use "File>Save as" to name and save your game. Name it "distance-to intermediate" for now ..

 

 

7. Add the following script to the Me sprite ..

This script ensures that the sprite never completely catches up with the mouse-pointer, always staying at least 20 pixels away.

 

Press the "B" key on your keyboard to begin your game and then move your mouse-pointer around the stage. Notice how the sprite follows the mouse-pointer at a constant pace. Let's make this more dynamic.

Press the "E" key to end your game.

 

 

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

 

 

9. Let's add some enemy sprites which emerge from their "home" and chase the Me sprite when it is close.

First, create a "home" sprite by clicking the "paint new sprite" button under the stage and painting a simple coloured square ..

 

Re-name it "Enemy home" and then add this script to it ..

 

 

10. Let's create an Enemy sprite which will emerge from the Home sprite and chase the Me sprite when the Me sprite gets close to it.

Click the "paint new sprite" button under the stage and create a simple coloured square with a yellow border along its right hand edge ..

Click OK when you are done and re-name this sprite "Enemy 1" ..

 

 

11. Let's add a simple script to make the Enemy sprite always point towards the Me sprite. Add this script and then test it ..

 

 

12. Let's add some script blocks to make this Enemy sprite chase the Me sprite when the Me sprite gets close.

Add an "If else" script block beneath the existing "point towards Me" script block, and then add the blocks to make the Enemy sprite follow the Me sprite ..

Press "B" to test this script and then "E" when you are ready to add the final script blocks.

 

 

13. It would be great if the Enemy sprite not only gives up the chase when it is more than 120 pixels from the Me sprite, but also returns home. Let's add the scripts to make this happen.

Add the final script blocks to the "else" part of the "If else" block ..

"Repeat until" is an excellent way to make some script blocks run until a condition is met. In this case the condition is "touching Enemy home".

 

 

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

 

 

15. Well done, you've finished this exercise. Now, turn it into a complete game. You can do mods such as ..