<!DOCTYPE html>
<html>
<head>
<title>Flappy Bird</title>
<style>
canvas {
border: 2px solid black;
background: #4EC0CA;
}
body {
display: flex;
flex-direction: column;
align-items: center;
background: #333;
color: white;
}
</style>
</head>
<body>
<h2>Flappy Bird</h2>
<canvas height="600" id="game" width="400"></canvas>
<p>Press Space or Click to Flap!</p>
<script>
const canvas = document.getElementById('game');
const ctx = canvas.getContext('2d');
// Game state
let bird = {
x: 50,
y: 300,
velocity: 0,
gravity: 0.5,
jump: -8,
size: 20
};
let pipes = [];
let score = 0;
let gameState = 'start'; // 'start', 'playing', 'gameover'
const pipeGap = 150;
const pipeWidth = 50;
const pipeSpacing = 200;
// Game controls
document.addEventListener('keydown', (e) => {
if (e.code === 'Space') handleJump();
});
canvas.addEventListener('click', handleJump);
function handleJump() {
if (gameState === 'playing') {
bird.velocity = bird.jump;
} else if (gameState !== 'playing') {
startGame();
}
}
function startGame() {
bird.y = 300;
bird.velocity = 0;
pipes = [];
score = 0;
gameState = 'playing';
gameLoop();
}
function createPipe() {
const gapPosition = Math.random() * (canvas.height - pipeGap - 100) + 50;
pipes.push({
x: canvas.width,
topHeight: gapPosition,
bottomY: gapPosition + pipeGap,
passed: false
});
}
function update() {
if (gameState !== 'playing') return;
// Bird physics
bird.velocity += bird.gravity;
bird.y += bird.velocity;
// Pipe management
if (pipes.length === 0 || pipes[pipes.length-1].x < canvas.width - pipeSpacing) {
createPipe();
}
// Update pipes and check collisions
pipes.forEach(pipe => {
pipe.x -= 2;
// Collision detection
if (
bird.x + bird.size > pipe.x &&
bird.x - bird.size < pipe.x + pipeWidth &&
(bird.y - bird.size < pipe.topHeight ||
bird.y + bird.size > pipe.bottomY)
) {
gameState = 'gameover';
}
// Score counting
if (!pipe.passed && bird.x > pipe.x + pipeWidth) {
pipe.passed = true;
score++;
}
});
// Screen boundaries
if (bird.y + bird.size > canvas.height || bird.y - bird.size < 0) {
gameState = 'gameover';
}
}
function draw() {
// Clear canvas
ctx.fillStyle = '#4EC0CA';
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Draw bird
ctx.fillStyle = '#FFD700';
ctx.beginPath();
ctx.arc(bird.x, bird.y, bird.size, 0, Math.PI * 2);
ctx.fill();
// Draw pipes
ctx.fillStyle = '#2AA52A';
pipes.forEach(pipe => {
ctx.fillRect(pipe.x, 0, pipeWidth, pipe.topHeight);
ctx.fillRect(pipe.x, pipe.bottomY, pipeWidth, canvas.height - pipe.bottomY);
});
// Draw score
ctx.fillStyle = 'black';
ctx.font = '24px Arial';
ctx.fillText(`Score: ${score}`, 10, 30);
// Game state messages
if (gameState === 'start') {
ctx.fillStyle = 'black';
ctx.font = '30px Arial';
ctx.textAlign = 'center';
ctx.fillText('Click or Press Space to Start', canvas.width/2, canvas.height/2);
} else if (gameState === 'gameover') {
ctx.fillStyle = 'red';
ctx.font = '30px Arial';
ctx.textAlign = 'center';
ctx.fillText('Game Over! Click to Restart', canvas.width/2, canvas.height/2);
}
}
function gameLoop() {
update();
draw();
if (gameState === 'playing') {
requestAnimationFrame(gameLoop);
}
}
// Initial draw
draw();
</script>
</body>
</html>
This implementation includes:
- Smooth bird physics with gravity and jumping
- Randomly generated pipe obstacles
- Score tracking
- Collision detection
- Start/game over screens
- Responsive controls (spacebar or mouse click)
- Simple but effective graphics using Canvas API
To play:
- Save as HTML file
- Open in web browser
- Click or press Space to start
- Avoid pipes and keep the bird flying!
The bird is yellow, pipes are green, and the background is sky blue. The game gets progressively more challenging as you score points, requiring precise timing to navigate through the pipe gaps.