Scratch exercise 91

Fire at closest

This exercise shows how an advanced algorithm can be constructed to allow a turret gun to select, point and fire at the closest of 3 moving targets.

Before attempting this exercises, you should already be good at using variables, sensing blocks and operators in your own scripts.

 

1. Create a new game and delete the cat sprite.

 

2. Paint the stage black and add these scripts to it ..

 

3. Paint a simple red bullet / shell sprite ..

Re-name the sprite "Shell".

 

4. Now we need to create 3 target sprites for our turret gun to fire at. Paint a simple square with two costumes, one red. Name the first costume "moving" and the second costume "hit" ..

Re-name the sprite "Target 1".

 

4. We are going to make "Target 1" move randomly around the stage. When it is hit by a Shell from the Turret Gun it will turn red for a moment, then disappear and re-spawn. Add these scripts to it ..

NOTE: It is important that all 3 target sprites are not to large, so adjust the "set size to" value until they are a similar size to those shown here ..

 

5. Save your game. Name it "Fire at closest v1".

 

6. Duplicate "Target 1", re-colour it's "moving" costume to something different (not red), and re-name it "Target 2". Look at the scripts and note that both "Target 1" and "Target 2" spawn from near the top of the stage.

 

7. Duplicate "Target 1" again and re-name it "Target 3". This time change the colour of its "moving" costume and edit it's "go to" scripts so it spawns from the bottom of the stage.

 

8. Test your game to ensure the 3 targets move randomly around the stage.

 

9. Re-save your game.

 

10. Paint a simple turret gun sprite in a neutral colour ..

Re-name it "Turret" and add these scripts to it ..

Adjust the "set size to" value until the turret is a similar size to the one shown here ..

 

11. Create the following variables (for all sprites)..

Take time to understand what these variables will be used for ..

All of these variable will constantly update (approximately 25 times per second). Leave them visible on the screen for the time being.

 

12. Create this new list ..

On the stage click the "+" button on the list 3 times to create 3 items ..

Each of these 3 item will be used to hold one of the distances in pixels to one of the targets. So item 1 will hold the distance to "Target 1", item 2 will hold the distance to "Target 2", and item 3 will hold the distance to "Target 3".

 

13. Select the "Turret" and add these scripts to it ..

This script will ensure that the list is always updated with the distances from the turret to the 3 targets. Test it now and you will see there is a problem. The values are too accurate. We need to round them to whole numbers. Edit your script to round the numbers to the nearest whole number ..

Test your script to see the values are now whole numbers.

 

14. Re-save your game.

 

15. We now need to start to construct the algorithm which will check the list and find which item contains the smallest number (the closest target). Add this script ..

Note the following ..

Now we have the counter working we need to add the script blocks which will check each item in the list to find which has the smallest number (closest distance).

Create this "if" script block ..

This script works by repeatedly checking to see if the value of each item in the list is smaller than the current value of the "distance to closest target" variable. Remember that "distance to closest target" is initially set to "300" so item 1 in the list is bound to be smaller.

Therefore the value in "distance to closest target" is updated with the value in list item 1. Also, "closest target number" is updated to the current "counter" value which will be 1. The algorithm now knows that "Target 1" is the closest target to the "Turret".

The repeat loop does this 2 more times with the "counter" value at 2 and then at 3.

Add the "if" block to the repeat loop ..

 

16. Re-save your game.

 

17. When the repeat loop has cycled 3 times ("repeat 3") the algorithm knows which target is closest and moves onto the next part of the script which points the Turret at the closest target.

We don't want the Turret to fire until at least one of the targets is close (in this case we will make the minimum distance 160 pixels). Create this script block ..

Note that if all the targets are further away than 160 pixels, the Turret just rotates left and right randomly to give the impression it is "watching" the targets. Put this script block under the Repeat loop block but within the forever block ..

Test your game. The Turret should point at the closest target, but it won't shoot yet.

 

18. Re-save your game.

 

19. Our final step is to add the scripts to the Shell. You may need to adjust the "set size to" value ..

 

20. You're done! Re-save your game again then and test it.

 

21. Can you develop this exercise into a complete game?