Want to tweak the game difficulty? Look at the gravity, pipe speed, and gap size in the JS code.
html Copy code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>Flappy Bird Clone</title>
<style>
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
font-family: Arial, sans-serif;
background-color: #70c5ce;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
canvas {
background: #70c5ce;
border: 2px solid #000;
}
#score {
position: absolute;
top: 20px;
font-size: 24px;
color: white;
font-weight: bold;
text-shadow: 2px 2px 4px #000;
}
#game-over {
display: none;
position: absolute;
top: 50%;
transform: translateY(-50%);
background: rgba(0, 0, 0, 0.7);
color: white;
padding: 20px;
border-radius: 10px;
text-align: center;
font-size: 24px;
}
#restart {
margin-top: 15px;
padding: 10px 20px;
font-size: 18px;
cursor: pointer;
background-color: #ffcc00;
border: none;
border-radius: 5px;
}
#restart:hover {
background-color: #ffaa00;
}
</style>
</head>
<body>
<div id="score">Score: 0</div>
<div id="game-over">
<p>Game Over!</p>
<p id="final-score"></p>
<button id="restart">Restart</button>
</div>
<canvas id="gameCanvas" width="400" height="600"></canvas>
<script>
// Canvas setup
const canvas = document.getElementById("gameCanvas");
const ctx = canvas.getContext("2d");
// UI elements
const scoreDisplay = document.getElementById("score");
const gameOverDiv = document.getElementById("game-over");
const finalScoreText = document.getElementById("final-score");
const restartButton = document.getElementById("restart");
// Bird settings
const bird = {
x: 80,
y: 200,
width: 30,
height: 30,
velocity: 0,
gravity: 0.6,
jumpStrength: -10,
color: "#FFD700"
};
let pipes = [];
const pipeWidth = 60;
const pipeGap = 150;
const pipeSpeed = 2;
const pipeInterval = 1500; // milliseconds
let lastPipeTime = 0;
let score = 0;
let isGameOver = false;
// Game reset
function resetGame() {
bird.y = 200;
bird.velocity = 0;
pipes = [];
score = 0;
isGameOver = false;
scoreDisplay.textContent = "Score: 0";
gameOverDiv.style.display = "none";
lastPipeTime = performance.now();
animate();
}
// Pipe generator
function createPipe() {
const minHeight = 50;
const maxHeight = canvas.height - pipeGap - minHeight;
const topPipeHeight = Math.floor(Math.random() * maxHeight) + minHeight;
const bottomPipeY = topPipeHeight + pipeGap;
pipes.push({
x: canvas.width,
top: {
y: 0,
height: topPipeHeight
},
bottom: {
y: bottomPipeY,
height: canvas.height - bottomPipeY,
},
passed: false
});
}
// Collision detection
function detectCollision(pipe) {
// Check bird vs. pipe
const inPipeX = bird.x + bird.width > pipe.x && bird.x < pipe.x + pipeWidth;
const hitsTop = bird.y < pipe.top.height;
const hitsBottom = bird.y + bird.height > pipe.bottom.y;
return inPipeX && (hitsTop || hitsBottom);
}
// Game loop
function animate(currentTime = 0) {
if (isGameOver) return;
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Background
ctx.fillStyle = "#70c5ce";
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Bird physics
bird.velocity += bird.gravity;
bird.y += bird.velocity;
// Bird draw
ctx.fillStyle = bird.color;
ctx.fillRect(bird.x, bird.y, bird.width, bird.height);
// Ground / ceiling collision
if (bird.y + bird.height > canvas.height || bird.y < 0) {
endGame();
return;
}
// Pipe generation
if (currentTime - lastPipeTime > pipeInterval) {
createPipe();
lastPipeTime = currentTime;
}
// Pipes update
pipes.forEach((pipe, index) => {
pipe.x -= pipeSpeed;
// Top pipe
ctx.fillStyle = "#228B22";
ctx.fillRect(pipe.x, pipe.top.y, pipeWidth, pipe.top.height);
// Bottom pipe
ctx.fillRect(pipe.x, pipe.bottom.y, pipeWidth, pipe.bottom.height);
// Check for score
if (!pipe.passed && pipe.x + pipeWidth < bird.x) {
pipe.passed = true;
score++;
scoreDisplay.textContent = `Score: ${score}`;
}
// Collision
if (detectCollision(pipe)) {
endGame();
}
});
// Remove off-screen pipes
pipes = pipes.filter(pipe => pipe.x + pipeWidth > 0);
requestAnimationFrame(animate);
}
// End Game
function endGame() {
isGameOver = true;
finalScoreText.textContent = `Your final score: ${score}`;
gameOverDiv.style.display = "block";
}
// Jump control
function flap() {
if (isGameOver) return;
bird.velocity = bird.jumpStrength;
}
// Event listeners
document.addEventListener("keydown", e => {
if (e.code === "Space") {
flap();
}
});
canvas.addEventListener("click", flap);
restartButton.addEventListener("click", resetGame);
// Start game
resetGame();
</script>
</body>
</html>