Balloons

Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

Projects

Balloons
Learn how to make a balloon-popping game!

Step 1 Introduction

You are going to make a balloon-popping game!


What you will make

What you will need


Hardware
A computer capable of running Scratch
Software
Scratch 3 (either online (https://rpf.io/scratchon) or offline (https://rpf.io/scratchoff))

What you will learn


How to use animation to make sprites move
How to use random numbers
How to draw sprites
How to create clones of a sprite
Additional information for educators
You can find the completed project here (https://rpf.io/p/en/balloons-get).
Step 2 Animating a balloon

Open a new Scratch project.


Online: open a new online Scratch project (http://rpf.io/scratch-new).
If you have a Scratch account you can make a copy by clicking Remix.

Offline: open a new project in the offline editor.


If you need to download and install the Scratch offline editor, you can find it at rpf.io/scratchoff (http://
rpf.io/scratchoff).

Delete the cat sprite.

Add in a new balloon sprite, and a suitable stage backdrop.


Add this code to your balloon, so that it bounces around the screen:

when clicked

go to x: 0 y: 0

point in direction 45

forever

move 1 steps

if on edge, bounce

Test out your balloon. Does it move too slowly? Change the numbers in your code if you want to speed it
up a bit.
Did you also notice that your balloon flips as it moves around the screen?

Balloons don’t move like this! To fix this, click on the balloon sprite icon, and then click the direction.
In the ‘rotation style’ section, click ‘Do not rotate’ to stop the balloon rotating.

Test your program again to see if the problem is fixed.


Step 3 Random balloons

With the code you have now, your balloon will always start in the same place and move in the same path.
Click the flag a few times to start your program, and you’ll see it’s the same every time.

Instead of using the same x and y position each time, you can let Scratch pick a random number
instead. Change your balloon’s code, so that it looks like this:

when clicked

go to x: pick random -150 to 150 y: pick random -150 to 150

point in direction 45

forever

move 1 steps

if on edge, bounce

If you click the green flag a few times, you should notice that your balloon starts in a different place each
time.
You could even use a random number to choose a random balloon colour each time:

I need a hint

Your code should look like this:

when clicked

go to x: pick random -150 to 150 y: pick random -150 to 150

point in direction 45

change colour effect by pick random 0 to 200

forever

move 1 steps

if on edge, bounce

What happens if this code is put at the start of your program? Does anything different happen if you put this code
inside the forever loop? Which do you prefer?
Challenge!

Challenge: More randomness


Can you make your balloon start by pointing in a random direction (between -90 and 180)?
Step 4 Popping balloons

Lets allow the player to pop the balloons!

Click on your balloon sprite, and then click the Costumes tab. You can delete all of the other costumes,
just leaving 1 balloon costume. Add a new costume, by clicking Paint new costume and create a new
costume called burst.
Make sure that your balloon switches to the right costume when the game starts. Your code should now
look like this:

when clicked

switch costume to balloon1-a

point in direction pick random -90 to 180

go to x: pick random -150 to 150 y: pick random -150 to 150

change color effect by pick random 0 to 200

forever

move 1 steps

if on edge, bounce

To allow the player to burst a balloon, add this code:

when this sprite clicked

switch costume to burst

start sound pop


Test out your project. Can you pop the balloon? Does it work as you expected?

You’ll need to improve this code, so that when the balloon is clicked, it shows the burst costume for a
short time, and is then hidden.

You can make all of this happen by changing your balloon when sprite clicked code to this:

when this sprite clicked

switch costume to burst

start sound pop

wait 0.3 seconds

hide

Now that you’re deleting the balloon when it’s clicked, you’ll also need to add a show block to the start of
the when flag clicked code.

when clicked

show

switch costume to balloon1-a

point in direction pick random -90 to 180


Try popping a balloon again, to check that it works properly.
Step 5 Adding a score

Let’s make things more interesting by keeping score.

To keep the player’s score, you need a place to put it. Create a new variable called score.

Add a variable in Scratch

Click on Variables in the Code tab, then click on Make a Variable.

Type in the name of your variable. You can choose whether you would like your variable to be
available to all sprites, or to only this sprite. Press OK.

Once you have created the variable, it will be displayed on the Stage, or you can untick the
variable in the Scripts tab to hide it.
When a new game is started (by clicking the flag), you should set the player’s score to 0. Add this code to
the top of the balloon’s when flag clicked code:

when clicked

set score to 0

show

switch costume to balloon1-a

Whenever a balloon is popped, you need to add 1 to the score:

when this sprite clicked

switch costume to burst

start sound pop

wait 0.3 seconds

change score by 1

hide

Run your program again and click the balloon. Does your score change?
Step 6 Lots of balloons

Popping 1 balloon isn’t much of a game, so let’s add lots more!


One simple way to get lots of balloons is just to right-click on the balloon sprite and click duplicate. This is OK if you
only want a few, but what if you need 20? or 100? Are you really going to click duplicate that many times?

A much better way of getting lots of balloons is to clone the balloon sprite.
Drag your balloon when flag clicked code to a new when I start as a clone control block.
when clicked

set score to 0

show

switch costume to balloon1-a

point in direction pick random -90 to 180

go to x: pick random -150 to 150 y: pick random -150 to 150

change color effect by pick random 0 to 200

forever

move 1 steps

if on edge, bounce

when I start as a clone

show

switch costume to balloon1-a

point in direction pick random -90 to 180

go to x: pick random -150 to 150 y: pick random -150 to 150

change color effect by pick random 0 to 200

forever

move 1 steps

if on edge, bounce
Add code to create 20 balloon clones to the when flag clicked code.

when clicked

set score to 0

hide

repeat 20

create clone of myself


You should also replace the hide block in the balloon-clicking script with a delete this clone
block.

when this sprite clicked

switch costume to burst

start sound pop

wait 0.3 seconds

change score by 1

hide

delete this clone

Test your project! Now when the flag is clicked, your main balloon sprite will hide and then clone itself 20
times. When each of these 20 clones is started, they will each bounce around the screen randomly, just
as they did before. See if you can pop the 20 balloons!
Step 7 Adding a timer

You can make your game more interesting, by only giving your player 10 seconds to pop as many balloons as
possible.

You can use another variable to store the remaining time left. Click on the stage, and create a new
variable called time.

This is how the timer should work:


The timer should start at 10 seconds;
The timer should count down every second;
The game should stop when the timer gets to 0.

Here’s the code to do this, which you can add to your stage:

when clicked

set time to 10

repeat until time = 0

wait 1 seconds

change time by -1

stop all
Drag your ‘time’ variable display to the right side of the stage. You can also right-click on the variable
display and choose ‘large readout’ to change how the time is displayed.

Test your game. How many points can you score? If your game is too easy, you can:

Give the player less time;


Have more balloons;
Make the balloons move faster;
Make the balloons smaller.
Play your game a few times until you’re happy that it’s the right level of difficulty.
Challenge!

Challenge: More objects


Can you add in other objects to your game? You can add good objects, like donuts, that give you lots of points, or
bad objects, like bats, that take points away.

You’ll need to think about the objects you’re adding. Think about:
How many will there be?
How big is it? How does it move?
How many points will you score (or lose) for clicking it?
Will it move faster or slower than the balloons?
What will it look/sound like when it’s been clicked?
If you need help adding another object, you can reuse the previous steps!
Step 8 What next?

Take a look at the Tech Toys (https://projects.raspberrypi.org/en/projects/tech-toys) Scratch project.

Published by Raspberry Pi Foundation (https://www.raspberrypi.org) under a Creative Commons


license (https://creativecommons.org/licenses/by-sa/4.0/).
View project & license on GitHub (https://github.com/RaspberryPiLearning/balloons)

You might also like