Public: anyone can read this; the crew can change it.
Migrated from github.com/amirmushichge/pixel-plays
Add Bird Watch: the playable page and its place in the catalogue, on the rules in `bird-watch/watch.js` #230
vesper/pixel-plays:vesper/181-add-bird-watch-the-playable-page-and-its → main 6 commits fork
9 files
changed +542
−2
README.md +3 −2
Diff not shown — a line 2,056 characters wide (limit 2,000). +3 −2
View filebird-watch/index.html added +434
| @@ -0,0 +1,434 @@ | ||
| 1 | ||
| 2 | <html lang="en"> | |
| 3 | <head> | |
| 4 | <meta charset="utf-8"> | |
| 5 | <meta name="viewport" content="width=device-width,initial-scale=1,viewport-fit=cover"> | |
| 6 | <title>Bird Watch</title> | |
| 7 | <!-- og:begin --> | |
| 8 | <meta name="description" content="Remember where each bird perched"> | |
| 9 | <link rel="canonical" href="https://pixel-plays--forge-trevato-apps.open-platform.sh/bird-watch/"> | |
| 10 | <meta property="og:type" content="website"> | |
| 11 | <meta property="og:site_name" content="Pixel Plays"> | |
| 12 | <meta property="og:title" content="Bird Watch"> | |
| 13 | <meta property="og:description" content="Remember where each bird perched"> | |
| 14 | <meta property="og:url" content="https://pixel-plays--forge-trevato-apps.open-platform.sh/bird-watch/"> | |
| 15 | <meta property="og:image" content="https://pixel-plays--forge-trevato-apps.open-platform.sh/og/bird-watch.png"> | |
| 16 | <meta property="og:image:type" content="image/png"> | |
| 17 | <meta property="og:image:width" content="1200"> | |
| 18 | <meta property="og:image:height" content="630"> | |
| 19 | <meta property="og:image:alt" content="Bird Watch: the opening scene of the game"> | |
| 20 | <meta name="twitter:card" content="summary_large_image"> | |
| 21 | <!-- og:end --> | |
| 22 | <style> | |
| 23 | :root { : #1c1b19; : #6b675f; : #fff; : #6b8f3a; } | |
| 24 | * { box-sizing: border-box; } | |
| 25 | html, body { -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; -webkit-tap-highlight-color: transparent; } | |
| 26 | html, body { height: 100%; margin: 0; background: var(); color: var(); } | |
| 27 | body { font: 13px/1.35 ui-monospace, "Cascadia Mono", Consolas, Menlo, monospace; overflow: hidden; } | |
| 28 | #stage { position: fixed; inset: 0; display: grid; place-items: center; } | |
| 29 | canvas { display: block; image-rendering: pixelated; touch-action: none; cursor: pointer; outline: none; } | |
| 30 | .hud { position: fixed; left: max(16px, env(safe-area-inset-left)); right: max(16px, env(safe-area-inset-right)); display: flex; justify-content: space-between; align-items: flex-end; gap: 16px; pointer-events: none; } | |
| 31 | #scores { position: fixed; top: max(14px, env(safe-area-inset-top)); left: max(16px, env(safe-area-inset-left)); display: grid; gap: 2px; pointer-events: none; } | |
| 32 | #bottom { bottom: max(20px, env(safe-area-inset-bottom)); } | |
| 33 | .row { display: flex; align-items: center; gap: 8px; color: var(); } | |
| 34 | .row b { font-size: 16px; font-weight: 600; min-width: 1ch; font-variant-numeric: tabular-nums; } | |
| 35 | .row span { color: var(); } | |
| 36 | #status { color: var(); min-height: 1.35em; } | |
| 37 | button { pointer-events: auto; font: inherit; color: var(); background: none; border: 0; padding: 13px 10px; margin: -13px -10px; min-height: 44px; cursor: pointer; } | |
| 38 | button:hover, button:focus-visible { color: var(); } | |
| 39 | button:focus-visible { outline: 2px solid var(); outline-offset: 2px; } | |
| 40 | noscript p { position: fixed; inset: 40% 16px auto; margin: 0; text-align: center; color: var(); } | |
| 41 | </style> | |
| 42 | </head> | |
| 43 | <body> | |
| 44 | <div id="stage"><canvas id="c" tabindex="0" aria-label="A garden with an oak, a fence, a shed roof and a bird feeder. Birds land on the perches for a moment and fly off; then the game names a bird. Tap the perch where it sat, or move with the arrow keys and press Space to choose. Five rounds, three pairs of binoculars; each species has its own shape."></canvas></div> | |
| 45 | <noscript><p>Bird Watch needs JavaScript to play.</p></noscript> | |
| 46 | <div id="scores"> | |
| 47 | <div class="row"><b id="round">1</b><span>round of 5</span></div> | |
| 48 | <div class="row"><b id="right">0</b><span>answers right</span></div> | |
| 49 | <div class="row"><b id="glasses">3</b><span>binoculars left</span></div> | |
| 50 | </div> | |
| 51 | <div class="hud" id="bottom"> | |
| 52 | <div id="status" role="status" aria-live="polite"></div> | |
| 53 | <button id="mute" type="button" aria-pressed="false">sound on</button> | |
| 54 | </div> | |
| 55 | <script src="../shell.js"></script> | |
| 56 | <script> | |
| 57 | PixelPlays.mount({ | |
| 58 | id: 'bird-watch', | |
| 59 | rules: [ | |
| 60 | 'Watch the birds land on the perches, then fly off.', | |
| 61 | 'The game names a bird: tap the perch where it sat, or use arrows and Space.', | |
| 62 | 'A wrong perch costs a pair of binoculars; you have 3.', | |
| 63 | 'Clear 4 of the 5 rounds to win.', | |
| 64 | ], | |
| 65 | }); | |
| 66 | </script> | |
| 67 | <script>window.PP_SCENE = { originY: 14, shadowX: 1.3, shadowY: -.12 };</script> | |
| 68 | <script src="../engine.js"></script> | |
| 69 | <script src="watch.js"></script> | |
| 70 | <script> | |
| 71 | 'use strict'; | |
| 72 | ||
| 73 | /* ---------- the garden ---------- */ | |
| 74 | const OAK = { x: -6.5, y: -3, scale: 1.4 }; | |
| 75 | const SHED = { x0: 1.2, x1: 9.4, y0: -7.4, y1: -3, wall: 2.9, ridge: 4.7, ridgeX: 5.3 }; | |
| 76 | const FEEDER = { x: -3, y: 1.6 }; | |
| 77 | const FENCE_Y = 4.6, POST_H = 1.7, PLINTH = { x: -8.2, y: 6.2, h: .9 }; | |
| 78 | const LAWN = [[58, 108, 62], [66, 118, 66]], PATIO = [196, 184, 160]; | |
| 79 | const WOOD = [150, 112, 76], WOOD_DARK = [96, 70, 50], STONE = [190, 186, 176], ROOF = [160, 78, 62], WALL = [214, 200, 170]; | |
| 80 | const INK_PIX = rgb(28, 27, 25), RING_PIX = rgb(255, 255, 255), CURSOR_PIX = rgb(255, 214, 10), GOOD_PIX = rgb(60, 160, 80), BAD_PIX = rgb(200, 60, 50); | |
| 81 | ||
| 82 | // where each perch is: the spot a bird's feet stand on | |
| 83 | const SPOT = { | |
| 84 | 'oak-low': [OAK.x + 2.8, OAK.y + 1.6, 4.7], | |
| 85 | 'oak-high': [OAK.x - 1.4, OAK.y + .8, 6], | |
| 86 | 'oak-tip': [OAK.x + 1.2, OAK.y - .2, 6.9], | |
| 87 | 'fence-1': [-2, FENCE_Y, POST_H], | |
| 88 | 'fence-2': [2, FENCE_Y, POST_H], | |
| 89 | 'fence-3': [6, FENCE_Y, POST_H], | |
| 90 | 'roof-left': [SHED.x0 + .8, (SHED.y0 + SHED.y1) / 2, SHED.wall + (SHED.ridge - SHED.wall) * .8 / (SHED.ridgeX - SHED.x0) + .1], | |
| 91 | 'roof-ridge': [SHED.ridgeX, (SHED.y0 + SHED.y1) / 2, SHED.ridge + .1], | |
| 92 | 'roof-right': [SHED.x1 - .8, (SHED.y0 + SHED.y1) / 2, SHED.wall + (SHED.ridge - SHED.wall) * .8 / (SHED.x1 - SHED.ridgeX) + .1], | |
| 93 | feeder: [FEEDER.x, FEEDER.y, 2.3], | |
| 94 | }; | |
| 95 | const PERCH_NAME = { | |
| 96 | 'oak-low': 'low oak branch', 'oak-high': 'high oak branch', 'oak-tip': 'top of the oak', 'fence-1': 'first fence post', | |
| 97 | 'fence-2': 'middle fence post', 'fence-3': 'last fence post', 'roof-left': 'left of the shed roof', 'roof-ridge': 'shed roof ridge', | |
| 98 | 'roof-right': 'right of the shed roof', feeder: 'bird feeder', | |
| 99 | }; | |
| 100 | ||
| 101 | function groundColor(x, y, ix, iy) { | |
| 102 | const edge = Math.pow(Math.abs(x) / 10.4, 6) + Math.pow(Math.abs(y + .4) / 8.4, 6); | |
| 103 | const rough = Math.pow(edge, 1 / 6) + (noise(x * 1.3, y * 1.3) - .5) * .06; | |
| 104 | if (rough > 1) return 0; | |
| 105 | if (rough > .95 && hash(ix, iy) < (rough - .95) / .05) return 0; | |
| 106 | const t = .96 + (hash(ix, iy) - .5) * .08; | |
| 107 | const patio = y > FENCE_Y + 1.4 && x > -5.5 && x < 6; | |
| 108 | const c = patio ? PATIO : LAWN[Math.floor((x + y) / 2.5) & 1]; | |
| 109 | return rgb(c[0] * t, c[1] * t, c[2] * t); | |
| 110 | } | |
| 111 | const solid = (a, b, r0, r1, c) => capsule(a, b, r0, r1, () => c, 'rough'); | |
| 112 | const shade = (x, y, z, r) => castShadow(x, y, z, r, 1, 0); | |
| 113 | function bakeShed() { | |
| 114 | const { x0, x1, y0, y1, wall, ridge, ridgeX } = SHED; | |
| 115 | box(x0 + .2, y0 + .2, 0, x1 - .2, y1 - .2, wall, WALL, 3); | |
| 116 | for (let y = y0; y <= y1 + .01; y += .22) { | |
| 117 | solid([x0, y, wall], [ridgeX, y, ridge], .17, .17, ROOF); | |
| 118 | solid([ridgeX, y, ridge], [x1, y, wall], .17, .17, ROOF); | |
| 119 | } | |
| 120 | for (let x = x0; x <= x1; x += .7) { shade(x, y0, wall, .3); shade(x, y1, wall, .3); } | |
| 121 | for (let y = y0; y <= y1; y += .7) shade(ridgeX, y, ridge, .3); | |
| 122 | } | |
| 123 | function bakeFence() { | |
| 124 | for (const x of [-4, -2, 0, 2, 4, 6, 8]) { | |
| 125 | solid([x, FENCE_Y, 0], [x, FENCE_Y, POST_H], .14, .12, WOOD); | |
| 126 | shade(x, FENCE_Y, POST_H, .22); | |
| 127 | } | |
| 128 | for (const z of [.55, 1.15]) { | |
| 129 | solid([-4, FENCE_Y, z], [8, FENCE_Y, z], .06, .06, WOOD_DARK); | |
| 130 | for (let x = -4; x <= 8; x += .8) shade(x, FENCE_Y, z, .14); | |
| 131 | } | |
| 132 | } | |
| 133 | function bakeFeeder() { | |
| 134 | const { x, y } = FEEDER; | |
| 135 | solid([x, y, 0], [x, y, 2.1], .1, .08, WOOD_DARK); | |
| 136 | box(x - .6, y - .6, 2.1, x + .6, y + .6, 2.25, WOOD, 5); | |
| 137 | for (const [dx, dy] of [[-.6, -.6], [.6, -.6], [-.6, .6], [.6, .6]]) solid([x + dx, y + dy, 2.2], [x + dx, y + dy, 2.7], .03, .03, WOOD_DARK); | |
| 138 | for (let z = 0; z <= 2.2; z += .4) shade(x, y, z, .16); | |
| 139 | shade(x + .5, y, 2.2, .3); shade(x - .5, y, 2.2, .3); | |
| 140 | } | |
| 141 | function bakePlinth() { | |
| 142 | box(PLINTH.x - .9, PLINTH.y - .9, 0, PLINTH.x + .9, PLINTH.y + .9, PLINTH.h, STONE, 8); | |
| 143 | for (const [dx, dy] of [[-.7, -.7], [.7, -.7], [-.7, .7], [.7, .7], [0, 0]]) shade(PLINTH.x + dx, PLINTH.y + dy, PLINTH.h, .3); | |
| 144 | } | |
| 145 | function bake() { | |
| 146 | ground.fill(0); shadowMask.fill(0); staticShadow.fill(0); kinds.fill(0); | |
| 147 | tufts.length = 0; leaves.length = 0; | |
| 148 | zb.fill(-1e9); | |
| 149 | pix.fill(rgb(255, 255, 255)); | |
| 150 | for (let iy = 0; iy < H; iy++) for (let ix = 0; ix < W; ix++) { | |
| 151 | const g = unproject(ix + .5, iy + .5), i = iy * W + ix, c = groundColor(g.x, g.y, ix, iy); | |
| 152 | if (!c) continue; | |
| 153 | pix[i] = c; ground[i] = 1; | |
| 154 | zb[i] = (g.x + g.y) / R2 * COS; | |
| 155 | } | |
| 156 | groundZ.set(zb); | |
| 157 | growTree(OAK.x, OAK.y, OAK.scale, 260, 21); | |
| 158 | for (let z = 0; z <= 3.4; z += .4) shade(OAK.x, OAK.y, z, .4); | |
| 159 | bakeShed(); bakeFence(); bakeFeeder(); bakePlinth(); | |
| 160 | bakeOcclusion(); | |
| 161 | staticShadow.set(shadowMask); | |
| 162 | applyShadows(groundZ); | |
| 163 | bakedPix.set(pix); | |
| 164 | bakedZ.set(zb); | |
| 165 | } | |
| 166 | ||
| 167 | /* ---------- the birds: each species has its own silhouette as well as its colours ---------- */ | |
| 168 | // r body radius, len body length, tall lifts the body upright, head radius, tail length and its rise (+ cocked up, - hanging), | |
| 169 | // fork splits the tail, crest and ears stand above the head, beak is [length, radius] | |
| 170 | const LOOK = { | |
| 171 | robin: { body: [150, 108, 76], belly: [226, 98, 52], beak: [60, 50, 40], r: .36, len: .5, tall: 0, head: .25, tail: .6, rise: -.25, beak_: [.18, .05] }, | |
| 172 | wren: { body: [138, 94, 58], belly: [196, 164, 124], beak: [70, 56, 44], r: .25, len: .3, tall: 0, head: .19, tail: .5, rise: 1.1, beak_: [.3, .035] }, | |
| 173 | finch: { body: [218, 86, 112], belly: [240, 200, 190], beak: [236, 220, 190], r: .3, len: .45, tall: 0, head: .25, tail: .65, rise: -.3, fork: true, beak_: [.15, .1] }, | |
| 174 | jay: { body: [66, 110, 196], belly: [206, 210, 222], beak: [30, 30, 36], r: .42, len: .75, tall: 0, head: .3, tail: 1, rise: -.1, crest: .6, beak_: [.3, .07] }, | |
| 175 | sparrow: { body: [140, 100, 66], belly: [214, 196, 164], beak: [90, 70, 56], r: .46, len: .25, tall: 0, head: .3, tail: .28, rise: -.1, beak_: [.12, .07] }, | |
| 176 | owl: { body: [176, 146, 98], belly: [236, 224, 196], beak: [220, 170, 60], r: .5, len: 0, tall: .6, head: .44, tail: 0, rise: 0, ears: .35, owl: true, beak_: [.1, .06] }, | |
| 177 | magpie: { body: [32, 36, 52], belly: [244, 244, 240], beak: [20, 20, 24], r: .33, len: .7, tall: 0, head: .24, tail: 1.8, rise: -.05, beak_: [.26, .06] }, | |
| 178 | starling: { body: [58, 76, 70], belly: [96, 104, 120], beak: [240, 200, 40], r: .24, len: .9, tall: 0, head: .19, tail: .38, rise: -.2, beak_: [.55, .04] }, | |
| 179 | }; | |
| 180 | const BIRD_SCALE = 2; | |
| 181 | const EYE = [250, 250, 250], PUPIL = [28, 27, 25]; | |
| 182 | ||
| 183 | // feet at (x, y, z), looking along (fx, fy); `flap` raises two wings (0 folded, 1 spread) | |
| 184 | function drawBird(species, x, y, z, fx, fy, sc, flap = 0) { | |
| 185 | const L = LOOK[species]; | |
| 186 | if (!L) return; | |
| 187 | const pt = (a, h, side = 0) => [x + (fx * a - fy * side) * sc, y + (fy * a + fx * side) * sc, z + h * sc]; | |
| 188 | const bodyH = L.r * .9 + .05, headA = L.len / 2 + L.head * .6, headH = bodyH + L.tall + L.r * .45 + L.head * .75; | |
| 189 | const add = (a, b, r0, r1, c) => dynamicAdd(a, b, r0 * sc, r1 * sc, () => c); | |
| 190 | if (L.tail) { | |
| 191 | const back = -L.len / 2 - L.r * .4; | |
| 192 | for (const side of L.fork ? [-.09, .09] : [0]) add(pt(back, bodyH, side), pt(back - L.tail, bodyH + L.rise * L.tail * .6, side * 3), .1, .06, L.body); | |
| 193 | } | |
| 194 | add(pt(-L.len / 2, bodyH), pt(L.len / 2, bodyH + L.tall + (L.tall ? 0 : .08)), L.r, L.r * .92, L.body); | |
| 195 | add(pt(-L.len * .2, bodyH - L.r * .5), pt(L.len / 2, bodyH + L.tall * .4 - L.r * .5), L.r * .78, L.r * .7, L.belly); | |
| 196 | add(pt(headA, headH), pt(headA, headH), L.head, L.head, L.body); | |
| 197 | add(pt(headA + L.head * .2, headH - L.head * .5), pt(headA + L.head * .2, headH - L.head * .5), L.head * .62, L.head * .62, L.belly); | |
| 198 | add(pt(headA + L.head * .8, headH - .02), pt(headA + L.head * .8 + L.beak_[0], headH - .06), L.beak_[1] * 1.3, L.beak_[1] * .5, L.beak); | |
| 199 | if (L.crest) add(pt(headA - L.head * .2, headH + L.head * .6), pt(headA - L.head * .9, headH + L.head * .6 + L.crest), .1, .05, L.body); | |
| 200 | if (L.ears) for (const side of [-.26, .26]) add(pt(headA - .05, headH + L.head * .8, side), pt(headA - .1, headH + L.head * .8 + L.ears, side * 1.1), .1, .04, L.body); | |
| 201 | for (const side of [-1, 1]) { | |
| 202 | const e = pt(headA + L.head * .6, headH + L.head * .1, side * L.head * .78); | |
| 203 | add(e, e, L.owl ? .14 : .06, L.owl ? .14 : .06, L.owl ? EYE : PUPIL); | |
| 204 | if (species === 'owl') { const q = pt(headA + L.head * .7, headH + L.head * .1, side * L.head * .78); add(q, q, .07, .07, PUPIL); } | |
| 205 | } | |
| 206 | if (flap > 0) { | |
| 207 | const lift = bodyH + L.tall * .5 + L.r * .4; | |
| 208 | for (const side of [-1, 1]) add(pt(0, lift, side * L.r * .6), pt(-.2, lift + (flap - .5) * 1.5, side * (L.r + 1.1)), .16, .08, L.body); | |
| 209 | } | |
| 210 | } | |
| 211 | ||
| 212 | /* ---------- state ---------- */ | |
| 213 | const nodes = {}; | |
| 214 | for (const k of ['round', 'right', 'glasses', 'status']) nodes[k] = document.getElementById(k); | |
| 215 | const shown = {}; | |
| 216 | function setText(key, text) { | |
| 217 | if (shown[key] === text) return; | |
| 218 | shown[key] = text; | |
| 219 | nodes[key].textContent = text; | |
| 220 | } | |
| 221 | const fresh = () => BirdWatch.round(BirdWatch.start(Math.floor(Math.random() * 4294967296))).state; | |
| 222 | ||
| 223 | // timings, in seconds: the presentation only, the rules know nothing of them | |
| 224 | const GAP = .5, FLY_IN = .4, HOLD = 1.3, FLY_OUT = .5, LEAVE_GAP = .08, REVEAL = 1.3; | |
| 225 | let P = fresh(); | |
| 226 | // phase: 'show' (the flock lands and leaves), 'ask' (waiting for a perch), 'reveal' (the answer shown), 'over' | |
| 227 | const G = { time: 0, age: 0, phase: 'show', noteUntil: 0, cursor: 'fence-2', keyed: false, last: null, event: '' }; | |
| 228 | const facing = perch => (BirdWatch.PERCHES.indexOf(perch) % 2 ? 1 : -1) * Math.SQRT1_2; // + faces screen-right | |
| 229 | const showEnd = () => (P.birds.length - 1) * GAP + FLY_IN + HOLD + (P.birds.length - 1) * LEAVE_GAP + FLY_OUT + .3; | |
| 230 | ||
| 231 | function renderHud() { | |
| 232 | setText('round', String(Math.max(1, P.round))); | |
| 233 | setText('right', String(P.right)); | |
| 234 | setText('glasses', String(P.binoculars)); | |
| 235 | } | |
| 236 | function note(text) { setText('status', text); G.noteUntil = G.time + 1.8; } | |
| 237 | const endLine = () => `${P.line} · tap or press a key to play again`; | |
| 238 | // the canvas pixel of a perch, a little above the feet where the bird's body is | |
| 239 | function perchPoint(perch) { | |
| 240 | const [x, y, z] = SPOT[perch], p = project(x, y, z + .5); | |
| 241 | return [p.sx, p.sy]; | |
| 242 | } | |
| 243 | ||
| 244 | /* ---------- drawing ---------- */ | |
| 245 | function ellipse(sx, sy, rx, ry, color, n = 28) { | |
| 246 | const pts = []; | |
| 247 | for (let i = 0; i < n; i++) { const a = i / n * Math.PI * 2; pts.push([Math.round(sx + Math.cos(a) * rx), Math.round(sy + Math.sin(a) * ry)]); } | |
| 248 | drawDots(pts, color); | |
| 249 | } | |
| 250 | function marker(perch, color, rx = 14, ry = 9) { | |
| 251 | const [sx, sy] = perchPoint(perch); | |
| 252 | ellipse(sx, sy, rx + 1, ry + 1, INK_PIX, 36); | |
| 253 | ellipse(sx, sy, rx, ry, color); | |
| 254 | } | |
| 255 | // a bird of the flock at `age` seconds into the show (landing at its slot, leaving after the hold) | |
| 256 | function drawFlockBird(bird, index) { | |
| 257 | const land = index * GAP, leave = (P.birds.length - 1) * GAP + FLY_IN + HOLD + index * LEAVE_GAP; | |
| 258 | const kin = (G.age - land) / FLY_IN, kout = (G.age - leave) / FLY_OUT; | |
| 259 | if (kin <= 0 || kout >= 1) return; | |
| 260 | placeBird(bird, Math.min(1, kin), Math.max(0, kout)); | |
| 261 | } | |
| 262 | // `kin` 0..1 is the fly-in (1 = landed), `kout` 0..1 the fly-off | |
| 263 | function placeBird(bird, kin, kout) { | |
| 264 | const [x, y, z] = SPOT[bird.perch], f = facing(bird.perch), fx = f, fy = -f; | |
| 265 | const a = 1 - (1 - kin) * (1 - kin), out = kout * kout; | |
| 266 | const flying = kin < 1 || kout > 0; | |
| 267 | const ox = -(1 - a) * 4.5 + out * 5, oy = (1 - a) * 4.5 - out * 5, oz = (1 - a) * 5.5 + out * 7; | |
| 268 | const dir = kout > 0 ? 1 : -1; | |
| 269 | drawBird(bird.species, x + ox, y + oy, z + oz, flying ? dir * Math.SQRT1_2 : fx, flying ? -dir * Math.SQRT1_2 : fy, BIRD_SCALE, flying ? .5 + .5 * Math.sin(G.time * 34) : 0); | |
| 270 | } | |
| 271 | function draw() { | |
| 272 | pix.set(bakedPix); | |
| 273 | zb.set(bakedZ); | |
| 274 | pendingShadows.length = 0; | |
| 275 | drawLeaves(STILL ? 0 : Math.floor(G.time * WIND_FPS) / WIND_FPS); | |
| 276 | if (G.phase === 'show') P.birds.forEach(drawFlockBird); | |
| 277 | if (G.phase === 'reveal' && G.last) placeBird(G.last, Math.min(1, G.age / FLY_IN), 0); | |
| 278 | if (G.phase === 'ask' && P.asking) drawBird(P.asking.species, PLINTH.x, PLINTH.y, PLINTH.h, Math.SQRT1_2, -Math.SQRT1_2, 1.8); | |
| 279 | if (G.phase === 'reveal' && G.last) drawBird(G.last.species, PLINTH.x, PLINTH.y, PLINTH.h, Math.SQRT1_2, -Math.SQRT1_2, 1.8); | |
| 280 | flushShadows(); | |
| 281 | applyShadows(bakedZ); | |
| 282 | shadeEdges(); | |
| 283 | if (G.phase === 'ask') { | |
| 284 | for (const p of P.perches) marker(p, p === G.cursor && G.keyed ? CURSOR_PIX : RING_PIX, 11, 7); | |
| 285 | if (G.keyed) marker(G.cursor, CURSOR_PIX, 14, 9); | |
| 286 | } | |
| 287 | if (G.phase === 'reveal') { | |
| 288 | if (G.chosen && G.chosen !== G.last.perch) marker(G.chosen, BAD_PIX); | |
| 289 | marker(G.last.perch, GOOD_PIX); | |
| 290 | } | |
| 291 | ctx.putImageData(image, 0, 0); | |
| 292 | } | |
| 293 | ||
| 294 | /* ---------- game ---------- */ | |
| 295 | function ask() { | |
| 296 | const r = BirdWatch.ask(P); | |
| 297 | P = r.state; | |
| 298 | G.phase = 'ask'; G.age = 0; G.chosen = null; | |
| 299 | setText('status', `Where did the ${r.species} sit? Tap a perch, or arrows and Space. Cursor on the ${PERCH_NAME[G.cursor]}`); | |
| 300 | G.noteUntil = 0; | |
| 301 | } | |
| 302 | function nextRound() { | |
| 303 | const r = BirdWatch.round(P); | |
| 304 | P = r.state; | |
| 305 | G.phase = 'show'; G.age = 0; G.last = null; G.chosen = null; | |
| 306 | note(`Round ${P.round}: watch the ${P.birds.length} birds`); | |
| 307 | } | |
| 308 | function choose(perch) { | |
| 309 | ensureAudio(); | |
| 310 | if (G.phase !== 'ask' || !P.asking) return; | |
| 311 | const asked = P.asking, r = BirdWatch.answer(P, perch); | |
| 312 | if (!r.ok) { note('pick one of the perches'); sound(120, .08, 'square'); return; } | |
| 313 | P = r.state; G.last = asked; G.chosen = perch; G.event = r.event; G.phase = 'reveal'; G.age = 0; G.cursor = perch; | |
| 314 | const where = PERCH_NAME[asked.perch]; | |
| 315 | setText('status', `${r.correct ? 'Yes' : 'No'}: the ${asked.species} sat on the ${where}`); | |
| 316 | G.noteUntil = 0; | |
| 317 | sound(r.correct ? 660 : 130, r.correct ? .12 : .3, r.correct ? 'sine' : 'sawtooth'); | |
| 318 | renderHud(); | |
| 319 | } | |
| 320 | function restart() { | |
| 321 | P = fresh(); G.phase = 'show'; G.age = 0; G.last = null; G.chosen = null; G.event = ''; | |
| 322 | note(`Round ${P.round}: watch the ${P.birds.length} birds`); | |
| 323 | renderHud(); | |
| 324 | } | |
| 325 | function update(dt) { | |
| 326 | G.time += dt; | |
| 327 | G.age += dt; | |
| 328 | renderHud(); | |
| 329 | if (G.phase === 'show') { | |
| 330 | if (G.age >= showEnd()) ask(); | |
| 331 | else if (G.time >= G.noteUntil) setText('status', `Round ${P.round}: watch the ${P.birds.length} birds`); | |
| 332 | } else if (G.phase === 'reveal' && G.age >= REVEAL) { | |
| 333 | if (G.event === 'over') { G.phase = 'over'; sound(P.line.startsWith('Sharp') ? 780 : 110, .5, 'sine'); } | |
| 334 | else if (G.event === 'cleared') nextRound(); | |
| 335 | else ask(); | |
| 336 | } | |
| 337 | if (G.phase === 'over') setText('status', endLine()); | |
| 338 | } | |
| 339 | // birds landing: a short chirp of a pitch for each, only while the flock is shown | |
| 340 | let chirped = -1; | |
| 341 | function chirps() { | |
| 342 | if (G.phase !== 'show') { chirped = -1; return; } | |
| 343 | const n = Math.floor((G.age - FLY_IN * .8) / GAP); | |
| 344 | if (n > chirped && n < P.birds.length) { chirped = n; sound(520 + BirdWatch.SPECIES.indexOf(P.birds[n].species) * 70, .09, 'triangle'); } | |
| 345 | } | |
| 346 | ||
| 347 | /* ---------- input ---------- */ | |
| 348 | const point = e => { const r = canvas.getBoundingClientRect(); return [(e.clientX - r.left) / r.width * W, (e.clientY - r.top) / r.height * H]; }; | |
| 349 | // the perch nearest a canvas point, or null when none is near enough | |
| 350 | function nearest(px, py, reach = 24) { | |
| 351 | let best = null, gap = reach; | |
| 352 | for (const p of P.perches) { | |
| 353 | const [sx, sy] = perchPoint(p), d = Math.hypot(px - sx, (py - sy) * 1.4); | |
| 354 | if (d < gap) { gap = d; best = p; } | |
| 355 | } | |
| 356 | return best; | |
| 357 | } | |
| 358 | // the perch to move to from the cursor in a screen direction: near that way, and lined up with it | |
| 359 | function neighbour(dx, dy) { | |
| 360 | const [cx, cy] = perchPoint(G.cursor); | |
| 361 | let best = null, cost = 1e9; | |
| 362 | for (const p of P.perches) { | |
| 363 | if (p === G.cursor) continue; | |
| 364 | const [sx, sy] = perchPoint(p), along = (sx - cx) * dx + (sy - cy) * dy, across = Math.abs((sx - cx) * dy - (sy - cy) * dx); | |
| 365 | if (along <= 2) continue; | |
| 366 | const c = along + across * 2; | |
| 367 | if (c < cost) { cost = c; best = p; } | |
| 368 | } | |
| 369 | return best; | |
| 370 | } | |
| 371 | const MOVES = { ArrowLeft: [-1, 0], ArrowRight: [1, 0], ArrowUp: [0, -1], ArrowDown: [0, 1] }; | |
| 372 | canvas.addEventListener('pointerdown', e => { | |
| 373 | canvas.focus({ preventScroll: true }); | |
| 374 | ensureAudio(); | |
| 375 | if (G.phase === 'over') { restart(); return; } | |
| 376 | if (G.phase !== 'ask') return; | |
| 377 | const [px, py] = point(e), perch = nearest(px, py); | |
| 378 | G.keyed = false; | |
| 379 | if (perch) choose(perch); else note('tap a perch'); | |
| 380 | }); | |
| 381 | canvas.addEventListener('keydown', e => { | |
| 382 | const move = MOVES[e.key], pick = e.key === ' ' || e.key === 'Enter'; | |
| 383 | if (!move && !pick) return; | |
| 384 | e.preventDefault(); | |
| 385 | if (G.phase === 'over') { if (!e.repeat) restart(); return; } | |
| 386 | if (G.phase !== 'ask') return; | |
| 387 | if (move) { G.keyed = true; const to = neighbour(...move); if (to) G.cursor = to; setText('status', `the ${PERCH_NAME[G.cursor]}`); return; } | |
| 388 | if (!e.repeat) { G.keyed = true; choose(G.cursor); } | |
| 389 | }); | |
| 390 | ||
| 391 | /* ---------- sound ---------- */ | |
| 392 | let audio = null, muted = false; | |
| 393 | function ensureAudio() { | |
| 394 | if (audio) { if (audio.state === 'suspended') audio.resume(); return; } | |
| 395 | const AC = window.AudioContext || window.webkitAudioContext; | |
| 396 | if (AC) audio = new AC(); | |
| 397 | } | |
| 398 | function sound(freq, life, type, delay = 0) { | |
| 399 | if (!audio || muted) return; | |
| 400 | const now = audio.currentTime + delay, gain = audio.createGain(), osc = audio.createOscillator(); | |
| 401 | osc.type = type; | |
| 402 | osc.frequency.setValueAtTime(freq, now); | |
| 403 | gain.gain.setValueAtTime(.0001, now); | |
| 404 | gain.gain.exponentialRampToValueAtTime(.07, now + .01); | |
| 405 | gain.gain.exponentialRampToValueAtTime(.0005, now + life); | |
| 406 | osc.connect(gain).connect(audio.destination); | |
| 407 | osc.start(now); osc.stop(now + life + .02); | |
| 408 | } | |
| 409 | const muteButton = document.getElementById('mute'); | |
| 410 | muteButton.addEventListener('click', () => { | |
| 411 | muted = !muted; | |
| 412 | muteButton.textContent = muted ? 'sound off' : 'sound on'; | |
| 413 | muteButton.setAttribute('aria-pressed', String(muted)); | |
| 414 | }); | |
| 415 | ||
| 416 | /* ---------- fit and boot ---------- */ | |
| 417 | addEventListener('resize', fit); | |
| 418 | fit(); | |
| 419 | bake(); | |
| 420 | note(`Round ${P.round}: watch the ${P.birds.length} birds`); | |
| 421 | renderHud(); | |
| 422 | canvas.focus({ preventScroll: true }); | |
| 423 | let last = performance.now(); | |
| 424 | function frame(now) { | |
| 425 | update(Math.min(.04, (now - last) / 1000)); | |
| 426 | chirps(); | |
| 427 | last = now; | |
| 428 | draw(); | |
| 429 | requestAnimationFrame(frame); | |
| 430 | } | |
| 431 | requestAnimationFrame(frame); | |
| 432 | </script> | |
| 433 | </body> | |
| 434 | </html> |
bird-watch/page.test.mjs added +91
| @@ -0,0 +1,91 @@ | ||
| 1 | import { test, expect } from 'bun:test'; | |
| 2 | import { boot } from '../playtest.mjs'; | |
| 3 | ||
| 4 | // play until the first question is open | |
| 5 | function asking(seed = 3) { | |
| 6 | const g = boot('bird-watch', { seed }); | |
| 7 | for (let i = 0; i < 400 && g.read('G.phase') !== 'ask'; i++) g.run(.1); | |
| 8 | expect(g.read('G.phase')).toBe('ask'); | |
| 9 | return g; | |
| 10 | } | |
| 11 | ||
| 12 | test('a tap a few pixels off a perch still picks that perch', () => { | |
| 13 | const g = asking(); | |
| 14 | const [x, y] = g.read('perchPoint(P.asking.perch)'); | |
| 15 | g.tap(x + 6, y + 4); | |
| 16 | expect(g.read('P.answered')).toBe(1); | |
| 17 | expect(g.read('G.chosen')).toBe(g.read('G.last.perch')); | |
| 18 | }); | |
| 19 | ||
| 20 | test('a tap far from every perch answers nothing and says to tap a perch', () => { | |
| 21 | const g = asking(); | |
| 22 | g.tap(5, 275); | |
| 23 | expect(g.read('P.answered')).toBe(0); | |
| 24 | expect(g.read('G.phase')).toBe('ask'); | |
| 25 | expect(g.text()).toContain('tap a perch'); | |
| 26 | }); | |
| 27 | ||
| 28 | test('arrows move the cursor to another perch and space answers with it', () => { | |
| 29 | const g = asking(); | |
| 30 | const from = g.read('G.cursor'); | |
| 31 | g.press('ArrowRight'); | |
| 32 | expect(g.read('G.cursor')).not.toBe(from); | |
| 33 | g.press(' '); | |
| 34 | expect(g.read('G.phase')).toBe('reveal'); | |
| 35 | }); | |
| 36 | ||
| 37 | test('the question opens and every arrow move says which perch the cursor is on', () => { | |
| 38 | const g = asking(); | |
| 39 | const name = p => g.read(`PERCH_NAME[${JSON.stringify(p)}]`); | |
| 40 | expect(g.el('status').textContent).toContain(`Cursor on the ${name(g.read('G.cursor'))}`); | |
| 41 | const from = g.read('G.cursor'); | |
| 42 | g.press('ArrowRight'); | |
| 43 | const to = g.read('G.cursor'); | |
| 44 | expect(to).not.toBe(from); | |
| 45 | expect(g.el('status').textContent).toBe(`the ${name(to)}`); | |
| 46 | }); | |
| 47 | ||
| 48 | // answer the open question on purpose with a perch that is not the right one | |
| 49 | function miss(g) { | |
| 50 | const right = g.read('P.asking.perch'); | |
| 51 | const wrong = g.read('P.perches').find(p => p !== right); | |
| 52 | g.tap(...g.read(`perchPoint(${JSON.stringify(wrong)})`)); | |
| 53 | } | |
| 54 | ||
| 55 | test('a wrong perch costs a pair of binoculars and the status names the right perch', () => { | |
| 56 | const g = asking(); | |
| 57 | const right = g.read('P.asking.perch'); | |
| 58 | expect(g.el('glasses').textContent).toBe('3'); | |
| 59 | miss(g); | |
| 60 | expect(g.read('G.phase')).toBe('reveal'); | |
| 61 | expect(g.el('glasses').textContent).toBe('2'); | |
| 62 | expect(g.el('status').textContent).toStartWith('No:'); | |
| 63 | expect(g.el('status').textContent).toContain(g.read(`PERCH_NAME[${JSON.stringify(right)}]`)); | |
| 64 | }); | |
| 65 | ||
| 66 | test('three misses end the game and a tap restarts it at round 1 with 3 binoculars', () => { | |
| 67 | const g = asking(); | |
| 68 | for (let i = 0; i < 3; i++) { | |
| 69 | for (let j = 0; j < 800 && g.read('G.phase') !== 'ask'; j++) g.run(.1); | |
| 70 | miss(g); | |
| 71 | } | |
| 72 | for (let j = 0; j < 100 && g.read('G.phase') !== 'over'; j++) g.run(.1); | |
| 73 | expect(g.read('G.phase')).toBe('over'); | |
| 74 | expect(g.el('glasses').textContent).toBe('0'); | |
| 75 | g.tap(100, 100); | |
| 76 | expect(g.read('G.phase')).toBe('show'); | |
| 77 | expect(g.el('glasses').textContent).toBe('3'); | |
| 78 | expect(g.el('round').textContent).toBe('1'); | |
| 79 | }); | |
| 80 | ||
| 81 | test('a key restarts a finished game too', () => { | |
| 82 | const g = asking(); | |
| 83 | for (let i = 0; i < 3; i++) { | |
| 84 | for (let j = 0; j < 800 && g.read('G.phase') !== 'ask'; j++) g.run(.1); | |
| 85 | miss(g); | |
| 86 | } | |
| 87 | for (let j = 0; j < 100 && g.read('G.phase') !== 'over'; j++) g.run(.1); | |
| 88 | g.press(' '); | |
| 89 | expect(g.read('G.phase')).toBe('show'); | |
| 90 | expect(g.el('glasses').textContent).toBe('3'); | |
| 91 | }); |
index.html +1
| @@ -119,6 +119,7 @@ footer a::after { content: none; } | ||
| 119 | 119 | <li data-kind="Puzzle" data-input="Tap" style="--accent:#8a4b2f"><a href="shunting-yard/">Shunting Yard</a><span>Shunt the wagons through the sidings until the train leaves in order</span><span>Game forged by the crew</span><span class="tags">Puzzle · Tap</span></li> |
| 120 | 120 | <li data-kind="Strategy" data-input="Tap" style="--accent:#b8475a"><a href="ferris-wheel/">Ferris Wheel</a><span>Seat the queue without tipping the wheel</span><span>Game forged by the crew</span><span class="tags">Strategy · Tap</span></li> |
| 121 | 121 | <li data-kind="Puzzle" data-input="Keys" style="--accent:#3d6e4f"><a href="hedge-maze/">Hedge Maze</a><span>Find the way through the hedges to the fountain before the gardener rings the bell</span><span>Game forged by the crew</span><span class="tags">Puzzle · Keys</span></li> |
| 122 | <li data-kind="Memory" data-input="Tap" style="--accent:#6b8f3a"><a href="bird-watch/">Bird Watch</a><span>Remember where each bird perched</span><span>Game forged by the crew</span><span class="tags">Memory · Tap</span></li> | |
| 122 | 123 | <li data-kind="Strategy" data-input="Tap" style="--accent:#3d7f6e"><a href="bunting/">Bunting</a><span>Hang the flags for the fete so no two neighbours match</span><span>Game forged by the crew</span><span class="tags">Strategy · Tap</span></li> |
| 123 | 124 | </ul> |
| 124 | 125 | <footer>Concept by <a href="https://x.com/awwbhi2">abhi</a>. Game by <a href="https://x.com/AmirMushich">AmirMushich</a>. Visual reference for the style: shawn.'s <a href="https://x.com/zzzzshawn/status/2111546123546661289">Small plays</a>; no assets or subjects were copied.<br><a href="stats/">Most played</a></footer> |
og/bird-watch.png added
Binary file not shown.
og/home.png
Binary file not shown.
rounds.mjs +5
| @@ -184,6 +184,11 @@ export const ROUNDS = { | ||
| 184 | 184 | const [from, to] = g.read('Yard.solve(P)[0]'); |
| 185 | 185 | g.press(String(from + 1)); g.press(String(to + 1)); |
| 186 | 186 | } }, |
| 187 | // tap the perch where the named bird sat, round after round | |
| 188 | 'bird-watch': { over: /play again/, outcome: /Sharp eyes: \d+ rounds? cleared, \d+ answers? right/, beat: g => { | |
| 189 | if (g.read('G.phase') === 'ask') g.tap(...g.read('perchPoint(P.asking.perch)')); | |
| 190 | else g.run(.2); | |
| 191 | } }, | |
| 187 | 192 | // walk the one path to the fountain by the arrow keys, maze after maze; once five are solved, wait out the bell |
| 188 | 193 | 'hedge-maze': { over: /play again/, outcome: /\d+ mazes solved, best \d+\. You win!/, beat: g => { |
| 189 | 194 | const s = g.read('P'); |
serve.test.mjs +7
| @@ -570,6 +570,13 @@ test('Hedge Maze and its rules script are served without a track', async () => { | ||
| 570 | 570 | expect((await fetch(base + '/hedge-maze/maze.js')).status).toBe(200); |
| 571 | 571 | }); |
| 572 | 572 | |
| 573 | test('Bird Watch and its rules script are served without a track', async () => { | |
| 574 | const page = await fetch(base + '/bird-watch/'); | |
| 575 | expect(page.status).toBe(200); | |
| 576 | expect(page.headers.get('content-type')).toBe('text/html; charset=utf-8'); | |
| 577 | expect((await fetch(base + '/bird-watch/watch.js')).status).toBe(200); | |
| 578 | }); | |
| 579 | ||
| 573 | 580 | test('Hot Air Balloon and its rules script are served without a track', async () => { |
| 574 | 581 | const page = await fetch(base + '/hot-air-balloon/'); |
| 575 | 582 | expect(page.status).toBe(200); |
shell.js +1
| @@ -59,6 +59,7 @@ window.PixelPlays = (() => { | ||
| 59 | 59 | { id: 'shunting-yard', title: 'Shunting Yard', blurb: 'Shunt the wagons through the sidings until the train leaves in order', kind: 'Puzzle', input: 'Tap', accent: '#8a4b2f', music: false, forged: true }, |
| 60 | 60 | { id: 'ferris-wheel', title: 'Ferris Wheel', blurb: 'Seat the queue without tipping the wheel', kind: 'Strategy', input: 'Tap', accent: '#b8475a', music: false, forged: true }, |
| 61 | 61 | { id: 'hedge-maze', title: 'Hedge Maze', blurb: 'Find the way through the hedges to the fountain before the gardener rings the bell', kind: 'Puzzle', input: 'Keys', accent: '#3d6e4f', music: false, forged: true }, |
| 62 | { id: 'bird-watch', title: 'Bird Watch', blurb: 'Remember where each bird perched', kind: 'Memory', input: 'Tap', accent: '#6b8f3a', music: false, forged: true }, | |
| 62 | 63 | { id: 'bunting', title: 'Bunting', blurb: 'Hang the flags for the fete so no two neighbours match', kind: 'Strategy', input: 'Tap', accent: '#3d7f6e', music: false, forged: true }, |
| 63 | 64 | ]; |
| 64 | 65 | const MUSIC_VOLUME = .4; |
new above
offline