Public: anyone can read this; the crew can change it.
Migrated from github.com/amirmushichge/pixel-plays
Add Skipping Rope: the playable page and its place in the catalogue, on the rules in `skipping-rope/rope.js` #295
Check · Review · Land · Live done — Landed and live in production.
vesper/pixel-plays:vesper/272-add-skipping-rope-the-playable-page-and → main 4 commits fork
32 files
changed +420
−1
.bash_profile added
No content changes.
.bashrc added
No content changes.
.claude/agents added
No content changes.
.claude/commands added
No content changes.
.claude/hooks added
No content changes.
.claude/launch.json added
No content changes.
.claude/loop.md added
No content changes.
.claude/output-styles added
No content changes.
.claude/routines added
No content changes.
.claude/scheduled_tasks.json added
No content changes.
.claude/settings.json added
No content changes.
.claude/settings.local.json added
No content changes.
.claude/skills added
No content changes.
.claude/workflows added
No content changes.
.gitconfig added
No content changes.
.gitmodules added
No content changes.
.idea added
No content changes.
.mcp.json added
No content changes.
.profile added
No content changes.
.ripgreprc added
No content changes.
.vscode added
No content changes.
.zprofile added
No content changes.
.zshrc added
No content changes.
README.md +2 −1
| @@ -62,6 +62,7 @@ Concept by [abhi](https://x.com/awwbhi2). Game by [AmirMushich](https://x.com/Am | ||
| 62 | 62 | | [](stained-glass/) | A church window of leaded glass panes in a stone frame, with the palette in a tray below it: tap a colour, then a pane, to paint it, and tap a painted pane again to clear it; panes that share a lead line must not match, a clash is marked by a thick dashed lead line, every colour carries its own pattern (stripes, dots, cross-hatch, checks) and a rivet marks a fixed pane; three windows (3, 3 and 4 colours), solved when every pane is coloured with no clash, scoring 100 and 50 more at or under par strokes (forged by the crew on the engine and shell above, not by the original creators; rules in `stained-glass/window.js`) | Tap, or arrows, 1-4, Space and R | |
| 63 | 63 | | [](jam-jars/) | A pantry shelf of 3 to 5 glass jars with their fruit layers (plum, fig, quince) visible through the glass: tap a jar, or press its number key, to pick it up (it lifts and is outlined), then tap another to pour the top run of one fruit into it, as many layers as fit, onto the same fruit or into free space; a rejected pour says why and changes nothing; undo (button or U) reverses the last pour and costs a pour, R starts the shelf again; every jar must end empty or full of one fruit, the HUD showing the shelf, pours against par and points; each fruit carries its own pip pattern (plum dots, fig stripes, quince checks) as well as its colour, so it can be solved without telling colours apart; four shelves, scoring 100 and 50 more at or under par pours (forged by the crew on the engine and shell above, not by the original creators; rules in `jam-jars/shelf.js`) | Tap a jar, or 1-5, arrows, Space, U and R | |
| 64 | 64 | | [](library-shelf/) | An oak library shelf with two runners and books of different widths, the returned book (red, with a ribbon bookmark) waiting at one end and a dashed gap marking where it must reach: drag a book along its runner, or press Tab to choose one and use the arrow keys, to slide it; each book slides only the way the arrow on its spine points and cannot pass another, however far a slide goes it counts as one, undo (button or U) takes the last slide back at the cost of a slide, R begins the level again, and all 3 levels cleared wins, with a bonus for each within par slides (forged by the crew on the engine and shell above, not by the original creators; rules in `library-shelf/shelf.js`) | Drag; Tab and arrows | |
| 65 | | [](skipping-rope/) | A school yard of tarmac with a grass verge, a hopscotch grid, a fence and a tree, where two friends turn a long rope and you skip in the line between them (you wear a yellow cap; the others wear none): tap the canvas or press Space to jump, and be in the air when the rope touches the ground, which a chalk line on the ground flashes; the rope quickens and another skipper joins every 3 clears, each joined skipper adds 5 to every clear, so win by making 12 clears and lose by landing on the rope (forged by the crew on the engine and shell above, not by the original creators; rules in `skipping-rope/rope.js`) | Tap the canvas; space | | |
| 65 | 66 | | [](conkers/) | A school playground under a horse-chestnut tree, your conker (double knot and a chalk cross) and the rival's hanging side by side on their strings, and a power meter that sweeps up and down: tap the canvas, or press Space or Enter, to swing when the meter is where you want it; the gold sweet spot (marked by a notch) does 4 damage, anywhere else up to the hatched end does 1, and the hatched end tangles the strings, does nothing and passes the turn; you and the rival swing in turn (their reading comes from the seed, shown as a red pointer), crack all 3 rivals' conkers (6, 8 and 10 hit points) before yours (12) cracks, scoring 100 for each and 10 for each hit point left; cracks show on the conkers (forged by the crew on the engine and shell above, not by the original creators; rules in `conkers/match.js`) | Tap, or Space | |
| 66 | 67 | | [](empire/) | A real-time strategy game on a 44 by 44 map against a red bot (easy, normal or hard): four villagers and a town center each, gather food, wood, gold and stone, build houses, camps, farms and military buildings, climb the Dark, Feudal, Castle and Imperial Ages, research upgrades, train militia, spearmen, archers, knights and rams, and destroy every town center and castle of the enemy; fog of war, a minimap, attack alerts, pause, 1x/2x/4x speed, an autopilot, a save that survives a reload and a best time per level (forged by the crew on the engine and shell above, not by the original creators) | Tap or drag a box to select, tap to command, panel buttons to build and train; arrows move the view, Enter taps its centre cross, 1-9 press buttons | |
| 67 | 68 | | [](hot-air-balloon/) | Five flights over patchwork farm fields with hedgerows, a windsock and one field marked with a white cross: hold the burner and the balloon climbs slowly after a short lag, let go and it sinks; the air has three height bands that each blow their own way and a little differently each flight, shown by drifting cloud wisps, three arrows at the left and a pennant on the basket, so you steer only by choosing a height (the ready screen before each flight already shows that flight's winds, so read them first); touching the ground ends the flight and the landing scores by distance from the cross (on it 5, inside its field 3, a neighbouring field 1, trees or the pond 0), each flight from a new launch point; 15 or more of 25 wins; the balloon's shadow on the fields shows where it is over (forged by the crew on the engine and shell above, not by the original creators) | Press and hold the canvas; hold space or the up arrow | |
| @@ -94,7 +95,7 @@ On the forge, `forge.json` runs `bun serve.mjs` with health check `/`. State is | ||
| 94 | 95 | Every page carries Open Graph and Twitter tags (between `og:begin` and `og:end`), and `og/<id>.png` is a 1200x630 card: the game's own opening frame, scaled 2x on white; `og/home.png` is a grid of all of them. Because the games are static, `bun scripts/og.mjs` makes them without a browser: it runs each page's scripts in a `vm` against a stub DOM, captures the canvas pixels and writes the PNG (zlib only, no dependency) and the tags. Run it after adding a game or changing a scene; `serve.test.mjs` fails when a card or its tags are missing or stale. Scenes are rendered with a seeded `Math.random`, so the cards are byte-for-byte reproducible. Absolute URLs use `ORIGIN` at the top of the script (the forge production site), so change it there if the site moves. |
| 95 | 96 | |
| 96 | 97 | ## Add a game |
| 97 | Create `<id>/index.html`, load `../shell.js`, call `PixelPlays.mount({ id, rules })`, set `window.PP_SCENE`, load `../engine.js`, then add the game to `GAMES` in `shell.js` with a one-line `blurb`; the home page lists every `GAMES` entry as a card with its `kind`, `input` and `accent`, searchable and filterable by kind (a static fallback list for no-JS is kept in sync by `serve.test.mjs`, so add the matching `<li>` to `index.html` too, with the same `data-kind`, `data-input`, `--accent` and tags line). Then run `bun scripts/og.mjs` to make its share card and tags. The page needs a `#mute` button. Put its background track at `<id>/<id>.mp3`, or set `music: false` on its `GAMES` entry when it has none (Stone Skip, Kite Hill, Paper Route, Harbour Crane, Pizza Toss, Sled Run, Pottery Wheel, Row Home, Orchard, Lighthouse, Market Scale, Window Washer, Domino Run, Campfire, Beekeeper, Bread Oven, Ice Fishing, Tightrope, Tram Line, Archery, Bell Tower, Espresso Bar, Goalkeeper, Wood Split, Signal Box, Soapbox Derby, Sheepdog, Firefly Jar, Hot Air Balloon, Cloakroom, Go-Kart Fete, Empire Age, Egg Basket, Dinghy Race, Coconut Shy, Bowls Green, Seed Packets, Hoopla Stall, Lock Keeper, Ferryman, Tower Duel, Sandcastle Tide, Hedge Maze, Allotment, Lily Pond, Market Barrow, Curling Rink, Shunting Yard, Ferris Wheel, Bird Watch, Bunting, Patchwork Quilt, Mill Race, Rain Barrel, Rock Pool, Stained Glass, Conkers, Jam Jars and Library Shelf play on their own sound effects). Rules that need testing go in a plain script with a `.test.mjs` beside it, as in `kite-hill/model.js`. | |
| 98 | Create `<id>/index.html`, load `../shell.js`, call `PixelPlays.mount({ id, rules })`, set `window.PP_SCENE`, load `../engine.js`, then add the game to `GAMES` in `shell.js` with a one-line `blurb`; the home page lists every `GAMES` entry as a card with its `kind`, `input` and `accent`, searchable and filterable by kind (a static fallback list for no-JS is kept in sync by `serve.test.mjs`, so add the matching `<li>` to `index.html` too, with the same `data-kind`, `data-input`, `--accent` and tags line). Then run `bun scripts/og.mjs` to make its share card and tags. The page needs a `#mute` button. Put its background track at `<id>/<id>.mp3`, or set `music: false` on its `GAMES` entry when it has none (Stone Skip, Kite Hill, Paper Route, Harbour Crane, Pizza Toss, Sled Run, Pottery Wheel, Row Home, Orchard, Lighthouse, Market Scale, Window Washer, Domino Run, Campfire, Beekeeper, Bread Oven, Ice Fishing, Tightrope, Tram Line, Archery, Bell Tower, Espresso Bar, Goalkeeper, Wood Split, Signal Box, Soapbox Derby, Sheepdog, Firefly Jar, Hot Air Balloon, Cloakroom, Go-Kart Fete, Empire Age, Egg Basket, Dinghy Race, Coconut Shy, Bowls Green, Seed Packets, Hoopla Stall, Lock Keeper, Ferryman, Tower Duel, Sandcastle Tide, Hedge Maze, Allotment, Lily Pond, Market Barrow, Curling Rink, Shunting Yard, Ferris Wheel, Bird Watch, Bunting, Patchwork Quilt, Mill Race, Rain Barrel, Rock Pool, Stained Glass, Conkers, Jam Jars, Library Shelf and Skipping Rope play on their own sound effects). Rules that need testing go in a plain script with a `.test.mjs` beside it, as in `kite-hill/model.js`. | |
| 98 | 99 | |
| 99 | 100 | - `town.js` is the town's layout, pure data with no drawing: `window.PixelTown.layout(GAMES)` gives one isometric building per game (plot, screen position under the fixed 30-degree camera, accent, id) with trees in the spare plots, and `pick(town, x, y, filter)` gives the id of the front-most building under a screen point, or null. Nothing loads it yet; the drawing and the page are the second half of #99. |
| 100 | 101 | - `shell.js` provides the game menu, the Rules popup, the credits and the background music. The menu lists the games grouped under their kind (the not-yet-made ones last, under Soon) and scrolls within the screen; type in its find box to narrow it by a word of the title, kind, input or blurb (keys typed there never reach the game), and press Escape to close it. |
index.html +1
| @@ -129,6 +129,7 @@ footer a::after { content: none; } | ||
| 129 | 129 | <li data-kind="Puzzle" data-input="Tap" style="--accent:#b8324a"><a href="jam-jars/">Jam Jars</a><span>Pour the fruit between jars until every jar holds just one jam</span><span>Game forged by the crew</span><span class="tags">Puzzle · Tap</span></li> |
| 130 | 130 | <li data-kind="Puzzle" data-input="Drag" style="--accent:#6b4a2e"><a href="library-shelf/">Library Shelf</a><span>Slide the books along their runners to make room for the returned one</span><span>Game forged by the crew</span><span class="tags">Puzzle · Drag</span></li> |
| 131 | 131 | <li data-kind="Sport" data-input="Tap" style="--accent:#8a4b22"><a href="conkers/">Conkers</a><span>Time your swing to crack every rival conker in the playground</span><span>Game forged by the crew</span><span class="tags">Sport · Tap</span></li> |
| 132 | <li data-kind="Timing" data-input="Tap" style="--accent:#d0573c"><a href="skipping-rope/">Skipping Rope</a><span>Jump the turning rope as it quickens and friends join the line</span><span>Game forged by the crew</span><span class="tags">Timing · Tap</span></li> | |
| 132 | 133 | </ul> |
| 133 | 134 | <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> |
| 134 | 135 | </main> |
og/home.png
Binary file not shown.
og/skipping-rope.png added
Binary file not shown.
rounds.mjs +6
| @@ -441,6 +441,12 @@ export const ROUNDS = { | ||
| 441 | 441 | const { index, offset } = g.read('Rope.nearestBeat(R.t)'); |
| 442 | 442 | if (g.read('R.phase') === 'pull' && Math.abs(offset) < .05 && g.read('R.lastBeat') !== index) g.press(' ', .04); else g.step(1); |
| 443 | 443 | } }, |
| 444 | // jump when the rules say the rope is a fifth of a second from the ground and you are on it | |
| 445 | 'skipping-rope': { over: /play again/, outcome: /You win: 12 skips, score \d+/, beat: g => { | |
| 446 | if (g.read('P.status') !== 'playing') { g.run(.3); return; } | |
| 447 | if (g.read('!M.airborne(P) && P.nextCross - P.t <= .25')) g.press(' ', .04); | |
| 448 | else g.run(.04); | |
| 449 | } }, | |
| 444 | 450 | 'window-washer': { over: /play again/, outcome: /[1-9]\d* of 500/, beat: g => g.drag([60, 100], [300, 100], .6) }, |
| 445 | 451 | }; |
| 446 | 452 |
serve.test.mjs +7
| @@ -705,3 +705,10 @@ test('Conkers and its rules script are served without a track', async () => { | ||
| 705 | 705 | expect(page.headers.get('content-type')).toBe('text/html; charset=utf-8'); |
| 706 | 706 | expect((await fetch(base + '/conkers/match.js')).status).toBe(200); |
| 707 | 707 | }); |
| 708 | ||
| 709 | test('Skipping Rope and its rules script are served without a track', async () => { | |
| 710 | const page = await fetch(base + '/skipping-rope/'); | |
| 711 | expect(page.status).toBe(200); | |
| 712 | expect(page.headers.get('content-type')).toBe('text/html; charset=utf-8'); | |
| 713 | expect((await fetch(base + '/skipping-rope/rope.js')).status).toBe(200); | |
| 714 | }); |
shell.js +1
| @@ -69,6 +69,7 @@ window.PixelPlays = (() => { | ||
| 69 | 69 | { id: 'jam-jars', title: 'Jam Jars', blurb: 'Pour the fruit between jars until every jar holds just one jam', kind: 'Puzzle', input: 'Tap', accent: '#b8324a', music: false, forged: true }, |
| 70 | 70 | { id: 'library-shelf', title: 'Library Shelf', blurb: 'Slide the books along their runners to make room for the returned one', kind: 'Puzzle', input: 'Drag', accent: '#6b4a2e', music: false, forged: true }, |
| 71 | 71 | { id: 'conkers', title: 'Conkers', blurb: 'Time your swing to crack every rival conker in the playground', kind: 'Sport', input: 'Tap', accent: '#8a4b22', music: false, forged: true }, |
| 72 | { id: 'skipping-rope', title: 'Skipping Rope', blurb: 'Jump the turning rope as it quickens and friends join the line', kind: 'Timing', input: 'Tap', accent: '#d0573c', music: false, forged: true }, | |
| 72 | 73 | ]; |
| 73 | 74 | const MUSIC_VOLUME = .4; |
| 74 | 75 | const CREDITS = [ |
skipping-rope/index.html added +339
| @@ -0,0 +1,339 @@ | ||
| 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>Skipping Rope</title> | |
| 7 | <!-- og:begin --> | |
| 8 | <meta name="description" content="Jump the turning rope as it quickens and friends join the line"> | |
| 9 | <link rel="canonical" href="https://pixel-plays--forge-trevato-apps.open-platform.sh/skipping-rope/"> | |
| 10 | <meta property="og:type" content="website"> | |
| 11 | <meta property="og:site_name" content="Pixel Plays"> | |
| 12 | <meta property="og:title" content="Skipping Rope"> | |
| 13 | <meta property="og:description" content="Jump the turning rope as it quickens and friends join the line"> | |
| 14 | <meta property="og:url" content="https://pixel-plays--forge-trevato-apps.open-platform.sh/skipping-rope/"> | |
| 15 | <meta property="og:image" content="https://pixel-plays--forge-trevato-apps.open-platform.sh/og/skipping-rope.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="Skipping Rope: 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; : #d0573c; } | |
| 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 | canvas:focus-visible { outline: 2px solid var(); outline-offset: -2px; } | |
| 31 | .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; } | |
| 32 | #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; } | |
| 33 | #bottom { bottom: max(20px, env(safe-area-inset-bottom)); } | |
| 34 | .row { display: flex; align-items: center; gap: 8px; color: var(); } | |
| 35 | .row b { font-size: 16px; font-weight: 600; min-width: 1ch; font-variant-numeric: tabular-nums; } | |
| 36 | .row span { color: var(); } | |
| 37 | #status { color: var(); min-height: 1.35em; } | |
| 38 | button { pointer-events: auto; font: inherit; color: var(); background: none; border: 0; padding: 13px 10px; margin: -13px -10px; min-height: 44px; min-width: 44px; cursor: pointer; } | |
| 39 | button:hover, button:focus-visible { color: var(); } | |
| 40 | button:focus-visible { outline: 2px solid var(); outline-offset: 2px; } | |
| 41 | noscript p { position: fixed; inset: 40% 16px auto; margin: 0; text-align: center; color: var(); } | |
| 42 | </style> | |
| 43 | </head> | |
| 44 | <body> | |
| 45 | <div id="stage"><canvas id="c" tabindex="0" aria-label="Skipping Rope: a school yard where two friends turn a long rope and you skip in the line between them, in the yellow cap. Tap the canvas or press Space to jump, and be in the air when the rope touches the ground, shown by the chalk line flashing. The rope quickens and more skippers join. Twelve clears win; one miss and you are caught."></canvas></div> | |
| 46 | <noscript><p>Skipping Rope needs JavaScript to play.</p></noscript> | |
| 47 | <div id="scores"> | |
| 48 | <div class="row"><b id="skips">0</b><span id="of">of 12 skips</span></div> | |
| 49 | <div class="row"><b id="score">0</b><span>points</span></div> | |
| 50 | <div class="row"><b id="skippers">1</b><span>on the rope</span></div> | |
| 51 | <div class="row"><b id="turn">1.000</b><span>s a turn</span></div> | |
| 52 | <div class="row"><b id="best">0</b><span>best skips</span></div> | |
| 53 | </div> | |
| 54 | <div class="hud" id="bottom"> | |
| 55 | <div id="status" role="status" aria-live="polite"></div> | |
| 56 | <button id="mute" type="button" aria-pressed="false">sound on</button> | |
| 57 | </div> | |
| 58 | <script src="../shell.js"></script> | |
| 59 | <script> | |
| 60 | PixelPlays.mount({ | |
| 61 | id: 'skipping-rope', | |
| 62 | rules: [ | |
| 63 | 'Tap the canvas, or press Space, to jump. Be in the air when the rope touches the ground and the chalk line flashes.', | |
| 64 | 'Every 3 clears the rope quickens and another friend joins the line; each joined skipper adds 5 to every clear.', | |
| 65 | '12 clears win. Land on the rope and you are caught.', | |
| 66 | ], | |
| 67 | }); | |
| 68 | </script> | |
| 69 | <script>window.PP_SCENE = { originY: 10, shadowX: 1.3, shadowY: -.12 };</script> | |
| 70 | <script src="../engine.js"></script> | |
| 71 | <script src="rope.js"></script> | |
| 72 | <script> | |
| 73 | 'use strict'; | |
| 74 | ||
| 75 | const M = window.SkippingRope; | |
| 76 | const BEST_KEY = 'pixel-plays-skipping-rope-best'; | |
| 77 | const TARMAC = [[88, 94, 110], [100, 106, 122], [112, 118, 132], [124, 130, 144]]; | |
| 78 | const CHALK = [238, 238, 228], CHALK_DIM = [150, 154, 160], ROPE = [214, 176, 108], RAIL = [150, 112, 74]; | |
| 79 | const SKIN = [[222, 170, 132], [176, 124, 92], [236, 190, 150], [150, 100, 72]]; | |
| 80 | const HAIR = [[74, 50, 34], [36, 30, 28], [196, 150, 70], [60, 40, 30]]; | |
| 81 | // your skipper: a red shirt and a yellow cap; the others wear their own colours and no cap | |
| 82 | const YOU = { shirt: [208, 87, 60], stripe: [240, 230, 210], legs: [52, 66, 96], hair: HAIR[0], skin: SKIN[0] }; | |
| 83 | const CAP = [250, 206, 36], BRIM = [196, 140, 20]; | |
| 84 | const FRIENDS = [ | |
| 85 | { shirt: [64, 118, 190], stripe: [240, 232, 210], legs: [60, 60, 70], hair: HAIR[1], skin: SKIN[1] }, | |
| 86 | { shirt: [86, 160, 96], stripe: [240, 232, 210], legs: [52, 66, 96], hair: HAIR[2], skin: SKIN[2] }, | |
| 87 | { shirt: [150, 80, 168], stripe: [240, 232, 210], legs: [44, 50, 60], hair: HAIR[3], skin: SKIN[3] }, | |
| 88 | ]; | |
| 89 | const TURNERS = [ | |
| 90 | { shirt: [236, 196, 60], stripe: [240, 232, 210], legs: [70, 80, 100], hair: HAIR[1], skin: SKIN[2] }, | |
| 91 | { shirt: [60, 150, 150], stripe: [240, 232, 210], legs: [70, 80, 100], hair: HAIR[2], skin: SKIN[1] }, | |
| 92 | ]; | |
| 93 | const SLOTS = [0, -2.2, 2.2, -4.4]; // where along the rope each skipper stands: you in the middle | |
| 94 | const TURN_A = [-9.2, -.2], TURN_B = [9.2, .64], LINE_B = .22, HOLD_ARM = 1.5; | |
| 95 | const FLASH = .2; // seconds the chalk line stays lit after the rope touches the ground | |
| 96 | // local yard space: a runs across (screen right is +), b toward the camera, z up; the world axes sit 45 degrees off | |
| 97 | const toWorld = (a, b, z) => [(a + b) / R2, (b - a) / R2, z]; | |
| 98 | const toLocal = (x, y) => ({ a: (x - y) / R2, b: (x + y) / R2 }); | |
| 99 | ||
| 100 | /* ---------- state ---------- */ | |
| 101 | const nodes = {}; | |
| 102 | for (const k of ['skips', 'of', 'score', 'skippers', 'turn', 'best', 'status']) nodes[k] = document.getElementById(k); | |
| 103 | const shown = {}; | |
| 104 | function setText(key, text) { | |
| 105 | if (shown[key] === text) return; | |
| 106 | shown[key] = text; | |
| 107 | nodes[key].textContent = text; | |
| 108 | } | |
| 109 | const best0 = (() => { try { return Number(localStorage.getItem(BEST_KEY)) || 0; } catch (e) { return 0; } })(); | |
| 110 | let P = M.start(); | |
| 111 | // flash: seconds of lit chalk line left; overAt: when the game ended, so a frantic tap cannot restart it at once | |
| 112 | const G = { time: 0, flash: 0, noteUntil: 0, overAt: 0, best: best0 }; | |
| 113 | const HINT = 'tap or press Space to jump as the rope comes down'; | |
| 114 | ||
| 115 | function renderHud() { | |
| 116 | setText('skips', String(P.skips)); | |
| 117 | setText('of', `of ${M.WIN_CLEARS} skips`); | |
| 118 | setText('score', String(P.score)); | |
| 119 | setText('skippers', String(P.skippers)); | |
| 120 | setText('turn', P.period.toFixed(3)); | |
| 121 | setText('best', String(G.best)); | |
| 122 | } | |
| 123 | function note(text, seconds = 1.6) { setText('status', text); G.noteUntil = G.time + seconds; } | |
| 124 | ||
| 125 | /* ---------- ground: a school yard of tarmac with a grass verge, hopscotch and a fence ---------- */ | |
| 126 | function chalkAt(a, b) { | |
| 127 | const w = .1; | |
| 128 | if (Math.abs(b - LINE_B) < w && Math.abs(a) < 8.2) return 1; // the line the rope strikes | |
| 129 | if (a > -13.2 && a < -10.2 && b > 2.2 && b < 6.4) { // hopscotch: three squares and a pair | |
| 130 | const row = Math.floor((b - 2.2) / 1.05), ra = (a + 13.2) / 3; | |
| 131 | const lines = Math.abs(((b - 2.2) % 1.05)) < w || Math.abs(a + 13.2) < w || Math.abs(a + 10.2) < w; | |
| 132 | if (lines) return 1; | |
| 133 | if (row % 2 === 1 && Math.abs(ra - .5) < .02) return 1; | |
| 134 | } | |
| 135 | return 0; | |
| 136 | } | |
| 137 | function groundColor(x, y, ix, iy, i) { | |
| 138 | const { a, b } = toLocal(x, y); | |
| 139 | const edge = Math.pow(Math.pow(Math.abs(a) / 14.5, 4) + Math.pow(Math.abs(b) / 7, 4), .25); | |
| 140 | const rough = edge + (noise(a * 1.3, b * 1.3) - .5) * .1; | |
| 141 | if (rough > 1.04) return 0; | |
| 142 | if (rough > .94 && hash(ix, iy) < (rough - .94) / .1) return 0; | |
| 143 | if (rough > .86) { | |
| 144 | kinds[i] = KIND_GRASS; | |
| 145 | const t = noise(a * .9, b * .9) * 1.15 + (hash(ix, iy) - .5) * .55 - .08; | |
| 146 | return rgb(...GRASS[clamp(Math.floor(t * 4.2), 0, 3)]); | |
| 147 | } | |
| 148 | const c = chalkAt(a, b); | |
| 149 | if (c) return rgb(...(c === 1 && Math.abs(b - LINE_B) < .1 ? CHALK_DIM : CHALK)); | |
| 150 | const t = noise(a * .7, b * .7) * .7 + (hash(ix, iy) - .5) * .35 + (bayerAt(ix, iy) - .5) * .2; | |
| 151 | return rgb(...TARMAC[clamp(Math.floor(t * 3.6), 0, 3)]); | |
| 152 | } | |
| 153 | function bakeFence() { | |
| 154 | const b = -6.3; | |
| 155 | for (let a = -12.6; a <= 12.6; a += 2.1) { | |
| 156 | capsule(toWorld(a, b, 0), toWorld(a, b, 2.3), .1, .09, () => RAIL, 'solid'); | |
| 157 | for (let k = 0; k <= 10; k++) castShadow(...toWorld(a, b, 2.3 * k / 10), .1, 1, 0); | |
| 158 | } | |
| 159 | for (const z of [1.0, 2.0]) for (let a = -12.6; a < 12.6; a += .5) capsule(toWorld(a, b, z), toWorld(a + .5, b, z), .06, .06, () => RAIL, 'solid'); | |
| 160 | } | |
| 161 | function bake() { | |
| 162 | ground.fill(0); shadowMask.fill(0); staticShadow.fill(0); kinds.fill(0); | |
| 163 | zb.fill(-1e9); | |
| 164 | pix.fill(PAPER); | |
| 165 | for (let iy = 0; iy < H; iy++) for (let ix = 0; ix < W; ix++) { | |
| 166 | const g = unproject(ix + .5, iy + .5), i = iy * W + ix, c = groundColor(g.x, g.y, ix, iy, i); | |
| 167 | if (!c) continue; | |
| 168 | pix[i] = c; ground[i] = 1; | |
| 169 | zb[i] = (g.x + g.y) / R2 * COS; | |
| 170 | } | |
| 171 | growGrass(); | |
| 172 | groundZ.set(zb); | |
| 173 | bakeFence(); | |
| 174 | const tree = toWorld(11.6, -4.6, 0); | |
| 175 | growTree(tree[0], tree[1], .85, 260, 5); | |
| 176 | castShadow(tree[0], tree[1], 1.3, 1.1, 1, .4); | |
| 177 | bakeOcclusion(); | |
| 178 | staticShadow.set(shadowMask); | |
| 179 | applyShadows(groundZ); | |
| 180 | bakedPix.set(pix); | |
| 181 | bakedZ.set(zb); | |
| 182 | } | |
| 183 | ||
| 184 | /* ---------- the rope, the turners and the skippers ---------- */ | |
| 185 | // the rope's turn is the rules' own angle (0 at the ground, 0.5 overhead); nothing here decides when it lands | |
| 186 | const turnAngle = () => M.angle(P); | |
| 187 | function drawPerson(a, b, yaw, pose, pal, cap) { | |
| 188 | const [x, y] = toWorld(a, b, 0); | |
| 189 | const hand = figure(x, y, yaw, pose, pal, dynamicAdd); | |
| 190 | if (cap) { | |
| 191 | const z = (pose.z || 0) + 2.78, fx = Math.cos(yaw), fy = Math.sin(yaw); | |
| 192 | dynamicAdd([x, y, z], [x, y, z], .23, .23, () => CAP); | |
| 193 | dynamicAdd([x + fx * .12, y + fy * .12, z - .12], [x + fx * .42, y + fy * .42, z - .13], .06, .06, () => BRIM); | |
| 194 | } | |
| 195 | return hand; | |
| 196 | } | |
| 197 | function drawRope(handA, handB) { | |
| 198 | const A = { ...toLocal(handA[0], handA[1]), z: handA[2] }, B = { ...toLocal(handB[0], handB[1]), z: handB[2] }; | |
| 199 | const turn = turnAngle() * Math.PI * 2, n = 32, pts = []; | |
| 200 | const zc = (A.z + B.z) / 2, radius = zc - .1; | |
| 201 | for (let k = 0; k <= n; k++) { | |
| 202 | const s = k / n, env = Math.min(1, Math.sin(Math.PI * s) * 1.5); | |
| 203 | pts.push(toWorld(lerp(A.a, B.a, s), lerp(A.b, B.b, s) + radius * Math.sin(turn) * env, Math.max(.1, lerp(A.z, B.z, s) - (zc - .1) * Math.cos(turn) * env))); | |
| 204 | } | |
| 205 | // the axis sits at the hands' height, so at the bottom of the swing the rope lies on the ground | |
| 206 | for (let k = 0; k < n; k++) capsule(pts[k], pts[k + 1], .085, .085, () => ROPE, 'solid'); | |
| 207 | for (let k = 0; k <= n; k += 2) castShadow(...pts[k], .1, 1, 0); | |
| 208 | } | |
| 209 | // the height of a skipper above the ground, from how far through the rules' airtime they are | |
| 210 | function lift() { | |
| 211 | if (!M.airborne(P)) return 0; | |
| 212 | const p = 1 - (P.airUntil - P.t) / P.airtime; | |
| 213 | return 1.3 * Math.min(1, Math.sin(Math.PI * clamp(p, 0, 1)) * 2.4); | |
| 214 | } | |
| 215 | function drawScene() { | |
| 216 | const th = turnAngle(), swing = Math.sin(th * Math.PI * 2) * .1; | |
| 217 | const handA = drawPerson(TURN_A[0], TURN_A[1], -Math.PI / 4, { armR: HOLD_ARM + swing, armL: HOLD_ARM - swing, stance: .1 }, TURNERS[0], false); | |
| 218 | const handB = drawPerson(TURN_B[0], TURN_B[1], 3 * Math.PI / 4, { armR: HOLD_ARM + swing, armL: HOLD_ARM - swing, stance: .1 }, TURNERS[1], false); | |
| 219 | const z = lift(), up = z > 0; | |
| 220 | for (let k = 0; k < P.skippers; k++) { | |
| 221 | const pose = { z, stance: up ? .3 : .1, crouch: up ? 0 : .08, armR: up ? .7 : .05, armL: up ? .7 : .12 }; | |
| 222 | drawPerson(SLOTS[k], LINE_B, Math.PI / 4, pose, k === 0 ? YOU : FRIENDS[k - 1], k === 0); | |
| 223 | } | |
| 224 | drawRope(handA, handB); | |
| 225 | } | |
| 226 | // the chalk line the rope strikes: dim all the time, lit white for a moment each time the rope touches the ground | |
| 227 | function drawChalkFlash() { | |
| 228 | if (G.flash <= 0) return; | |
| 229 | const lit = rgb(255, 255, 240), core = rgb(255, 214, 10); | |
| 230 | for (let a = -8.2; a <= 8.2; a += .05) for (let b = LINE_B - .16; b <= LINE_B + .16; b += .04) { | |
| 231 | plot(...toWorld(a, b, .02), Math.abs(b - LINE_B) < .06 ? core : lit); | |
| 232 | } | |
| 233 | } | |
| 234 | function draw() { | |
| 235 | pix.set(bakedPix); | |
| 236 | zb.set(bakedZ); | |
| 237 | pendingShadows.length = 0; | |
| 238 | const t = STILL ? 0 : Math.floor(G.time * WIND_FPS) / WIND_FPS; | |
| 239 | drawScene(); | |
| 240 | drawLeaves(t); | |
| 241 | drawTufts(t); | |
| 242 | flushShadows(); | |
| 243 | applyShadows(bakedZ); | |
| 244 | shadeEdges(); | |
| 245 | drawChalkFlash(); | |
| 246 | ctx.putImageData(image, 0, 0); | |
| 247 | } | |
| 248 | ||
| 249 | /* ---------- game: every rule is the rules script's ---------- */ | |
| 250 | function saveBest() { | |
| 251 | if (P.skips <= G.best) return; | |
| 252 | G.best = P.skips; | |
| 253 | try { localStorage.setItem(BEST_KEY, String(G.best)); } catch (e) { /* private window: keep it for this visit */ } | |
| 254 | } | |
| 255 | function react(before) { | |
| 256 | if (P.skips > before.skips) { | |
| 257 | G.flash = FLASH; | |
| 258 | sound(520, .07, 'triangle'); | |
| 259 | if (P.period < before.period) { note('the rope quickens'); sound(660, .1, 'triangle', .08); } | |
| 260 | if (P.skippers > before.skippers) note(P.period < before.period ? 'the rope quickens and a friend joins the line' : 'a friend joins the line'); | |
| 261 | } | |
| 262 | if (P.status === 'lost') { G.flash = FLASH * 2; G.overAt = G.time; G.noteUntil = 0; sound(120, .3, 'sawtooth'); saveBest(); } | |
| 263 | if (P.status === 'won') { G.flash = FLASH; G.overAt = G.time; G.noteUntil = 0; sound(523, .12, 'triangle'); sound(659, .12, 'triangle', .12); sound(784, .4, 'triangle', .24); saveBest(); } | |
| 264 | } | |
| 265 | function update(dt) { | |
| 266 | G.time += dt; | |
| 267 | if (G.flash > 0) G.flash -= dt; | |
| 268 | if (P.status === 'playing') { | |
| 269 | const before = P, r = M.tick(P, dt); | |
| 270 | if (r.ok) { P = r.state; react(before); } | |
| 271 | } | |
| 272 | if (G.time >= G.noteUntil) setText('status', P.status === 'playing' ? HINT : `${P.line} · tap or press Space to play again`); | |
| 273 | renderHud(); | |
| 274 | } | |
| 275 | function restart() { | |
| 276 | P = M.start(); G.flash = 0; G.noteUntil = 0; | |
| 277 | note(HINT); | |
| 278 | renderHud(); | |
| 279 | } | |
| 280 | function act() { | |
| 281 | ensureAudio(); | |
| 282 | if (P.status !== 'playing') { if (G.time - G.overAt > .6) restart(); return; } | |
| 283 | const r = M.jump(P); | |
| 284 | if (r.ok) { P = r.state; sound(300, .08, 'square'); } | |
| 285 | } | |
| 286 | ||
| 287 | /* ---------- input ---------- */ | |
| 288 | canvas.addEventListener('pointerdown', e => { canvas.focus({ preventScroll: true }); act(); }); | |
| 289 | canvas.addEventListener('contextmenu', e => e.preventDefault()); | |
| 290 | canvas.addEventListener('keydown', e => { | |
| 291 | if (e.ctrlKey || e.metaKey || e.altKey) return; | |
| 292 | if (e.key !== ' ' && e.key !== 'Enter' && e.key !== 'ArrowUp') return; | |
| 293 | e.preventDefault(); | |
| 294 | if (!e.repeat) act(); | |
| 295 | }); | |
| 296 | ||
| 297 | /* ---------- sound ---------- */ | |
| 298 | let audio = null, muted = false; | |
| 299 | function ensureAudio() { | |
| 300 | if (audio) { if (audio.state === 'suspended') audio.resume(); return; } | |
| 301 | const AC = window.AudioContext || window.webkitAudioContext; | |
| 302 | if (AC) audio = new AC(); | |
| 303 | } | |
| 304 | function sound(freq, life, type, delay = 0) { | |
| 305 | if (!audio || muted) return; | |
| 306 | const now = audio.currentTime + delay, gain = audio.createGain(), osc = audio.createOscillator(); | |
| 307 | osc.type = type; | |
| 308 | osc.frequency.setValueAtTime(freq, now); | |
| 309 | gain.gain.setValueAtTime(.0001, now); | |
| 310 | gain.gain.exponentialRampToValueAtTime(.06, now + .01); | |
| 311 | gain.gain.exponentialRampToValueAtTime(.0005, now + life); | |
| 312 | osc.connect(gain).connect(audio.destination); | |
| 313 | osc.start(now); osc.stop(now + life + .02); | |
| 314 | } | |
| 315 | const muteButton = document.getElementById('mute'); | |
| 316 | muteButton.addEventListener('click', () => { | |
| 317 | muted = !muted; | |
| 318 | muteButton.textContent = muted ? 'sound off' : 'sound on'; | |
| 319 | muteButton.setAttribute('aria-pressed', String(muted)); | |
| 320 | }); | |
| 321 | ||
| 322 | /* ---------- fit and boot ---------- */ | |
| 323 | addEventListener('resize', fit); | |
| 324 | fit(); | |
| 325 | bake(); | |
| 326 | renderHud(); | |
| 327 | note(HINT, 3); | |
| 328 | canvas.focus({ preventScroll: true }); | |
| 329 | let last = performance.now(); | |
| 330 | function frame(now) { | |
| 331 | update(Math.min(.04, (now - last) / 1000)); | |
| 332 | last = now; | |
| 333 | draw(); | |
| 334 | requestAnimationFrame(frame); | |
| 335 | } | |
| 336 | requestAnimationFrame(frame); | |
| 337 | </script> | |
| 338 | </body> | |
| 339 | </html> |
skipping-rope/page.test.mjs added +64
| @@ -0,0 +1,64 @@ | ||
| 1 | import { test, expect } from 'bun:test'; | |
| 2 | import { boot } from '../playtest.mjs'; | |
| 3 | ||
| 4 | // jump when the rules say the rope is `lead` seconds from the ground, until it has been skipped `n` times | |
| 5 | function skip(g, n, lead = .25) { | |
| 6 | while (g.read('P.status') === 'playing' && g.read('P.skips') < n) { | |
| 7 | if (g.read(`!M.airborne(P) && P.nextCross - P.t <= ${lead}`)) g.press(' ', .04); else g.run(.04); | |
| 8 | } | |
| 9 | } | |
| 10 | ||
| 11 | test('Space and a tap jump through the rules, and the HUD reports the rules state', () => { | |
| 12 | const g = boot('skipping-rope'); | |
| 13 | g.press(' '); | |
| 14 | expect(g.read('M.airborne(P)') || g.read('P.airUntil')).toBeTruthy(); | |
| 15 | skip(g, 3); | |
| 16 | expect(g.read('P.skips')).toBe(3); | |
| 17 | expect(g.el('skips').textContent).toBe('3'); | |
| 18 | expect(g.el('score').textContent).toBe(String(g.read('P.score'))); | |
| 19 | expect(g.el('skippers').textContent).toBe(String(g.read('P.skippers'))); | |
| 20 | expect(g.el('turn').textContent).toBe(g.read('P.period.toFixed(3)')); | |
| 21 | const t = boot('skipping-rope'); | |
| 22 | t.tap(180, 140); | |
| 23 | expect(t.read('P.airUntil')).toBeGreaterThan(0); | |
| 24 | }); | |
| 25 | ||
| 26 | test('the chalk line flashes the moment the rope touches the ground, and the picture shows it', () => { | |
| 27 | const g = boot('skipping-rope'); | |
| 28 | skip(g, 1); | |
| 29 | expect(g.read('G.flash')).toBeGreaterThan(0); | |
| 30 | const lit = g.picture(); | |
| 31 | g.run(.4); | |
| 32 | expect(g.read('G.flash')).toBeLessThanOrEqual(0); | |
| 33 | expect(g.picture()).not.toBe(lit); | |
| 34 | }); | |
| 35 | ||
| 36 | test('missing the rope ends the game with the rules line, and a later tap starts it again', () => { | |
| 37 | const g = boot('skipping-rope'); | |
| 38 | g.run(1.5); | |
| 39 | expect(g.read('P.status')).toBe('lost'); | |
| 40 | expect(g.el('status').textContent).toContain(g.read('P.line')); | |
| 41 | g.run(1); | |
| 42 | g.tap(180, 140); | |
| 43 | expect(g.read('P.status')).toBe('playing'); | |
| 44 | expect(g.read('P.skips')).toBe(0); | |
| 45 | }); | |
| 46 | ||
| 47 | test('mute toggles its label and aria-pressed', () => { | |
| 48 | const g = boot('skipping-rope'); | |
| 49 | g.el('mute').fire('click'); | |
| 50 | expect(g.el('mute').textContent).toBe('sound off'); | |
| 51 | expect(g.el('mute').attrs['aria-pressed']).toBe('true'); | |
| 52 | }); | |
| 53 | ||
| 54 | test('the page source says how to play, wears a cap, stills the grass, holds the rules to four lines and names no .mp3', async () => { | |
| 55 | const html = await Bun.file(new URL('./index.html', import.meta.url)).text(); | |
| 56 | expect(html).toMatch(/<canvas id="c"[^>]*aria-label="[^"]*Space[^"]*jump/); | |
| 57 | expect(html).toMatch(/min-height: 44px; min-width: 44px/); | |
| 58 | expect(html).toMatch(/id="mute"[^>]*aria-pressed="false"/); | |
| 59 | expect(html).toContain('const t = STILL ? 0'); | |
| 60 | expect(html).toContain('k === 0 ? YOU'); | |
| 61 | expect(html).not.toContain('.mp3'); | |
| 62 | const rules = html.match(/rules: \[([\s\S]*?)\]/)[1].split('\n').filter(l => l.trim()); | |
| 63 | expect(rules.length).toBeLessThanOrEqual(4); | |
| 64 | }); |
new above
offline