# Space Shooter

## Mathematics Scratch GameHard

In this lesson we will control a spaceship with your mouse and shoot down balloons that randomly appear on the screen. We'll use variables to keep track of your score and how many lives you have left.

1

#### Create a new Scratch project

Create a new Scratch project and delete the cat sprite by right clicking on the cat and then clicking on the 'delete' option in the menu that appears.

2

Open the backdrop library (by clicking the backdrop library button highlighted in yellow in the figure on the right) and scroll to find the stars backdrop. Add it to you project by double clicking on it (or selecting it and clicking the 'OK' button). This will add the backdrop to your project.

3

#### Create 'score' and 'lives' variables

Each time you shoot a balloon you'll get a point and each time a balloon touches your spaceship you'll lose a life. To keep track of these we need to create two new variables, one called 'score' and one called 'lives'.

In the Data palette, create two new variables by clicking the 'Make a Variable' button.

1. 'score' this will store your score
2. 'lives' this will store your lives

When you create your first variable you will notice that new blocks for variables become available to use in the Data palette.

4

#### Setup the variables

At the start of the game we should have 0 points and 3 lives. Add the following code to your backdrop to setup your variables for the start of the game:

1. events > when clicked
2. data > set [score] to 0
3. data > set [lives] to 3

5

Open the sprite library and scroll down and find the spaceship sprite. Select the sprite by clicking on it and then click 'OK' to add it to your project (you can also double click it to add it).

Once you've added the spaceship sprite we need to edit which way it is pointing to make it point to the right. Click on the 'Costumes' tab to open the sprite editor and follow these steps:

1. Click on the Select tool (the arrow button).
2. Click on the spaceship so the box around it appears.
3. Drag the small circle that's at the top of the box around so that the spaceship points to the right.
4. Click on the 'Scripts' tab to go back to your code.

6

We're going to program our spaceship to point in whichever direction our mouse pointer is and program the up arrow to move the spaceship towards our mouse pointer. We'll also shrink the spaceship sprite so it fits on the screen better.

Add the following code to the spaceship sprite:

1. events > when clicked
2. looks > set size to 30 %
3. control > forever
1. motion > point towards [mouse-pointer]
2. control > if then
(put sensing > key [up arrow] pressed as the condition)
1. motion > move 2 steps

7

The spaceship is going to shoot out balls at the balloons. Open the sprite library and scroll down and find the ball sprite. Select the sprite by clicking on it and then click 'OK' to add it to your project (you can also double click it to add it).

At the start of the game we're going to shrink the ball so it's the right size compared to the spaceship and we're also going to hide it (it will appear each time it is fired). Add the following code to the ball sprite:

1. events > when  clicked
2. looks > set size to 10 %
3. looks > hide

8

Open the sprite library (by clicking the sprite library button highlighted in yellow in the figure on the right) and scroll down and find the balloon sprite. Select the sprite by clicking on it and then click 'OK' to add it to your project (you can also double click it to add it).

At the start of the game we're going to shrink the balloon so it's the right size and we're also going to hide it (we'll make copies of it appear in a later step). Add the following code to the balloon sprite:

1. events > when clicked
2. looks > set size to 20 %
3. looks > hide

9

#### Detect a mouse click

We're going to make the spaceship fire a ball when you click on the left mouse button. To detect these mouse clicks we'll need to add some code to the backdrop, so click on the backdrop in the bottom left of your project editor. Once you click the backdrop it should have a blue box around it.

When we detect a mouse click we're going to send a message to the other sprites. When the ball sprite receives this message it will run some code to fire out of the spaceship (we'll do this in a later step).

For the moment add the following code to the backdrop:

1. events > when stage clicked

10

#### Fire a ball

Each time you click on the stage we want a ball to fire out from the spaceship. To do this we'll need to create clones or copies of the ball. In a previous step we sent 'message1' when the stage is clicked. Now in this step we'll create a clone of the ball each time we receive that message, add the following code to the ball sprite:

1. events > when I receive [message1]
2. control > create clone of [myself]

Next we need to make the ball appear wherever the spaceship is and then make it shoot across the screen until it touches a balloon or the edge of the screen and then we can delete it.

Add the following code to the ball sprite:

1. control > when I start as a clone
2. motion > go to [spaceship]
3. motion > point in direction 90  (replace [90] with sensing > [direction] of [spaceship])
4. looks > show
5. control > repeat until
(we're going to test two conditions so use a operators >  or  block)
(put sensing > touching [edge] as the first condition)
(put sensing > touching [Balloon1] as the second condition)
1. motion > move 10 steps
6. control > delete this clone

11

#### Make the balloons appear

Now we'll make the balloons randomly appear on the screen and start floating around in a random direction.

First make a copy of the balloon randomly between 1 and 5 seconds. Add this code to the balloon sprite after the hide block:

1. control > forever
1. control > wait 1 secs
(replace [1] with operators > pick random 1 to 5)
2. control > create clone of [myself]

Next make the balloon appear in a random location and make it start floating in a random direction. If it touches the edge of the stage area we'll make it bouce. Add the following code to the balloon sprite:

1. control > when I start as a clone
2. motion > go to x: 0 y: 0
(replace x: [0] with operators > pick random -170 to 170)
(replace y: [0] with operators > pick random -170 to 170)
3. motion > point in direction 90
(replace [90] with operators > pick random 0 to 359)
4. looks > show
5. control > forever
1. motion > move 1 steps
2. motion > if on edge, bounce

12

#### Score a point

In the game each time you hit a balloon with a ball the balloon will disappear and you'll get a point. Add the following code underneath the if on edge, bounce block in the balloon sprite:

1. control > if then
(put
sensing > touching [Ball] as the condition)
1. data > change [score] by 1
2. sound > play sound [pop]
3. control > delete this clone

13

#### Loose a life

If a balloon touches your spaceship then you'll loose a life (remember we programmed the game to start with 3 lives). Add the following code underneath the if then statement for scoring a point:

1. control > if  then
(put
sensing > touching [Spaceship] as the condition)
1. data > change [lives] by -1
2. control > delete this clone

14

#### Game over

Finally our last piece of coding is to check if we've 0 lives left and if so then it's game over!

Add the following code to the backdrop underneath set [lives] to 3 :

1. control > forever
1. control > if then
(test the condition using operators >   =  )
(replace the first [] with data > lives)
(replace the second [] with [0])
1. control > stop [all]

15

#### Play the game!

That's it, you've finished all the coding! How many points can you score?

Have you any ideas on how you would improve or change the game? What code would you need to add or change for your idea?

End of lesson