Scratch game exercise 123

Select character

In this exercise you will learn how to select a character before the game begins, and then use it in a simple shooter game.

 

Complete these steps ..

 

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

 

 

2. Select the stage and then it's "backgrounds" tab.

Download, save and then use the "import" button to import these 2 background images ..

 

Have a look at the backgrounds. You will notice that the "select character" background has some simple text on it. We will display the choice of character sprites here.

 

3. The game will start with the "select character" background. Add these scripts to the stage ..

Notice how we are broadcasting "select character" instead of "begin game". We will broadcast "begin game" after the player has selected a character.

 

 

4. Create the following variables ("for all sprites") ..

 

Here is an explanation of what each variable will be used for ..

Variable Description
1st character x pos This will help us arrange the characters in a grid
1st character y pos This will help us arrange the characters in a grid
character x width This will help us space the characters in the grid
which character When the character has been selected, this variable will hold the characters name
enemy health This will be displayed on the stage
player health This will be displayed on the stage
player speed This will make it easy for us to change the players speed

 

 

5. Add these scripts to the stage ..

Notice how the "Which character" variable is set to "none" until the character is selected.

 

 

6. Use "File>Save as" to save your game. Name it "Select character".

 

 

7. We need some character sprites to choose from. Download, save and then use the "choose new sprite from file" button under the stage to import these sprites ..

 

 

 

8. Select the "Tracer" sprite. We will start with a script which positions this sprite ready to be selected. Add this script ..

Notice how we are using a calculation to set the sprite position. We set the variables for the position and width of the 1st character sprite, which in this case is "Tracer".

 

 

9. Select the "Junkrat" sprite and add this script ..

Notice how the script is very similar to the 2nd half of the "Tracer" script, except that we are multiplying the "character x width" variable by 1.

Now drag and copy this script to "Reaper" ..

Edit the script so that we are multiplying the "character x width" variable by 2.

 

Drag this script to "Symetra", "McCree", and "Roadhog", each time changing the multiplier by 1.

Sprite "character x width" multiple
Tracer 0
Junkrat 1
Reaper 2
Symetra 3
McCree 4
Roadhog 5

 

Now, press the "B" key to test your game. The characters should spread out across the stage in a single row. Press "E" to end when you are done.

 

 

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

 

 

11. Select "Tracer" and add this script ..

Notice how this script sets the "Which character" variable to "Tracer" if the "Tracer" sprite is clicked. When the game begins "Tracer" will remain visible and the other sprites will disappear.

Copy this script to the 5 other character sprites, then edit it so that "if which character =" is set to the correct name for each sprite.

 

 

12. Now, press the "B" key to test your game. Choose a character by clicking it and the game should begin. The background should change, the other characters should disappear, and the health variables should appear. Press "E" to end when you are done.

 

 

13. Select "Tracer" again and add this script ..

Notice that if "Tracer" is selected the sprite is re-sized and positioned, otherwise it will hide.

Copy this script to the 5 other character sprites, then edit it so that "if which character =" is set to the correct name for each sprite.

 

 

14. Press the "B" key to test your game. Press "E" to end when you are done.

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

 

 

15. Select "Tracer" again and add the script to control the sprites movements with the arrow keys ..

Notice how this script uses the "player speed" variable. We can easily change this speed by editing the "set player speed to" variable script on the stage.

Copy this script to the 5 other character sprites, then edit it so that "if which character =" is set to the correct name for each sprite.

Press the "B" key to test your game. You should now be able to control the selected character sprite with the arrow keys. Press "E" to end when you are done.

 

 

16. Select "Tracer" again. Add the script that takes health from the character sprite when the bullet hits it ..

 

Copy this script to all the other character sprites (not the enemy).

 

 

17. The "enemy" sprite will chase the character sprite and fire automatically.

Select it and set it to only face left-right ..

 

Now add it's first scripts ..

 

Now add the script which control's its movements ..

 

And finally add the script for it's health ..

 

Press the "B" key to test your game. Press "E" to end when you are done.

 

 

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

 

 

19. Select the "enemy bullet" sprite and add click the "can rotate" button ..

 

Now add these scripts to allow the bullet to fire randomly and automatically at the character sprite ..

 

 

20. Add this script to all 6 character sprites to make them react when they are hit by the "enemy bullet" ..

 

Press the "B" key to test your game. Press "E" to end when you are done.

 

 

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

 

 

22. Select the "player bullet" sprite and add click the "can rotate" button ..

 

Now add these scripts ..

 

 

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

Press the "B" key to test your game. Press "E" to end when you are done.

 

 

24. Well done. You've finished. Can you add your own ideas to this game?