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
- 2 Add the Stars backdrop
- 3 Create 'score' and 'lives' variables
- 4 Setup the variables
- 5 Add the Rocketship sprite
- 6 Program your Rocketship
- 7 Add the Ball sprite
- 8 Add the Balloon1 sprite
- 9 Detect a mouse click
- 10 Fire a ball
- 11 Make the balloons appear
- 12 Score a point
- 13 Loose a life
- 14 Game over
- 15 Play the game!
Create a new Scratch project and delete the cat sprite.
Go to the Scratch website using the link below and click on the 'Create' link in the blue bar at the top.
By default, each new project starts with the cat sprite already added. To delete the cat click on the x in the blue circle beside the cat in the sprite list.
Open the backdrop library and add the Stars backdrop to your project.
To add a backdrop from the backdrop library follow these steps:
- Put your mouse over the Choose a Backdrop button
- Click on the magnifying glass
- Find the backdrop you want in the library
- Click on your chosen backdrop
You can use search box or the filter links (Fantasy, Music, Sports etc) to locate your backdrop.
Each time you shoot a balloon you'll get a point and each time a balloon touches your rocketship 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.
- 'score' this will store your score
- '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.
In the Variables palette, create a new variable by clicking the 'Make a Variable' button.
Once you click this button a box will appear asking what you want to call your variable. Give it a name that reminds you what you will be using it for. For example, if you wanted to keep track of your score in a game, you would create a variable called 'score'.
At the start of the game we should have 0 points and 3 lives. Add the following code to the stage to setup your variables for the start of the game.
when green flag clicked
set [score v] to (0)
set [lives v] to (3)
Open the sprite library and add the Rocketship sprite to your project.
Once you've added the Rocketship 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:
- Click on the Select tool (the arrow button).
- Hold down the left mouse button and draw a box over all the rocketship. This selects it and a box appears around it.
- Drag the small arrows at the bottom of the box around so that the rocketship points to the right.
- Click on the 'Scripts' tab to go back to your code.
This lesson is copyright of Coding Ireland. Unauthorised use, copying or distribution is not allowed.
Scratch is developed by the Lifelong Kindergarten Group at the MIT Media Lab. It is available for free at https://scratch.mit.edu