Posted on

Scratch Coding – Flappy Bird

The starter project already has the art assets for flappy bird loaded.

Flappy bird is a game of skill; you must try to fly your bird through the moving pipes. We use the space bar to add some height to our bird’s flight, otherwise he’ll fall from the sky.

Our game will be setup so that the bird stays in the same X (across) position, we will move the upper and lower pipes across the screen.

The first task is to get our bird to always flap his wings.

We can do this by adding a forever block to our bird sprite using a forever block. With a small pause after each costume change it will look like our bird is flapping around:

The next step is to make our bird fly and fall from the sky. To do this we are going to need to create a variable that we can use to store the Y position of the bird.

Create a variable called ‘FallSpeed’:

The idea is that we will set this value to 0 every time the space bar is pressed, otherwise we will reduce it by a small amount and apply to the Y value of the bird’s position.

When we use the space bar, we want to set the falling speed back to zero and add some height to the bird:

Give that a test, find some nice values for moving the bird up on the space press and for the falling speed.

The next step is to move the pipes along the screen.

Coding the pipes:

We start the pipe on the right-hand side and move it to the left. Once we get very close to the left hand side, we move the pipe back into position.

Use this same logic for both pipe sprites.

We made our pipes move horizontally, but the game can also have us move our pipes vertically as well. We add some randomness to the Y position, so that each time the game resets the pipe’s position, we give it a different height.

What happens if we don’t reset the Y position on each reset?

We now need to add some logic to make game end if we hit a pipe or the ground.

On our bird sprite:

We broadcast a Game Over event which we can listen for on our Game Over sprite:

We also stop all the scripts from running once the game has ended.

Extras:

  • Add scoring, so that each time the pipes are reset you get a point
  • Make the game gradually get faster. (hint: you’ll need to use a pipe speed variable that can be changed on each reset).
  • Add another sprite that if the bird touches will make you hold your position for a few seconds.