/* ===================================================================== BUZZER BEATER — Sport on Earth Games arcade Single-file canvas game in the Family Arcade house style. EMBED (inline):
CONTROLS: tap the court, click, or press Space to release the shot. RULES: eight last-second scenarios — varying time, distance and defensive pressure. One shot each. A timing marker races back and forth across the bar while the scenario clock burns in real time; release with the marker inside the green to make it (dead center = perfect). If the clock hits zero before you shoot, the chance dies. SCORING: each scenario pays its difficulty — 2, 3 or 5 points. Max score 28. ===================================================================== */ (function () { "use strict"; /* ================= LEADERBOARD BACKEND — UPGRADE HOOK ================= Default: LOCAL board (localStorage top-10) so the game runs standalone. To switch to the shared family KV leaderboard later, set KV_ENABLED to true. The site must then serve the paper's functions/api/arcade.js with "buzzer-beater" added to its GAMES list and ARCADE_KV bound — the fetch calls below already mirror that API exactly: GET /api/arcade/leaderboard?game= -> { game, scores:[{manager,score,at}] } POST /api/arcade/score {game, manager, score} -> { ok, rank } No other code changes are needed. */ var KV_ENABLED = false; var GAME_ID = "buzzer-beater"; var LS_KEY = "fbiArcade." + GAME_ID; var LS_NAME = "fbiArcade.name"; /* Team Account identity (Batch 3.1, Task I, Oct 22, 2026): when a Team Account session is live, ACCT = { username, club } (from the accounts function's GET /api/accounts/me) and score saves go under that identity automatically — the visitor never re-types who they are. The board itself stays exactly what it has always been: this device's localStorage top-10 (KV_ENABLED above remains off — there is no /api/arcade backend on this site, and none is invented here). Guests keep the free-text name flow unchanged. */ var ACCT = null; function escapeHtml(s) { return String(s).replace(/[&<>"']/g, function (c) { return { "&": "&", "<": "<", ">": ">", '"': """, "'": "'" }[c]; }); } function localScores() { try { return JSON.parse(localStorage.getItem(LS_KEY)) || []; } catch (e) { return []; } } function renderBoard(el, scores) { if (!scores || !scores.length) { el.innerHTML = "
  • No scores yet — be the first!
  • "; return; } el.innerHTML = scores.slice(0, 10).map(function (s, i) { return "
  • #" + (i + 1) + " " + escapeHtml(s.manager) + (s.club ? " · " + escapeHtml(s.club) : "") + " — " + s.score + "
  • "; }).join(""); } function loadBoard(el) { if (!KV_ENABLED) { renderBoard(el, localScores()); return; } fetch("/api/arcade/leaderboard?game=" + encodeURIComponent(GAME_ID)) .then(function (r) { return r.json(); }) .then(function (d) { renderBoard(el, d.scores || []); }) .catch(function () { el.innerHTML = "
  • Could not load scores.
  • "; }); } function saveScore(name, score, msgEl, boardEl) { if (ACCT) name = ACCT.username; name = (name || "").trim(); if (!name) { msgEl.textContent = "Enter your name first!"; return; } try { localStorage.setItem(LS_NAME, name); } catch (e) {} if (!KV_ENABLED) { var scores = localScores(); var entry = { manager: name.slice(0, 40), score: Math.floor(score), at: new Date().toISOString() }; if (ACCT) { entry.club = ACCT.club || ""; entry.account = true; } scores.push(entry); scores.sort(function (a, b) { return b.score - a.score; }); scores = scores.slice(0, 10); try { localStorage.setItem(LS_KEY, JSON.stringify(scores)); } catch (e) {} var rank = scores.indexOf(entry) + 1; msgEl.textContent = rank ? ("Saved! You rank #" + rank + " on this device's board.") : "Score saved!"; renderBoard(boardEl, scores); return; } fetch("/api/arcade/score", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ game: GAME_ID, manager: name, club: ACCT ? (ACCT.club || "") : "", score: Math.floor(score) }) }).then(function (r) { return r.json(); }) .then(function (d) { msgEl.textContent = d.rank ? ("Saved! You rank #" + d.rank + " on the family board.") : "Score saved!"; loadBoard(boardEl); }) .catch(function () { msgEl.textContent = "Could not save score."; }); } /* ================= SHARED ARCADE CSS (injected once per page) ================= */ if (!document.getElementById("hoopsArcadeCss")) { var st = document.createElement("style"); st.id = "hoopsArcadeCss"; st.textContent = ".hoops-arcade{max-width:440px;margin:0 auto;font-family:inherit;-webkit-user-select:none;user-select:none;-webkit-touch-callout:none}" + ".hoops-arcade *{-webkit-user-select:none;user-select:none;-webkit-touch-callout:none}" + ".hoops-arcade input,.hoops-arcade select{-webkit-user-select:text;user-select:text;-webkit-touch-callout:default}" + ".hoops-arcade .arcade-canvas{display:block;width:100%;max-width:400px;height:auto;margin:0 auto;background:#8a5a2b;border:3px solid var(--line,#d8cfc0);border-radius:8px;touch-action:none;-webkit-user-select:none;user-select:none;-webkit-touch-callout:none}" + ".hoops-arcade .arcade-ui{text-align:center;margin:12px 0}" + ".hoops-arcade .arcade-score{font-size:.85rem;margin-bottom:8px}" + ".hoops-arcade .arcade-score b{color:var(--red,#c8102e)}" + ".hoops-arcade .arcade-btn{background:var(--red,#c8102e);color:#fff;border:none;border-radius:6px;padding:10px 24px;font-size:1rem;font-weight:700;cursor:pointer}" + ".hoops-arcade .arcade-btn.small{padding:6px 12px;font-size:.8rem}" + ".hoops-arcade .arcade-btn:disabled{opacity:.5;cursor:default}" + ".hoops-arcade .arcade-msg{min-height:2.4em;font-size:.8rem;color:var(--muted,#666);margin:8px 0}" + ".hoops-arcade .arcade-name{display:flex;gap:8px;justify-content:center;margin:8px 0}" + ".hoops-arcade .arcade-name input{padding:8px;border:1px solid var(--line,#d8cfc0);border-radius:6px;font-size:.9rem;max-width:220px}" + ".hoops-arcade .arcade-board{margin-top:16px}" + ".hoops-arcade .arcade-board h3{font-size:1rem;margin-bottom:8px}" + ".hoops-arcade .arcade-board ol{list-style-position:inside;padding:0}" + ".hoops-arcade .arcade-board li{padding:4px 0;border-bottom:1px solid var(--line,#d8cfc0);font-size:.85rem}"; document.head.appendChild(st); } /* Long-press fix (iOS copy callout on held controls): kill the context menu anywhere inside an arcade mount. Installed once per page. */ if (!window.__hoopsNoCallout) { window.__hoopsNoCallout = true; document.addEventListener("contextmenu", function (e) { var t = e.target; if (t && t.closest && t.closest(".hoops-arcade")) e.preventDefault(); }); } /* ================= MOUNT ================= */ var mount = document.querySelector('[data-hoops-game="' + GAME_ID + '"]'); if (!mount) { mount = document.createElement("div"); mount.setAttribute("data-hoops-game", GAME_ID); var cs = document.currentScript; if (cs && cs.parentNode) cs.parentNode.insertBefore(mount, cs); else document.body.appendChild(mount); } mount.classList.add("hoops-arcade"); mount.innerHTML = '' + '
    ' + '
    Score: 0 • Scenario: –/8 • This look: –
    ' + '
    ' + '
    Eight endings, one shot each. Release inside the green before the scenario clock dies!
    ' + '
    ' + '
    ' + "
    " + '

    Leaderboard — Buzzer Beater

    1. Loading…
    '; var cv = mount.querySelector("canvas"), ctx = cv.getContext("2d"); var el = { score: mount.querySelector(".sc"), round: mount.querySelector(".round"), pts: mount.querySelector(".pts"), msg: mount.querySelector(".arcade-msg"), start: mount.querySelector(".start"), name: mount.querySelector(".arcade-name input"), save: mount.querySelector(".save"), board: mount.querySelector(".arcade-board ol") }; try { el.name.value = localStorage.getItem(LS_NAME) || ""; } catch (e) {} /* Team Account session check (Task I): non-blocking; any failure simply leaves the guest flow exactly as it was. */ try { fetch("/api/accounts/me", { credentials: "same-origin" }) .then(function (r) { if (!r.ok) throw 0; return r.json(); }) .then(function (d) { if (!d || !d.ok || !d.username) return; ACCT = { username: d.username, club: d.club || "" }; el.name.value = d.username; el.name.readOnly = true; el.name.title = "Signed in — your scores save under your team account"; var hint = document.createElement("div"); hint.className = "arcade-msg acct-hint"; hint.textContent = "🏀 Signed in as " + d.username + (d.club ? " · " + d.club : "") + " — your high scores save under your account, no typing needed."; el.name.parentNode.parentNode.insertBefore(hint, el.name.parentNode); }) .catch(function () {}); } catch (e) {} /* ================= GAME ================= */ var W = 400, H = 480; var HOOP = { x: 200, y: 128 }; var BAR = { x0: 50, x1: 350, y: 398 }; // t: scenario clock (real seconds) · pts: payout · green: half-width px // gc: green-zone center offset px · speed: marker px/s · def: defender 0 open / 1 close / 2 draped var SCEN = [ { t: 6.0, pts: 2, green: 42, gc: 0, speed: 240, def: 0, title: "DOWN 1 · 6.0 SECONDS", sub: "Free-throw-line jumper · no defender" }, { t: 5.0, pts: 2, green: 38, gc: -26, speed: 280, def: 1, title: "TIED · 5.0 SECONDS", sub: "Elbow jumper · late closeout" }, { t: 4.2, pts: 3, green: 34, gc: 30, speed: 320, def: 1, title: "DOWN 2 · 4.2 SECONDS", sub: "Corner three · defender closing" }, { t: 3.8, pts: 3, green: 30, gc: -44, speed: 360, def: 2, title: "DOWN 3 · 3.8 SECONDS", sub: "Wing three · hand in the face" }, { t: 3.2, pts: 3, green: 27, gc: 48, speed: 400, def: 2, title: "DOWN 2 · 3.2 SECONDS", sub: "Step-back three · tight coverage" }, { t: 2.6, pts: 5, green: 24, gc: -58, speed: 440, def: 2, title: "DOWN 4 · 2.6 SECONDS", sub: "Deep three · double team" }, { t: 2.0, pts: 5, green: 21, gc: 64, speed: 490, def: 2, title: "DOWN 3 · 2.0 SECONDS", sub: "Heave from the logo · draped" }, { t: 1.4, pts: 5, green: 18, gc: 0, speed: 540, def: 2, title: "DOWN 2 · 1.4 SECONDS", sub: "Catch-and-shoot for the win · all over him" } ]; var MAXSCORE = SCEN.reduce(function (a, s) { return a + s.pts; }, 0); var state = "idle"; // idle | playing | done var round = 0, score = 0, made = 0; var results = []; // "make" | "miss" | "noshot" per scenario var clock = 0, marker = BAR.x0, markerDir = 1, bannerT = 0; var ball = null, floaters = [], netJiggle = 0, pauseT = 0, awaiting = false; function scen() { return SCEN[Math.min(round, SCEN.length - 1)]; } function verdict() { if (score >= 24) return "🐐 CLUTCH GOD — the moment isn't big enough."; if (score >= 17) return "❄️ ICE IN THE VEINS"; if (score >= 11) return "🎯 Big-shot maker"; if (score >= 6) return "Role player, big heart"; return "The moment was big tonight."; } function reset() { state = "idle"; round = 0; score = 0; made = 0; results = []; clock = 0; marker = BAR.x0; markerDir = 1; bannerT = 0; ball = null; floaters = []; pauseT = 0; awaiting = false; updateUI(); } function updateUI() { el.score.textContent = score; el.round.textContent = (state === "idle" ? "–" : Math.min(8, round + 1)) + "/8"; el.pts.textContent = state === "idle" ? "–" : (scen().pts + " pts"); } function newScenario() { var s = scen(); clock = s.t; marker = BAR.x0; markerDir = 1; bannerT = 1.7; awaiting = true; pauseT = 0; el.msg.textContent = s.title + " — " + s.sub + ". Worth " + s.pts + ". Release in the green!"; updateUI(); } function startGame() { reset(); state = "playing"; el.start.textContent = "Restart"; newScenario(); } function release() { if (state !== "playing" || !awaiting || ball || pauseT > 0) return; awaiting = false; var s = scen(), center = 200 + s.gc; var d = Math.abs(marker - center); var kind = d <= 4 ? "perfect" : d <= s.green ? "make" : d <= s.green + 13 ? "rimout" : "miss"; var tx = HOOP.x, ty = HOOP.y + 6; if (kind === "rimout") tx += marker < center ? -13 : 13; if (kind === "miss") { tx += marker < center ? -52 : 52; ty += 18; } ball = { x0: 200, y0: 336, x1: tx, y1: ty, cx: (200 + tx) / 2, cy: Math.min(336, ty) - 130, t: 0, kind: kind, bouncing: false, bx: 0, by: 0, bvx: 0, bvy: 0, bt: 0 }; } function resolveShot() { var s = scen(), kind = ball.kind; if (kind === "perfect" || kind === "make") { score += s.pts; made++; netJiggle = 14; results.push("make"); floaters.push({ x: HOOP.x, y: HOOP.y + 38, text: kind === "perfect" ? "AT THE BUZZER! +" + s.pts : "BUZZER BEATER! +" + s.pts, life: 1 }); el.msg.textContent = kind === "perfect" ? "🎯 DEAD CENTER — IT COUNTS! +" + s.pts + " points!" : "IT'S GOOD AT THE BUZZER! +" + s.pts + " points!"; } else { results.push("miss"); floaters.push({ x: HOOP.x, y: HOOP.y + 38, text: kind === "rimout" ? "Off the iron…" : "No good…", life: 1 }); el.msg.textContent = kind === "rimout" ? "Off the rim as the horn sounds. It doesn't fall." : (s.def === 2 ? "Smothered — the look never had a chance." : "Airball at the horn. The crowd groans."); } updateUI(); } function noShot() { awaiting = false; results.push("noshot"); floaters.push({ x: 200, y: 250, text: "NO SHOT — BUZZER!", life: 1 }); el.msg.textContent = "🚨 The clock hit zero — no shot! The chance dies."; updateUI(); pauseT = 1.0; } function advance() { if (round >= SCEN.length - 1) { state = "done"; awaiting = false; el.msg.textContent = "That's all eight endings — final score " + score + " of " + MAXSCORE + ". Save it to the board!"; updateUI(); return; } round++; newScenario(); } function drawCourt() { ctx.fillStyle = "#5b3a17"; ctx.fillRect(0, 0, W, 66); ctx.fillStyle = "rgba(255,255,255,.13)"; for (var i = 0; i < 30; i++) ctx.fillRect((i * 41) % W, 10 + (i * 11) % 42, 5, 5); ctx.fillStyle = "#b57e3e"; ctx.fillRect(0, 66, W, H - 66); ctx.fillStyle = "rgba(0,0,0,.05)"; for (var y = 96; y < H; y += 44) ctx.fillRect(0, y, W, 2); ctx.fillStyle = "rgba(200,16,46,.20)"; ctx.fillRect(HOOP.x - 54, 66, 108, 218); ctx.strokeStyle = "rgba(255,255,255,.75)"; ctx.lineWidth = 2; ctx.strokeRect(HOOP.x - 54, 66, 108, 218); ctx.beginPath(); ctx.moveTo(0, 356); ctx.lineTo(W, 356); ctx.stroke(); ctx.fillStyle = "#f4f4f4"; ctx.fillRect(HOOP.x - 36, HOOP.y - 34, 72, 28); ctx.strokeStyle = "#c8102e"; ctx.lineWidth = 2; ctx.strokeRect(HOOP.x - 36, HOOP.y - 34, 72, 28); ctx.strokeRect(HOOP.x - 11, HOOP.y - 23, 22, 15); ctx.strokeStyle = "#e25822"; ctx.lineWidth = 4; ctx.beginPath(); ctx.moveTo(HOOP.x - 16, HOOP.y); ctx.lineTo(HOOP.x + 16, HOOP.y); ctx.stroke(); ctx.strokeStyle = "rgba(255,255,255,.9)"; ctx.lineWidth = 1.5; var jig = netJiggle > 0 ? Math.sin(netJiggle * 1.4) * 3 : 0; for (var i = -1; i <= 1; i++) { ctx.beginPath(); ctx.moveTo(HOOP.x + i * 14, HOOP.y + 2); ctx.lineTo(HOOP.x + i * 7 + jig, HOOP.y + 26); ctx.stroke(); } } function drawPlayersAndBar() { // Big scenario clock if (state !== "idle") { ctx.fillStyle = clock <= 2 && awaiting ? "#ff5a4e" : "#fff"; ctx.font = "bold 30px sans-serif"; ctx.textAlign = "left"; ctx.fillText(Math.max(0, clock).toFixed(1), 12, 40); ctx.font = "11px sans-serif"; ctx.fillStyle = "rgba(255,255,255,.85)"; ctx.textAlign = "right"; ctx.fillText("SCENARIO " + Math.min(8, round + 1) + " OF 8", W - 12, 40); } // Shooter + defender var s = state === "idle" ? null : scen(); var Fig = window.HoopsFig || null; if (Fig) { var apB = Fig.appearance('bb-shooter'); Fig.draw(ctx, { x: 200, y: 388, s: 0.85, pose: 'shoot', t: Date.now() / 1000, ball: (!ball && state !== 'done'), jersey: '#14418f', num: '23', skin: apB.skin, hair: apB.hair, hairColor: apB.hairColor }); } else { ctx.fillStyle = "rgba(0,0,0,.2)"; ctx.beginPath(); ctx.ellipse(200, 388, 17, 5, 0, 0, 7); ctx.fill(); ctx.fillStyle = "#14418f"; ctx.beginPath(); ctx.arc(200, 372, 13, 0, 7); ctx.fill(); ctx.fillStyle = "#f2c79b"; ctx.beginPath(); ctx.arc(200, 351, 7, 0, 7); ctx.fill(); if (!ball && state !== "done") { ctx.fillStyle = "#e25822"; ctx.beginPath(); ctx.arc(200, 336, 8, 0, 7); ctx.fill(); ctx.strokeStyle = "rgba(0,0,0,.35)"; ctx.lineWidth = 1; ctx.beginPath(); ctx.arc(200, 336, 8, 0, 7); ctx.stroke(); } } if (s && s.def > 0) { var dx = 200 + (s.def === 1 ? 46 : 26), bob = Math.sin(Date.now() / 180) * 2; if (Fig) { var apBD = Fig.appearance('bb-defender'); Fig.draw(ctx, { x: dx, y: 388, s: 0.85, pose: 'defend', t: Date.now() / 1000, ball: false, jersey: '#7a1f1f', num: '5', flip: true, skin: apBD.skin, hair: apBD.hair, hairColor: apBD.hairColor }); } else { ctx.fillStyle = "rgba(0,0,0,.2)"; ctx.beginPath(); ctx.ellipse(dx, 388, 15, 4, 0, 0, 7); ctx.fill(); ctx.fillStyle = "#7a1f1f"; ctx.beginPath(); ctx.arc(dx, 372 + bob, 12, 0, 7); ctx.fill(); ctx.fillStyle = "#8a5a3b"; ctx.beginPath(); ctx.arc(dx, 352 + bob, 6.5, 0, 7); ctx.fill(); } } if (state === "idle") return; // Timing bar ctx.fillStyle = "rgba(0,0,0,.45)"; ctx.fillRect(BAR.x0, BAR.y, BAR.x1 - BAR.x0, 16); var center = 200 + s.gc; ctx.fillStyle = "#2ecc40"; ctx.fillRect(center - s.green, BAR.y, s.green * 2, 16); ctx.fillStyle = "#ffd700"; ctx.fillRect(center - 4, BAR.y, 8, 16); ctx.strokeStyle = "rgba(255,255,255,.7)"; ctx.lineWidth = 1.5; ctx.strokeRect(BAR.x0, BAR.y, BAR.x1 - BAR.x0, 16); // Marker var inGreen = Math.abs(marker - center) <= s.green; ctx.fillStyle = awaiting ? (inGreen ? "#2ecc40" : "#ffd700") : "#fff"; ctx.beginPath(); ctx.moveTo(marker, BAR.y - 10); ctx.lineTo(marker - 6, BAR.y - 20); ctx.lineTo(marker + 6, BAR.y - 20); ctx.closePath(); ctx.fill(); ctx.fillRect(marker - 1.5, BAR.y - 10, 3, 26); // Scenario pips for (var i = 0; i < SCEN.length; i++) { ctx.fillStyle = results[i] === "make" ? "#2ecc40" : results[i] ? "#c8102e" : i === round && state === "playing" ? "#ffd700" : "rgba(255,255,255,.35)"; ctx.beginPath(); ctx.arc(146 + i * 16, 462, 5, 0, 7); ctx.fill(); } // Scenario banner if (bannerT > 0 && state === "playing") { ctx.fillStyle = "rgba(0,0,0,.68)"; ctx.fillRect(0, 196, W, 96); ctx.fillStyle = "#ffd700"; ctx.font = "bold 21px sans-serif"; ctx.textAlign = "center"; ctx.fillText(s.title, W / 2, 234); ctx.fillStyle = "#fff"; ctx.font = "14px sans-serif"; ctx.fillText(s.sub, W / 2, 260); } } function draw() { ctx.clearRect(0, 0, W, H); drawCourt(); drawPlayersAndBar(); if (ball) { var bx, by; if (!ball.bouncing) { var t = ball.t; bx = (1 - t) * (1 - t) * ball.x0 + 2 * (1 - t) * t * ball.cx + t * t * ball.x1; by = (1 - t) * (1 - t) * ball.y0 + 2 * (1 - t) * t * ball.cy + t * t * ball.y1; } else { bx = ball.bx; by = ball.by; } ctx.fillStyle = "#e25822"; ctx.beginPath(); ctx.arc(bx, by, 9, 0, 7); ctx.fill(); ctx.strokeStyle = "rgba(0,0,0,.4)"; ctx.lineWidth = 1; ctx.beginPath(); ctx.arc(bx, by, 9, 0, 7); ctx.stroke(); } floaters.forEach(function (f) { ctx.globalAlpha = Math.max(0, f.life); ctx.fillStyle = "#ffd700"; ctx.font = "bold 17px sans-serif"; ctx.textAlign = "center"; ctx.fillText(f.text, f.x, f.y); ctx.globalAlpha = 1; }); if (state === "idle") { ctx.fillStyle = "rgba(0,0,0,.55)"; ctx.fillRect(0, 196, W, 92); ctx.fillStyle = "#fff"; ctx.font = "bold 23px sans-serif"; ctx.textAlign = "center"; ctx.fillText("⏱️ BUZZER BEATER", W / 2, 232); ctx.font = "14px sans-serif"; ctx.fillText("Eight endings. One shot each. No timeouts.", W / 2, 256); } if (state === "done") { ctx.fillStyle = "rgba(0,0,0,.72)"; ctx.fillRect(0, 172, W, 140); ctx.fillStyle = "#fff"; ctx.font = "bold 24px sans-serif"; ctx.textAlign = "center"; ctx.fillText("🏁 The horn sounds", W / 2, 208); ctx.font = "16px sans-serif"; ctx.fillText("Final score: " + score + " / " + MAXSCORE + " • " + made + "/8 makes", W / 2, 236); ctx.font = "bold 15px sans-serif"; ctx.fillStyle = "#ffd700"; ctx.fillText(verdict(), W / 2, 262); ctx.font = "13px sans-serif"; ctx.fillStyle = "#fff"; ctx.fillText("Enter your name below and Save Score!", W / 2, 288); } } var lastFrame = Date.now(); function loop() { var now = Date.now(); var dt = Math.min(0.05, (now - lastFrame) / 1000); lastFrame = now; if (state === "playing") { if (bannerT > 0) bannerT -= dt; if (pauseT > 0) { pauseT -= dt; if (pauseT <= 0 && !ball) advance(); } else if (awaiting && !ball) { clock -= dt; marker += markerDir * scen().speed * dt; if (marker >= BAR.x1) { marker = BAR.x1; markerDir = -1; } if (marker <= BAR.x0) { marker = BAR.x0; markerDir = 1; } if (clock <= 0) { clock = 0; noShot(); } } if (ball) { if (!ball.bouncing) { ball.t += dt / 0.55; if (ball.t >= 1) { var wasMake = ball.kind === "perfect" || ball.kind === "make"; resolveShot(); if (wasMake) { ball = null; pauseT = 0.9; } else { ball.bouncing = true; ball.bx = ball.x1; ball.by = ball.y1; ball.bvx = (ball.x1 < HOOP.x ? -1 : 1) * (40 + Math.random() * 50); ball.bvy = -110; ball.bt = 0; } } } else { ball.bvy += 700 * dt; ball.bx += ball.bvx * dt; ball.by += ball.bvy * dt; ball.bt += dt; if (ball.bt > 0.45 || ball.by > H - 8) { ball = null; pauseT = 0.6; } } } if (netJiggle > 0) netJiggle--; floaters.forEach(function (f) { f.life -= dt * 0.9; f.y -= 26 * dt; }); floaters = floaters.filter(function (f) { return f.life > 0; }); if ((loop.frame = (loop.frame || 0) + 1) % 15 === 0) updateUI(); } draw(); requestAnimationFrame(loop); } /* ================= INPUT ================= Space only shoots while the scenarios are live — with five games on one page, an idle-Space auto-start in every game would fire them all at once. */ /* Touch overhaul: flick/swipe on the court to shoot — a press that travels 26px+ releases the shot mid-gesture, the way a finger actually shoots. A plain tap still works: it releases on lift. */ var flickStart = null, flickFired = false; cv.addEventListener("pointerdown", function (e) { e.preventDefault(); if (state !== "playing") return; flickStart = { x: e.clientX, y: e.clientY }; flickFired = false; }); cv.addEventListener("pointermove", function (e) { if (!flickStart || flickFired || state !== "playing") return; var dx = e.clientX - flickStart.x, dy = e.clientY - flickStart.y; if (dx * dx + dy * dy >= 26 * 26) { flickFired = true; release(); } }); function flickEnd() { if (flickStart && !flickFired && state === "playing") release(); flickStart = null; } cv.addEventListener("pointerup", flickEnd); cv.addEventListener("pointercancel", function () { flickStart = null; }); window.addEventListener("keydown", function (e) { if (e.code === "Space") { if (document.activeElement && /INPUT|BUTTON/.test(document.activeElement.tagName)) return; e.preventDefault(); if (e.repeat) return; if (state === "playing") release(); } }); el.start.addEventListener("click", startGame); el.save.addEventListener("click", function () { saveScore(el.name.value, score, el.msg, el.board); }); loadBoard(el.board); reset(); draw(); loop(); })();