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.
Certainly! Below is a simple implementation of a Flappy Bird game using HTML, CSS, and JavaScript. This version is basic and can be expanded with more features and improvements.
<!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>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div id="gameContainer">
<div id="bird"></div>
<div class="pipe topPipe"></div>
<div class="pipe bottomPipe"></div>
<div id="score">Score: 0</div>
</div>
<script src="script.js"></script>
</body>
</html>
body {
margin: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #70c5ce;
}
#gameContainer {
position: relative;
width: 300px;
height: 500px;
background-color: #70c5ce;
}
#bird {
position: absolute;
width: 30px;
height: 30px;
background-color: yellow;
border-radius: 50%;
left: 50px;
top: 50%;
}
.pipe {
position: absolute;
width: 50px;
background-color: green;
}
.topPipe {
top: 0;
height: 150px;
}
.bottomPipe {
bottom: 0;
height: 150px;
}
#score {
position: absolute;
top: 10px;
left: 10px;
color: white;
font-size: 20px;
}
const gameContainer = document.getElementById('gameContainer');
const bird = document.getElementById('bird');
const scoreElement = document.getElementById('score');
const topPipe = document.querySelector('.topPipe');
const bottomPipe = document.querySelector('.bottomPipe');
let birdTop = 210;
let gravity = 2;
let isGameOver = false;
let score = 0;
function startGame() {
birdTop += gravity;
bird.style.top = birdTop + 'px';
checkCollision();
updateScore();
requestAnimationFrame(startGame);
}
function jump() {
if (birdTop > 0) {
birdTop -= 35;
}
}
function checkCollision() {
const birdRect = bird.getBoundingClientRect();
const topPipeRect = topPipe.getBoundingClientRect();
const bottomPipeRect = bottomPipe.getBoundingClientRect();
if (
birdRect.right > topPipeRect.left && birdRect.left < topPipeRect.right && birdRect.top < topPipeRect.bottom ||
birdRect.right > bottomPipeRect.left && birdRect.left < bottomPipeRect.right && birdRect.bottom > bottomPipeRect.top ||
birdRect.top <= 0 || birdRect.bottom >= gameContainer.clientHeight
) {
isGameOver = true;
alert('Game Over! Your score: ' + score);
location.reload();
}
}
function updateScore() {
const birdRect = bird.getBoundingClientRect();
const pipeRect = topPipe.getBoundingClientRect();
if (birdRect.left > pipeRect.right) {
score++;
scoreElement.innerText = 'Score: ' + score;
}
}
document.addEventListener('keydown', jump);
startGame();
setInterval(() => {
const pipeGap = 150;
const maxPipeHeight = 300;
const minPipeHeight = 100;
const pipeHeight = Math.floor(Math.random() * (maxPipeHeight - minPipeHeight + 1)) + minPipeHeight;
topPipe.style.height = pipeHeight + 'px';
bottomPipe.style.height = (gameContainer.clientHeight - pipeGap - pipeHeight) + 'px';
topPipe.style.left = gameContainer.clientWidth + 'px';
bottomPipe.style.left = gameContainer.clientWidth + 'px';
function movePipes() {
if (isGameOver) return;
let pipeLeft = parseFloat(topPipe.style.left) || gameContainer.clientWidth;
pipeLeft -= 3;
if (pipeLeft < -50) {
pipeLeft = gameContainer.clientWidth;
}
topPipe.style.left = pipeLeft + 'px';
bottomPipe.style.left = pipeLeft + 'px';
requestAnimationFrame(movePipes);
}
movePipes();
}, 2000);
This is a basic version, and you can enhance it by adding more features like better graphics, sound effects, and more complex gameplay mechanics.