Scratch exercise tutorial 140

Distance to exercise - advanced

In this tutorial you will learn how to use the "distance-to" sensing script block to control sprite 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 advanced" 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. Edit the script you have just made so that rather than moving at a constant 10 steps, the "Me" sprite moves at a speed relative to its distance to the mouse-pointer. Remember that the "Distance-to" value will be constantly changing, greater (faster) when it is further away, smaller (slower) when it is closer ..

Note how we are not simplifying using "move distance to mouse-pointer" because this would be too fast. Instead we are dividing the "distance to mouse-pointer" number by 12 to create a more usable range of speeds. You can experiment with different values such as 8, 12, 18, 20 etc



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



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



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




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



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

Notice how this time in the move block we are not only using "distance to Me / 22" but are also adding 2 steps to ensure the Enemy sprite never completely slows down. It will always move at least 4 steps.

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



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



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



16. It might be fun to have several enemy sprites some more dangerous than others. Let's create a second sprite which is a little less keen to chase the Me sprite.

Duplicate the Enemy 1 sprite and re-name it "Enemy 2". Perhaps change its colour so you can easily identify it on the stage.


Now edit the values in Enemy 2's "distance to" and "move ? steps" script blocks. You can try the values below or experiment with your own ..



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



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