-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
1106 lines (952 loc) Β· 41.6 KB
/
Copy pathindex.html
File metadata and controls
1106 lines (952 loc) Β· 41.6 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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en" data-theme="dark">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
<title>Snake</title>
<style>
/* βββ CSS Custom Properties (themes) βββββββββββββββββββββββββββββββββββ */
:root[data-theme="dark"] {
--bg: #0d1117;
--surface: #161b22;
--border: #30363d;
--text: #e6edf3;
--text-muted: #8b949e;
--accent: #3ddc84;
--accent-dark: #2ab56a;
--food: #ff6b6b;
--food-glow: rgba(255, 107, 107, 0.5);
--grid-line: rgba(255, 255, 255, 0.04);
--canvas-bg: #0d1117;
}
:root[data-theme="light"] {
--bg: #f6f8fa;
--surface: #ffffff;
--border: #d0d7de;
--text: #24292f;
--text-muted: #57606a;
--accent: #1a7f37;
--accent-dark: #2da44e;
--food: #cf222e;
--food-glow: rgba(207, 34, 46, 0.4);
--grid-line: rgba(0, 0, 0, 0.05);
--canvas-bg: #f6f8fa;
}
/* βββ Reset & Base βββββββββββββββββββββββββββββββββββββββββββββββββββββ */
*, *::before, *::after { margin: 0; padding: 0; box-sizing: border-box; }
body {
background: var(--bg);
color: var(--text);
font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
min-height: 100svh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
padding: 16px;
gap: 12px;
transition: background 0.3s, color 0.3s;
}
/* βββ Header βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ */
.header {
display: flex;
align-items: center;
justify-content: space-between;
width: min(400px, 100%);
}
.title {
font-size: 1.4rem;
font-weight: 800;
letter-spacing: 4px;
text-transform: uppercase;
color: var(--accent);
}
.btn-group { display: flex; gap: 6px; }
.icon-btn {
width: 34px;
height: 34px;
border-radius: 8px;
border: 1px solid var(--border);
background: var(--surface);
color: var(--text);
font-size: 1rem;
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
transition: background 0.15s, transform 0.1s;
user-select: none;
}
.icon-btn:hover { background: var(--border); }
.icon-btn:active { transform: scale(0.93); }
/* βββ Score Row ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ */
.score-row {
display: flex;
align-items: stretch;
gap: 8px;
width: min(400px, 100%);
}
.score-box {
flex: 1;
background: var(--surface);
border: 1px solid var(--border);
border-radius: 10px;
padding: 8px 10px;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
gap: 2px;
}
.score-label {
font-size: 0.6rem;
font-weight: 600;
text-transform: uppercase;
letter-spacing: 1.5px;
color: var(--text-muted);
}
.score-value {
font-size: 1.5rem;
font-weight: 700;
color: var(--accent);
line-height: 1;
}
@keyframes score-bump {
0% { transform: scale(1); }
40% { transform: scale(1.35); }
100% { transform: scale(1); }
}
.score-value.bump { animation: score-bump 0.2s ease-out; }
/* Speed level dots */
.speed-dots {
display: flex;
gap: 3px;
align-items: center;
padding-top: 2px;
}
.speed-dot {
width: 6px;
height: 6px;
border-radius: 50%;
background: var(--border);
transition: background 0.25s;
}
.speed-dot.on { background: var(--accent); }
/* βββ Game Wrapper & Canvas ββββββββββββββββββββββββββββββββββββββββββββ */
.game-wrapper {
position: relative;
width: min(400px, 100%);
aspect-ratio: 1 / 1;
border-radius: 12px;
overflow: hidden;
border: 2px solid var(--border);
transition: border-color 0.3s;
}
canvas {
width: 100%;
height: 100%;
display: block;
image-rendering: pixelated;
}
/* βββ Overlay (start / pause / game-over) βββββββββββββββββββββββββββββ */
.overlay {
position: absolute;
inset: 0;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
gap: 14px;
background: rgba(0, 0, 0, 0.72);
backdrop-filter: blur(6px);
-webkit-backdrop-filter: blur(6px);
transition: opacity 0.25s;
}
.overlay.hidden {
opacity: 0;
pointer-events: none;
}
.overlay-emoji { font-size: 3rem; line-height: 1; }
.overlay-title {
font-size: 1.8rem;
font-weight: 800;
letter-spacing: 3px;
text-transform: uppercase;
color: var(--accent);
}
.overlay-sub {
font-size: 0.8rem;
color: #aaa;
text-align: center;
max-width: 220px;
line-height: 1.5;
}
.overlay-score-display {
font-size: 1.1rem;
color: var(--text);
font-weight: 600;
}
.play-btn {
background: var(--accent);
color: #000;
border: none;
border-radius: 8px;
padding: 11px 36px;
font-size: 0.95rem;
font-weight: 700;
letter-spacing: 1px;
text-transform: uppercase;
cursor: pointer;
margin-top: 4px;
transition: opacity 0.15s, transform 0.1s;
}
.play-btn:hover { opacity: 0.85; }
.play-btn:active { transform: scale(0.96); }
/* βββ Mobile D-Pad βββββββββββββββββββββββββββββββββββββββββββββββββββββ */
.dpad {
display: none; /* hidden by default, shown on touch */
grid-template-columns: repeat(3, 52px);
grid-template-rows: repeat(2, 52px);
gap: 6px;
justify-content: center;
}
/* Only render D-pad on touch-primary devices */
@media (hover: none) and (pointer: coarse) {
.dpad { display: grid; }
}
.dpad-btn {
background: var(--surface);
border: 1px solid var(--border);
border-radius: 10px;
color: var(--text);
font-size: 1.3rem;
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
user-select: none;
-webkit-user-select: none;
touch-action: manipulation;
transition: background 0.1s, transform 0.1s;
}
.dpad-btn:active { background: var(--border); transform: scale(0.9); }
/* Grid placement */
.dpad-up { grid-column: 2; grid-row: 1; }
.dpad-left { grid-column: 1; grid-row: 2; }
.dpad-down { grid-column: 2; grid-row: 2; }
.dpad-right { grid-column: 3; grid-row: 2; }
/* βββ Footer βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ */
footer {
font-size: 0.68rem;
color: var(--text-muted);
text-align: center;
letter-spacing: 0.5px;
}
</style>
</head>
<body>
<!-- ββ Header ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ -->
<header class="header">
<div class="title">Snake</div>
<div class="btn-group">
<button class="icon-btn" id="themeBtn" title="Toggle theme (T)">π</button>
<button class="icon-btn" id="pauseBtn" title="Pause / Resume (P)">βΈ</button>
<button class="icon-btn" id="soundBtn" title="Toggle sound (M)">π</button>
</div>
</header>
<!-- ββ Score Row ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ -->
<div class="score-row">
<div class="score-box">
<div class="score-label">Score</div>
<div class="score-value" id="scoreVal">0</div>
</div>
<div class="score-box">
<div class="score-label">Speed</div>
<div class="speed-dots" id="speedDots"></div>
</div>
<div class="score-box">
<div class="score-label">Best</div>
<div class="score-value" id="bestVal">0</div>
</div>
</div>
<!-- ββ Game Canvas + Overlay ββββββββββββββββββββββββββββββββββββββββββββββ -->
<div class="game-wrapper">
<canvas id="gameCanvas" width="400" height="400"></canvas>
<div class="overlay" id="overlay">
<div class="overlay-emoji" id="overlayEmoji">π</div>
<div class="overlay-title" id="overlayTitle">Snake</div>
<div class="overlay-sub" id="overlaySub">Arrow keys or WASD to move<br>P to pause Β· M to mute</div>
<div class="overlay-score-display" id="overlayScore"></div>
<button class="play-btn" id="playBtn">Start Game</button>
</div>
</div>
<!-- ββ Mobile D-Pad βββββββββββββββββββββββββββββββββββββββββββββββββββββββ -->
<div class="dpad" id="dpad" aria-label="Direction controls">
<button class="dpad-btn dpad-up" data-dir="UP" >β²</button>
<button class="dpad-btn dpad-left" data-dir="LEFT" >β</button>
<button class="dpad-btn dpad-down" data-dir="DOWN" >βΌ</button>
<button class="dpad-btn dpad-right" data-dir="RIGHT">βΆ</button>
</div>
<footer>Arrow keys / WASD Β· P = Pause Β· M = Mute Β· T = Theme</footer>
<!-- ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
JAVASCRIPT
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ -->
<script>
'use strict';
// ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// CONFIG β all magic numbers live here
// ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
const CONFIG = Object.freeze({
COLS: 20, // grid columns
ROWS: 20, // grid rows
CELL: 20, // pixels per cell (COLS Γ CELL = 400 = canvas size)
CANVAS_PX: 400,
INIT_INTERVAL: 150, // ms per game tick (lower = faster)
MIN_INTERVAL: 60, // fastest possible tick
SPEED_STEP: 5, // points earned before a speed bump
SPEED_DEC: 9, // ms removed per speed level
MAX_LVL_DOTS: 10, // number of speed-dot indicators
INIT_LENGTH: 3, // starting snake length
MIN_SWIPE: 20, // px threshold for swipe detection
});
// Direction unit vectors
const DIR = Object.freeze({
UP: { x: 0, y: -1 },
DOWN: { x: 0, y: 1 },
LEFT: { x: -1, y: 0 },
RIGHT: { x: 1, y: 0 },
});
// Maps each direction to the one that would cause a 180Β° reversal
const OPPOSITE = Object.freeze({
UP: 'DOWN', DOWN: 'UP', LEFT: 'RIGHT', RIGHT: 'LEFT',
});
// ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// AudioManager β Web Audio API synthesised SFX (no external files)
// ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
class AudioManager {
constructor() {
this.muted = false;
this._ctx = null; // lazy-init: AudioContext needs a user gesture first
}
/** Returns (or creates) the shared AudioContext. */
_ctx_() {
if (!this._ctx) {
this._ctx = new (window.AudioContext || window.webkitAudioContext)();
}
return this._ctx;
}
/**
* Play a synthesised tone.
* @param {number} freq - Hz
* @param {string} type - OscillatorNode type
* @param {number} dur - seconds
* @param {number} gain - initial gain 0β1
* @param {number} delay - seconds before sound starts
*/
_tone(freq, type, dur, gain = 0.25, delay = 0) {
if (this.muted) return;
try {
const ctx = this._ctx_();
const t = ctx.currentTime + delay;
const osc = ctx.createOscillator();
const vol = ctx.createGain();
osc.connect(vol);
vol.connect(ctx.destination);
osc.type = type;
osc.frequency.setValueAtTime(freq, t);
vol.gain.setValueAtTime(gain, t);
vol.gain.exponentialRampToValueAtTime(0.001, t + dur);
osc.start(t);
osc.stop(t + dur + 0.01);
} catch (_) { /* AudioContext may be blocked β fail silently */ }
}
/** Short ascending two-note chime when food is eaten. */
eat() {
this._tone(523, 'square', 0.08, 0.18); // C5
this._tone(659, 'square', 0.08, 0.14, 0.09); // E5
}
/** Descending crash sound on game over. */
die() {
[440, 370, 311, 261].forEach((f, i) =>
this._tone(f, 'sawtooth', 0.14, 0.22, i * 0.11)
);
}
}
// ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// Snake β encapsulates all snake state and movement
// ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
class Snake {
constructor() { this.reset(); }
reset() {
// Start in the horizontal centre heading right
const midX = Math.floor(CONFIG.COLS / 2);
const midY = Math.floor(CONFIG.ROWS / 2);
// body[0] is the head; segments are ordered head β tail
// i=0 β head at midX, i=1 β midX-1, i=2 β midX-2 (tail)
this.body = [];
for (let i = 0; i < CONFIG.INIT_LENGTH; i++) {
this.body.push({ x: midX - i, y: midY });
}
this.dir = 'RIGHT'; // current committed direction
this.nextDir = 'RIGHT'; // buffered input (applied on next tick)
this._queue = []; // allows exactly one queued direction change
this._grow = 0; // pending growth segments
}
get head() { return this.body[0]; }
get length() { return this.body.length; }
/**
* Buffer a direction change.
* Rejects 180Β° reversals and duplicate requests.
*/
enqueueDirection(dir) {
// Determine the "current effective direction" for reversal checks
const last = this._queue.length > 0 ? this._queue[this._queue.length - 1] : this.dir;
if (OPPOSITE[last] !== dir && last !== dir) {
this._queue.push(dir);
if (this._queue.length > 2) this._queue.shift(); // cap at 2 buffered inputs
}
}
/** Increase pending growth by n. */
grow(n = 1) { this._grow += n; }
/**
* Advance the snake one step.
* @returns {{ x, y }} β the new head position (for collision checking)
*/
step() {
// Consume next buffered direction
if (this._queue.length > 0) {
this.dir = this._queue.shift();
}
const d = DIR[this.dir];
const newHead = { x: this.head.x + d.x, y: this.head.y + d.y };
this.body.unshift(newHead);
if (this._grow > 0) {
this._grow--; // absorb tail removal (snake grows)
} else {
this.body.pop(); // normal: remove tail segment
}
return newHead;
}
/** Returns true if `pos` overlaps any body segment (skipping index 0 = head by default). */
hitsBody(pos, skipHead = true) {
return this.body
.slice(skipHead ? 1 : 0)
.some(s => s.x === pos.x && s.y === pos.y);
}
/** Returns true if `pos` is outside the grid boundaries. */
hitsWall(pos) {
return pos.x < 0 || pos.x >= CONFIG.COLS
|| pos.y < 0 || pos.y >= CONFIG.ROWS;
}
}
// ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// Food β manages food position and pulse animation
// ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
class Food {
constructor() {
this.pos = { x: 5, y: 5 };
this.phase = 0; // animation phase in radians
}
/**
* Spawn at a random free cell (not occupied by the snake).
* Returns false if the board is completely full.
*/
spawn(snake) {
const free = [];
for (let x = 0; x < CONFIG.COLS; x++) {
for (let y = 0; y < CONFIG.ROWS; y++) {
if (!snake.body.some(s => s.x === x && s.y === y)) {
free.push({ x, y });
}
}
}
if (free.length === 0) return false;
this.pos = free[(Math.random() * free.length) | 0];
this.phase = 0;
return true;
}
/** Advance pulse animation by dt milliseconds. */
tick(dt) {
this.phase = (this.phase + dt * 0.0042) % (Math.PI * 2);
}
}
// ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// Renderer β all canvas drawing, zero game logic
// ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
class Renderer {
constructor(canvas) {
this.canvas = canvas;
this.ctx = canvas.getContext('2d');
}
// ββ Helpers ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
/**
* Draws a rounded rectangle path.
* Uses native ctx.roundRect where available; falls back to bezier curves.
*/
_roundRect(x, y, w, h, r) {
const ctx = this.ctx;
if (typeof ctx.roundRect === 'function') {
ctx.roundRect(x, y, w, h, r);
return;
}
// Manual fallback for older browsers
ctx.moveTo(x + r, y);
ctx.lineTo(x + w - r, y);
ctx.quadraticCurveTo(x + w, y, x + w, y + r);
ctx.lineTo(x + w, y + h - r);
ctx.quadraticCurveTo(x + w, y + h, x + w - r, y + h);
ctx.lineTo(x + r, y + h);
ctx.quadraticCurveTo(x, y + h, x, y + h - r);
ctx.lineTo(x, y + r);
ctx.quadraticCurveTo(x, y, x + r, y);
ctx.closePath();
}
// ββ Scene drawing βββββββββββββββββββββββββββββββββββββββββββββββββββββ
/** Fill the canvas background. */
_drawBg(theme) {
const ctx = this.ctx;
ctx.fillStyle = theme === 'dark' ? '#0d1117' : '#f6f8fa';
ctx.fillRect(0, 0, CONFIG.CANVAS_PX, CONFIG.CANVAS_PX);
}
/** Draw the subtle grid lines. */
_drawGrid(theme) {
const ctx = this.ctx;
ctx.save();
ctx.strokeStyle = theme === 'dark'
? 'rgba(255,255,255,0.04)'
: 'rgba(0,0,0,0.05)';
ctx.lineWidth = 0.5;
for (let x = 0; x <= CONFIG.COLS; x++) {
ctx.beginPath();
ctx.moveTo(x * CONFIG.CELL, 0);
ctx.lineTo(x * CONFIG.CELL, CONFIG.CANVAS_PX);
ctx.stroke();
}
for (let y = 0; y <= CONFIG.ROWS; y++) {
ctx.beginPath();
ctx.moveTo(0, y * CONFIG.CELL);
ctx.lineTo(CONFIG.CANVAS_PX, y * CONFIG.CELL);
ctx.stroke();
}
ctx.restore();
}
/** Draw the pulsing food orb. */
_drawFood(food, theme) {
const ctx = this.ctx;
const { pos, phase } = food;
const cx = pos.x * CONFIG.CELL + CONFIG.CELL / 2;
const cy = pos.y * CONFIG.CELL + CONFIG.CELL / 2;
const pulse = Math.sin(phase); // β1 β¦ +1
const r = CONFIG.CELL * 0.36 + pulse * 0.9;
const color = theme === 'dark' ? '#ff6b6b' : '#cf222e';
ctx.save();
ctx.shadowColor = color;
ctx.shadowBlur = 12 + pulse * 4;
// Main body
ctx.fillStyle = color;
ctx.beginPath();
ctx.arc(cx, cy, r, 0, Math.PI * 2);
ctx.fill();
// Specular highlight
ctx.shadowBlur = 0;
ctx.fillStyle = 'rgba(255,255,255,0.45)';
ctx.beginPath();
ctx.arc(cx - r * 0.28, cy - r * 0.28, r * 0.28, 0, Math.PI * 2);
ctx.fill();
ctx.restore();
}
/** Draw the snake body with gradient colouring. */
_drawSnake(snake, theme) {
const ctx = this.ctx;
const len = snake.body.length;
snake.body.forEach((seg, i) => {
const t = i / Math.max(len - 1, 1); // 0 = head, 1 = tail
const pad = i === 0 ? 1 : 2; // head is slightly larger
const sz = CONFIG.CELL - pad * 2;
const x = seg.x * CONFIG.CELL + pad;
const y = seg.y * CONFIG.CELL + pad;
const rad = i === 0 ? 5 : 4;
if (theme === 'dark') {
// Bright green β dim teal towards tail
const g = Math.round(220 - t * 110);
const b = Math.round(100 - t * 60);
const a = 1 - t * 0.45;
ctx.fillStyle = i === 0
? '#3ddc84'
: `rgba(0, ${g}, ${b}, ${a})`;
} else {
// Dark green β medium green towards tail
const r = Math.round(20 + t * 80);
const g = Math.round(127 - t * 65);
const b = Math.round(50 - t * 30);
const a = 1 - t * 0.4;
ctx.fillStyle = i === 0
? '#1a7f37'
: `rgba(${r}, ${g}, ${b}, ${a})`;
}
ctx.beginPath();
this._roundRect(x, y, sz, sz, rad);
ctx.fill();
});
// Eyes drawn on top of the head
this._drawEyes(snake);
}
/** Draw the snake's eyes (positioned by current facing direction). */
_drawEyes(snake) {
const ctx = this.ctx;
const head = snake.body[0];
const dir = snake.dir;
const cx = head.x * CONFIG.CELL + CONFIG.CELL / 2;
const cy = head.y * CONFIG.CELL + CONFIG.CELL / 2;
const fwd = DIR[dir];
// Perpendicular axis to the direction of travel
const perp = (dir === 'UP' || dir === 'DOWN')
? { x: 1, y: 0 }
: { x: 0, y: 1 };
const reach = 3.8; // how far forward the eyes sit
const sep = 3.2; // half-separation between the two eyes
const eyeR = 2.2;
const pupR = 1.1;
const eyes = [
{ x: cx + fwd.x * reach + perp.x * sep, y: cy + fwd.y * reach + perp.y * sep },
{ x: cx + fwd.x * reach - perp.x * sep, y: cy + fwd.y * reach - perp.y * sep },
];
ctx.save();
// White sclerae
ctx.fillStyle = '#fff';
eyes.forEach(e => {
ctx.beginPath();
ctx.arc(e.x, e.y, eyeR, 0, Math.PI * 2);
ctx.fill();
});
// Dark pupils (shifted slightly forward for character)
ctx.fillStyle = '#111';
eyes.forEach(e => {
ctx.beginPath();
ctx.arc(e.x + fwd.x * 0.9, e.y + fwd.y * 0.9, pupR, 0, Math.PI * 2);
ctx.fill();
});
ctx.restore();
}
// ββ Public API ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
/** Render one complete frame. */
render(snake, food, theme) {
this._drawBg(theme);
this._drawGrid(theme);
this._drawFood(food, theme);
this._drawSnake(snake, theme);
}
}
// ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// Game β main orchestrator: lifecycle, input, loop, UI updates
// ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
class Game {
constructor() {
// ββ Sub-systems ββββββββββββββββββββββββββββββββββββββββββββββββββββ
this.snake = new Snake();
this.food = new Food();
this.renderer = new Renderer(document.getElementById('gameCanvas'));
this.audio = new AudioManager();
// ββ Persisted state ββββββββββββββββββββββββββββββββββββββββββββββββ
this.highScore = parseInt(localStorage.getItem('snakeHS') || '0', 10);
this.theme = localStorage.getItem('snakeTheme') || 'dark';
// ββ Runtime state ββββββββββββββββββββββββββββββββββββββββββββββββββ
this.score = 0;
this.running = false;
this.paused = false;
this.rafId = null;
// Fixed-timestep loop variables
this.lastTimestamp = 0;
this.accumulated = 0;
this.interval = CONFIG.INIT_INTERVAL; // ms per tick
// Touch tracking (swipe detection)
this._touchOrigin = null;
// ββ Bootstrap ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
this._buildSpeedDots();
this._bindUI();
this._applyTheme(false); // no re-render yet
this._setHighScore(this.highScore);
this.renderer.render(this.snake, this.food, this.theme); // first frame
}
// ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// UI helpers
// ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
_buildSpeedDots() {
const container = document.getElementById('speedDots');
for (let i = 0; i < CONFIG.MAX_LVL_DOTS; i++) {
const d = document.createElement('div');
d.className = 'speed-dot';
container.appendChild(d);
}
}
_showOverlay(visible) {
document.getElementById('overlay').classList.toggle('hidden', !visible);
}
_setScore(n) {
this.score = n;
const el = document.getElementById('scoreVal');
el.textContent = n;
el.classList.remove('bump');
void el.offsetWidth; // force reflow to restart the animation
el.classList.add('bump');
}
_setHighScore(n) {
this.highScore = n;
document.getElementById('bestVal').textContent = n;
}
_updateSpeedDots(level) {
document.querySelectorAll('.speed-dot').forEach((d, i) =>
d.classList.toggle('on', i < Math.min(level, CONFIG.MAX_LVL_DOTS))
);
}
_applyTheme(redraw = true) {
document.documentElement.setAttribute('data-theme', this.theme);
document.getElementById('themeBtn').textContent =
this.theme === 'dark' ? 'π' : 'βοΈ';
if (redraw) this.renderer.render(this.snake, this.food, this.theme);
}
// ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// Input binding
// ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
_bindUI() {
// ββ Header buttons ββββββββββββββββββββββββββββββββββββββββββββββββ
document.getElementById('themeBtn').addEventListener('click', () => {
this.theme = this.theme === 'dark' ? 'light' : 'dark';
localStorage.setItem('snakeTheme', this.theme);
this._applyTheme();
});
document.getElementById('pauseBtn').addEventListener('click', () => {
this._togglePause();
});
document.getElementById('soundBtn').addEventListener('click', () => {
this._toggleSound();
});
// ββ Overlay play button βββββββββββββββββββββββββββββββββββββββββββ
document.getElementById('playBtn').addEventListener('click', () => {
if (this.paused) this._togglePause();
else this._start();
});
// ββ Keyboard ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
window.addEventListener('keydown', e => this._onKey(e));
// ββ Mobile D-pad ββββββββββββββββββββββββββββββββββββββββββββββββββ
document.querySelectorAll('.dpad-btn').forEach(btn => {
// touchstart for instant response; mousedown for desktop testing
btn.addEventListener('touchstart', e => {
e.preventDefault();
this._onDirectionInput(btn.dataset.dir);
}, { passive: false });
btn.addEventListener('mousedown', () => {
this._onDirectionInput(btn.dataset.dir);
});
});
// ββ Swipe on canvas βββββββββββββββββββββββββββββββββββββββββββββββ
const canvas = document.getElementById('gameCanvas');
canvas.addEventListener('touchstart', e => {
e.preventDefault();
this._touchOrigin = { x: e.touches[0].clientX, y: e.touches[0].clientY };
}, { passive: false });
canvas.addEventListener('touchend', e => {
if (!this._touchOrigin) return;
const dx = e.changedTouches[0].clientX - this._touchOrigin.x;
const dy = e.changedTouches[0].clientY - this._touchOrigin.y;
const adx = Math.abs(dx), ady = Math.abs(dy);
if (adx < CONFIG.MIN_SWIPE && ady < CONFIG.MIN_SWIPE) return;
const dir = adx > ady
? (dx > 0 ? 'RIGHT' : 'LEFT')
: (dy > 0 ? 'DOWN' : 'UP');
this._onDirectionInput(dir);
this._touchOrigin = null;
}, { passive: false });
}
_onKey(e) {
const MAP = {
ArrowUp: 'UP', ArrowDown: 'DOWN', ArrowLeft: 'LEFT', ArrowRight: 'RIGHT',
w: 'UP', s: 'DOWN', a: 'LEFT', d: 'RIGHT',
W: 'UP', S: 'DOWN', A: 'LEFT', D: 'RIGHT',
};
// Prevent page scrolling on arrow/space keys
if (['ArrowUp','ArrowDown','ArrowLeft','ArrowRight',' '].includes(e.key)) {
e.preventDefault();
}
switch (e.key) {
case 'p': case 'P': this._togglePause(); return;
case 'm': case 'M': this._toggleSound(); return;
case 't': case 'T':
this.theme = this.theme === 'dark' ? 'light' : 'dark';
localStorage.setItem('snakeTheme', this.theme);
this._applyTheme();
return;
case 'Enter': case ' ':
if (!this.running || this.paused) {
if (this.paused) this._togglePause();
else this._start();
}
return;
}
const dir = MAP[e.key];
if (dir) this._onDirectionInput(dir);
}
/**
* Shared handler for all direction inputs (keyboard, D-pad, swipe).
* Starts or resumes the game if not currently active.
*/
_onDirectionInput(dir) {
if (!this.running) { this._start(); return; }
if (this.paused) { this._togglePause(); return; }
this.snake.enqueueDirection(dir);
}
// ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// Game lifecycle
// ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
/** Start (or restart) the game from scratch. */
_start() {
cancelAnimationFrame(this.rafId);
this.snake.reset();
this.food.spawn(this.snake);
this.score = 0;
this.interval = CONFIG.INIT_INTERVAL;
this.accumulated = 0;
this.running = true;
this.paused = false;
this._setScore(0);
this._updateSpeedDots(0);
this._showOverlay(false);
document.getElementById('pauseBtn').textContent = 'βΈ';
this.lastTimestamp = performance.now();
this.rafId = requestAnimationFrame(ts => this._loop(ts));
}
/** Toggle pause / resume. */
_togglePause() {
if (!this.running) return;
if (this.paused) {
// ββ Resume ββ
this.paused = false;
this.lastTimestamp = performance.now(); // reset to prevent time jump
this.accumulated = 0;
document.getElementById('pauseBtn').textContent = 'βΈ';
this._showOverlay(false);
this.rafId = requestAnimationFrame(ts => this._loop(ts));
} else {
// ββ Pause ββ
this.paused = true;
cancelAnimationFrame(this.rafId);
document.getElementById('pauseBtn').textContent = 'βΆ';
// Update overlay content then show
document.getElementById('overlayEmoji').textContent = 'βΈ';
document.getElementById('overlayTitle').textContent = 'Paused';
document.getElementById('overlaySub').textContent = 'Press P or βΆ to resume';
document.getElementById('overlayScore').textContent = '';
document.getElementById('playBtn').textContent = 'Resume';
this._showOverlay(true);
}
}
_toggleSound() {
this.audio.muted = !this.audio.muted;
document.getElementById('soundBtn').textContent =
this.audio.muted ? 'π' : 'π';
}
/** Trigger game-over state. */
_gameOver() {
this.running = false;
cancelAnimationFrame(this.rafId);
this.audio.die();
// Persist high score
if (this.score > this.highScore) {
localStorage.setItem('snakeHS', this.score);
this._setHighScore(this.score);
}
// Show game-over overlay
document.getElementById('overlayEmoji').textContent = 'π';
document.getElementById('overlayTitle').textContent = 'Game Over';
document.getElementById('overlaySub').textContent = 'Better luck next time!';
document.getElementById('overlayScore').textContent = `Score: ${this.score}`;
document.getElementById('playBtn').textContent = 'Play Again';
this._showOverlay(true);