code a fun flappy bird html game
- User
code a fun flappy bird html game
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
code a fun flappy bird html game
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Here's a simple implementation of a Flappy Bird-style game using HTML, CSS, and JavaScript. You can copy this code into an HTML file and open it in a browser to play.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Flappy Bird</title>
<style>
body {
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #70c5ce;
font-family: Arial, sans-serif;
}
#game-container {
position: relative;
width: 400px;
height: 600px;
overflow: hidden;
background-color: #70c5ce;
border: 2px solid #333;
}
#bird {
position: absolute;
width: 40px;
height: 30px;
background-color: #ffcc00;
border-radius: 50% 50% 50% 50% / 60% 60% 40% 40%;
z-index: 10;
}
.pipe {
position: absolute;
width: 60px;
background-color: #5cb85c;
border: 2px solid #4cae4c;
border-radius: 5px;
}
#score {
position: absolute;
top: 20px;
left: 50%;
transform: translateX(-50%);
font-size: 24px;
color: white;
text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5);
z-index: 20;
}
#game-over {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: rgba(0, 0, 0, 0.7);
color: white;
padding: 20px;
border-radius: 10px;
text-align: center;
display: none;
z-index: 30;
}
#restart-btn {
margin-top: 10px;
padding: 8px 16px;
background-color: #5cb85c;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
}
#ground {
position: absolute;
bottom: 0;
width: 100%;
height: 20px;
background-color: #8b4513;
}
</style>
</head>
<body>
<div id="game-container">
<div id="bird"></div>
<div id="score">0</div>
<div id="game-over">
<h2>Game Over!</h2>
<p>Your score: <span id="final-score">0</span></p>
<button id="restart-btn">Play Again</button>
</div>
<div id="ground"></div>
</div>
<script>
// Game variables
const bird = document.getElementById('bird');
const gameContainer = document.getElementById('game-container');
const scoreElement = document.getElementById('score');
const gameOverElement = document.getElementById('game-over');
const finalScoreElement = document.getElementById('final-score');
const restartBtn = document.getElementById('restart-btn');
const ground = document.getElementById('ground');
let birdY = 300;
let birdVelocity = 0;
let gravity = 0.5;
let jumpForce = -10;
let gameRunning = true;
let score = 0;
let pipes = [];
let pipeGap = 150;
let pipeFrequency = 1500; // milliseconds
let lastPipeTime = 0;
let gameSpeed = 2;
// Initialize bird position
bird.style.top = birdY + 'px';
bird.style.left = '100px';
// Event listeners
document.addEventListener('keydown', function(e) {
if (e.code === 'Space' && gameRunning) {
birdVelocity = jumpForce;
}
if (e.code === 'Space' && !gameRunning) {
restartGame();
}
});
gameContainer.addEventListener('click', function() {
if (gameRunning) {
birdVelocity = jumpForce;
} else {
restartGame();
}
});
restartBtn.addEventListener('click', restartGame);
// Game loop
function gameLoop(timestamp) {
if (!gameRunning) return;
// Update bird position
birdVelocity += gravity;
birdY += birdVelocity;
bird.style.top = birdY + 'px';
// Generate pipes
if (timestamp - lastPipeTime > pipeFrequency) {
createPipe();
lastPipeTime = timestamp;
}
// Update pipes
updatePipes();
// Check collisions
checkCollisions();
// Check if bird hits the ground
if (birdY + bird.offsetHeight > gameContainer.offsetHeight - ground.offsetHeight) {
gameOver();
}
// Check if bird goes above the screen
if (birdY < 0) {
birdY = 0;
birdVelocity = 0;
}
requestAnimationFrame(gameLoop);
}
// Create a new pipe
function createPipe() {
const pipeHeight = Math.floor(Math.random() * 200) + 100;
// Top pipe
const topPipe = document.createElement('div');
topPipe.className = 'pipe';
topPipe.style.height = pipeHeight + 'px';
topPipe.style.top = '0';
topPipe.style.left = gameContainer.offsetWidth + 'px';
gameContainer.appendChild(topPipe);
// Bottom pipe
const bottomPipe = document.createElement('div');
bottomPipe.className = 'pipe';
bottomPipe.style.height = (gameContainer.offsetHeight - pipeHeight - pipeGap - ground.offsetHeight) + 'px';
bottomPipe.style.bottom = ground.offsetHeight + 'px';
bottomPipe.style.left = gameContainer.offsetWidth + 'px';
gameContainer.appendChild(bottomPipe);
pipes.push({
element: topPipe,
x: gameContainer.offsetWidth,
height: pipeHeight,
passed: false
});
pipes.push({
element: bottomPipe,
x: gameContainer.offsetWidth,
height: gameContainer.offsetHeight - pipeHeight - pipeGap - ground.offsetHeight,
passed: false
});
}
// Update all pipes
function updatePipes() {
for (let i = 0; i < pipes.length; i++) {
const pipe = pipes[i];
pipe.x -= gameSpeed;
pipe.element.style.left = pipe.x + 'px';
// Check if pipe is passed by the bird
if (!pipe.passed && pipe.x + pipe.element.offsetWidth < 100) {
pipe.passed = true;
if (i % 2 === 0) { // Only count once per pair
score++;
scoreElement.textContent = score;
// Increase difficulty
if (score % 5 === 0) {
gameSpeed += 0.5;
pipeFrequency = Math.max(800, pipeFrequency - 100);
}
}
}
// Remove pipes that are off screen
if (pipe.x + pipe.element.offsetWidth < 0) {
pipe.element.remove();
pipes.splice(i, 1);
i--;
}
}
}
// Check for collisions
function checkCollisions() {
const birdRect = bird.getBoundingClientRect();
for (const pipe of pipes) {
const pipeRect = pipe.element.getBoundingClientRect();
if (
birdRect.right > pipeRect.left &&
birdRect.left < pipeRect.right &&
birdRect.bottom > pipeRect.top &&
birdRect.top < pipeRect.bottom
) {
gameOver();
break;
}
}
}
// Game over
function gameOver() {
gameRunning = false;
finalScoreElement.textContent = score;
gameOverElement.style.display = 'block';
}
// Restart game
function restartGame() {
// Remove all pipes
for (const pipe of pipes) {
pipe.element.remove();
}
pipes = [];
// Reset game state
birdY = 300;
birdVelocity = 0;
bird.style.top = birdY + 'px';
score = 0;
scoreElement.textContent = score;
gameSpeed = 2;
pipeFrequency = 1500;
lastPipeTime = 0;
// Hide game over screen
gameOverElement.style.display = 'none';
// Start game
gameRunning = true;
requestAnimationFrame(gameLoop);
}
// Start the game
requestAnimationFrame(gameLoop);
</script>
</body>
</html>