Scratch exercise 148

Turret battle

In this tutorial you will complete an exercise in which two turrets battle to see which can hit a moving target the most. When you have completed it you can try and develop it into a game by adding player controlled sprites.

 

 

Complete these steps ..

 

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

 

 

2. Right-click on the Cat sprite on or under the stage to delete it.

 

 

3. Select the stage, paint it black and then add these scripts ..

 

 

4. Create the following three variables and then arrange them at the top of the stage ..

 

 

5. Now add the following script to the stage to display the scores for each turret and to show which is winning ..

 

If you prefer you can hide the "T1 score" and "T2 score" displays and just leave the "Winning" display ..

 

 

6. Use "File>Save as" to name and save your game. Name it "Turret battle" ..

 

 

7. Now use the "Choose new sprite from file" button under the stage to import these sprites ..

 

 

8. Select the turret 1 sprite ("T1") and add the scripts to resize and locate it on the stage, and to make it always point towards the Target sprite ..

 

Press the "B" key on your keyboard to test your game.

When you are ready press the "E" key to end your game.

 

 

9. Copy these scripts to the T2 sprite and just change the "go to x: y:" script values to locate it towards the top right of the stage ..

 

Press the "B" key on your keyboard to test your game.

When you are ready press the "E" key to end your game.

 

 

10. The "T1 shell" and the "T2 shell" will fire automatically at the Target sprite.

Add these scripts to the "T1 shell" ..

 

Press the "B" key on your keyboard to test your game.

When you are ready press the "E" key to end your game.

 

 

11. Copy the "T1 shell"scripts to "T2 shell" and then edit them to ensure "T2 shell" goes to "T1" before firing and the score scripts are correct ..

 

Press the "B" key on your keyboard to test your game.

When you are ready press the "E" key to end your game.

 

 

12. Use "File>Save" to re-save it ..

 

 

13. Next select the "Target" sprite.

This sprite is going to move constantly across the stage. Add the first script to set its starting properties ..

 

Notice how it is going to set off in a random direction.

 

 

14. It will be great if when the Target sprite reaches the edge of the stage it reappears on the opposite edge. Add these scripts ..

 

Press the "B" key on your keyboard to test your game.

When you are ready press the "E" key to end your game.

 

 

15. Let's make the movement of the Target sprite a bit more interesting. Let's make it turn away when it gets close to a turret.

Add this script ..

 

Press the "B" key on your keyboard to test your game.

When you are ready press the "E" key to end your game.

 

 

16. Use "File>Save" to re-save the game ..

 

 

17. Well done, you've finished this exercise. Now it's time to add your own player controlled sprites and develop this exercise into your own complete game. There are scripts here which will help you.