Files
fbird/fbird.html
T

741 lines
26 KiB
HTML
Raw Normal View History

2026-06-25 01:30:06 +01:00
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
2026-06-28 00:02:10 +01:00
2026-06-25 01:30:06 +01:00
<meta name="viewport" content="
2026-06-28 00:02:10 +01:00
width=device-width,
initial-scale=1.0,
maximum-scale=1.0,
user-scalable=no,
viewport-fit=cover">
2026-06-25 01:30:06 +01:00
<link rel="shortcut icon"
2026-06-28 00:02:10 +01:00
href="https://upload.wikimedia.org/wikipedia/commons/thumb/c/cf/UZ_road_sign_1.25.2.svg/960px-UZ_road_sign_1.25.2.svg.png"
type="image/svg+xml">
<title>famous bird</title>
2026-06-25 01:30:06 +01:00
<style>
2026-06-28 00:02:10 +01:00
/* ==========================================
BASE LAYOUT & TYPOGRAPHY
========================================== */
2026-06-25 01:30:06 +01:00
body {
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
2026-06-28 00:02:10 +01:00
min-height: 100dvh;
/* Background gradient outside the game container */
2026-06-25 01:30:06 +01:00
background: linear-gradient(to bottom, #64b3f4, #c2e59c);
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
overflow: hidden;
2026-06-28 00:02:10 +01:00
/* Prevents accidental text selection or zooming
while frantically tapping the screen */
2026-06-25 01:30:06 +01:00
touch-action: none;
-webkit-touch-callout: none;
-webkit-user-select: none;
user-select: none;
}
2026-06-28 00:02:10 +01:00
/* ==========================================
GAME CONTAINER & CANVAS
========================================== */
2026-06-25 01:30:06 +01:00
.game-container {
position: relative;
box-shadow: 0 10px 40px rgba(0, 0, 0, 0.6);
overflow: hidden;
2026-06-28 00:02:10 +01:00
/* Maintains a standard portrait aspect ratio */
2026-06-25 01:30:06 +01:00
aspect-ratio: 360 / 640;
width: 90vw;
height: 90dvh;
max-width: 90%;
max-height: 90%;
2026-06-28 00:02:10 +01:00
border-radius: 16px;
2026-06-25 01:30:06 +01:00
container-type: inline-size;
}
2026-06-28 00:02:10 +01:00
/* Adjusts sizing for desktop monitors */
2026-06-25 01:30:06 +01:00
@media (min-width: 600px) {
.game-container {
2026-06-28 00:02:10 +01:00
height: 95dvh;
max-height: 95%;
width: auto;
border-radius: 16px;
2026-06-25 01:30:06 +01:00
}
}
canvas {
display: block;
2026-06-28 00:02:10 +01:00
width: 100%;
2026-06-25 01:30:06 +01:00
height: 100%;
background: #70c5ce;
}
2026-06-28 00:02:10 +01:00
/* ==========================================
USER INTERFACE (UI) OVERLAY
========================================== */
/* Acts as a transparent layer sitting on top of the
HTML5 canvas to hold our HTML text and buttons */
2026-06-25 01:30:06 +01:00
.ui-overlay {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
color: white;
text-align: center;
2026-06-28 00:02:10 +01:00
/* Allows clicks to pass through the overlay
and hit the canvas behind it */
2026-06-25 01:30:06 +01:00
pointer-events: none;
box-sizing: border-box;
2026-06-28 00:02:10 +01:00
padding: 4cqw;
2026-06-25 01:30:06 +01:00
}
2026-06-28 00:02:10 +01:00
/* Groups the different UI screens */
2026-06-25 01:30:06 +01:00
#startScreen, #gameOverScreen, #gameUI {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
width: 100%;
}
.game-title {
font-size: 13cqw;
font-weight: 800;
margin: 0 0 4cqw 0;
text-shadow: 0 0.8cqw 2cqw rgba(0,0,0,0.6);
line-height: 1.1;
}
.score-display {
font-size: 16cqw;
font-weight: bold;
text-shadow: 0 0.6cqw 2cqw rgba(0, 0, 0, 0.5);
2026-06-28 00:02:10 +01:00
margin-top: 2cqw;
2026-06-25 01:30:06 +01:00
}
.message {
background: rgba(0, 0, 0, 0.75);
padding: 4cqw 6cqw;
border-radius: 3cqw;
font-size: 6cqw;
width: 85%;
box-sizing: border-box;
2026-06-28 00:02:10 +01:00
margin-bottom: 4cqw;
2026-06-25 01:30:06 +01:00
box-shadow: 0 1cqw 3cqw rgba(0, 0, 0, 0.3);
border: 2px solid rgba(255,255,255,0.1);
}
.instructions {
background: rgba(0, 0, 0, 0.6);
padding: 3.5cqw 5cqw;
border-radius: 2cqw;
font-size: 4.5cqw;
2026-06-28 00:02:10 +01:00
margin-top: 3cqw;
width: 85%;
2026-06-25 01:30:06 +01:00
box-sizing: border-box;
}
button {
background: #f1c40f;
border: none;
padding: 4cqw 10cqw;
font-size: 6.5cqw;
border-radius: 10cqw;
cursor: pointer;
font-weight: 900;
color: #2c3e50;
box-shadow: 0 1.5cqw 4cqw rgba(0, 0, 0, 0.4);
transition: transform 0.1s;
2026-06-28 00:02:10 +01:00
/* Re-enables clicking specifically for buttons */
2026-06-25 01:30:06 +01:00
pointer-events: auto;
margin-top: 2cqw;
}
2026-06-28 00:02:10 +01:00
/* Adds a slight physical "press" effect when clicked */
2026-06-25 01:30:06 +01:00
button:active {
transform: scale(0.92);
}
2026-06-28 00:02:10 +01:00
/* ==========================================
LEADERBOARD & HIGHSCORE CSS
========================================== */
input[type="text"] {
background: rgba(255, 255, 255, 0.95);
border: 2px solid #2c3e50;
padding: 2.5cqw 4cqw;
border-radius: 2cqw;
font-size: 5cqw;
font-family: inherit;
font-weight: bold;
color: #2c3e50;
text-align: center;
width: 60%;
margin-bottom: 3cqw;
text-transform: uppercase;
outline: none;
/* Re-enables clicking and typing for the input box */
pointer-events: auto;
touch-action: manipulation;
}
input[type="text"]:focus {
border-color: #f1c40f;
box-shadow: 0 0 10px rgba(241, 196, 15, 0.5);
}
.leaderboard-row {
display: flex;
justify-content: space-between;
margin-bottom: 1.5cqw;
border-bottom: 1px solid rgba(255,255,255,0.2);
padding-bottom: 1cqw;
}
/* Utility class to easily hide UI elements */
2026-06-25 01:30:06 +01:00
.hidden {
display: none !important;
}
</style>
</head>
<body>
<div class="game-container">
<canvas id="gameCanvas" width="360" height="640"></canvas>
2026-06-28 00:02:10 +01:00
2026-06-25 01:30:06 +01:00
<div class="ui-overlay">
2026-06-28 00:02:10 +01:00
2026-06-25 01:30:06 +01:00
<div id="startScreen">
2026-06-28 00:02:10 +01:00
<h1 class="game-title">famous bird</h1>
2026-06-25 01:30:06 +01:00
<div class="message">
Tap the screen or<br>
click the mouse or<br>
hit spacebar to<br>
2026-06-28 00:02:10 +01:00
"flap your wings"
2026-06-25 01:30:06 +01:00
</div>
2026-06-28 00:02:10 +01:00
<button id="startButton">START GAME</button>
2026-06-25 01:30:06 +01:00
</div>
2026-06-28 00:02:10 +01:00
2026-06-25 01:30:06 +01:00
<div id="gameOverScreen" class="hidden">
2026-06-28 00:02:10 +01:00
<div class="message"
style="font-size: 8cqw; font-weight:bold; color: #ff4757; margin-bottom: 2cqw;">
2026-06-25 01:30:06 +01:00
GAME OVER
</div>
2026-06-28 00:02:10 +01:00
<div class="score-display"
style="margin-top: 0; margin-bottom: 2cqw;">
2026-06-25 01:30:06 +01:00
Score: <span id="finalScore">0</span>
</div>
2026-06-28 00:02:10 +01:00
<div id="highscoreEntry" class="hidden"
style="display: flex; flex-direction: column; align-items: center; width: 100%;">
<div style="font-size: 5cqw; margin-bottom: 2cqw; color: #f1c40f; font-weight: bold; text-shadow: 0 2px 4px rgba(0,0,0,0.5);">
NEW HIGH SCORE!
</div>
<input type="text" id="playerName" placeholder="YOUR NAME" maxlength="13">
<button id="saveScoreButton" style="font-size: 5cqw; padding: 2.5cqw 8cqw;">
SAVE SCORE
</button>
</div>
<div id="leaderboard" class="instructions"
style="margin-top: 2cqw; font-size: 4.5cqw;">
<div style="font-weight: 900; margin-bottom: 3cqw; text-align: center; color: #f1c40f;">
TOP 5 SCORES
</div>
<div id="leaderboardList" style="text-align: left;">
</div>
</div>
<button id="restartButton" style="margin-top: 3cqw;">TRY AGAIN</button>
2026-06-25 01:30:06 +01:00
</div>
2026-06-28 00:02:10 +01:00
2026-06-25 01:30:06 +01:00
<div id="gameUI" class="hidden">
<div class="score-display" id="score">0</div>
</div>
</div>
</div>
<script>
2026-06-28 00:02:10 +01:00
// ==========================================
// 1. DOM ELEMENT BINDINGS
// ==========================================
// Grabbing references to HTML elements so we can
// manipulate them via JavaScript later.
2026-06-25 01:30:06 +01:00
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const startScreen = document.getElementById('startScreen');
const gameOverScreen = document.getElementById('gameOverScreen');
const gameUI = document.getElementById('gameUI');
const scoreDisplay = document.getElementById('score');
const finalScoreDisplay = document.getElementById('finalScore');
const startButton = document.getElementById('startButton');
const restartButton = document.getElementById('restartButton');
2026-06-28 00:02:10 +01:00
// ==========================================
// 2. GLOBAL GAME STATE & TRACKERS
// ==========================================
// Tracks the current menu/game phase.
let gameState = 'start';
// Current session score.
2026-06-25 01:30:06 +01:00
let score = 0;
2026-06-28 00:02:10 +01:00
// Array to hold {name, score} objects for the leaderboard.
let highscores = [];
// Time and Speed setup for smooth animations.
let lastTime = 0;
let pipeSpawnTimer = 0;
// Target pixel distance between pipes. Ensures that when
// the game speeds up, the horizontal gap stays identical.
const PIPE_DISTANCE = 220;
// Starting speed in pixels per second.
const BASE_SPEED = 120;
// How much faster to go every 10 points.
const SPEED_INCREMENT = 15;
// ==========================================
// 3. GAME ENTITIES (BIRD & PIPES)
// ==========================================
2026-06-25 01:30:06 +01:00
const bird = {
2026-06-28 00:02:10 +01:00
// Position and dimensions
2026-06-25 01:30:06 +01:00
x: 50,
y: canvas.height / 2 - 10,
width: 34,
height: 24,
2026-06-28 00:02:10 +01:00
// Physics variables tuned by the user
gravity: 20,
2026-06-25 01:30:06 +01:00
velocity: 0,
2026-06-28 00:02:10 +01:00
jump: -500,
2026-06-25 01:30:06 +01:00
2026-06-28 00:02:10 +01:00
// Renders the bird using basic Canvas shapes
2026-06-25 01:30:06 +01:00
draw: function() {
2026-06-28 00:02:10 +01:00
// Body (Yellow Circle)
2026-06-25 01:30:06 +01:00
ctx.fillStyle = '#FFD700';
ctx.beginPath();
ctx.arc(this.x, this.y, 15, 0, Math.PI * 2);
ctx.fill();
2026-06-28 00:02:10 +01:00
// Eye (Black Dot)
2026-06-25 01:30:06 +01:00
ctx.fillStyle = 'black';
ctx.beginPath();
ctx.arc(this.x + 8, this.y - 3, 4, 0, Math.PI * 2);
ctx.fill();
2026-06-28 00:02:10 +01:00
// Beak (Orange Triangle)
2026-06-25 01:30:06 +01:00
ctx.fillStyle = '#FF8C00';
ctx.beginPath();
ctx.moveTo(this.x + 15, this.y);
ctx.lineTo(this.x + 30, this.y);
ctx.lineTo(this.x + 15, this.y + 8);
ctx.fill();
2026-06-28 00:02:10 +01:00
// Wing (Darker Orange Ellipse)
2026-06-25 01:30:06 +01:00
ctx.fillStyle = '#FFA500';
ctx.beginPath();
ctx.ellipse(this.x - 5, this.y + 5, 10, 7, 0, 0, Math.PI * 2);
ctx.fill();
},
2026-06-28 00:02:10 +01:00
// Applies physics to the bird's vertical position
update: function(dt) {
2026-06-25 01:30:06 +01:00
if (gameState === 'playing') {
2026-06-28 00:02:10 +01:00
// Apply gravity over time
this.velocity += this.gravity * dt * 60;
// Move the bird by the current velocity
this.y += this.velocity * dt;
2026-06-25 01:30:06 +01:00
2026-06-28 00:02:10 +01:00
// Floor collision check
2026-06-25 01:30:06 +01:00
if (this.y + this.height/2 >= canvas.height - groundHeight) {
this.y = canvas.height - groundHeight - this.height/2;
2026-06-28 00:02:10 +01:00
if (gameState === 'playing') gameOver();
2026-06-25 01:30:06 +01:00
}
2026-06-28 00:02:10 +01:00
// Ceiling collision check
2026-06-25 01:30:06 +01:00
if (this.y - this.height/2 <= 0) {
this.y = this.height/2;
2026-06-28 00:02:10 +01:00
// Hitting the ceiling stops upward momentum
2026-06-25 01:30:06 +01:00
this.velocity = 0;
}
}
},
2026-06-28 00:02:10 +01:00
// Triggers a jump by applying negative velocity
2026-06-25 01:30:06 +01:00
flap: function() {
this.velocity = this.jump;
},
2026-06-28 00:02:10 +01:00
// Resets bird position for a new game
2026-06-25 01:30:06 +01:00
reset: function() {
this.y = canvas.height / 2 - 10;
this.velocity = 0;
}
};
const pipes = {
2026-06-28 00:02:10 +01:00
// Array to store active pipes currently on screen
2026-06-25 01:30:06 +01:00
position: [],
2026-06-28 00:02:10 +01:00
// Vertical gap between the top and bottom pipe
2026-06-25 01:30:06 +01:00
gap: 180,
2026-06-28 00:02:10 +01:00
// Defines how high the top pipe can randomly spawn
2026-06-25 01:30:06 +01:00
maxYPos: -150,
2026-06-28 00:02:10 +01:00
// Current horizontal speed (dx)
dx: BASE_SPEED,
2026-06-25 01:30:06 +01:00
2026-06-28 00:02:10 +01:00
// Renders all active pipes in the array
2026-06-25 01:30:06 +01:00
draw: function() {
for (let i = 0; i < this.position.length; i++) {
let p = this.position[i];
2026-06-28 00:02:10 +01:00
// Draw Top Pipe
2026-06-25 01:30:06 +01:00
ctx.fillStyle = '#2ECC71';
ctx.fillRect(p.x, p.y, p.width, p.height);
2026-06-28 00:02:10 +01:00
// Draw Top Pipe Cap
2026-06-25 01:30:06 +01:00
ctx.fillStyle = '#27AE60';
ctx.fillRect(p.x - 3, p.y + p.height - 20, p.width + 6, 20);
2026-06-28 00:02:10 +01:00
// Draw Bottom Pipe
2026-06-25 01:30:06 +01:00
ctx.fillStyle = '#2ECC71';
ctx.fillRect(p.x, p.y + p.height + this.gap, p.width, canvas.height);
2026-06-28 00:02:10 +01:00
// Draw Bottom Pipe Cap
2026-06-25 01:30:06 +01:00
ctx.fillStyle = '#27AE60';
ctx.fillRect(p.x - 3, p.y + p.height + this.gap, p.width + 6, 20);
}
},
2026-06-28 00:02:10 +01:00
// Handles spawning, moving, scoring, and pipe collision
update: function(dt) {
2026-06-25 01:30:06 +01:00
if (gameState !== 'playing') return;
2026-06-28 00:02:10 +01:00
// Advance the timer by delta time
pipeSpawnTimer += dt;
// Calculate time required to travel the target distance
// at the current speed, keeping gaps consistent.
let currentSpawnInterval = PIPE_DISTANCE / this.dx;
// Spawn a new pipe if enough time has passed
if (pipeSpawnTimer >= currentSpawnInterval) {
2026-06-25 01:30:06 +01:00
this.position.push({
x: canvas.width,
y: this.maxYPos * (Math.random() + 1),
width: 60,
height: 300
});
2026-06-28 00:02:10 +01:00
// Reset timer after spawning
pipeSpawnTimer = 0;
2026-06-25 01:30:06 +01:00
}
2026-06-28 00:02:10 +01:00
// Update positions and check collisions
2026-06-25 01:30:06 +01:00
for (let i = 0; i < this.position.length; i++) {
let p = this.position[i];
// Move pipe to the left
2026-06-28 00:02:10 +01:00
p.x -= this.dx * dt;
2026-06-25 01:30:06 +01:00
2026-06-28 00:02:10 +01:00
// If pipe moves entirely off-screen to the left:
// Remove it, add a point, and check for speed up.
2026-06-25 01:30:06 +01:00
if (p.x + p.width <= 0) {
2026-06-28 00:02:10 +01:00
this.position.splice(i, 1);
2026-06-25 01:30:06 +01:00
score++;
scoreDisplay.innerHTML = score;
2026-06-28 00:02:10 +01:00
// Increase difficulty speed every 10 points
if (score > 0 && score % 10 === 0) {
this.dx += SPEED_INCREMENT;
}
// Move to the next pipe in the loop
continue;
2026-06-25 01:30:06 +01:00
}
2026-06-28 00:02:10 +01:00
// COLLISION LOGIC
// Check collision with the TOP pipe
2026-06-25 01:30:06 +01:00
if (
bird.x + bird.width/2 > p.x &&
bird.x - bird.width/2 < p.x + p.width &&
bird.y - bird.height/2 < p.y + p.height
) {
gameOver();
}
2026-06-28 00:02:10 +01:00
// Check collision with the BOTTOM pipe
2026-06-25 01:30:06 +01:00
if (
bird.x + bird.width/2 > p.x &&
bird.x - bird.width/2 < p.x + p.width &&
bird.y + bird.height/2 > p.y + p.height + this.gap
) {
gameOver();
}
}
},
2026-06-28 00:02:10 +01:00
// Clears pipes and resets speed for a new game
2026-06-25 01:30:06 +01:00
reset: function() {
this.position = [];
2026-06-28 00:02:10 +01:00
this.dx = BASE_SPEED;
pipeSpawnTimer = 0;
2026-06-25 01:30:06 +01:00
}
};
2026-06-28 00:02:10 +01:00
// ==========================================
// 4. ENVIRONMENT RENDERING
// ==========================================
2026-06-25 01:30:06 +01:00
const groundHeight = 80;
function drawGround() {
2026-06-28 00:02:10 +01:00
// Main ground color
2026-06-25 01:30:06 +01:00
ctx.fillStyle = '#DAA520';
ctx.fillRect(0, canvas.height - groundHeight, canvas.width, groundHeight);
2026-06-28 00:02:10 +01:00
// Green grass line on top of the ground
2026-06-25 01:30:06 +01:00
ctx.fillStyle = '#2ECC71';
ctx.fillRect(0, canvas.height - groundHeight, canvas.width, 20);
2026-06-28 00:02:10 +01:00
// Small brown decorative dashes across the ground
2026-06-25 01:30:06 +01:00
ctx.fillStyle = '#8B4513';
for (let i = 0; i < canvas.width; i += 30) {
ctx.fillRect(i, canvas.height - groundHeight + 20, 15, 10);
}
}
function drawBackground() {
2026-06-28 00:02:10 +01:00
// Create a sky gradient from top to bottom
2026-06-25 01:30:06 +01:00
const skyGradient = ctx.createLinearGradient(0, 0, 0, canvas.height);
skyGradient.addColorStop(0, '#70c5ce');
skyGradient.addColorStop(1, '#e0f7fa');
2026-06-28 00:02:10 +01:00
// Fill background with gradient
2026-06-25 01:30:06 +01:00
ctx.fillStyle = skyGradient;
ctx.fillRect(0, 0, canvas.width, canvas.height);
2026-06-28 00:02:10 +01:00
// Draw decorative clouds using overlapping arcs
2026-06-25 01:30:06 +01:00
ctx.fillStyle = 'rgba(255, 255, 255, 0.8)';
2026-06-28 00:02:10 +01:00
// Cloud 1
2026-06-25 01:30:06 +01:00
ctx.beginPath();
ctx.arc(100, 80, 30, 0, Math.PI * 2);
ctx.arc(130, 70, 35, 0, Math.PI * 2);
ctx.arc(160, 80, 25, 0, Math.PI * 2);
ctx.fill();
2026-06-28 00:02:10 +01:00
// Cloud 2
2026-06-25 01:30:06 +01:00
ctx.beginPath();
ctx.arc(300, 120, 30, 0, Math.PI * 2);
ctx.arc(330, 110, 35, 0, Math.PI * 2);
ctx.arc(360, 120, 25, 0, Math.PI * 2);
ctx.fill();
}
2026-06-28 00:02:10 +01:00
// ==========================================
// 5. HIGHSCORE & LEADERBOARD LOGIC
// ==========================================
// Iterates through the highscores array and updates the DOM
function renderLeaderboard() {
const list = document.getElementById('leaderboardList');
// Display empty state if no scores exist
if (highscores.length === 0) {
list.innerHTML = "<div style='text-align:center;'>No scores yet!</div>";
return;
}
// Map the array into HTML strings for display
list.innerHTML = highscores.map((entry, i) =>
`<div class="leaderboard-row">
<span>${i + 1}. ${entry.name}</span>
<span style="color: #f1c40f; font-weight: bold;">${entry.score}</span>
</div>`
).join('');
}
// Handles saving a new highscore when the user clicks submit
document.getElementById('saveScoreButton').addEventListener('click', () => {
const nameInput = document.getElementById('playerName');
// Get value, trim spaces, make uppercase.
// Fallback to 'ANONYMOUS' if left empty.
const name = nameInput.value.trim().toUpperCase() || 'ANONYMOUS';
// Push the new score object into the array
highscores.push({ name, score });
// Sort the array in descending order based on score
highscores.sort((a, b) => b.score - a.score);
// Enforce a maximum of 5 entries in the leaderboard
if (highscores.length > 5) {
highscores.pop();
}
// Hide the input form and show the try again button
document.getElementById('highscoreEntry').classList.add('hidden');
document.getElementById('restartButton').classList.remove('hidden');
// Clear the input box for the next session
nameInput.value = '';
// Re-render the visual list
renderLeaderboard();
});
// ==========================================
// 6. GAME FLOW CONTROLS
// ==========================================
// Triggers the end sequence when the bird crashes
2026-06-25 01:30:06 +01:00
function gameOver() {
gameState = 'gameover';
finalScoreDisplay.innerHTML = score;
2026-06-28 00:02:10 +01:00
// Check if the current score qualifies for the top 5
const isHighScore = score > 0 &&
(highscores.length < 5 || score > highscores[highscores.length - 1].score);
if (isHighScore) {
// Show input block, force user to save before restarting
document.getElementById('highscoreEntry').classList.remove('hidden');
document.getElementById('restartButton').classList.add('hidden');
} else {
// Skip input block, just show standard restart button
document.getElementById('highscoreEntry').classList.add('hidden');
document.getElementById('restartButton').classList.remove('hidden');
}
// Ensure leaderboard is updated visually
renderLeaderboard();
// Switch UI screens
2026-06-25 01:30:06 +01:00
gameOverScreen.classList.remove('hidden');
gameUI.classList.add('hidden');
}
2026-06-28 00:02:10 +01:00
// Resets variables to start a fresh run
2026-06-25 01:30:06 +01:00
function resetGame() {
2026-06-28 00:02:10 +01:00
score = 0;
2026-06-25 01:30:06 +01:00
bird.reset();
pipes.reset();
scoreDisplay.innerHTML = score;
}
2026-06-28 00:02:10 +01:00
// Begins the active game loop
2026-06-25 01:30:06 +01:00
function startGame() {
gameState = 'playing';
2026-06-28 00:02:10 +01:00
// Hide menus, show in-game overlay
2026-06-25 01:30:06 +01:00
startScreen.classList.add('hidden');
gameOverScreen.classList.add('hidden');
gameUI.classList.remove('hidden');
2026-06-28 00:02:10 +01:00
2026-06-25 01:30:06 +01:00
resetGame();
}
2026-06-28 00:02:10 +01:00
// ==========================================
// 7. CORE RENDER & UPDATE LOOP
// ==========================================
// Master draw function that executes in visual order (back to front)
2026-06-25 01:30:06 +01:00
function draw() {
drawBackground();
pipes.draw();
drawGround();
bird.draw();
}
2026-06-28 00:02:10 +01:00
// Master update function that processes physics
function update(dt) {
bird.update(dt);
pipes.update(dt);
2026-06-25 01:30:06 +01:00
}
2026-06-28 00:02:10 +01:00
// The main game engine loop triggered by requestAnimationFrame
function loop(timestamp) {
// Calculate delta time (dt) in seconds.
// This ensures smooth movement regardless of framerate.
let dt = (timestamp - lastTime) / 1000;
lastTime = timestamp;
// Cap delta time to prevent physics glitches if the tab is backgrounded
if (dt > 0.1) dt = 0.1;
2026-06-25 01:30:06 +01:00
2026-06-28 00:02:10 +01:00
update(dt);
draw();
// Recursively call the loop for the next frame
2026-06-25 01:30:06 +01:00
requestAnimationFrame(loop);
}
2026-06-28 00:02:10 +01:00
// ==========================================
// 8. INPUT LISTENERS
// ==========================================
// Wrapper function to process bird flapping
2026-06-25 01:30:06 +01:00
function flapBird() {
if (gameState === 'playing') {
bird.flap();
}
}
2026-06-28 00:02:10 +01:00
// Touchscreen input processing
2026-06-25 01:30:06 +01:00
canvas.addEventListener('touchstart', function(e) {
2026-06-28 00:02:10 +01:00
// Only prevent default zooming behavior during active gameplay.
// This allows the highscore form typing to work normally.
if (gameState === 'playing') {
e.preventDefault();
flapBird();
}
2026-06-25 01:30:06 +01:00
}, { passive: false });
2026-06-28 00:02:10 +01:00
// Mouse click input processing
2026-06-25 01:30:06 +01:00
canvas.addEventListener('mousedown', flapBird);
2026-06-28 00:02:10 +01:00
// Keyboard spacebar input processing
2026-06-25 01:30:06 +01:00
document.addEventListener('keydown', function(e) {
if (e.code === 'Space') {
2026-06-28 00:02:10 +01:00
// Prevent jumping if the user is currently typing their name
if (document.activeElement.id !== 'playerName') {
flapBird();
e.preventDefault();
}
2026-06-25 01:30:06 +01:00
}
});
2026-06-28 00:02:10 +01:00
// UI button bindings
2026-06-25 01:30:06 +01:00
startButton.addEventListener('click', startGame);
restartButton.addEventListener('click', startGame);
2026-06-28 00:02:10 +01:00
// Prevents double-tap zooming on iOS devices during active play
2026-06-25 01:30:06 +01:00
let lastTouchEnd = 0;
document.addEventListener('touchend', function(e) {
2026-06-28 00:02:10 +01:00
if (gameState === 'playing') {
const now = (new Date()).getTime();
if (now - lastTouchEnd <= 300) {
e.preventDefault();
}
lastTouchEnd = now;
2026-06-25 01:30:06 +01:00
}
}, { passive: false });
2026-06-28 00:02:10 +01:00
// Initializes the game loop upon loading the script
requestAnimationFrame((timestamp) => {
lastTime = timestamp;
requestAnimationFrame(loop);
});
2026-06-25 01:30:06 +01:00
</script>
</body>
</html>