Files
dev.urandom/js-submission.html
2026-10-15 11:23:08 -04:00

243 lines
7.2 KiB
HTML

<!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>
* { box-sizing: border-box; }
html, body {
margin: 0;
min-height: 100%;
font-family: system-ui, -apple-system, Segoe UI, sans-serif;
background: #000;
color: #fff;
}
body {
min-height: 100vh;
display: grid;
place-items: center;
padding: 18px;
}
.game {
position: relative;
overflow: hidden;
border: 4px solid #f7f2d5;
background: #70c5ce;
aspect-ratio: 2 / 3;
width: 100%;
touch-action: manipulation;
}
canvas { display: block; width: 100%; height: 100%; }
.overlay {
position: absolute;
inset: 0;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
gap: 12px;
padding: 24px;
background: #13243b44;
}
.overlay.hidden { display: none; }
button {
border: 0;
border-radius: 999px;
padding: 12px 25px;
background: #ffcf43;
color: #493b12;
font: inherit;
font-weight: 800;
cursor: pointer;
}
button:active { transform: translateY(2px); box-shadow: 0 2px 0 #c48c22; }
.help { margin: 12px 0 0; color: #cbd5e1; font-size: .92rem; }
</style>
</head>
<body>
<main class="game-wrap">
<h1>Flappy Bird</h1>
<div class="game" id="game">
<canvas id="canvas" width="400" height="600"></canvas>
<div class="overlay" id="overlay">
<h2 id="overlayTitle">Flappy Bird</h2>
<p id="overlayText"></p>
<button id="startButton">Start Game</button>
</div>
</div>
</main>
<script>
(() => {
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
const overlay = document.getElementById("overlay");
const overlayTitle = document.getElementById("overlayTitle");
const overlayText = document.getElementById("overlayText");
const startButton = document.getElementById("startButton");
const W = canvas.width, H = canvas.height;
const groundH = 70;
const bird = { x: 105, y: 260, r: 15, vy: 0, rotation: 0 };
const pipeW = 66, pipeGap = 165, pipeSpeed = 2.6, pipeEvery = 94;
let pipes = [], score = 0, best = 0, frame = 0;
let state = "ready";
let groundOffset = 0;
function reset() {
bird.y = 260; bird.vy = 0; bird.rotation = 0;
pipes = []; score = 0; frame = 0; groundOffset = 0;
state = "playing";
overlay.classList.add("hidden");
}
function flap() {
if (state === "ready" || state === "over") {
reset();
bird.vy = -6.3;
return;
}
if (state === "playing") bird.vy = -6.3;
}
function endGame() {
if (state !== "playing") return;
state = "over";
best = Math.max(best, score);
overlayTitle.textContent = "Game Over";
overlayText.innerHTML = `Score: <strong>${score}</strong> &nbsp; Best: <strong>${best}</strong>`;
startButton.textContent = "Play Again";
overlay.classList.remove("hidden");
}
function addPipe() {
const margin = 55;
const top = margin + Math.random() * (H - groundH - pipeGap - margin * 2);
pipes.push({ x: W + 10, top, scored: false });
}
function drawBackground() {
const sky = ctx.createLinearGradient(0, 0, 0, H);
sky.addColorStop(0, "#70c5ce");
sky.addColorStop(1, "#b8ecdb");
ctx.fillStyle = sky;
ctx.fillRect(0, 0, W, H);
}
function drawPipe(p) {
const bottomY = p.top + pipeGap;
ctx.fillStyle = "#0f0";
ctx.strokeStyle = "#246c2a";
ctx.lineWidth = 3;
ctx.fillRect(p.x, 0, pipeW, p.top - 18);
ctx.strokeRect(p.x, -2, pipeW, p.top - 16);
ctx.fillRect(p.x - 5, p.top - 22, pipeW + 10, 22);
ctx.strokeRect(p.x - 5, p.top - 22, pipeW + 10, 22);
ctx.fillRect(p.x, bottomY + 18, pipeW, H - groundH - bottomY);
ctx.strokeRect(p.x, bottomY + 18, pipeW, H - groundH - bottomY);
ctx.fillRect(p.x - 5, bottomY, pipeW + 10, 22);
ctx.strokeRect(p.x - 5, bottomY, pipeW + 10, 22);
}
function drawBird() {
ctx.save();
ctx.translate(bird.x, bird.y);
ctx.fillStyle = "#ffdc45";
ctx.strokeStyle = "#c88a21";
ctx.lineWidth = 2;
ctx.beginPath();
ctx.ellipse(0, 0, bird.r + 2, bird.r, 0, 0, Math.PI * 2);
ctx.fill(); ctx.stroke();
ctx.restore();
}
function drawGround() {
ctx.fillStyle = "#ded895";
ctx.fillRect(0, H - groundH, W, groundH);
ctx.fillStyle = "#83c94c";
ctx.fillRect(0, H - groundH, W, 13);
ctx.fillStyle = "#b5a765";
ctx.fillRect(0, H - groundH + 13, W, 4);
}
function update() {
frame++;
if (state === "playing") {
bird.vy += 0.34;
bird.y += bird.vy;
bird.rotation = Math.min(Math.PI / 2.4, Math.max(-0.45, bird.vy * 0.07));
groundOffset = (groundOffset - pipeSpeed) % 32;
if (frame % pipeEvery === 0) addPipe();
for (const p of pipes) {
p.x -= pipeSpeed;
if (!p.scored && p.x + pipeW < bird.x) {
p.scored = true;
score++;
}
const overlapsX = bird.x + bird.r - 3 > p.x && bird.x - bird.r + 3 < p.x + pipeW;
if (overlapsX && (bird.y - bird.r + 3 < p.top || bird.y + bird.r - 3 > p.top + pipeGap)) {
endGame();
}
}
pipes = pipes.filter(p => p.x + pipeW + 10 > 0);
if (bird.y - bird.r < 0 || bird.y + bird.r > H - groundH) endGame();
}
}
function draw() {
drawBackground();
pipes.forEach(drawPipe);
drawGround();
drawBird();
if (state === "playing") {
ctx.textAlign = "center";
ctx.font = "bold 42px system-ui, sans-serif";
ctx.lineWidth = 4;
ctx.strokeStyle = "#2b6248";
ctx.fillStyle = "#fff";
ctx.strokeText(score, W / 2, 65);
ctx.fillText(score, W / 2, 65);
} else if (state === "ready") {
ctx.textAlign = "center";
ctx.font = "bold 19px system-ui, sans-serif";
ctx.fillStyle = "#fff";
ctx.strokeStyle = "#356c68";
ctx.lineWidth = 3;
ctx.strokeText("Tap or press Space to fly!", W / 2, 355);
ctx.fillText("Tap or press Space to fly!", W / 2, 355);
}
}
function loop() {
update();
draw();
requestAnimationFrame(loop);
}
startButton.addEventListener("click", () => {
reset();
bird.vy = -6.3;
});
canvas.addEventListener("pointerdown", (e) => {
e.preventDefault();
flap();
});
document.addEventListener("keydown", (e) => {
if (e.code === "Space" || e.code === "ArrowUp") {
e.preventDefault();
if (!e.repeat) flap();
}
});
loop();
})();
</script>
</body>
</html>