Hi There. . .
Learn how to recreate this addictive game from scratch with smooth animations challenging obstacles responsive controls and thrilling game play will guide you through every step from setting up the project to implementing the game logic such as dynamic text on canvas featuring smooth graphics to handling collision deduction auto update on scoring with end results and much more with JavaScript Here's a preview of what we'll be creating today first user can see the container with play button and score of zero and Below we can see the text tap to jump on mobile or space bar to jump on desktop and we need to hit the play button to start the game once we start our Fluffy Bird game will future to render the game board with background image and music with flying board that the player can control to fly between the pipes via tap the screen or pressing the space key and the game will track the score based on the pipe crossed by the user this game is endless until we hit the pipes once we hit the pipe the game is over and we'll get the replay button without any game object like bird or pipes on top we can see the total number of pipes crossed by the user as a score again we can hit the replay button it will reset the game to play don't miss out this fun and educational project and I upload unique project videos every week make sure to subscribe our channel Sharathchandar K your support means a lot that helps me to create more content for you and hit that notification bell to stay updated with our latest content now let's dive into the code let's open this Flappy Bird project folder in my favorite code editor Visual Studio code let me know your's favorite and why in our comment box below inside the directory I have already added index. html style. css and script.
js along with this we have two folders one is sounds and one is images game supporting files here inside the index. html I have already added HTML boiler plate with the help of exclamation mark along with this I have changed the title to Flappy Bird game by Sharathchandar K and below I have linked the files style. css with the help of Link element script.
js with the help of Script element now inside the body we need to add the game element for that we'll add the canvas this element is the hero of today's project canvas is used to draw graph on the Fly via scripting with the help of ID game canvas will set the width of 350 and Ito 480 along with this we'll add the span tag to display the game score and we'll add one more paragraph tag to display the user what to do with this mobile or desktop with the help of text says tap to jump on mobile with break space bar to jump on desktop and next to this we'll add the button value of play now we need to style this for that I need to jump to the style. css here in the style. css I have already added the import URL of font family press do along with this I have added the global selector with the help of * this will help us to reset the margin to 0px padding to 0px box sizing border box font family press start from the import URL now we need to add the styles to the body with the help of element select our body now we'll set the background color to blue color and we'll set the Min height 100vh display Flex we'll set the justify content to be center now we need to add the styles to the canvas with the help of element selector canvas we'll set the background color of white we'll set the Box Shadow to this with the help of 14px 14px 0 and 0 with black color and we'll set the margin of 1.
5rem now we'll style this span tag with the help of span we'll set the position to Absolute this will help us to set on top of the canvas with left of 50% will set to top 55px we'll set the transform translate to -50% , 50% and we'll set the color of light yellow color and we'll set the font way to bold we'll set the font size to 20px we'll set the padding inner spacing with top and bottom to 5px right and left to 10px we'll set the cursor 2 pointer now we'll set the text Shadow to this with - 2px 0 and color of black again we'll set zero 2px again the color of black again 2px and then position 0 with a black and 0- 2px with the color of black again and we'll set the font family to press start now let us see our HTML file in action for that in Visual Studio code we have a extension called go live once we click in the status bar the tool will automatically run our project into the default browser here here we can see the canvas with text and then score with Shadow effect now we need to style this with the help of paragraph element of P we'll set the position to Absolute again and we'll set the left to 50px and we'll set to top of 75px we'll set the transform translate again with minus 50% and 50% we'll set the text line to be Center we'll set the back to light yellow color we'll set the padding inner spacing to be 10px we'll set the phone family to press start again we'll set the font size to 10px and we'll set the color of gray if i save if I go to the browser here we can see the tap to jump on mobile it is placed into the bottom of the screen the same as we need to design the button also with the help of element selector button we'll set the position to absolute we'll set the left to 50% and we'll set the top to 50% this should be into the center of the canvas and we'll set the transform again to translate -50% , 50% we'll set the background to orange color we'll set the color to light yellow color we'll set the font way to bold want size to be 20px adding inner spacing top and bottom to be 5px right and left to 10px and we'll set the cursor to pointer if I save if I go to the browser here we can see the button on center of the canvas now we can set the over effect to this with the help of button over we set the background color to light orange color if I save if I go to the browser if I go on over we can see the effect to this that's all for the HTML and CSS now we need to add the functionalities with the help of JavaScript for that I need to declare the Declaration and we'll do the action on it for that I need to jump to the script. js here in the script. js we need to declare the Declaration with the help of Const with the value of canvas element equal to document document is an owner of your web page with the help of document we can do whatever action we want do global selector this will help us to pick that particular element of span tag and next thing we'll add the Let text equal to document dot query selector of that paragraph of P tag and we'll add one more Declaration of play button equal to document dot query selector with the help of element selector button this will help us to get all this element into the index.
HTML the next thing we add one more const canvas was equal to document dot get element by ID we can use element selector or we can use ID also this is the easiest way and we need to add the one more Declaration of CTX canvas dot get contact into 2D and we'll declare two more Global Declaration of let score equal to 0 running equal to false we'll see some of the Declaration what this values contains with the help of console. log so I will use score element comma canvas if I save if I go to the browser if I insert element here we can see the first element of span tag with a value of 0 and then second element of canvas with ID and attributes of width and height that is how the Dom tree will work with the help of this we need to manipulate the game here we have declared all the Declaration with the help of this we need to load the images and we need to do the animations to play the game first we load the background image to this canvas with the help of const draw background we'll create one separate Const with the help of this const function we'll create and display the background image with the help of CTX dot fill style equal to this will help us to fill this background style we need to declare this function with the help of draw background before that we need to fill text with the help of 0 comma 0 X and Y the Canvas is a two dimensional array with the help of X and Y we can add the graphics and we can add the canvas. width this will help us to set this Fill react to the entire width and we'll set the canvas.
height also if I save if I go to the browser here we can see the background color to this same like this we need to load the images also or that we have a CTX dot draw image function with that we need to add the canvas image and then DX and DY for this we need to create this canvas image object with the help of new declaration background image equal to New Image object this will help us to create that image source with the help of background image do Source equal to we have the folder of image here slash BG bottom this is the background image so I will use the same BG bottom do jpg and we'll set the background image to this draw image and we need to use the zero for this DX and we'll set the canvas. minus background image. he this is what the Dy if I save if I go to the browser still the image is not yet visible if I go to the console here we are not getting any errors if I if I go to the network if I refresh here can see the BG bottom.
jpg the status code is 304 this is said it's not loaded so we need to do some trick on it so we'll use the background image do onload function this will help us to check if this background images is loaded or not once it's loaded then only this should be filled if I save if I go to the browser now we can see the background image to this once we click play we need to display the flying bird here right for that we have six more images for all the images we can't do like this so instead of that we can do a window do onload with the help of this function we can load all the images and the windows load time so I can use this background image image source and I will cut and paste it here and I can use the background image. onload once it's loaded we'll call this function of draw background and I will remove the calling function from here if I save if I go to the browser here we can see the background image as same as previous along with this we need to declare the birds images same like this here we have a three set of birds image one is flippy bird. png 1 this is three and this is two here we can see the wings is on three Direction with the help of this we need to create the animations like the birds are flying so I can use the same like this bird one images source and this is the bird one and bird two and this is bird three again I have used the bird two for the bird images so they should fly to top to bottom so only I'm using this four along with this we have two more images one is ground and one is pipe so I can declare this two also with ground image and then pipe image along with this images we have a sounds also we need to declare the sounds to for that we have a three sound and one is it sound if I go and play the it sound it is this and this is the background song and this is the background song these are the three songs we need to use in this project now once we hit the play button we need to do that bird flying right for that we need to create one more add even listener of this play button of this click of function inside this function once we are click we need to remove this play button as as this text to for that document dot body dot remove child we need to remove the child of play button and the same thing we can use it for the help text also if I save if I go to the browser if I click on play now we can see the play button and then text is gone again we can refresh it will be here and then we'll set the running State equal to true so here by default we have set this running State equal to false and score also to be zero they should be resetting the value now once we hit play button we need to start the music for that we can use the background music.
Loop equal to true because here we have a 2 minutes of song only this should be run on the loop and we'll set the background music do play function if I save if I go to the browser if I click on play we can get the background sound the next thing once we hit play we need to add the game Loop for that we need to create one more con off game Loop this will help us to Loop through the function of graph x and we'll set the bird to fly with the help of CTX dot clear react of 0 comma 0 we'll set the canvas dot entire width to be 0 and canvas do entire height to be clear and this game Loop function should be called inside this play button if I save if I go to the browser if I click on play now the everything is gone again we need to draw the background why we are doing this again and again because once we start the game and we hit the pipe somewhere that time the game is over again we need to restart right so only we are doing this practice now we need to draw the ground also for this for that we need to create one more const of ground equal to this is an object we need to set the Xto0 and Yto0 canvas dot height minus we need to set the ground height to this for the ground what is the height here this is the ground so we can set the ground I to 20 now we can set this Y axis to this and we'll set the ground right here this will reduce this canvas I to minus ground I so this is 480 and it will minus to 20 and it will set to 460 on the y axis and we'll set the width of canvas width because this should be filled into the entire canvas and we'll set the he also for the he we have already defined that is a ground right to be 20 and we'll set the speed also for this value of one and we need to update this ground every time with the help of update function say this update function we'll use this do minus equal to this dot read and we'll check if this dot X less than equal to minus this dot width then we'll set this dot a x to 0 this should be update to the starting point of x to be zero and we need to draw this with the help of draw function inside this draw function again we need to create the draw image of that ground image come on we can use this this dot X and this dot y of this position as well as this dot width come on this do height also and we need to call this draw function with the help of ground dot draw if I say if I go to the browser here we can see once we click play then we can see the ground image here if I refresh if I click play again we can see this and then same as this we need to create the bird also for this I will use cons object of bird equal to this x of 50 should be y of canvas. height divided by 2 should be set to the center of the screen before creating this bird we need to add some Declaration of bird image frame equal to zero and we need to add three more cons of that flipping travel and then bird gravity and then bird jump now we'll set the width to this of 45 and we'll set the I to it to 30 and we'll set the speed of zero and we'll set the gravity to this bird that gravity contains from that bird gravity declaration and then we'll use the jump of this board jump and we need to draw the board with the help of function inside this function we can create the bird same as CTX dot draw image of that bird image one and we'll set uh this dot X Comm on this dot y I need to call this draw function with the help of board. draw if I save if I go to the browser if I click on play here we can see the bird image but here it is not jumping or not doing the animation for that we need to create all the birds like bird one bird two bird three and bird four if I save now if I go to the browser if I click on play here we can see all the bird images is added on top of the same image now we have created this bird image now we need to do the animations to this to fly for that we need to create one more function of update this function inside this function we'll check this dot speed plus equal to this dot gravity and this dot y+ equal to this dot speed so here the speed is zero the gravity is bird Gravity the bird gravity is 0.
2 and this will update all the time to this this dot speed and this will automatically do the gravity of 0 to every time and this will automatically change the position of Y to that bird so if I hit on play now it is not updating why because here as I said this is a animation so we need to request the request frame animation with the help of same game Loop along with this we need to call that bird dot update function also the first time it will be y to 0. 2 again this request animation frame will run on the loop so the Y is automatically updating to 0. 2 0.
2 so if I go to the browser if I on play so now we can see that bird is going to the down store that is how the Y AIS will work so instead of 0. 2 I will set to 0. 9 if I save I go on play now we can see the speed of that gravity we click the space or the tab the bird should be stay in this frame for that we need to create the event listener of canvas start add event listener of click whenever we click this canvas that time also we need to fly the bird with bird.
speed equal to bird do jump and then same for space key also document dot add event listener of the key down function inside this key down function we'll get that e as a event the help of this e will check e do key code triple equal to 32 this 32 is pointed to this space key so again the same code I will add inside here if I save if I go to the browser if I click on Play If I click on space now we can see the bird is automatically jumping on the top how simple is this right again once we go down the bird should not go to the down side this should be stay into the canvas frame for that we can add one if condition inside the game Loop we'll check if bird do y plus bird do I height greater than canvas do height minus ground height this should be set to the top of the ground right and then if this is present we'll set the bird dot y equal to canvas. height minus ground height minus bird do height and we'll set the bird do speed equal to zero you'll save we'll check bird dot y less than 0 we'll set the bird dot Y to 0 and then bird. Y to 0 if I save if I go to the browser if I on play now we can see the bird is went and placed to the bottom of the screen if I click on canvas it is automatically going to the top or else if I hit on Space it is automatically going to the top now the next thing we need to do the flying effect to the bird for that inside the draw we need to add some condition we'll check if this do speed less than zero then only we need to fly this with the help of CTX do save as a function this will save this again we'll use the CTX dot translate of this do X plus this dot width divided by two again this dot y plus this dot height divided by 2 this will translate with this and we'll set the rotate to this CTX do rotate minus M dot height divided by 16 if this is matches we'll check if bird image frame percentage by 3 equal to 0 then we'll set CTX do draw image of this bird one comma minus this do width divided by two same as minus this dot height divided by 2 and we'll set this dot width and we'll set the this do height also so here if I save if I go to the browser if I click on play we can see the bird image here the frame animation it's looping the bird we need to do the bird image frame based on this if condition so again we can add else if condition I can copy this so I can use this next to this led by three instead of zero this should be one I will set to image of two the same thing I will use the another El I can check if this is two now we can set the image of three and then last one as we'll set the else condition insert this I will remove this if condition and I can set for image of four here end of this if condition we set CTX dot restore if I save if I go to the browser if I go on if I play so here we can see the bird is present and it is jumping on the top and it is not going on the top of the canvas because we are rest restricted in the game Loop the same as it is not going to down stare here it is restricted but still we are not getting that fly effect so for that we need to create one interval of set interval and we'll set the set interval to this bird fragment bird image fragment will set Plus+ and we'll set the flap interval to this this flap interval is for millisecond for every 50 millisecond this bird fragment will be counted time it is zero it will display the bird image one the second time it is one it will display the bird image 2 and three and four as same as this it will be rotating the image so only we are using this percentage so this will calculate for every zero one and two if I save if I go to the browser if I hit on play now we can see the flying effect on this but it is going on the top whenever it is coming to the down this should be rotate right the angle should be changed else this is going to the downstair so for that we need to change the angle so I will use this same condition above and will change some parameters here I will copy this and I will paste it inside this condition so so here instead of this CTX dot rotate for the top of rotate 16 we can remove this minus this will change the rotation direction to the bottom we'll check that if I save if I go to the browser if I click on play now we can see the up and down speed is up if it is going up and if it is down the rotation of the bird nose is moving to the downstair of the canvas simple is this right whenever we start the game the base also should be moved right for that I will go to this ground inside this ground we need to update this ground image with CTX dot draw image with that ground image again and we'll set this dot X plus this dot width this do y Comm on this dot width Comm on this do height and we'll update the ground also in the game Loop so I will jump to the game Loop here here in the game Loop We'll add the ground dot update this update having this functionality to rotate the speed of the width if I save if I go to the browser if I click on play now we can see the ground is moving and then bird also flying now we can feel we are moving to the right hand side along with this we need to add the pipes whenever we start the game right for the pipes we need to create one more cons of add pipe equal to function inside this function we are going to add the pipe before this need to add some declaration to this let pipes well to empty array and we'll set some cons Declaration of con pipe width to 52 one more con of Min Gap equal to 110 and one more const of Max Gap equal to 190 and one more con off we need to calculate this pipe Gap equal to math do floor We'll add the math dot random function to calculate this maintain gap of this space we'll use Mac Gap minus Min Gap plus one and we'll get plus Min Gap this will help us to get the minimum gap between the pipes inside this create one more con of I equal to math.
floor math. random we'll calculate this height of canvas. height divided by 2 and we'll add + 50 this is the height of this base and we'll set the cones y = to height minus pipe cap divided by two now we need to push all the pipes to that array of pipe do push inside this as an array object X of n do width and Y of we have already added the Y on top and then width of pip width what we have created above and then height of height again now we need to add this pipe whenever we starting the game for that I need to go to that even listener of the check because in the click only we are going to add this so as same as this I will reset the Pipe to be empty and I will add the function of this add pipes and we check console.
log of what this pipe contains if I save if I go to the browser if I hit on play here we can see the Y is not defined error inside this so here I have missed that space so that is Con why so if I save if I go to the browser if I click on play now the bird is came and the array also present here inside the array we can see the of this pipe is 160 and then width it start from 350 and then y of 95 so we have added the pipe next thing we need to generate the pipes and to display so I will remove this console. log and I will add the pipes also the same as the BG image and then B for this I will use function with the help of keyword and draw pipes inside this function We'll add the for of l i equal to z i less then pipes dot length and i++ whenever this pipe press and then only we need to display right for that only I am checking this as a loop and we'll add the CTX do field react as pipes of I dox is the position and we'll set the white zero and we'll set the pipes of I to the width of dot width Comm we'll set the pipes of I we'll get the with from here and again we can fill this CTX dot fil react of the bottom also because we need to display the top and bottom pipe right so again pipes of I do X Comm pipes of I dot y plus pipe Gap here what we are doing the first pipe is on the top so only we set the Y to zero so the second pipe we need to set to the bottom of this Cano and we need to use the Gap also that why I have set with that pipe Gap and then we'll set the pipe of I do width because this width is dynamic as same as for the Y for the height we can use the canvas do I because this is dynamic for the top side I will use this pipe of I do y minus pipe Gap we need call this draw pipe function on the game Loop itself then only we can able to see the pipes inside the game loop I will use this draw pipe also if I save if I go to the browser if I don't play now we can't see the pipes so here we have defined the pipes we need to create the pipes of top and bottom fill that CTX dot begin path function set the CTX dot talk style equal to that green colorx do line word equal 4 CTX dot main to of this pipes array of I dox pipes of I dot y this should be M to from starting to the point will be inside the 4 and then we'll set the CTX do line two of the same Piper is of I dox plus pipes of I do width and we'll set the pipes of array. I doy and then we need to sto this of that CTX do Stoke and we can create the image of CTX do draw image of that pipe image what we have already defined on top and we'll set the pipe array of i.
x this the position and we'll set the first pipe of Y = to 0 and pipes of I do word comma pipes of array I do y if I save if I go to the browser if I start if I click on play we are getting that CTX do stroke is not an function we have missed that c if I save if I go to the browser if I don't play now we can't see the the pipes because we have created the pipes in the fixed place we need to move the pipes right for that we can use that array of I do x minus = to 1 if I save if I go to the browser if I click on play now we can see the stock style to this but the image is not displayed we need to check this why because here again I have missed the r from the stroke style if I save if I go to the browser if I click on play now we can see the top pipe here and then bottom pipe is so I need to create the bottom pipe also with same like this so I will copy this and I will paste it next to this same as begin Po and then color also same I will use little lighter then line width also same I need to move the move two instead of this y with pipe Gap and then same for the line two also with of this and then y from plus pipe Gap and then stroke is same and then draw image of the same pipe and then we need to add the Y form here pipes of array of I dot y plus pipe Gap and then it also we need to add instead of this y we need to set to the canvas. height minus pipe array of I dot y minus pipe Gap minus that ground it if I save if I go to the browser if I hit on play now we can see the pipes we are getting with bird is flying and once the pipe is went again we need to get the pipe again so if I play now we can see the space Also here that also we need to fix once we hit the pipe the game will be over for that we need to draw the Baseline to ears for that we can draw the ground dot draw if I save if I go to the browser if I hit on play now we can see the ground is filled to that pipe also now once we hit the pipe we need to reset the game or else once we crossed the pipe we need to update the score for that we need to check the conditional statement if Bird dot X less than pipes of I with X Plus of I do width and same for bird do X Plus bird do width greater than pipes of array dot X and then we are checking if this bird X and pipe X is match with the WID else if the bird X and then pipe width is match else check bird do y less than pipes of array. I dot y or we'll check bird do y + bird do I greater than pipes of array dot height dot y + pipe Gap if all this condition satisfied then what will happen what this condition is doing so here we are checking bird X and then pipe of X and Y if everything is merge we need to set the game over all that we'll check do log of game over if I save if I go to the browser if I hit on play now we are not getting any of the console if i hit on the pipe here we are getting the game over so here we can see this that is how the collision deduction will happen instead of this console I can set this running equal to false and then I will use this running into this game Loop here if not off running then we'll set from return here so this will not allow to update the board or update the loop from here so only we are using this flag on the first then we'll change the ground draw and we need to add the button to this const let button equal document dot create element of this button we are going to create one more button and this replay button do inner text equal to will set for replay and we'll app this button into the document of the body document dot body dot append child of replay button if I save if I go to the browser if i hit on play I'm going to it on the pipe it on the pipe we can see the replay button with over effect this style is automatically taken from the style.
css now we can see the pipe and then bird because inside the game Loop we have stop the request frame animation so it will not going to request again once we click replay we need to restart the game and once we passed the hit we need to update the score also right for this first we set the event listener for this replay button dot add vente listener off click again with function inside this function we can remove the document. body dot remove child we can move this replay button and we set that running equal to true so this will help us to get that request frame animation to be fire and we'll set the score equal to 0 and we'll set the pipe. length equal to 0 again we need to add the pipes because we are resetting this and we'll play the game Loop here and once the game is ended and we need to return from here and how we need to update the score for updating the score we need to add one more conditional statement of if bird.
X greater than pipes of array dot height dox plus pipes of array dot with and not of pipes I dot cost if this is present then we'll set pipes of I do po equal to true because we are posed that pipe and we set the score ++ it will not going to update the score we need to update the score also end of this we can update this score element do text content equal to this score if I save if I go to the browser if i h on play now the score is zero I'm going pause this pip now we can see the score is one again we are not getting any pipes that we need to create again we can add one more if condition we'll set the pipes of I dox plus pip of I do width Less Than Zero so once this is crashed and we need to remove this pipe from the array with the help of lice of I comma 1 this will remove the current object from an array again we need to do the I minus minus and we need to add the add pipes from here I save if I go to the browser if I hit on play we'll get that bird it is flying and once we cross to the pipe of the width of the Cano again it will create one more pipe again it is cross now the score is two we are crossed the two pipes now game is over if I click on replay again we'll get that fresh game with fresh pipes and then scores also zero we can cross the pipes and we can do the thing now we need to add the sounds to this with the help of whenever we cross to the pipe we need to add the point sound. play function and whenever we it on that pipe we need to play the it sound.