Scratch Programming Basics With Answers
Scratch is a basic programming language that makes use of the block-building style for creating…
Since you are reading this I am quite sure that you are familiar with this one hit wonder mobile application, which in fact kind of ruined the creators life a bit. It is simply because he did not expect it to bring any monetary returns, and he stated that he was doing it for pure entertainment. Anyways, I am going to show you step by step how to re-create this game on Scratch programming language. Start by right clicking on the pictures and downloading the main character, the pipes and the background which I have provided in this article somewhere near this paragraph.
Then click on the backdrops, go to costumes tab and upload the flappy bird background picture. Then use the duplicate tool to extend the frame covering the whole stage. Next I am going to make a program for the bird, and I will also create 3 additional costumes to it. This will make the game more dynamical.
Before I add any script to this flappy bird on Scratch, I will create 3 more copies of the bird’s costume. The first will be default looking straight forward, the second will tilt its head upwards, the third will be just like the first one. The last costume will tilt its head downwards. If you want, you can name the costumes 1, 2, 3 and 4 for convenience. Now comes the scripting part. Start by adding two ‘when space key pressed’ event blocks to the scripts area. Then create a new variable called ‘speed’. Add the ‘set speed to 10’ block under the first event block. Right under it add the ‘change y by’ motion block and set its value to be equal to that of the ‘speed’ variable. Simply drag the block inside its window. When I as a player will hit the space-bar the bird will change its y position by 10. For the other event block add the ‘repeat’ control block. Inside it add the ‘next costume’ looks block, and one ‘wait 0.07 seconds’ control block. Dont forget to set the repeat loop block to repeat itself 4 times. Finally, add the ‘switch costume to’, and in the drop down menu choose the last face of the costume. This will make the flappy bird on scratch to be flying upwards and then coming back down. And the last script is going to be triggered by simply click on the the green flag. Add another data block that sets the speed to 0, and make the bird start with the default, first costume. Its starting position should be at x-coordinates of -140 and 0 for y-coordinates. This is approximately the left middle position on the stage. Make the bird be visible by adding the ‘show’ looks block.
Now I will script a forever loop that will check if the bird is touching the pipes or not. The for the sensing blocks the pipes will be available once I import the pipes to the game. You will have to come back to this script later. Before we proceed any further, create a new sprite. When the canvas open, draw a green, straight line at the bottom of the screen. This will works as an indicator to know when the bird touches the ground. Now lets create the first if-then statement that says, if the bird is not touching the ground, the speed will be changed by -0.8. The ‘not’ block can be found in the operators category. This will cause the bird constantly obey the gravity force, which brings it back down if we dont keep pressing the space key on the keyboard. To make the flappy bird actually change its y value, we have to insert the ‘change y by’ motion block and insert the ‘speed’ variable block inside it. The second if-then statement will constantly check if the bird is touching either the pipe or the ground. When it does, the whole game will stop. So far, so good. Test the program to see if everything works as scripted. And if something is not working quite right, carefully examine my script with yours. You can also remix the numbers and add additional code to it to make it more authentic to the real game. But I think it is rather good as it is. Next I will script the pipes to make the spawn and move to the left side of the screen. In fact, the bird only moves up and down. We are 50% done with this flappy bird on Scratch.
Start with two first event blocks. The first script will make pipe clones. Therefore, hide it when the game starts and inside the forever loop make it create clones of itself every 2 second. Now when it starts as a clone, make it hide. Then make it go to the position where the x value is 260 and y value is -2. It is right side of the screen, a little behind the stage itself. Then make it wait 2 second. End before I add the ‘switch costume to a random costume between 1 and 3’ you have to create 2 costumes for the pipe sprite. The default one has a hole in the middle. The other one should have an escape hole somewhere near the top and the third should have it at the lower side of the screen. You can make many costumes with different height variations. What you have to change is the ‘pick random’ range of numbers. Since I have 3 costumes, it randomly picks one of them for each pipe clone. Next, make it appear by using the ‘show’ looks block. Then add the glide motion block which makes the sprite glide to the given coordinates. The -250 for x-coordinates and -2 for y-coordinates makes the sprite glide over the screen to the left side. Lastly, the script deletes this clone, to clear the memory space for the flappy bird game. Click the green flag and you should expect the green set of pipes to be constantly moving to the left side. Each set should appear with 2 second interval, and they should come in random lengths. Finally, I will make a script for the score count for this game.
Make a new variable called ‘score’, and add the ‘set score to 0’ block under the second event block. This will reset the score to 0 each time the game starts. Inside the forever loop block place the ‘if-then’ statement block. To change the score by 1, the following condition must be true. The x position of the pipe should be less than -117 and greater than -120. This is the approximate position on the stage when the bird passes the gap between the pipes. Making it score a point. Then simply duplicate this script and place it under the ‘when I start as a clone’ control block. This will make the score count work for clones too, because the game does not start with a clone pipe sprite. As for the finishing touch, create another sprite that says ‘Score’ and place it at the left upper corner of the stage, and drag the ‘score’ variable right next to it. To make it look aesthetically good, right click on the ‘score’ variable box and pick the large readout. Feel free to remix this game and make an even better version of it!