-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmario-level-1.html
More file actions
557 lines (510 loc) · 21.1 KB
/
Copy pathmario-level-1.html
File metadata and controls
557 lines (510 loc) · 21.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Mario 1-1 — Standalone Reference</title>
<style>
html, body {
margin: 0; padding: 0;
background: #201c29;
color: #e6e6e6;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
display: flex; flex-direction: column; align-items: center;
min-height: 100vh;
}
h1 { margin: 18px 0 6px; font-size: 16px; font-weight: 600; letter-spacing: .5px; }
canvas {
image-rendering: pixelated;
border: 2px solid #444;
border-radius: 6px;
background: #5c94fc;
}
.hud { margin-top: 10px; font-size: 12px; opacity: .7; }
.hud b { color: #ffd76a; }
</style>
</head>
<body>
<h1>Mario 1-1 — standalone reference</h1>
<canvas id="game" width="640" height="480"></canvas>
<div class="hud">
<b>←/A</b> <b>→/D</b> move · <b>SPACE/W/↑</b> jump (hold for higher) · <b>R</b> restart
</div>
<script>
// ─────────────────────────────────────────────────────────────────────────
// Mario 1-1 — single-file reference platformer.
//
// Designed to port 1:1 to CryptoBlocks:
// • Level is a list of strings (easy to store as a 2D list block)
// • All state lives on one `state` object
// • Physics + collision are short pure functions
// • Rendering is entirely from `state` (no DOM/React)
//
// Entities: mario + goombas. Tiles: ground, brick, ?-block, pipe, flag, pit.
// Mechanics: variable-height jump, AABB tile collision, stomp goombas,
// scrolling camera, death + restart, reach flag to win.
// ─────────────────────────────────────────────────────────────────────────
// ─── Constants ───────────────────────────────────────────────────────────
const TILE = 32;
const VIEW_W = 640;
const VIEW_H = 480;
const GRAVITY = 0.55;
const MOVE_ACCEL = 0.35;
const MAX_SPEED = 4;
const FRICTION = 0.82;
const JUMP_VEL = -11;
const JUMP_HOLD = -0.30; // extra lift while holding jump
const MAX_FALL = 12;
const GOOMBA_SPD = 1;
// ─── Level spec ──────────────────────────────────────────────────────────
// Declarative level: features described by position/size, not hand-typed
// strings. `compileLevel()` builds the mutable grid from this spec.
//
// Tile glyphs in the grid:
// ' ' sky G ground B brick Q ?-block U used ?-block
// P pipe body ^ pipe top F flag pole # flag base
const LEVEL_SPEC = {
width: 160,
height: 16,
groundRow: 14,
// Column ranges that have NO ground (pits). Inclusive.
pits: [[45, 52], [85, 91]],
// { col, height } — 2 tiles wide, `height` tall, sits on the ground
pipes: [
{ col: 22, height: 2 },
{ col: 35, height: 2 },
],
// Floating goodies on the "high" row
questionBlocks: [
{ col: 14, row: 5 },
{ col: 22, row: 5 },
{ col: 24, row: 5 },
],
// Individual bricks dropped at (col, row)
bricks: [
{ col: 21, row: 5 },
{ col: 23, row: 5 },
{ col: 25, row: 5 },
],
// Pyramid staircases — wide base of `width` at `row`, narrowing by
// `step` (and centering) for each row upward.
staircases: [
{ col: 67, row: 13, width: 12, step: 2 },
],
flag: { col: 135, topRow: 5, baseRow: 13 },
marioSpawn: { col: 2, row: 13 },
goombas: [
{ col: 26, row: 13 },
{ col: 52, row: 13 },
{ col: 90, row: 13 },
{ col: 112, row: 13 },
],
};
// Turn the declarative spec into a mutable 2D grid of glyph chars.
function compileLevel(spec) {
const g = Array.from({ length: spec.height }, () => new Array(spec.width).fill(' '));
// Ground — 2 rows thick, skipping pit ranges
const inPit = (x) => spec.pits.some(([a, b]) => x >= a && x <= b);
for (let x = 0; x < spec.width; x++) {
if (inPit(x)) continue;
g[spec.groundRow][x] = 'G';
if (spec.groundRow + 1 < spec.height) g[spec.groundRow + 1][x] = 'G';
}
// Pipes — 2 wide, `height` tall; top row is '^', rest is 'P'
for (const p of spec.pipes) {
const topRow = spec.groundRow - p.height;
for (let y = topRow; y < spec.groundRow; y++) {
const glyph = y === topRow ? '^' : 'P';
g[y][p.col] = glyph;
g[y][p.col + 1] = glyph;
}
}
// Bricks
for (const b of spec.bricks) g[b.row][b.col] = 'B';
// Question blocks
for (const q of spec.questionBlocks) g[q.row][q.col] = 'Q';
// Staircases — base at (col, row, width), each row up narrows and centers
for (const s of spec.staircases) {
let w = s.width, c = s.col, r = s.row;
while (w > 0 && r >= 0) {
for (let x = c; x < c + w; x++) g[r][x] = 'B';
w -= s.step;
c += Math.floor(s.step / 2);
r--;
}
}
// Flag — vertical pole, solid base block at the bottom
for (let y = spec.flag.topRow; y < spec.flag.baseRow; y++) g[y][spec.flag.col] = 'F';
g[spec.flag.baseRow][spec.flag.col] = '#';
return g;
}
const LEVEL_COLS = LEVEL_SPEC.width;
const LEVEL_ROWS = LEVEL_SPEC.height;
// Mutable working grid — bricks break, ?-blocks get used. Rebuilt on restart.
let grid = compileLevel(LEVEL_SPEC);
// Short-lived bump animations for visual feedback when a block is hit
let bumps = []; // { col, row, t } (t counts down)
// Tiles a character/enemy can't walk through.
function isSolid(glyph) {
return glyph === 'G' || glyph === 'B' || glyph === 'Q' || glyph === 'U' || glyph === 'P' || glyph === '^';
}
// What a tile looks like at a world position.
function tileAt(x, y) {
const col = Math.floor(x / TILE);
const row = Math.floor(y / TILE);
if (row < 0 || row >= LEVEL_ROWS) return ' ';
if (col < 0) return 'G'; // clamp left edge
if (col >= LEVEL_COLS) return ' '; // past the right edge = empty
return grid[row][col];
}
// ─── Game state ──────────────────────────────────────────────────────────
const SPAWN = { x: LEVEL_SPEC.marioSpawn.col * TILE, y: LEVEL_SPEC.marioSpawn.row * TILE };
const GOOMBA_SPAWNS = LEVEL_SPEC.goombas.map(g => ({ x: g.col * TILE, y: g.row * TILE }));
const state = {
mario: makeMario(),
goombas: GOOMBA_SPAWNS.map(makeGoomba),
camera: { x: 0, y: 0 },
score: 0,
coins: 0,
won: false,
keys: { left: false, right: false, jump: false, jumpPressed: false },
};
function makeMario() {
return { x: SPAWN.x, y: SPAWN.y, vx: 0, vy: 0, w: 24, h: 28, onGround: false, alive: true, facing: 1 };
}
function makeGoomba(s) {
return { x: s.x, y: s.y, vx: -GOOMBA_SPD, vy: 0, w: 28, h: 28, alive: true };
}
// ─── Input ───────────────────────────────────────────────────────────────
window.addEventListener('keydown', (e) => {
if (e.code === 'ArrowLeft' || e.code === 'KeyA') state.keys.left = true;
if (e.code === 'ArrowRight' || e.code === 'KeyD') state.keys.right = true;
if (e.code === 'Space' || e.code === 'ArrowUp' || e.code === 'KeyW') {
if (!state.keys.jump) state.keys.jumpPressed = true;
state.keys.jump = true;
}
if (e.code === 'KeyR') restart();
if (['ArrowLeft','ArrowRight','ArrowUp','ArrowDown','Space'].includes(e.code)) e.preventDefault();
});
window.addEventListener('keyup', (e) => {
if (e.code === 'ArrowLeft' || e.code === 'KeyA') state.keys.left = false;
if (e.code === 'ArrowRight' || e.code === 'KeyD') state.keys.right = false;
if (e.code === 'Space' || e.code === 'ArrowUp' || e.code === 'KeyW') state.keys.jump = false;
});
// ─── Collision ───────────────────────────────────────────────────────────
// Resolve motion along one axis at a time — classic tile-based AABB.
function moveAndCollide(e, dx, dy) {
// X axis
e.x += dx;
if (dx !== 0) {
const corners = [
{ x: e.x, y: e.y + 1 },
{ x: e.x + e.w, y: e.y + 1 },
{ x: e.x, y: e.y + e.h - 1 },
{ x: e.x + e.w, y: e.y + e.h - 1 },
];
for (const c of corners) {
if (!isSolid(tileAt(c.x, c.y))) continue;
if (dx > 0) e.x = Math.floor(c.x / TILE) * TILE - e.w;
else e.x = Math.floor(c.x / TILE + 1) * TILE;
e.vx = 0;
e.hitWall = true;
break;
}
}
// Y axis
e.y += dy;
e.onGround = false;
const hitFromBelow = []; // tiles this entity head-bumped this step
if (dy !== 0) {
const corners = [
{ x: e.x + 1, y: e.y },
{ x: e.x + e.w - 1, y: e.y },
{ x: e.x + 1, y: e.y + e.h },
{ x: e.x + e.w - 1, y: e.y + e.h },
];
for (const c of corners) {
const glyph = tileAt(c.x, c.y);
if (!isSolid(glyph)) continue;
if (dy > 0) { e.y = Math.floor(c.y / TILE) * TILE - e.h; e.onGround = true; }
else {
e.y = Math.floor(c.y / TILE + 1) * TILE;
hitFromBelow.push({ glyph, col: Math.floor(c.x / TILE), row: Math.floor(c.y / TILE) });
}
e.vy = 0;
break;
}
}
return { hitFromBelow };
}
function overlaps(a, b) {
return a.x < b.x + b.w && a.x + a.w > b.x && a.y < b.y + b.h && a.y + a.h > b.y;
}
// ─── Update ──────────────────────────────────────────────────────────────
function update() {
const m = state.mario;
if (!m.alive) { state.keys.jumpPressed = false; return; }
if (state.won) { state.keys.jumpPressed = false; return; }
// Horizontal input
if (state.keys.left) { m.vx -= MOVE_ACCEL; m.facing = -1; }
if (state.keys.right) { m.vx += MOVE_ACCEL; m.facing = 1; }
if (!state.keys.left && !state.keys.right) m.vx *= FRICTION;
if (Math.abs(m.vx) < 0.05) m.vx = 0;
m.vx = Math.max(-MAX_SPEED, Math.min(MAX_SPEED, m.vx));
// Jump: initial impulse on edge, hold-to-go-higher while rising
if (state.keys.jumpPressed && m.onGround) { m.vy = JUMP_VEL; }
else if (state.keys.jump && m.vy < 0) { m.vy += JUMP_HOLD; }
state.keys.jumpPressed = false;
// Gravity
m.vy = Math.min(m.vy + GRAVITY, MAX_FALL);
// Move + collide
moveAndCollide(m, m.vx, 0);
const hit = moveAndCollide(m, 0, m.vy);
// Break or use any block Mario head-bumped this frame
for (const h of hit.hitFromBelow) {
if (h.glyph === 'B') {
grid[h.row][h.col] = ' ';
state.score += 50;
bumps.push({ col: h.col, row: h.row, t: 12, broke: true });
} else if (h.glyph === 'Q') {
grid[h.row][h.col] = 'U';
state.score += 200;
state.coins += 1;
bumps.push({ col: h.col, row: h.row, t: 12, broke: false });
}
}
// Pit death
if (m.y > LEVEL_ROWS * TILE + 200) m.alive = false;
// Camera follows mario (right of center only — classic sidescroller feel)
const targetCamX = m.x - VIEW_W / 3;
state.camera.x = Math.max(state.camera.x, targetCamX);
state.camera.x = Math.max(0, Math.min(state.camera.x, LEVEL_COLS * TILE - VIEW_W));
// Goombas: walk, reverse at walls and at pit edges, fall with gravity
for (const g of state.goombas) {
if (!g.alive) continue;
g.vy = Math.min(g.vy + GRAVITY, MAX_FALL);
// Peek-then-flip: look one tile ahead and decide before we move.
// This avoids getting stuck when collision zeroes vx.
if (g.onGround) {
const ahead = g.vx > 0 ? g.x + g.w + 1 : g.x - 1;
const wallY = g.y + g.h / 2; // mid-body → hits a wall
const pitY = g.y + g.h + 2; // just below feet → no ground = pit
if (isSolid(tileAt(ahead, wallY))) g.vx = -g.vx;
else if (!isSolid(tileAt(ahead, pitY))) g.vx = -g.vx;
}
moveAndCollide(g, g.vx, 0);
moveAndCollide(g, 0, g.vy);
// Off the bottom of the level — despawn
if (g.y > LEVEL_ROWS * TILE + 200) g.alive = false;
// Mario collision
if (overlaps(m, g)) {
const stomping = m.vy > 0 && m.y + m.h - g.y < 16;
if (stomping) {
g.alive = false;
m.vy = JUMP_VEL * 0.55;
state.score += 100;
} else {
m.alive = false;
}
}
}
// Flag touch → win
const centerTile = tileAt(m.x + m.w / 2, m.y + m.h / 2);
if (centerTile === 'F' || centerTile === '#') state.won = true;
// Decay bump animations
bumps = bumps.filter(b => --b.t > 0);
}
// ─── Render ──────────────────────────────────────────────────────────────
const canvas = document.getElementById('game');
const ctx = canvas.getContext('2d');
ctx.imageSmoothingEnabled = false;
function drawTile(glyph, px, py) {
switch (glyph) {
case 'G': {
ctx.fillStyle = '#c07832'; ctx.fillRect(px, py, TILE, TILE);
ctx.fillStyle = '#e89c4c'; ctx.fillRect(px + 2, py + 2, TILE - 4, 6);
ctx.fillStyle = '#8a501d';
ctx.fillRect(px + 4, py + 14, 5, 3);
ctx.fillRect(px + 20, py + 20, 5, 3);
break;
}
case 'B': {
ctx.fillStyle = '#d47a2c'; ctx.fillRect(px, py, TILE, TILE);
ctx.strokeStyle = '#7c3a00'; ctx.lineWidth = 2;
ctx.strokeRect(px + 1, py + 1, TILE/2 - 2, TILE/2 - 2);
ctx.strokeRect(px + TILE/2 + 1, py + 1, TILE/2 - 2, TILE/2 - 2);
ctx.strokeRect(px + 1, py + TILE/2 + 1, TILE/2 - 2, TILE/2 - 2);
ctx.strokeRect(px + TILE/2 + 1, py + TILE/2 + 1, TILE/2 - 2, TILE/2 - 2);
break;
}
case 'Q': {
ctx.fillStyle = '#f9a634'; ctx.fillRect(px, py, TILE, TILE);
ctx.fillStyle = '#d07818'; ctx.fillRect(px + 2, py + TILE - 6, TILE - 4, 4);
ctx.fillStyle = '#fff'; ctx.font = 'bold 22px monospace';
ctx.textAlign = 'center'; ctx.fillText('?', px + TILE/2, py + 23);
ctx.textAlign = 'left';
break;
}
case 'U': {
ctx.fillStyle = '#8a5a1a'; ctx.fillRect(px, py, TILE, TILE);
ctx.fillStyle = '#5c3a0c'; ctx.fillRect(px + 2, py + TILE - 6, TILE - 4, 4);
ctx.strokeStyle = '#3a2208'; ctx.lineWidth = 1;
ctx.strokeRect(px + 2, py + 2, TILE - 4, TILE - 4);
break;
}
case 'P': case '^': {
ctx.fillStyle = '#2d882d'; ctx.fillRect(px, py, TILE, TILE);
ctx.fillStyle = '#4cbb4c'; ctx.fillRect(px + 3, py + 3, TILE - 6, TILE - 6);
if (glyph === '^') {
ctx.fillStyle = '#2d882d'; ctx.fillRect(px - 4, py, TILE + 8, 8);
ctx.fillStyle = '#4cbb4c'; ctx.fillRect(px - 2, py + 2, TILE + 4, 4);
}
break;
}
case 'F': {
ctx.fillStyle = '#888'; ctx.fillRect(px + 14, py, 4, TILE);
ctx.fillStyle = '#22cc66'; ctx.beginPath();
ctx.moveTo(px + 18, py + 4); ctx.lineTo(px + 30, py + 10); ctx.lineTo(px + 18, py + 16);
ctx.closePath(); ctx.fill();
break;
}
case '#': {
ctx.fillStyle = '#888'; ctx.fillRect(px + 14, py, 4, TILE);
ctx.fillStyle = '#333'; ctx.fillRect(px + 10, py + 20, 12, 12);
break;
}
}
}
function render() {
const camX = Math.floor(state.camera.x);
ctx.fillStyle = '#5c94fc';
ctx.fillRect(0, 0, VIEW_W, VIEW_H);
// Clouds (parallax)
ctx.fillStyle = 'rgba(255,255,255,0.9)';
for (let i = 0; i < 5; i++) {
const cx = ((i * 260) - camX * 0.3) % (LEVEL_COLS * TILE);
const cy = 50 + (i % 2) * 40;
ctx.beginPath();
ctx.arc(cx, cy, 18, 0, Math.PI * 2);
ctx.arc(cx + 20, cy + 4, 14, 0, Math.PI * 2);
ctx.arc(cx - 20, cy + 4, 14, 0, Math.PI * 2);
ctx.fill();
}
// Tiles
const startCol = Math.max(0, Math.floor(camX / TILE));
const endCol = Math.min(LEVEL_COLS, Math.ceil((camX + VIEW_W) / TILE) + 1);
for (let row = 0; row < LEVEL_ROWS; row++) {
for (let col = startCol; col < endCol; col++) {
const glyph = grid[row][col];
if (glyph === ' ') continue;
drawTile(glyph, col * TILE - camX, row * TILE);
}
}
// Bump animations — small upward pop on head-bumped block, or shards for breaks
for (const b of bumps) {
const px = b.col * TILE - camX;
const py = b.row * TILE;
const lift = Math.sin((1 - b.t / 12) * Math.PI) * -6;
if (b.broke) {
// shards flying up + out
const p = 1 - b.t / 12;
ctx.fillStyle = '#d47a2c';
const size = Math.max(4, 10 - Math.floor(p * 6));
ctx.fillRect(px + 4, py + lift - p * 24, size, size);
ctx.fillRect(px + TILE - 12, py + lift - p * 28, size, size);
ctx.fillRect(px + 6, py + lift - p * 18 + 12, size, size);
ctx.fillRect(px + TILE - 14, py + lift - p * 22 + 12, size, size);
} else {
// ?-block pop: render the used block lifted briefly
drawTile('U', px, py + lift);
// little coin popping up
const coinLift = Math.sin((1 - b.t / 12) * Math.PI) * -18;
ctx.fillStyle = '#ffd76a';
ctx.fillRect(px + TILE/2 - 3, py - 8 + coinLift, 6, 10);
}
}
// Goombas
for (const g of state.goombas) {
if (!g.alive) continue;
const gx = g.x - camX;
ctx.fillStyle = '#8b4513';
ctx.fillRect(gx + 2, g.y + 8, g.w - 4, g.h - 8);
// head
ctx.fillStyle = '#6d3410';
ctx.fillRect(gx, g.y, g.w, 10);
// feet
ctx.fillStyle = '#2a160a';
ctx.fillRect(gx + 2, g.y + g.h - 4, 8, 4);
ctx.fillRect(gx + g.w - 10, g.y + g.h - 4, 8, 4);
// eyes
ctx.fillStyle = '#fff';
ctx.fillRect(gx + 6, g.y + 4, 4, 4);
ctx.fillRect(gx + 18, g.y + 4, 4, 4);
ctx.fillStyle = '#000';
ctx.fillRect(gx + 7, g.y + 5, 2, 2);
ctx.fillRect(gx + 19, g.y + 5, 2, 2);
}
// Mario
const m = state.mario;
const mx = m.x - camX;
if (m.alive) {
ctx.fillStyle = '#e52521'; // hat / shirt
ctx.fillRect(mx, m.y, m.w, 8);
ctx.fillRect(mx + 2, m.y + 8, m.w - 4, 10);
ctx.fillStyle = '#fac090'; // face
ctx.fillRect(mx + 4, m.y + 6, m.w - 8, 8);
ctx.fillStyle = '#1657c2'; // overalls
ctx.fillRect(mx + 2, m.y + 16, m.w - 4, m.h - 16);
ctx.fillStyle = '#532f1a'; // shoes
ctx.fillRect(mx, m.y + m.h - 4, m.w, 4);
ctx.fillStyle = '#000'; // eye
const eyeX = m.facing > 0 ? mx + m.w - 8 : mx + 4;
ctx.fillRect(eyeX, m.y + 8, 2, 3);
} else {
// dead pose — grey box
ctx.fillStyle = '#555';
ctx.fillRect(mx, m.y, m.w, m.h);
}
// HUD
ctx.fillStyle = '#fff';
ctx.font = 'bold 14px ui-monospace, monospace';
ctx.fillText('SCORE ' + String(state.score).padStart(6, '0'), 12, 22);
ctx.fillStyle = '#ffd76a'; ctx.fillText('● ' + String(state.coins).padStart(2, '0'), VIEW_W / 2 - 30, 22);
ctx.fillStyle = '#fff'; ctx.fillText('WORLD 1-1', VIEW_W - 120, 22);
// Overlays
if (!m.alive) {
ctx.fillStyle = 'rgba(0,0,0,0.65)'; ctx.fillRect(0, 0, VIEW_W, VIEW_H);
ctx.fillStyle = '#fff'; ctx.font = 'bold 36px monospace';
ctx.textAlign = 'center';
ctx.fillText('GAME OVER', VIEW_W/2, VIEW_H/2);
ctx.font = '14px monospace';
ctx.fillText('press R to restart', VIEW_W/2, VIEW_H/2 + 32);
ctx.textAlign = 'left';
} else if (state.won) {
ctx.fillStyle = 'rgba(0,0,0,0.65)'; ctx.fillRect(0, 0, VIEW_W, VIEW_H);
ctx.fillStyle = '#ffd76a'; ctx.font = 'bold 36px monospace';
ctx.textAlign = 'center';
ctx.fillText('YOU WIN!', VIEW_W/2, VIEW_H/2);
ctx.font = '14px monospace'; ctx.fillStyle = '#fff';
ctx.fillText('press R to play again', VIEW_W/2, VIEW_H/2 + 32);
ctx.textAlign = 'left';
}
}
// ─── Game loop / restart ─────────────────────────────────────────────────
function restart() {
Object.assign(state.mario, makeMario());
state.goombas = GOOMBA_SPAWNS.map(makeGoomba);
state.camera.x = 0;
state.score = 0;
state.coins = 0;
state.won = false;
grid = compileLevel(LEVEL_SPEC);
bumps = [];
}
(function loop() {
update();
render();
requestAnimationFrame(loop);
})();
</script>
</body>
</html>