something-flappy / index.html
MarkBrianAdam's picture
redefine the codes to make the game a little less challenging - Follow Up Deployment
e26fbc6 verified
Raw History Blame Contribute Delete
14 kB
<!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>
<script src="https://cdn.tailwindcss.com"></script>
<style>
@keyframes flap {
0%, 100% { transform: rotate(-20deg); }
50% { transform: rotate(20deg); }
}
@keyframes scrollBackground {
0% { background-position: 0 0; }
100% { background-position: -100% 0; }
}
.flap-animation {
animation: flap 0.3s ease-in-out;
}
.scrolling-bg {
background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100" viewBox="0 0 100 100"><rect width="50" height="50" x="0" y="0" fill="%234fd1c5"/><rect width="50" height="50" x="50" y="50" fill="%234fd1c5"/></svg>');
background-size: 100px 100px;
animation: scrollBackground 10s linear infinite;
}
.pipe {
position: absolute;
width: 60px;
background-color: #38a169;
border-left: 4px solid #2f855a;
border-right: 4px solid #2f855a;
}
.pipe-top {
top: 0;
border-top: 4px solid #2f855a;
border-radius: 0 0 8px 8px;
}
.pipe-bottom {
bottom: 0;
border-bottom: 4px solid #2f855a;
border-radius: 8px 8px 0 0;
}
.bird {
position: absolute;
width: 40px;
height: 30px;
background-color: #f6e05e;
border-radius: 50% 50% 50% 50% / 60% 60% 40% 40%;
z-index: 10;
}
.bird:before {
content: '';
position: absolute;
width: 10px;
height: 10px;
background-color: #000;
border-radius: 50%;
top: 10px;
right: 5px;
}
.bird:after {
content: '';
position: absolute;
width: 15px;
height: 8px;
background-color: #dd6b20;
border-radius: 50% 50% 50% 50% / 60% 60% 40% 40%;
top: 15px;
right: -5px;
}
.wing {
position: absolute;
width: 20px;
height: 15px;
background-color: #ecc94b;
border-radius: 50%;
top: 5px;
left: -10px;
transform-origin: right center;
}
.game-container {
position: relative;
width: 100%;
max-width: 400px;
height: 600px;
margin: 0 auto;
overflow: hidden;
background-color: #4fd1c5;
}
.ground {
position: absolute;
bottom: 0;
width: 100%;
height: 30px;
background-color: #dd6b20;
border-top: 4px solid #c05621;
}
.score-display {
position: absolute;
top: 20px;
width: 100%;
text-align: center;
font-size: 24px;
font-weight: bold;
color: white;
text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5);
z-index: 20;
}
.game-screen {
position: absolute;
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
background-color: rgba(0, 0, 0, 0.5);
z-index: 30;
}
.game-title {
font-size: 48px;
font-weight: bold;
color: white;
margin-bottom: 20px;
text-shadow: 4px 4px 8px rgba(0, 0, 0, 0.8);
}
.game-button {
padding: 12px 24px;
font-size: 20px;
font-weight: bold;
background-color: #f6e05e;
color: #2d3748;
border: none;
border-radius: 8px;
cursor: pointer;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
transition: all 0.2s;
}
.game-button:hover {
transform: translateY(-2px);
box-shadow: 0 6px 8px rgba(0, 0, 0, 0.15);
}
.game-button:active {
transform: translateY(0);
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
</style>
</head>
<body class="bg-gray-100 flex items-center justify-center min-h-screen">
<div class="game-container scrolling-bg">
<div id="score" class="score-display">0</div>
<div id="bird" class="bird">
<div class="wing"></div>
</div>
<div class="ground"></div>
<div id="start-screen" class="game-screen">
<h1 class="game-title">Flappy Bird</h1>
<button id="start-button" class="game-button">Play</button>
</div>
<div id="game-over-screen" class="game-screen hidden">
<h1 class="game-title">Game Over</h1>
<div id="final-score" class="text-white text-2xl mb-6">Score: 0</div>
<button id="restart-button" class="game-button">Play Again</button>
</div>
</div>
<script>
document.addEventListener('DOMContentLoaded', () => {
// Game elements
const gameContainer = document.querySelector('.game-container');
const bird = document.getElementById('bird');
const scoreDisplay = document.getElementById('score');
const startScreen = document.getElementById('start-screen');
const gameOverScreen = document.getElementById('game-over-screen');
const finalScoreDisplay = document.getElementById('final-score');
const startButton = document.getElementById('start-button');
const restartButton = document.getElementById('restart-button');
const wing = document.querySelector('.wing');
// Game variables
let gameRunning = false;
let score = 0;
let birdY = 300;
let birdVelocity = 0;
let gravity = 0.5;
let jumpForce = -10;
let pipes = [];
let pipeGap = 150;
let pipeFrequency = 1500; // ms
let lastPipeTime = 0;
let gameSpeed = 2;
let animationId;
// Game dimensions
const gameWidth = gameContainer.offsetWidth;
const gameHeight = gameContainer.offsetHeight;
const birdWidth = 40;
const birdHeight = 30;
const groundHeight = 30;
// Initialize bird position
bird.style.left = '100px';
bird.style.top = birdY + 'px';
// Event listeners
startButton.addEventListener('click', startGame);
restartButton.addEventListener('click', startGame);
gameContainer.addEventListener('click', () => {
if (gameRunning) {
jump();
}
});
// Keyboard controls
document.addEventListener('keydown', (e) => {
if ((e.code === 'Space' || e.key === 'ArrowUp') && gameRunning) {
jump();
}
});
// Jump function
function jump() {
birdVelocity = jumpForce;
bird.classList.add('flap-animation');
wing.style.transform = 'rotate(-60deg)';
setTimeout(() => {
bird.classList.remove('flap-animation');
wing.style.transform = 'rotate(0deg)';
}, 300);
}
// Start game
function startGame() {
// Reset game state
gameRunning = true;
score = 0;
birdY = 300;
birdVelocity = 0;
pipes = [];
lastPipeTime = 0;
// Update UI
scoreDisplay.textContent = score;
startScreen.classList.add('hidden');
gameOverScreen.classList.add('hidden');
// Clear existing pipes
document.querySelectorAll('.pipe').forEach(pipe => pipe.remove());
// Start game loop
gameLoop();
}
// Game over
function gameOver() {
gameRunning = false;
finalScoreDisplay.textContent = `Score: ${score}`;
gameOverScreen.classList.remove('hidden');
cancelAnimationFrame(animationId);
}
// Create new pipe
function createPipe() {
const pipeHeight = Math.floor(Math.random() * (gameHeight - pipeGap - groundHeight - 100)) + 50;
const topPipe = document.createElement('div');
topPipe.className = 'pipe pipe-top';
topPipe.style.height = pipeHeight + 'px';
topPipe.style.left = gameWidth + 'px';
gameContainer.appendChild(topPipe);
const bottomPipe = document.createElement('div');
bottomPipe.className = 'pipe pipe-bottom';
bottomPipe.style.height = (gameHeight - pipeHeight - pipeGap - groundHeight) + 'px';
bottomPipe.style.left = gameWidth + 'px';
gameContainer.appendChild(bottomPipe);
pipes.push({
top: topPipe,
bottom: bottomPipe,
x: gameWidth,
passed: false
});
}
// Update pipes
function updatePipes() {
const currentTime = Date.now();
if (currentTime - lastPipeTime > pipeFrequency) {
createPipe();
lastPipeTime = currentTime;
}
for (let i = pipes.length - 1; i >= 0; i--) {
const pipe = pipes[i];
pipe.x -= gameSpeed;
pipe.top.style.left = pipe.x + 'px';
pipe.bottom.style.left = pipe.x + 'px';
// Check if pipe is passed
if (!pipe.passed && pipe.x < 100 - birdWidth) {
pipe.passed = true;
score++;
scoreDisplay.textContent = score;
// Increase difficulty
if (score % 5 === 0) {
gameSpeed += 0.3;
pipeFrequency = Math.max(1200, pipeFrequency - 50);
}
}
// Remove pipes that are off screen
if (pipe.x < -60) {
pipe.top.remove();
pipe.bottom.remove();
pipes.splice(i, 1);
}
}
}
// Check collisions
function checkCollisions() {
// Ground collision
if (birdY + birdHeight > gameHeight - groundHeight) {
return true;
}
// Ceiling collision
if (birdY < 0) {
return true;
}
// Pipe collisions
for (const pipe of pipes) {
if (
100 + birdWidth > pipe.x &&
100 < pipe.x + 60 &&
(birdY < pipe.top.offsetHeight ||
birdY + birdHeight > gameHeight - pipe.bottom.offsetHeight - groundHeight)
) {
return true;
}
}
return false;
}
// Game loop
function gameLoop() {
if (!gameRunning) return;
// Update bird position
birdVelocity += gravity;
birdY += birdVelocity;
bird.style.top = birdY + 'px';
// Update pipes
updatePipes();
// Check collisions
if (checkCollisions()) {
gameOver();
return;
}
animationId = requestAnimationFrame(gameLoop);
}
});
</script>
<p style="border-radius: 8px; text-align: center; font-size: 12px; color: #fff; margin-top: 16px;position: fixed; left: 8px; bottom: 8px; z-index: 10; background: rgba(0, 0, 0, 0.8); padding: 4px 8px;">Made with <img src="https://enzostvs-deepsite.hf.space/logo.svg" alt="DeepSite Logo" style="width: 16px; height: 16px; vertical-align: middle;display:inline-block;margin-right:3px;filter:brightness(0) invert(1);"><a href="https://enzostvs-deepsite.hf.space" style="color: #fff;text-decoration: underline;" target="_blank" >DeepSite</a> - 🧬 <a href="https://enzostvs-deepsite.hf.space?remix=MarkBrianAdam/something-flappy" style="color: #fff;text-decoration: underline;" target="_blank" >Remix</a></p></body>
</html>