Sure! Below is a complete implementation of the Flappy Bird game using HTML, CSS, and JavaScript. This single HTML file incorporates all the necessary code and can be run directly in your browser. The game uses the <canvas> element for rendering, and the physics (gravity, flap strength, collision detection) are closely modeled to resemble the original Flappy Bird experience.
How to Use:
- Copy the entire code below and save it as an
.html file (e.g., flappyBird.html).
- Open the file in your web browser.
- Click anywhere on the screen or press the Spacebar to make the bird flap.
Complete Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Flappy Bird Clone</title>
<style>
body {
margin: 0;
background-color: #70c5ce; /* Sky blue background */
overflow: hidden;
font-family: sans-serif;
}
#gameCanvas {
display: block;
margin: 0 auto;
background-color: #70c5ce;
}
#score {
position: absolute;
top: 20px;
left: 50%;
transform: translateX(-50%);
font-size: 48px;
color: white;
text-shadow: 2px 2px 2px #000;
}
#gameOver {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
font-size: 48px;
color: white;
text-shadow: 2px 2px 2px #000;
display: none;
cursor: pointer;
}
</style>
</head>
<body>
<div id="score">0</div>
<div id="gameOver">Game Over<br>Click to Restart</div>
<canvas id="gameCanvas" width="400" height="600"></canvas>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const scoreElement = document.getElementById('score');
const gameOverElement = document.getElementById('gameOver');
const WIDTH = canvas.width;
const HEIGHT = canvas.height;
// Game Variables
let frames = 0;
const DEGREE = Math.PI / 180;
// Load Bird Image (Using simple circle for simplicity)
const bird = {
x: 50,
y: HEIGHT / 2,
radius: 12,
velocity: 0,
gravity: 0.5,
lift: -10,
update: function() {
this.velocity += this.gravity;
this.velocity *= 0.9; // air resistance
this.y += this.velocity;
if (this.y + this.radius >= HEIGHT - 80) { // ground collision
gameOver();
}
if (this.y - this.radius <= 0) { // ceiling collision
this.y = this.radius;
this.velocity = 0;
}
},
flap: function() {
this.velocity = this.lift;
},
draw: function() {
ctx.fillStyle = 'yellow';
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius, 0, 2 * Math.PI);
ctx.fill();
ctx.strokeStyle = 'orange';
ctx.stroke();
}
};
// Pipe Class
class Pipe {
constructor() {
this.top = 0;
this.bottom = 0;
this.x = WIDTH;
this.width = 60;
this.speed = 2;
this.size = 50;
this.gap = 120;
this.top = Math.random() * (HEIGHT / 2);
this.bottom = HEIGHT - (this.top + this.gap);
}
update() {
this.x -= this.speed;
}
draw() {
ctx.fillStyle = '#228B22'; // Forest green
// Top Pipe
ctx.fillRect(this.x, 0, this.width, this.top);
// Bottom Pipe
ctx.fillRect(this.x, HEIGHT - this.bottom, this.width, this.bottom);
}
offscreen() {
return this.x + this.width < 0;
}
hits(bird) {
if (bird.y - bird.radius < this.top || bird.y + bird.radius > HEIGHT - this.bottom) {
if (bird.x + bird.radius > this.x && bird.x - bird.radius < this.x + this.width) {
return true;
}
}
return false;
}
}
let pipes = [];
let pipeInterval = 90; // frames
let score = 0;
function reset() {
bird.y = HEIGHT / 2;
bird.velocity = 0;
pipes = [];
score = 0;
scoreElement.innerHTML = score;
frames = 0;
gameOverElement.style.display = 'none';
}
function gameOver() {
gameOverElement.style.display = 'block';
cancelAnimationFrame(animation);
}
// Handle Input
window.addEventListener('keydown', function(e) {
if (e.code === 'Space') {
bird.flap();
}
});
window.addEventListener('click', function() {
bird.flap();
});
gameOverElement.addEventListener('click', reset);
// Draw Ground (simple rectangle)
function drawGround() {
ctx.fillStyle = '#8B4513'; // SaddleBrown
ctx.fillRect(0, HEIGHT - 80, WIDTH, 80);
}
// Main Draw Function
function draw() {
ctx.fillStyle = '#70c5ce'; // Sky blue
ctx.fillRect(0, 0, WIDTH, HEIGHT);
bird.draw();
pipes.forEach(pipe => pipe.draw());
drawGround();
// Score
scoreElement.innerHTML = score;
}
// Update Function
function update() {
bird.update();
if (frames % pipeInterval === 0) {
pipes.push(new Pipe());
}
pipes.forEach((pipe, index) => {
pipe.update();
// Remove offscreen pipes
if (pipe.offscreen()) {
pipes.splice(index, 1);
}
// Collision
if (pipe.hits(bird)) {
gameOver();
}
// Scoring
if (!pipe.passed && pipe.x + pipe.width < bird.x) {
pipe.passed = true;
score++;
}
});
frames++;
}
let animation;
// Game Loop
function loop() {
update();
draw();
animation = requestAnimationFrame(loop);
}
loop();
</script>
</body>
</html>
Explanation of the Code:
-
HTML Structure:
- The
<canvas> element (gameCanvas) is used for drawing the game.
- The
<div id="score"> displays the current score at the top center.
- The
<div id="gameOver"> displays a "Game Over" message and instructions to restart the game.
-
CSS Styling:
- The body has a sky-blue background, and the canvas is centered.
- The score and game over messages are styled for visibility.
-
JavaScript Logic:
- Bird Object: Represents the player-controlled bird with properties like position, velocity, gravity, and methods to update its position and draw itself.
- Pipe Class: Represents the pipes (obstacles). Each pipe has a top and bottom part with a gap in between. Pipes move from right to left.
- Game Mechanics:
- The
update function updates the bird's position, handles pipe creation, movement, collision detection, and scoring.
- The
draw function renders all game elements on the canvas.
- The
loop function runs the game loop using requestAnimationFrame for smooth animations.
- Input Handling: The bird flaps (moves upward) when the spacebar is pressed or when the canvas is clicked.
- Collision Detection: The game checks if the bird collides with any pipes or the ground. If a collision occurs, the game stops and displays the "Game Over" message.
- Scoring: The score increments each time the bird successfully passes through a pair of pipes.
Customization:
You can further enhance this game by adding images for the bird and pipes, sounds, and more sophisticated graphics. However, this basic version provides the core mechanics and physics similar to the original Flappy Bird game.
Enjoy playing your Flappy Bird clone!