Your first game is going to be a Flappy Bird clone. While this might seem like a complicated task, it really can be broken down to a few easy scripts.
What kind of a game is Flappy Bird? It is a game where your goal is to keep on pressing a button to make a bird rise as it is constantly falling while navigating its way through the pipes of varying heights that are coming at it.
It can be broken down as follows:
So, now we know we need a minimum of two sprites: a bird and a pipe.
What else will we need? We will need a background, a floor, some variables, and a few costumes.
Before we begin, let's create a new project for our Flappy Bird clone. To do so, click on the File menu and then click on New. A brand new Scratch project will appear.
Now that we know what we need, we can start by creating our variables:
score
. Type this and click on OK. The score
variable will be used to keep track of how many walls you have passed. It will go up by one each time.Now, make three more variables with the following names:
scroll
: This will be used to keep track of where the floor is, since it will be movingtime
: This will keep track of how many seconds have passed since you started the gamepipeX
: This will keep track of where the pipe is across the screenWith our variables created, it is now time to create our game's backgrounds using the following steps:
0
, and adds 1 to it every second using a forever block. The forever block repeats what's inside of it until the game is stopped.If you click on the green flag, you should see the background change. Now would be a good time to save your game as well. Click on File, then choose Save. Give your game a name and click on OK.
The floor of the game serves two purposes. The first, is to make it look like you are moving because the floor scrolls across the stage. The second, is to be an avoidable object. If you touch the floor, the game is over. Our floor is actually going to be two sprites.
Let's start with the first one:
Floor1
by clicking on the name textbox in the middle column.Now, let's create the scripts as follows:
The first floor sprite has three scripts:
0
, and then until the game is over, it decreases by 1
.-180
. This puts it at the bottom of the stage. It then sets the x position of the floor to the current value of the scroll variable. This will make it move.0
if it reaches the end of the screen.Press the green flag button to watch the floor scroll. It seems like there's a small problem though: the floor isn't long enough. It scrolls off the screen with nothing behind it until it reaches the end. Don't worry though. It's a simple fix!
Floor2
. Now, click on it and choose Scripts. We have to make a few adjustments.Floor2
. You can do this by clicking and dragging them to the first (leftmost) column.By setting x to scroll + 480, you will now have a second floor that follows the first floor and continually repeats.
You can add some color and some grass to the costume to add to the scrolling effect.
The pipes are relatively straightforward. It is one sprite, with three costumes, and one script to make it scroll across the screen.
Now, let's make the pipe scripts!
The following steps explain the making of pipe scripts:
0
, which puts the wall right in the middle of the screen, and x to 300
, which moves it off the screen to the right.300
. We will use that variable later.-240
. This allows us to check where it is on the screen. If it is less than -240
(which puts the pipe to the left of the screen), reset the position of the pipe to the far right, by setting pipeX back to 300
. Then, change the costume to the next one.pipeX
. We now have a scrolling pipe!Now, it's time to make our main character! This sprite is going to need two costumes: one with the wings up and one with the wings down. Luckily, Scratch already includes costumes, so you don't have to draw it yourself (but feel free to do so if you want!).
Now, let's work on the scripts for our bat!
When the green flag is clicked, set the score variable to 0
, switch to the costume with the bat wings up, set the size to be 10
percent of the original sprite size, go to the topmost layer, and then move to those x and y positions. Play around by changing the size of your bat and experimenting with the x and y positions. Changing these will affect the difficulty of your game.
Now, we enter our forever loop. Each time this loop occurs, we set the sprite to be the bat with wings up, and move it down by 1
on the stage. This next block introduces us to user input. It says that if the space key is pressed on your keyboard, then it should switch to the bat with wings down and move up the stage by 5
. You can change the key to be anything on your keyboard. You can also change the 5
to increase or decrease the height at which the bat flies per button press.
The next two if
blocks follow the same formula. If your bat sprite touches Floor1, Floor2, or the pipes, then Game Over will appear next to the sprite and the game will stop.
Finally, if the x position of the bat is equal to the value of pipeX, then you should change the score by 1
, since it indicates that you have passed through one of the pipes successfully.
Now, try out your game!
Now that you have created your Flappy Bird clone, you should try to enhance and modify it. Here are some ideas:
When you are ready, we can move onto the next game.
3.140.198.12