Absolutely! Below is a complete, self-contained Flappy Bird game using the HTML Canvas API and JavaScript. Just copy and paste this code into an .html file and open it in your browser to play.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<title>Flappy Bird - Canvas</title>
<style>
body {
background: #70c5ce;
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
margin: 0;
}
canvas {
border: 2px solid #222;
background: #70c5ce;
display: block;
}
#restartBtn {
display: none;
position: absolute;
left: 50%;
top: 60%;
transform: translate(-50%, -50%);
font-size: 1.2em;
padding: 10px 30px;
background: #fff;
border: 2px solid #222;
border-radius: 5px;
cursor: pointer;
}
</style>
</head>
<body>
<canvas height="600" id="gameCanvas" width="400"></canvas>
<button id="restartBtn">Restart</button>
<script>
// Canvas setup
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
// Game constants
const GRAVITY = 0.5;
const FLAP = -8;
const BIRD_SIZE = 32;
const PIPE_WIDTH = 60;
const PIPE_GAP = 140;
const PIPE_SPEED = 2;
const GROUND_HEIGHT = 80;
// Game variables
let bird, pipes, score, highScore, gameOver, animationId;
// Bird object
function resetBird() {
return {
x: 80,
y: canvas.height / 2,
velocity: 0,
width: BIRD_SIZE,
height: BIRD_SIZE
};
}
// Pipe object
function createPipe() {
const topHeight = Math.random() * (canvas.height - PIPE_GAP - GROUND_HEIGHT - 100) + 40;
return {
x: canvas.width,
width: PIPE_WIDTH,
top: topHeight,
bottom: topHeight + PIPE_GAP
};
}
function resetGame() {
bird = resetBird();
pipes = [createPipe()];
score = 0;
gameOver = false;
document.getElementById('restartBtn').style.display = 'none';
loop();
}
function drawBird() {
ctx.save();
ctx.translate(bird.x + bird.width / 2, bird.y + bird.height / 2);
ctx.rotate(Math.min(bird.velocity / 10, Math.PI / 4));
ctx.fillStyle = "#ff0";
ctx.beginPath();
ctx.arc(0, 0, bird.width / 2, 0, Math.PI * 2);
ctx.fill();
// Eye
ctx.fillStyle = "#222";
ctx.beginPath();
ctx.arc(8, -6, 4, 0, Math.PI * 2);
ctx.fill();
// Beak
ctx.fillStyle = "#f90";
ctx.beginPath();
ctx.moveTo(bird.width / 2, 0);
ctx.lineTo(bird.width / 2 + 10, -5);
ctx.lineTo(bird.width / 2 + 10, 5);
ctx.closePath();
ctx.fill();
ctx.restore();
}
function drawPipes() {
ctx.fillStyle = "#0f0";
pipes.forEach(pipe => {
// Top pipe
ctx.fillRect(pipe.x, 0, pipe.width, pipe.top);
// Bottom pipe
ctx.fillRect(pipe.x, pipe.bottom, pipe.width, canvas.height - pipe.bottom - GROUND_HEIGHT);
});
}
function drawGround() {
ctx.fillStyle = "#ded895";
ctx.fillRect(0, canvas.height - GROUND_HEIGHT, canvas.width, GROUND_HEIGHT);
ctx.fillStyle = "#bcae6e";
for (let i = 0; i < canvas.width; i += 40) {
ctx.fillRect(i, canvas.height - GROUND_HEIGHT, 20, 20);
}
}
function drawScore() {
ctx.fillStyle = "#fff";
ctx.font = "bold 36px Arial";
ctx.textAlign = "center";
ctx.fillText(score, canvas.width / 2, 70);
ctx.font = "16px Arial";
ctx.fillText("High Score: " + (highScore || 0), canvas.width / 2, 100);
}
function drawGameOver() {
ctx.fillStyle = "#222";
ctx.font = "bold 48px Arial";
ctx.textAlign = "center";
ctx.fillText("Game Over", canvas.width / 2, canvas.height / 2 - 40);
ctx.font = "24px Arial";
ctx.fillStyle = "#fff";
ctx.fillText("Score: " + score, canvas.width / 2, canvas.height / 2);
ctx.fillText("Press Space or Click to Restart", canvas.width / 2, canvas.height / 2 + 40);
}
function update() {
// Bird physics
bird.velocity += GRAVITY;
bird.y += bird.velocity;
// Pipes movement
pipes.forEach(pipe => {
pipe.x -= PIPE_SPEED;
});
// Add new pipes
if (pipes[pipes.length - 1].x < canvas.width - 200) {
pipes.push(createPipe());
}
// Remove off-screen pipes
if (pipes[0].x + PIPE_WIDTH < 0) {
pipes.shift();
score++;
if (!highScore || score > highScore) {
highScore = score;
localStorage.setItem('flappyHighScore', highScore);
}
}
// Collision detection
for (let pipe of pipes) {
// Pipe collision
if (
bird.x + bird.width > pipe.x &&
bird.x < pipe.x + pipe.width &&
(bird.y < pipe.top || bird.y + bird.height > pipe.bottom)
) {
gameOver = true;
}
}
// Ground or ceiling collision
if (bird.y + bird.height > canvas.height - GROUND_HEIGHT || bird.y < 0) {
gameOver = true;
}
}
function loop() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawGround();
drawPipes();
drawBird();
drawScore();
if (!gameOver) {
update();
animationId = requestAnimationFrame(loop);
} else {
drawGameOver();
document.getElementById('restartBtn').style.display = 'block';
}
}
function flap() {
if (!gameOver) {
bird.velocity = FLAP;
} else {
resetGame();
}
}
// Event listeners
document.addEventListener('keydown', e => {
if (e.code === 'Space' || e.key === ' ') {
flap();
}
});
canvas.addEventListener('mousedown', flap);
canvas.addEventListener('touchstart', flap);
document.getElementById('restartBtn').addEventListener('click', resetGame);
// Load high score
highScore = parseInt(localStorage.getItem('flappyHighScore')) || 0;
// Start the game
resetGame();
</script>
</body>
</html>
Features:
- Simple graphics (bird, pipes, ground)
- Score and high score (saved in localStorage)
- Game over and restart functionality
- Controls: Spacebar, mouse click, or tap
How to play:
Press Space, click, or tap to flap and avoid the pipes!