Posted on

Scratch Coding – Angry Birds

Today we are going to build a simple Angry Birds game where we shoot a bird up to hit a nasty pig.

The first thing we need to do is create a new sprite for our nice red bird.

Normally we use the ‘Choose a sprite from the library’ button, but the library doesn’t contain the nice bird picture that we want to use. Instead we are going to use the ‘upload sprite from file’ button.

Once we click on this, we need to find our file. Do the same thing for the green pig. We should end up with two nice sprites:

Now that we have our sprites setup, we can start thinking about how the game should work.

Some ideas of the game might include:

  • Make sure the Angry bird starts in the correct spot
  • Make sure the Green Pig starts in a different spot each time
  • Ask the player the angle that the angry bird should be fire
  • Fire the angry bird off at the angle the player gave
  • Keep score of how many times the bird hit the green pig

The range of angles that we will use, looks like this:

Use this image to help play the game by placing it behind your Angry Bird sprite.

To start our game, we need to make sure that both the bird and the pig start in the correct spots.

On the Angry Bird:

Let’s add the ‘Green Flag Hat Block’ so that we can move the bird when the game starts:

Pick a good starting spot for your bird, I’ve chosen -175, -95.

Ask the player for an angle

Next we want to ask the player which angle we should use to shoot our Angry Bird:

We can then use the answer to turn the bird to the number given by the player.

Making the bird move

We would like our bird to move until it either hits the edge of the screen or if it hits the Pig. To do this we can use the ‘repeat until block’.

The code that we add into this block will run all the time while the bird doesn’t touch either the edge of the screen or the pig. We do however want our bird to move, so let’s add some steps to this code.

Test it out. We should see that the bird will stop either at the pig or on the side of the screen.

On the Pig:

Make the pig move to a random spot

We want to make the angle that the bird needs to fly different each time the game is played. We don’t really want to move the pig closer to the bird, we just want it to be higher or lower on the screen. This is the Y Axis, so when we pick a random spot, we will not change the X axis (across the screen).

Test out the game now. Does it work OK?

Adding a score:

Now that we have our game almost working, we would like to keep score. To add scoring to our game, we need to create a variable that the computer can use to store the score.

Once the variable has been created, we should have some new blocks:

We can now change the code on our Angry Bird sprite to check to see if the bird has touched the pig.

Notice that I’ve also added the block to point in the direction (0) (Up).

Test your game out. How many pigs can your angry bird hit?