Here's a complete implementation of Flappy Bird using the HTML Canvas API and JavaScript:
html
<!DOCTYPE html>
<html>
<head>
<title>Flappy Bird</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #f7f7f7;
font-family: Arial, sans-serif;
}
canvas {
border: 1px solid #000;
background-color: #70c5ce;
}
</style>
</head>
<body>
<canvas height="480" id="canvas" width="320"></canvas>
<script>
// Get canvas and context
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
// Game variables
let score = 0;
let highScore = 0;
let frames = 0;
let gameSpeed = 2;
let gravity = 0.5;
let playing = false;
// Bird object
const bird = {
x: 50,
y: 150,
width: 30,
height: 30,
radius: 12,
velocity: 0,
jump: 6.6,
color: '#f00',
update: function() {
if (playing) {
this.velocity += gravity;
this.y += this.velocity;
}
// Floor collision
if (this.y + this.radius >= canvas.height - 90) {
this.y = canvas.height - 90 - this.radius;
gameOver();
}
// Ceiling collision
if (this.y - this.radius <= 0) {
this.y = this.radius;
this.velocity = 0;
}
},
flap: function() {
this.velocity = -this.jump;
},
draw: function() {
ctx.fillStyle = this.color;
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2);
ctx.fill();
// Draw wing
ctx.fillStyle = "#fff";
ctx.beginPath();
ctx.ellipse(this.x - 5, this.y, 10, 6, 0, 0, Math.PI * 2);
ctx.fill();
// Draw eye
ctx.fillStyle = "#000";
ctx.beginPath();
ctx.arc(this.x + 8, this.y - 4, 3, 0, Math.PI * 2);
ctx.fill();
}
};
// Pipes array
const pipes = [];
// Pipe object constructor
function Pipe() {
this.x = canvas.width;
this.width = 52;
this.topHeight = Math.floor(Math.random() * (canvas.height - 200)) + 20;
this.gap = 120;
this.bottomY = this.topHeight + this.gap;
this.bottomHeight = canvas.height - this.bottomY - 90; // 90 is floor height
this.color = "#0a0";
this.counted = false;
this.update = function() {
this.x -= gameSpeed;
// Score increment
if (!this.counted && this.x + this.width < bird.x) {
score++;
this.counted = true;
}
// Collision detection
if (
bird.x + bird.radius > this.x &&
bird.x - bird.radius < this.x + this.width &&
(bird.y - bird.radius < this.topHeight ||
bird.y + bird.radius > this.bottomY)
) {
gameOver();
}
};
this.draw = function() {
// Top pipe
ctx.fillStyle = this.color;
ctx.fillRect(this.x, 0, this.width, this.topHeight);
// Pipe cap
ctx.fillStyle = "#0c0";
ctx.fillRect(this.x - 3, this.topHeight - 15, this.width + 6, 15);
// Bottom pipe
ctx.fillStyle = this.color;
ctx.fillRect(this.x, this.bottomY, this.width, this.bottomHeight);
// Pipe cap
ctx.fillStyle = "#0c0";
ctx.fillRect(this.x - 3, this.bottomY, this.width + 6, 15);
};
}
// Game over function
function gameOver() {
playing = false;
if (score > highScore) {
highScore = score;
localStorage.setItem('flappyHighScore', highScore);
}
}
// Reset game function
function resetGame() {
bird.y = 150;
bird.velocity = 0;
pipes.length = 0;
score = 0;
playing = true;
}
// Draw background elements
function drawBackground() {
// Sky already drawn with canvas background
// Draw clouds
ctx.fillStyle = "#fff";
ctx.beginPath();
ctx.arc(80, 80, 20, 0, Math.PI * 2);
ctx.arc(100, 70, 25, 0, Math.PI * 2);
ctx.arc(120, 85, 15, 0, Math.PI * 2);
ctx.fill();
ctx.beginPath();
ctx.arc(250, 100, 25, 0, Math.PI * 2);
ctx.arc(275, 85, 20, 0, Math.PI * 2);
ctx.arc(290, 105, 15, 0, Math.PI * 2);
ctx.fill();
// Draw ground
ctx.fillStyle = "#ded895";
ctx.fillRect(0, canvas.height - 90, canvas.width, 70);
// Draw grass
ctx.fillStyle = "#73bf2e";
ctx.fillRect(0, canvas.height - 90, canvas.width, 15);
}
// Main game loop
function gameLoop() {
// Clear canvas
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw background
drawBackground();
// Update and draw pipes
for (let i = 0; i < pipes.length; i++) {
pipes[i].update();
pipes[i].draw();
// Remove pipes that are off screen
if (pipes[i].x + pipes[i].width < 0) {
pipes.splice(i, 1);
i--;
}
}
// Generate new pipes
if (playing && frames % 120 === 0) {
pipes.push(new Pipe());
}
// Update and draw bird
bird.update();
bird.draw();
// Draw score
ctx.fillStyle = "#000";
ctx.font = "20px Arial";
ctx.fillText("Score: " + score, 10, 30);
ctx.fillText("High Score: " + highScore, 10, 60);
// Game state message
if (!playing) {
ctx.fillStyle = "rgba(0, 0, 0, 0.5)";
ctx.fillRect(0, canvas.height/2 - 50, canvas.width, 100);
ctx.fillStyle = "#fff";
ctx.font = "24px Arial";
ctx.fillText("Flappy Bird", canvas.width/2 - 60, canvas.height/2 - 15);
ctx.font = "18px Arial";
ctx.fillText("Click or Press Space to Play", canvas.width/2 - 100, canvas.height/2 + 20);
}
frames++;
requestAnimationFrame(gameLoop);
}
// Load high score from local storage
if (localStorage.getItem('flappyHighScore')) {
highScore = parseInt(localStorage.getItem('flappyHighScore'));
}
// Event listeners
canvas.addEventListener('click', function() {
if (playing) {
bird.flap();
} else {
resetGame();
}
});
// Keyboard controls
document.addEventListener('keydown', function(e) {
if (e.code === 'Space') {
if (playing) {
bird.flap();
} else {
resetGame();
}
e.preventDefault(); // Prevent space from scrolling the page
}
});
// Start the game loop
gameLoop();
</script>
</body>
</html>
Just copy and paste this code into an HTML file, open it in a web browser, and you're ready to play!