Creating a game - Save the Bird

Easy way of learning Scratch programming is to create Games in Scratch. I will show you how to create a game in Scratch. In this game you will see a bird flying across the screen, it will randomly  lay droppings. You need to prevent the droppings from falling on the ground. There is a dustbin on the ground which you can move using the Right and the Left Arrow keys. Follow the bird for 30 seconds and collect the droppings as best as you can. If the droppings hits the ground, a lightening strikes at random location, it may hit the bird, and the game stops. If you manage to keep the bird alive for 30 seconds, you win.

Such simple is the game. I have used most of the Sprites from the sprites library. I have downloaded an image of a green colored dustbin and used it in the program as this image was not available in the sprites library.

I have created two new sprites which are quite simple. One is the bird droppings, another is a horizontal line for the ground.

Below you can see the coding done for this game. You can also play the game on the project's homepage on MIT's scratch website on this link.   

Sprites used in this game

The backdrops

The backdrop used in this game is called "blue sky" from the backdrop library. I have duplicated this backdrop and renamed it as "Warning" and "Congrats". I have written the messages that will be displayed when the Game stops when the lightening hits the bird and when the timer exceeds 30 seconds.

I have added the following code in the backdrop.This code will switch the initial backdrop when the game starts.

Code in the droppings sprite

As the game starts, clones of the droppings are created at an interval of 2 to 4 seconds randomly. The clones are deleted automatically after this interval. The first hide block hides the original sprite of the droppings.

The clones of the droppings are visible. They will appear below the parrot sprite. Turn 0 to 180 degrees random is chosen to make the droppings to appear at different angles than each other. A sound pop2 is made when the dropping appears. The dropping will start moving downwards in the negative Y axis until it touches the Ground sprite or the Dustbin sprite. If the dropping touches the Ground then a message is broadcast. I have named it "hit". After that the dropping sprite is hidden.

Code in the parrot sprite

When the game begins, the parrot starts at the top right corner. Take care that it does not touch the edge otherwise it will remain stuck at that position. The parrot moves at a random pace of 10 to 40 steps until it touches the edge. When it touches the left edge it will reappear at it's initial position again.

When the Lightening sprite touches the bird, it broadcasts a message "Bird Hit". When the parrot sprite receives this message then it will make a sound and then change its costume, and the backdrop is changed and the script stops.

Code in the Lightening sprite

The lightening sprite is hidden when the game starts. The Ground sprite broadcasts a message "hit" when a dropping touches it. When the Lightening sprite receives this message, it becomes visible at a random location and pointind in a random direction. It then starts moving in that direction until it touches either the Ground, the edge or the Parrot. If the Lightening sprite touches the Parrot then it will broadcast a message. I have named in as "Bird Hit". Then the Lightening sprite is hidden.

Code in the Ground sprite

This code is not specific to the Ground sprite. It could be added with any other sprites. It is the code for the Timer. I have set the time limit of 30 seconds to keep the game short. You need to reset the timer each time the game begins. If the Timer exceeds 30 seconds then the backdrop changes and the game stops. 

Code in the dustbin sprite

The dustbin sprite has the following code. It checks if the Left or the Right arrow key has been pressed and moves the dustbin to Left or to Right accordingly. Each keypress moves the dustbin for a distance of 10 pixels.