DOWNLOAD:FLAPPY BIRD WITH JAVASCRIPT - SOURCE CODE
In general, to create a JavaScript game, you'll need two things, the first is the HTML5 canvas, and the second is JavaScript.
If you want to follow this tutorial step by step, you must first download our starter template, from our repository on GitHub, the starter template, have all files already created, including the images, audio files. Download the starter template.
Now let's open our index.html file, You first go and create the canvas element, inside your HTML file :
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<canvas id="flappyBird" width="288" height="512"></canvas> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="flappyBird.js"></script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let cvs = document.getElementById("flappyBird"); | |
let ctx = cvs.getContext("2d"); |
getContext('2d') method, has properties and methods that allow us to draw and DO different things on the canvas.
Now let's create our game images.
To create an image, we create an instance of the Image() object, using the new keyword.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let bird = new Image(); | |
let bg = new Image(); | |
let fg = new Image(); | |
let pipeNorth = new Image(); | |
let pipeSouth = new Image(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bird.src = "images/bird.png"; | |
bg.src = "images/bg.png"; | |
fg.src = "images/fg.png"; | |
pipeNorth.src = "images/pipeNorth.png"; | |
pipeSouth.src = "images/pipeSouth.png"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fly = new Audio(); | |
var scor = new Audio(); | |
fly.src = "sounds/fly.mp3"; | |
scor.src = "sounds/score.mp3"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// gap; is the gap in pixels between the south Pipe and North Pipe. | |
var gap = 85; | |
// the constant is the south Pipe position, and it is calculating by adding the gap to the north Pipe. | |
var constant; | |
// the bird X and Y positions. | |
var bX = 10; | |
var bY = 150; | |
// the bird falls by 1.5 pixels at a time. | |
var gravity = 1.5; | |
// we initiate the players score | |
var score = 0; |
We need to add an eventlistener to our document. and when the player press a key, we run a function moveUp(); that will move up the bird by 25px to the top, and also play a sound.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
document.addEventListener("keydown", moveUp); | |
function moveUp(){ | |
bY -= 25; | |
fly.play(); | |
} |
When the game starts, the first pipe X position is at 288px (= cvs.width).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var pipe = []; | |
pipe[0] = { | |
x : cvs.width, | |
y : 0 | |
}; |
Other Tutorials for games created using JavaScript :
Create the Tetris Game Using Javascript
Create the Snake Game Using Javascript
Tags:
JavaScript
Excellent
ReplyDelete