Scratch exercise 175b

React when hit - advanced

In this tutorial you will complete an exercise in which the Cat fires at enemies which are chasing him. Each enemy reacts in a different way when hit.

 

NOTE: You need to use Scratch Desktop (version 3 of Scratch) or higher.

 

Complete these steps ..

 

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

 

 

2. With the stage still selected, switch to the Code tab and add the following code ..

 

 

3. The player will control the Cat with their left hand and the WASD keys on the keyboard. Select the Cat sprite and add its properties code/script/code and movements code/script/code ..

 

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

When you are ready, press the "E" key to end your game and continue working on it.

 

 

4. Load/choose an enemy. You can change the enemy costume later but for now choose the beetle sprite ..

 

Name it "Beetle1". Add 2 more beetles and name them Beetle2 and Beetle3 ..

 

 

5. Select the Cat sprite, create a health variable for it and then add this code ..

 

 

6. The Cat will throw/fire a projectile at the attacking beetles to stop them chasing. You can change the projectile costume later but for now choose the Donut sprite ..

 

When you have loaded/imported it, add it's properties code ..

 

You need code so the Donut can be thrown by the Cat.

The following code/scripts allow the player to throw the Donut by aiming and firing with the mouse-pointer. There are 3 Beetle enemies so you must use nested "or" code blocks. In this example we have used 3 to allow for 4 touching code blocks.

Also, notice that the donut moves 14 steps towards the enemy before it is thrown. This is to ensure it doesn't get stuck on the edge of the stage if the Cat is at the edge of the stage.

 

Select the Donut and add the code to allow the Cat to throw/fire it ..

 

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

When you are ready, press the "E" key to end your game and continue working on it.

 

 

7. Use the File menu to save your game. Name it "React when hit advanced v1.sb3" ..

 

 

8. Each beetle will react in a different way when hit by the Donut.

Select Beetle1, create a speed variable for it and then add this code ..

 

Notice how this beetle starts at the top right corner of the stage, x=220 y=160.

 

Now create a health variable for it and add this code ..

 

Notice how this beetle starts at the top right corner of the stage, x=220 y=160.

 

 

9. Beetle1 will run back and get confused when the Donut hits it. To do so, it must know the direction the Donut is moving when it hits it.

Select the Donut sprite and make a new variable called "Donut direction" ("for all sprites"), then add this code ..

 

This code puts the Donut's "direction" property into the "Donut direction" variable. You can now use "Donut direction" to let any other sprite know which direction the Donut is moving.

 

 

10. Select Beetle1 and add the code for it's movements, and to make it react when hit.

Notice how it will lose health, become fainter, and speed up each time it is hit by the Donut ..

 

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

When you are ready, press the "E" key to end your game and continue working on it.

 

 

11. Use the File menu to save your game.

 

 

12. Select Beetle2 and add its properties and health code ..

 

Notice how this beetle starts at the middle right corner of the stage, x=220 y=0.

 

 

13. Beetle2 will explode/clone 20 times when hit, then re-spawn. Add it's movement and cloning code ..

 

Now add the code to make it's clones spiral away before deleting ..

 

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

When you are ready, press the "E" key to end your game and continue working on it.

 

 

14. Use the File menu to save your game.

 

 

15. Select Beetle3. This Beetle will clone and form a circle, then chase the Cat for a while before deleting and re-spawing.

Add its properties and health code first ..

 

Now add the code to clone it when hit by the Donut ..

 

Notice how each time the beetle is hit, more clones are created and the CloneWaitTime increases to allow them to make a formation before chasing the Cat.

Also notice how each clone 's direction is 4 degrees more than its predecessor. This will make them form a circle or part circle.

 

Now add the code to animate each clone to initially move outwards to form a circle or part circle, and then chase the Cat ..

 

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

When you are ready, press the "E" key to end your game and continue working on it.

 

 

16. Use the File menu to save your game.

 

 

17. Well done, you've completed this exercise. Can you add more beetles and create your own reactions?