Make a working flappy bird game for mobile web browser with good fluid animations.
- User
Make a working flappy bird game for mobile web browser with good fluid animations.
- Assistant
Make a working flappy bird game for mobile web browser with good fluid animations.
Creating a Flappy Bird game for mobile web browsers with smooth, fluid animations is an engaging project that combines HTML5, CSS3, and JavaScript. Here's a step-by-step guide to help you build the game from scratch.
Create a new directory for your game project and set up the basic files:
index.html — Main HTML file.style.css — CSS styles.game.js — JavaScript code.Your project structure should look like this:
- project-folder/
- index.html
- style.css
- game.js
- assets/
- bird.png
- pipe.png
- background.png
You'll need images for:
You can create simple images yourself or use placeholder images. Alternatively, there are free game asset websites.
Note: Ensure you have the rights to use any images included in your game.
In index.html, set up the basic HTML structure:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Flappy Bird Clone</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
</head>
<body>
<canvas id="gameCanvas"></canvas>
<div id="score">Score: 0</div>
<script src="game.js"></script>
</body>
</html>
In style.css, we'll make the canvas full-screen and style the score:
body {
margin: 0;
overflow: hidden;
}
#gameCanvas {
display: block;
background: url('assets/background.png') repeat-x;
}
#score {
position: absolute;
top: 10px;
left: 10px;
font-family: 'Arial', sans-serif;
font-size: 24px;
color: #fff;
text-shadow: 2px 2px 5px #000;
}
In game.js, we'll implement the game mechanics step by step.
First, set up the canvas and context:
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
// Set canvas dimensions to match window size
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
// Game variables
let frames = 0;
const gravity = 0.5;
let score = 0;
Define the bird object:
const bird = {
x: 50,
y: canvas.height / 2,
width: 34,
height: 24,
speed: 0,
jump: -10,
img: new Image(),
draw() {
this.img.src = 'assets/bird.png';
ctx.drawImage(this.img, this.x, this.y, this.width, this.height);
},
update() {
this.speed += gravity;
this.y += this.speed;
},
flap() {
this.speed = this.jump;
}
};
Add an event listener for user input (tap or click):
document.addEventListener('keydown', function(e) {
if (e.code === 'Space') {
bird.flap();
}
});
canvas.addEventListener('touchstart', function() {
bird.flap();
});
Create the pipes array and pipe object:
const pipes = [];
const pipeWidth = 52;
const pipeGap = 200; // Gap between top and bottom pipes
function generatePipe() {
const pipeY = Math.floor(Math.random() * (canvas.height - pipeGap - 100)) + 50;
const topPipe = {
x: canvas.width,
y: pipeY - pipeGap - 320, // Adjust for pipe image height
width: pipeWidth,
height: 320,
img: new Image(),
type: 'top'
};
const bottomPipe = {
x: canvas.width,
y: pipeY,
width: pipeWidth,
height: 320,
img: new Image(),
type: 'bottom'
};
topPipe.img.src = 'assets/pipe.png';
bottomPipe.img.src = 'assets/pipe.png';
pipes.push(topPipe, bottomPipe);
}
Update pipes and generate new ones:
function updatePipes() {
if (frames % 150 === 0) {
generatePipe();
}
for (let i = 0; i < pipes.length; i++) {
const p = pipes[i];
p.x -= 2;
ctx.drawImage(p.img, p.x, p.y, p.width, p.height);
// Remove off-screen pipes
if (p.x + p.width < 0) {
pipes.splice(i, 1);
i--;
// Increase score when pipe passes
if (p.type === 'top') {
score++;
document.getElementById('score').innerText = 'Score: ' + score;
}
}
}
}
Implement collision detection between the bird and pipes or boundaries:
function checkCollision(bird, pipe) {
return (
bird.x < pipe.x + pipe.width &&
bird.x + bird.width > pipe.x &&
bird.y < pipe.y + pipe.height &&
bird.y + bird.height > pipe.y
);
}
function checkGameOver() {
for (let i = 0; i < pipes.length; i++) {
if (checkCollision(bird, pipes[i])) {
return true;
}
}
// Check if bird hits the ground or goes off-screen
if (bird.y + bird.height >= canvas.height || bird.y < 0) {
return true;
}
return false;
}
We updated the score in the pipe update function. The score is increased when the top pipe goes off-screen.
Create the main game loop:
function update() {
frames++;
// Clear canvas
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw and update bird
bird.draw();
bird.update();
// Update pipes
updatePipes();
// Check for game over
if (checkGameOver()) {
alert('Game Over! Your score: ' + score);
document.location.reload();
} else {
requestAnimationFrame(update);
}
}
// Start the game
update();
Responsive Canvas: Update the canvas size on window resize.
window.addEventListener('resize', () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
});
High-Resolution Screens: Adjust for device pixel ratio.
const scale = window.devicePixelRatio;
canvas.width = Math.floor(window.innerWidth * scale);
canvas.height = Math.floor(window.innerHeight * scale);
ctx.scale(scale, scale);
Touch Input: Ensure touch events are correctly handled.
Here's the complete game.js code:
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
// Responsive canvas
function resizeCanvas() {
const scale = window.devicePixelRatio;
canvas.width = Math.floor(window.innerWidth * scale);
canvas.height = Math.floor(window.innerHeight * scale);
ctx.scale(scale, scale);
}
resizeCanvas();
window.addEventListener('resize', resizeCanvas);
// Game variables
let frames = 0;
const gravity = 0.5;
let score = 0;
const bird = {
x: 50,
y: canvas.height / 2,
width: 34,
height: 24,
speed: 0,
jump: -10,
img: new Image(),
draw() {
this.img.src = 'assets/bird.png';
ctx.drawImage(this.img, this.x, this.y, this.width, this.height);
},
update() {
this.speed += gravity;
this.y += this.speed;
},
flap() {
this.speed = this.jump;
}
};
document.addEventListener('keydown', function(e) {
if (e.code === 'Space') {
bird.flap();
}
});
canvas.addEventListener('touchstart', function() {
bird.flap();
});
const pipes = [];
function generatePipe() {
const pipeWidth = 52;
const pipeGap = 200;
const pipeY = Math.floor(Math.random() * (canvas.height - pipeGap - 100)) + 50;
const topPipe = {
x: canvas.width,
y: pipeY - pipeGap - 320,
width: pipeWidth,
height: 320,
img: new Image(),
type: 'top'
};
const bottomPipe = {
x: canvas.width,
y: pipeY,
width: pipeWidth,
height: 320,
img: new Image(),
type: 'bottom'
};
topPipe.img.src = 'assets/pipe.png';
bottomPipe.img.src = 'assets/pipe.png';
pipes.push(topPipe, bottomPipe);
}
function updatePipes() {
if (frames % 150 === 0) {
generatePipe();
}
for (let i = 0; i < pipes.length; i++) {
const p = pipes[i];
p.x -= 2;
ctx.drawImage(p.img, p.x, p.y, p.width, p.height);
if (p.x + p.width < 0) {
pipes.splice(i, 1);
i--;
if (p.type === 'top') {
score++;
document.getElementById('score').innerText = 'Score: ' + score;
}
}
}
}
function checkCollision(bird, pipe) {
return (
bird.x < pipe.x + pipe.width &&
bird.x + bird.width > pipe.x &&
bird.y < pipe.y + pipe.height &&
bird.y + bird.height > pipe.y
);
}
function checkGameOver() {
for (let i = 0; i < pipes.length; i++) {
if (checkCollision(bird, pipes[i])) {
return true;
}
}
if (bird.y + bird.height >= canvas.height || bird.y < 0) {
return true;
}
return false;
}
function update() {
frames++;
// Clear canvas
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw and update bird
bird.draw();
bird.update();
// Update pipes
updatePipes();
// Check for game over
if (checkGameOver()) {
alert('Game Over! Your score: ' + score);
document.location.reload();
} else {
requestAnimationFrame(update);
}
}
// Start the game
update();
Since I cannot host the live demo here, you can copy the code into your local environment to test it. Ensure all assets are correctly linked.
You've now created a basic Flappy Bird clone for mobile web browsers with smooth animations. From here, you can enhance the game by:
localStorage.Tip: Test your game on actual mobile devices to ensure touch controls and performance are optimal.
Disclaimer: The code provided is a simplified version for educational purposes. In a production environment, consider optimizing the code further and handling edge cases.
Not yet included in a published release.