-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
1150 lines (1071 loc) · 34.8 KB
/
index.html
File metadata and controls
1150 lines (1071 loc) · 34.8 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">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<title>Flappy Modern</title>
<style>
:root {
--sky-top: #6ec8ff;
--sky-bot: #b8e8ff;
--ink: #0b1f2a;
}
* { box-sizing: border-box; margin: 0; padding: 0; }
html, body {
height: 100%;
width: 100%;
overflow: hidden;
background: #000;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
color: var(--ink);
user-select: none;
-webkit-user-select: none;
-webkit-tap-highlight-color: transparent;
}
#wrap {
position: fixed;
inset: 0;
display: grid;
place-items: center;
background: radial-gradient(ellipse at top, #0b2030, #050a12);
}
canvas {
display: block;
background: linear-gradient(to bottom, var(--sky-top), var(--sky-bot));
border-radius: 18px;
box-shadow: 0 30px 80px rgba(0,0,0,.6), 0 0 0 1px rgba(255,255,255,.06) inset;
touch-action: manipulation;
cursor: pointer;
}
#hud {
position: fixed;
top: 18px;
left: 0;
width: 100%;
text-align: center;
pointer-events: none;
font-weight: 800;
font-size: 56px;
color: #fff;
text-shadow:
0 3px 0 rgba(0,0,0,.35),
0 0 22px rgba(0,0,0,.25);
letter-spacing: 1px;
transition: transform .08s ease;
}
#hud.pop { transform: scale(1.18); }
#coinHud {
position: fixed;
top: 22px;
right: 22px;
background: rgba(0,0,0,.35);
backdrop-filter: blur(6px);
-webkit-backdrop-filter: blur(6px);
color: #ffd93a;
font-weight: 800;
font-size: 18px;
padding: 8px 14px 8px 10px;
border-radius: 999px;
display: none;
align-items: center;
gap: 8px;
pointer-events: none;
text-shadow: 0 2px 0 rgba(0,0,0,.4);
transition: transform .08s ease;
}
#coinHud.pop { transform: scale(1.18); }
#coinHud.on { display: flex; }
#coinHud .dot {
width: 18px; height: 18px; border-radius: 50%;
background: radial-gradient(circle at 30% 30%, #fff2a8, #ffcf3a 60%, #a77900);
box-shadow: 0 0 10px rgba(255,207,58,.6);
}
#pauseHint {
position: fixed;
bottom: 16px;
left: 50%;
transform: translateX(-50%);
color: rgba(255,255,255,.55);
font-size: 11px;
letter-spacing: 1px;
text-transform: uppercase;
pointer-events: none;
display: none;
}
#pauseHint.on { display: block; }
#pauseOverlay {
position: fixed;
inset: 0;
display: none;
place-items: center;
background: rgba(5,10,18,.55);
backdrop-filter: blur(4px);
-webkit-backdrop-filter: blur(4px);
color: #fff;
font-size: 42px;
font-weight: 800;
letter-spacing: 4px;
pointer-events: none;
z-index: 5;
}
#pauseOverlay.on { display: grid; }
#overlay {
position: fixed;
inset: 0;
display: grid;
place-items: center;
pointer-events: none;
}
.card {
pointer-events: auto;
background: rgba(255,255,255,.92);
backdrop-filter: blur(10px);
-webkit-backdrop-filter: blur(10px);
border-radius: 20px;
padding: 28px 32px;
text-align: center;
box-shadow: 0 20px 60px rgba(0,0,0,.35);
max-width: 340px;
width: calc(100% - 48px);
transform: translateY(8px);
opacity: 0;
transition: opacity .25s ease, transform .25s ease;
}
.card.show { opacity: 1; transform: translateY(0); }
.card h1 {
font-size: 32px;
margin-bottom: 6px;
letter-spacing: .5px;
}
.card p { font-size: 15px; color: #39516a; margin-bottom: 14px; }
.scores {
display: flex;
justify-content: space-around;
gap: 12px;
margin: 10px 0 18px;
}
.score-box {
flex: 1;
background: #eef5fb;
border-radius: 12px;
padding: 10px;
}
.score-box .label {
font-size: 11px;
text-transform: uppercase;
letter-spacing: 1.5px;
color: #6b85a0;
}
.score-box .value {
font-size: 28px;
font-weight: 800;
color: var(--ink);
}
button {
font: inherit;
font-weight: 700;
font-size: 16px;
padding: 12px 22px;
border: none;
border-radius: 12px;
background: linear-gradient(180deg, #ffce3a, #ffae00);
color: #3b2900;
cursor: pointer;
box-shadow: 0 6px 0 #c78300, 0 10px 20px rgba(0,0,0,.2);
transition: transform .08s ease, box-shadow .08s ease;
}
button:active {
transform: translateY(3px);
box-shadow: 0 3px 0 #c78300, 0 6px 14px rgba(0,0,0,.2);
}
.hint { font-size: 12px; color: #7a93aa; margin-top: 10px; }
.medal {
font-size: 46px;
margin: 6px 0 2px;
line-height: 1;
}
</style>
</head>
<body>
<div id="wrap">
<canvas id="game"></canvas>
</div>
<div id="hud"></div>
<div id="coinHud"><span class="dot"></span><span id="coinCount">0</span></div>
<div id="pauseHint">P to pause</div>
<div id="pauseOverlay">PAUSED</div>
<div id="overlay">
<div id="startCard" class="card show">
<h1>Flappy Modern</h1>
<p>Tap, click, or press <b>Space</b> to flap. Thread the pipes.</p>
<button id="startBtn">Start</button>
<div class="hint">Best: <span id="bestStart">0</span> · Coins: <span id="coinStart">0</span></div>
</div>
<div id="overCard" class="card" style="display:none">
<div class="medal" id="medal"></div>
<h1 id="overTitle">Game Over</h1>
<div class="scores">
<div class="score-box"><div class="label">Score</div><div class="value" id="finalScore">0</div></div>
<div class="score-box"><div class="label">Best</div><div class="value" id="bestScore">0</div></div>
<div class="score-box"><div class="label">Coins</div><div class="value" id="runCoins">0</div></div>
</div>
<div class="hint" id="coinTotalLine" style="margin-bottom:14px">Total coins: <span id="coinTotal">0</span></div>
<button id="againBtn">Play Again</button>
<div class="hint">Space / click / tap to flap</div>
</div>
</div>
<script>
(() => {
'use strict';
const canvas = document.getElementById('game');
const ctx = canvas.getContext('2d');
const hud = document.getElementById('hud');
const startCard = document.getElementById('startCard');
const overCard = document.getElementById('overCard');
const startBtn = document.getElementById('startBtn');
const againBtn = document.getElementById('againBtn');
const bestStart = document.getElementById('bestStart');
const finalScoreEl = document.getElementById('finalScore');
const bestScoreEl = document.getElementById('bestScore');
const medalEl = document.getElementById('medal');
const overTitleEl = document.getElementById('overTitle');
const coinHud = document.getElementById('coinHud');
const coinCountEl = document.getElementById('coinCount');
const runCoinsEl = document.getElementById('runCoins');
const coinTotalEl = document.getElementById('coinTotal');
const coinStartEl = document.getElementById('coinStart');
const pauseHint = document.getElementById('pauseHint');
const pauseOverlay = document.getElementById('pauseOverlay');
// --- Virtual resolution ---------------------------------------------------
// We render at a fixed logical resolution and scale to fit the window,
// preserving aspect ratio. This keeps physics resolution-independent.
const VW = 480;
const VH = 800;
let dpr = Math.max(1, Math.min(window.devicePixelRatio || 1, 2));
let scale = 1;
function resize() {
const pad = 12;
const maxW = window.innerWidth - pad * 2;
const maxH = window.innerHeight - pad * 2;
const s = Math.min(maxW / VW, maxH / VH);
scale = s;
canvas.style.width = (VW * s) + 'px';
canvas.style.height = (VH * s) + 'px';
dpr = Math.max(1, Math.min(window.devicePixelRatio || 1, 2));
canvas.width = Math.round(VW * s * dpr);
canvas.height = Math.round(VH * s * dpr);
ctx.setTransform(s * dpr, 0, 0, s * dpr, 0, 0);
}
window.addEventListener('resize', resize);
resize();
// --- Game constants -------------------------------------------------------
const GROUND_H = 96;
const PLAY_H = VH - GROUND_H;
const GRAVITY = 1400; // px / s^2
const FLAP_V = -430; // px / s
const MAX_VY = 800;
const SCROLL_V0 = 170; // base pipe speed
const SCROLL_VMAX = 300;
const PIPE_W = 78;
const PIPE_GAP0 = 190;
const PIPE_GAPMIN = 130;
const PIPE_SPACING0 = 260;
const PIPE_SPACINGMIN = 210;
const COIN_R = 10;
const COIN_SPAWN_CHANCE = 0.65; // per pipe
const POWERUP_R = 14;
const POWERUP_SPAWN_CHANCE = 0.08; // per pipe
const SHIELD_PULSE_R = 28;
const CLOSECALL_THRESHOLD = 16; // px from pipe edge counts as close call
const SLOWMO_DUR = 0.35;
const SLOWMO_SCALE = 0.35;
const DAYNIGHT_CYCLE_SCORES = 40; // one full day->night over this many points
// --- State ----------------------------------------------------------------
const STATE = { READY: 0, PLAY: 1, DEAD: 2, OVER: 3 };
let state = STATE.READY;
const bird = {
x: VW * 0.3,
y: VH * 0.45,
vy: 0,
r: 18,
rot: 0,
flapPhase: 0,
};
const pipes = []; // { x, gapY, gapH, passed, coinTaken, powerTaken }
const particles = []; // { x, y, vx, vy, life, max, color, size }
const clouds = [];
const trailPuffs = []; // soft puffs behind bird
const coins = []; // { x, y, phase, taken }
const powerups = []; // { x, y, type, phase }
const sparkles = []; // { x, y, vx, vy, life, max, size }
const stars = []; // { x, y, s, twinkle }
let score = 0;
let runCoins = 0;
let best = 0;
let coinTotal = 0;
try { best = parseInt(localStorage.getItem('flappyModernBest') || '0', 10) || 0; } catch(e) {}
try { coinTotal = parseInt(localStorage.getItem('flappyModernCoins') || '0', 10) || 0; } catch(e) {}
bestStart.textContent = best;
coinStartEl.textContent = coinTotal;
// Power-ups / effects
let shieldTime = 0; // remaining seconds of shield
let slowmoTime = 0; // remaining seconds of slow-mo
let paused = false;
let timeSinceSpawn = 0;
let elapsed = 0;
let scrollV = SCROLL_V0;
let pipeGap = PIPE_GAP0;
let pipeSpacing = PIPE_SPACING0;
let shake = 0;
let shakeSeed = 0;
let flashAlpha = 0;
let groundOffset = 0;
// --- Audio (WebAudio tones — no assets) -----------------------------------
let audioCtx = null;
function ensureAudio() {
if (audioCtx) return;
try {
const AC = window.AudioContext || window.webkitAudioContext;
if (AC) audioCtx = new AC();
} catch(e) {}
}
function beep(freq, dur, type = 'square', gain = 0.06) {
if (!audioCtx) return;
const t = audioCtx.currentTime;
const osc = audioCtx.createOscillator();
const g = audioCtx.createGain();
osc.type = type;
osc.frequency.setValueAtTime(freq, t);
g.gain.setValueAtTime(0, t);
g.gain.linearRampToValueAtTime(gain, t + 0.01);
g.gain.exponentialRampToValueAtTime(0.0001, t + dur);
osc.connect(g).connect(audioCtx.destination);
osc.start(t);
osc.stop(t + dur + 0.02);
}
const sfx = {
flap: () => beep(520, 0.08, 'triangle', 0.05),
score: () => { beep(880, 0.07, 'square', 0.05); setTimeout(() => beep(1175, 0.09, 'square', 0.05), 70); },
hit: () => beep(120, 0.2, 'sawtooth', 0.1),
die: () => beep(80, 0.4, 'sawtooth', 0.1),
coin: () => { beep(1320, 0.06, 'square', 0.05); setTimeout(() => beep(1760, 0.08, 'square', 0.05), 55); },
power: () => { beep(660, 0.08, 'triangle', 0.06); setTimeout(() => beep(990, 0.1, 'triangle', 0.06), 70); setTimeout(() => beep(1320, 0.14, 'triangle', 0.06), 150); },
shieldHit: () => { beep(320, 0.15, 'square', 0.08); beep(480, 0.15, 'square', 0.05); },
};
// --- Clouds / parallax ----------------------------------------------------
function seedClouds() {
clouds.length = 0;
for (let i = 0; i < 7; i++) {
clouds.push({
x: Math.random() * VW,
y: 40 + Math.random() * (PLAY_H * 0.55),
s: 0.4 + Math.random() * 0.9,
speed: 10 + Math.random() * 30,
});
}
}
seedClouds();
function seedStars() {
stars.length = 0;
for (let i = 0; i < 50; i++) {
stars.push({
x: Math.random() * VW,
y: Math.random() * (PLAY_H * 0.7),
s: 0.6 + Math.random() * 1.4,
twinkle: Math.random() * Math.PI * 2,
});
}
}
seedStars();
// --- Reset / spawn --------------------------------------------------------
function reset() {
bird.x = VW * 0.3;
bird.y = VH * 0.45;
bird.vy = 0;
bird.rot = 0;
pipes.length = 0;
particles.length = 0;
trailPuffs.length = 0;
coins.length = 0;
powerups.length = 0;
sparkles.length = 0;
score = 0;
runCoins = 0;
elapsed = 0;
timeSinceSpawn = 0;
scrollV = SCROLL_V0;
pipeGap = PIPE_GAP0;
pipeSpacing = PIPE_SPACING0;
shake = 0;
flashAlpha = 0;
shieldTime = 0;
slowmoTime = 0;
paused = false;
pauseOverlay.classList.remove('on');
hud.textContent = '';
coinCountEl.textContent = '0';
}
function spawnPipe() {
const margin = 60;
const minY = margin + pipeGap * 0.5;
const maxY = PLAY_H - margin - pipeGap * 0.5;
const gapY = minY + Math.random() * (maxY - minY);
const px = VW + 20;
pipes.push({ x: px, gapY, gapH: pipeGap, passed: false });
// Power-up is rare and replaces the coin; don't spawn both
if (Math.random() < POWERUP_SPAWN_CHANCE) {
powerups.push({ x: px + PIPE_W / 2, y: gapY, type: 'shield', phase: Math.random() * Math.PI * 2 });
} else if (Math.random() < COIN_SPAWN_CHANCE) {
coins.push({ x: px + PIPE_W / 2, y: gapY, phase: Math.random() * Math.PI * 2, taken: false });
}
}
// --- Input ----------------------------------------------------------------
function flap() {
if (state === STATE.READY) {
startGame();
return;
}
if (state === STATE.PLAY) {
bird.vy = FLAP_V;
bird.flapPhase = 1;
sfx.flap();
// trail burst
for (let i = 0; i < 4; i++) {
trailPuffs.push({
x: bird.x - 10, y: bird.y + 4,
vx: -20 - Math.random() * 30, vy: 20 + Math.random() * 30,
life: 0.5, max: 0.5, size: 5 + Math.random() * 4,
});
}
}
}
function startGame() {
ensureAudio();
reset();
state = STATE.PLAY;
hideCard(startCard);
hideCard(overCard);
bird.vy = FLAP_V * 0.7;
coinHud.classList.add('on');
pauseHint.classList.add('on');
}
function die() {
if (state !== STATE.PLAY) return;
if (shieldTime > 0) {
// Absorb hit: pop shield, brief slow-mo + flash, small bump
shieldTime = 0;
slowmoTime = Math.max(slowmoTime, 0.25);
shake = 10;
flashAlpha = 0.35;
bird.vy = FLAP_V * 0.9;
sfx.shieldHit();
for (let i = 0; i < 20; i++) {
const a = Math.random() * Math.PI * 2;
const sp = 120 + Math.random() * 140;
sparkles.push({
x: bird.x, y: bird.y,
vx: Math.cos(a) * sp, vy: Math.sin(a) * sp,
life: 0.4, max: 0.4, size: 2 + Math.random() * 2,
});
}
return;
}
state = STATE.DEAD;
sfx.hit();
shake = 18;
flashAlpha = 0.8;
slowmoTime = 0.5;
// explosion
for (let i = 0; i < 36; i++) {
const a = Math.random() * Math.PI * 2;
const sp = 120 + Math.random() * 260;
particles.push({
x: bird.x, y: bird.y,
vx: Math.cos(a) * sp,
vy: Math.sin(a) * sp - 40,
life: 0.7 + Math.random() * 0.5,
max: 1.2,
color: ['#ffd93a','#ff8c2a','#ff4a3a','#fff1a8'][(Math.random()*4)|0],
size: 3 + Math.random() * 4,
});
}
}
function gameOver() {
state = STATE.OVER;
sfx.die();
if (score > best) {
best = score;
try { localStorage.setItem('flappyModernBest', String(best)); } catch(e) {}
}
coinTotal += runCoins;
try { localStorage.setItem('flappyModernCoins', String(coinTotal)); } catch(e) {}
finalScoreEl.textContent = score;
bestScoreEl.textContent = best;
runCoinsEl.textContent = runCoins;
coinTotalEl.textContent = coinTotal;
coinStartEl.textContent = coinTotal;
medalEl.textContent = medalFor(score);
overTitleEl.textContent = score > 0 && score === best && score > 3 ? 'New Best!' : 'Game Over';
coinHud.classList.remove('on');
pauseHint.classList.remove('on');
pauseOverlay.classList.remove('on');
showCard(overCard);
}
function medalFor(s) {
if (s >= 40) return '🏆';
if (s >= 20) return '🥇';
if (s >= 10) return '🥈';
if (s >= 5) return '🥉';
return '';
}
function showCard(el) {
el.style.display = 'block';
requestAnimationFrame(() => el.classList.add('show'));
}
function hideCard(el) {
el.classList.remove('show');
setTimeout(() => { el.style.display = 'none'; }, 250);
}
// Controls
window.addEventListener('keydown', (e) => {
if (e.code === 'Space' || e.code === 'ArrowUp' || e.code === 'KeyW') {
e.preventDefault();
if (state === STATE.OVER) { startGame(); return; }
if (paused) return;
flap();
} else if (e.code === 'KeyR' && state === STATE.OVER) {
startGame();
} else if (e.code === 'KeyP') {
if (state === STATE.PLAY || state === STATE.DEAD) {
paused = !paused;
pauseOverlay.classList.toggle('on', paused);
}
}
});
canvas.addEventListener('pointerdown', (e) => {
e.preventDefault();
if (state === STATE.OVER) { startGame(); return; }
if (paused) return;
flap();
});
startBtn.addEventListener('click', startGame);
againBtn.addEventListener('click', startGame);
// --- Collision ------------------------------------------------------------
function circleRect(cx, cy, cr, rx, ry, rw, rh) {
const tx = Math.max(rx, Math.min(cx, rx + rw));
const ty = Math.max(ry, Math.min(cy, ry + rh));
const dx = cx - tx, dy = cy - ty;
return dx * dx + dy * dy <= cr * cr;
}
// --- Update ---------------------------------------------------------------
function update(dtRaw) {
if (paused) return;
// Apply slow-mo to gameplay, not UI animations
const timeScale = slowmoTime > 0 ? SLOWMO_SCALE : 1;
if (slowmoTime > 0) slowmoTime = Math.max(0, slowmoTime - dtRaw);
const dt = dtRaw * timeScale;
elapsed += dtRaw;
// Clouds always move (real-time, not slow-mo)
for (const c of clouds) {
c.x -= c.speed * dtRaw * 0.4;
if (c.x < -60) { c.x = VW + 20; c.y = 40 + Math.random() * (PLAY_H * 0.55); c.s = 0.4 + Math.random() * 0.9; }
}
for (const s of stars) s.twinkle += dtRaw * 3;
if (state === STATE.READY) {
// Idle bob
bird.y = VH * 0.45 + Math.sin(elapsed * 3) * 8;
bird.rot = Math.sin(elapsed * 3) * 0.08;
groundOffset = (groundOffset + SCROLL_V0 * dtRaw) % 48;
return;
}
// Difficulty ramp — gentle curve, bounded
const ramp = Math.min(1, score / 30);
scrollV = SCROLL_V0 + (SCROLL_VMAX - SCROLL_V0) * ramp;
pipeGap = PIPE_GAP0 - (PIPE_GAP0 - PIPE_GAPMIN) * ramp;
pipeSpacing = PIPE_SPACING0 - (PIPE_SPACING0 - PIPE_SPACINGMIN)* ramp;
if (state === STATE.PLAY) {
// Bird physics
bird.vy = Math.min(MAX_VY, bird.vy + GRAVITY * dt);
bird.y += bird.vy * dt;
bird.rot = Math.max(-0.5, Math.min(1.2, bird.vy / 500));
bird.flapPhase = Math.max(0, bird.flapPhase - dt * 3);
// Pipes
timeSinceSpawn += dt;
const spawnInterval = pipeSpacing / scrollV;
if (timeSinceSpawn >= spawnInterval) {
timeSinceSpawn = 0;
spawnPipe();
}
for (const p of pipes) p.x -= scrollV * dt;
for (const c of coins) c.x -= scrollV * dt;
for (const pu of powerups) pu.x -= scrollV * dt;
// Score + close-call detection + cull
for (let i = pipes.length - 1; i >= 0; i--) {
const p = pipes[i];
if (!p.passed && p.x + PIPE_W < bird.x) {
p.passed = true;
score++;
sfx.score();
hud.textContent = score;
hud.classList.remove('pop');
void hud.offsetWidth; // restart animation
hud.classList.add('pop');
// Close-call: bird was threading near the edge of the gap
const dTop = bird.y - (p.gapY - p.gapH / 2);
const dBot = (p.gapY + p.gapH / 2) - bird.y;
const edge = Math.min(dTop, dBot);
if (edge < CLOSECALL_THRESHOLD + bird.r) {
slowmoTime = Math.max(slowmoTime, SLOWMO_DUR);
for (let k = 0; k < 12; k++) {
const a = Math.random() * Math.PI * 2;
const sp = 60 + Math.random() * 80;
sparkles.push({
x: bird.x, y: bird.y,
vx: Math.cos(a) * sp, vy: Math.sin(a) * sp,
life: 0.35, max: 0.35, size: 1.5 + Math.random() * 2,
});
}
}
}
if (p.x + PIPE_W < -30) pipes.splice(i, 1);
}
if (hud.textContent === '' && score === 0) hud.textContent = '0';
// Coins: collect + cull
for (let i = coins.length - 1; i >= 0; i--) {
const c = coins[i];
c.phase += dtRaw * 4;
if (!c.taken) {
const dx = bird.x - c.x, dy = bird.y - c.y;
if (dx * dx + dy * dy < (bird.r + COIN_R) * (bird.r + COIN_R)) {
c.taken = true;
runCoins++;
sfx.coin();
coinCountEl.textContent = runCoins;
coinHud.classList.remove('pop');
void coinHud.offsetWidth;
coinHud.classList.add('pop');
for (let k = 0; k < 8; k++) {
const a = Math.random() * Math.PI * 2;
sparkles.push({
x: c.x, y: c.y,
vx: Math.cos(a) * 120, vy: Math.sin(a) * 120 - 30,
life: 0.4, max: 0.4, size: 2 + Math.random() * 2,
});
}
}
}
if (c.x < -30 || c.taken) coins.splice(i, 1);
}
// Power-ups: collect + cull
for (let i = powerups.length - 1; i >= 0; i--) {
const pu = powerups[i];
pu.phase += dtRaw * 3;
const dx = bird.x - pu.x, dy = bird.y - pu.y;
if (dx * dx + dy * dy < (bird.r + POWERUP_R) * (bird.r + POWERUP_R)) {
if (pu.type === 'shield') shieldTime = 8; // seconds
sfx.power();
flashAlpha = Math.max(flashAlpha, 0.25);
for (let k = 0; k < 16; k++) {
const a = Math.random() * Math.PI * 2;
sparkles.push({
x: pu.x, y: pu.y,
vx: Math.cos(a) * 160, vy: Math.sin(a) * 160,
life: 0.5, max: 0.5, size: 2 + Math.random() * 2,
});
}
powerups.splice(i, 1);
continue;
}
if (pu.x < -30) powerups.splice(i, 1);
}
if (shieldTime > 0) shieldTime = Math.max(0, shieldTime - dtRaw);
// Collision
for (const p of pipes) {
const topH = p.gapY - p.gapH / 2;
const botY = p.gapY + p.gapH / 2;
if (circleRect(bird.x, bird.y, bird.r, p.x, 0, PIPE_W, topH) ||
circleRect(bird.x, bird.y, bird.r, p.x, botY, PIPE_W, PLAY_H - botY)) {
die();
break;
}
}
// Ceiling + ground
if (bird.y - bird.r < 0) { bird.y = bird.r; bird.vy = 0; }
if (bird.y + bird.r > PLAY_H) { bird.y = PLAY_H - bird.r; die(); }
groundOffset = (groundOffset + scrollV * dt) % 48;
} else if (state === STATE.DEAD) {
// Bird falls and freezes game world
bird.vy = Math.min(MAX_VY, bird.vy + GRAVITY * 1.4 * dt);
bird.y += bird.vy * dt;
bird.rot = Math.min(1.6, bird.rot + dt * 3);
if (bird.y + bird.r >= PLAY_H) {
bird.y = PLAY_H - bird.r;
bird.vy = 0;
gameOver();
}
}
// Particles (use real time so death feel doesn't stall)
for (let i = particles.length - 1; i >= 0; i--) {
const p = particles[i];
p.vy += 700 * dtRaw;
p.x += p.vx * dtRaw;
p.y += p.vy * dtRaw;
p.life -= dtRaw;
if (p.life <= 0 || p.y > PLAY_H + 30) particles.splice(i, 1);
}
for (let i = trailPuffs.length - 1; i >= 0; i--) {
const t = trailPuffs[i];
t.x += t.vx * dtRaw;
t.y += t.vy * dtRaw;
t.life -= dtRaw;
if (t.life <= 0) trailPuffs.splice(i, 1);
}
for (let i = sparkles.length - 1; i >= 0; i--) {
const s = sparkles[i];
s.vx *= (1 - dtRaw * 1.5);
s.vy *= (1 - dtRaw * 1.5);
s.x += s.vx * dtRaw;
s.y += s.vy * dtRaw;
s.life -= dtRaw;
if (s.life <= 0) sparkles.splice(i, 1);
}
// Shake + flash decay (real time)
if (shake > 0) shake = Math.max(0, shake - dtRaw * 40);
if (flashAlpha > 0) flashAlpha = Math.max(0, flashAlpha - dtRaw * 2.2);
shakeSeed += dtRaw;
}
// --- Render ---------------------------------------------------------------
function dayPhase() {
// 0 = day, 1 = night, smooth ping-pong
const t = (score % (DAYNIGHT_CYCLE_SCORES * 2)) / DAYNIGHT_CYCLE_SCORES;
return t <= 1 ? t : 2 - t;
}
function lerp(a, b, t) { return a + (b - a) * t; }
function lerpColor(c1, c2, t) {
return [lerp(c1[0], c2[0], t) | 0, lerp(c1[1], c2[1], t) | 0, lerp(c1[2], c2[2], t) | 0];
}
function rgb(c) { return `rgb(${c[0]},${c[1]},${c[2]})`; }
const SKY_DAY_TOP = [110, 200, 255];
const SKY_DAY_BOT = [205, 238, 255];
const SKY_NIGHT_TOP = [8, 14, 40];
const SKY_NIGHT_BOT = [58, 52, 110];
const CLOUD_DAY = [255, 255, 255];
const CLOUD_NIGHT = [180, 180, 210];
function drawSky(p) {
const top = lerpColor(SKY_DAY_TOP, SKY_NIGHT_TOP, p);
const bot = lerpColor(SKY_DAY_BOT, SKY_NIGHT_BOT, p);
const sky = ctx.createLinearGradient(0, 0, 0, PLAY_H);
sky.addColorStop(0, rgb(top));
sky.addColorStop(1, rgb(bot));
ctx.fillStyle = sky;
ctx.fillRect(0, 0, VW, PLAY_H);
}
function drawStars(p) {
if (p < 0.1) return;
const a = Math.min(1, (p - 0.1) / 0.6);
for (const s of stars) {
const twink = 0.55 + 0.45 * Math.sin(s.twinkle);
ctx.globalAlpha = a * twink;
ctx.fillStyle = '#fff';
ctx.beginPath();
ctx.arc(s.x, s.y, s.s, 0, Math.PI * 2);
ctx.fill();
}
ctx.globalAlpha = 1;
}
function drawCloud(c, cloudColor) {
ctx.save();
ctx.globalAlpha = 0.85;
ctx.fillStyle = cloudColor;
const s = c.s;
ctx.beginPath();
ctx.arc(c.x, c.y, 18 * s, 0, Math.PI * 2);
ctx.arc(c.x + 18 * s, c.y - 6*s, 22 * s, 0, Math.PI * 2);
ctx.arc(c.x + 38 * s, c.y, 18 * s, 0, Math.PI * 2);
ctx.arc(c.x + 22 * s, c.y + 8*s, 18 * s, 0, Math.PI * 2);
ctx.fill();
ctx.restore();
}
function drawCoin(c) {
const r = COIN_R;
const wob = Math.abs(Math.cos(c.phase)); // 0..1 — gives the "spinning coin" flatten
ctx.save();
ctx.translate(c.x, c.y + Math.sin(c.phase * 0.5) * 3);
// glow
ctx.globalAlpha = 0.5;
ctx.fillStyle = 'rgba(255,207,58,.5)';
ctx.beginPath();
ctx.arc(0, 0, r + 6, 0, Math.PI * 2);
ctx.fill();
ctx.globalAlpha = 1;
// coin body (ellipse for spin)
const grad = ctx.createRadialGradient(-r*0.3, -r*0.3, 1, 0, 0, r);
grad.addColorStop(0, '#fff3a8');
grad.addColorStop(0.6, '#ffcf3a');
grad.addColorStop(1, '#a77900');
ctx.fillStyle = grad;
ctx.beginPath();
ctx.ellipse(0, 0, r * Math.max(0.2, wob), r, 0, 0, Math.PI * 2);
ctx.fill();
// inner ring
ctx.strokeStyle = 'rgba(255,255,255,.5)';
ctx.lineWidth = 1.5;
ctx.beginPath();
ctx.ellipse(0, 0, r * 0.6 * Math.max(0.2, wob), r * 0.6, 0, 0, Math.PI * 2);
ctx.stroke();
ctx.restore();
}
function drawPowerup(pu) {
ctx.save();
const bob = Math.sin(pu.phase) * 4;
ctx.translate(pu.x, pu.y + bob);
// outer glow
ctx.globalAlpha = 0.45;
ctx.fillStyle = '#6fd6ff';
ctx.beginPath();
ctx.arc(0, 0, POWERUP_R + 10, 0, Math.PI * 2);
ctx.fill();
ctx.globalAlpha = 1;
// body
const g = ctx.createRadialGradient(-4, -4, 2, 0, 0, POWERUP_R);
g.addColorStop(0, '#e8f8ff');
g.addColorStop(0.5, '#7fd0ff');
g.addColorStop(1, '#2272c7');
ctx.fillStyle = g;
ctx.beginPath();
ctx.arc(0, 0, POWERUP_R, 0, Math.PI * 2);
ctx.fill();
// shield emblem
ctx.fillStyle = '#fff';
ctx.beginPath();
ctx.moveTo(0, -6);
ctx.lineTo(6, -3);
ctx.lineTo(6, 2);
ctx.lineTo(0, 7);
ctx.lineTo(-6, 2);
ctx.lineTo(-6, -3);
ctx.closePath();
ctx.fill();
ctx.restore();
}
function drawShieldBubble() {
if (shieldTime <= 0) return;
const pulse = 0.85 + 0.15 * Math.sin(elapsed * 10);
const fadeTail = shieldTime < 1.5 ? (0.3 + 0.7 * Math.abs(Math.sin(elapsed * 20))) : 1;
ctx.save();
ctx.translate(bird.x, bird.y);
ctx.globalAlpha = 0.35 * fadeTail;
const g = ctx.createRadialGradient(0, 0, SHIELD_PULSE_R * 0.4, 0, 0, SHIELD_PULSE_R * pulse);
g.addColorStop(0, 'rgba(130,210,255,0)');
g.addColorStop(0.6, 'rgba(130,210,255,.45)');
g.addColorStop(1, 'rgba(130,210,255,0)');
ctx.fillStyle = g;
ctx.beginPath();
ctx.arc(0, 0, SHIELD_PULSE_R * pulse, 0, Math.PI * 2);
ctx.fill();
ctx.globalAlpha = 0.75 * fadeTail;
ctx.strokeStyle = 'rgba(180,230,255,.9)';
ctx.lineWidth = 2;
ctx.beginPath();
ctx.arc(0, 0, SHIELD_PULSE_R * pulse, 0, Math.PI * 2);
ctx.stroke();
ctx.restore();
ctx.globalAlpha = 1;
}
function drawSparkles() {
for (const s of sparkles) {
const a = Math.max(0, s.life / s.max);
ctx.globalAlpha = a;
ctx.fillStyle = '#fff8c8';
ctx.beginPath();
ctx.arc(s.x, s.y, s.size, 0, Math.PI * 2);
ctx.fill();
}
ctx.globalAlpha = 1;
}
function drawBird() {
ctx.save();
ctx.translate(bird.x, bird.y);
ctx.rotate(bird.rot);
// Body shadow
ctx.fillStyle = 'rgba(0,0,0,.15)';
ctx.beginPath();
ctx.ellipse(2, 4, 20, 16, 0, 0, Math.PI * 2);
ctx.fill();
// Body
const grad = ctx.createLinearGradient(0, -18, 0, 18);
grad.addColorStop(0, '#ffe066');
grad.addColorStop(1, '#ffb020');
ctx.fillStyle = grad;
ctx.beginPath();
ctx.ellipse(0, 0, 20, 16, 0, 0, Math.PI * 2);
ctx.fill();
// Belly
ctx.fillStyle = '#fff3b8';
ctx.beginPath();
ctx.ellipse(-2, 5, 12, 8, 0, 0, Math.PI * 2);
ctx.fill();
// Wing (flap animation)
const wf = Math.sin(elapsed * 18) * 0.5 + 0.5;
const wing = bird.flapPhase > 0 ? 1 - bird.flapPhase : wf;
ctx.save();
ctx.translate(-4, 2);
ctx.rotate((-0.4 + wing * 0.8));
ctx.fillStyle = '#ff9a1a';
ctx.beginPath();
ctx.ellipse(0, 0, 10, 6, 0, 0, Math.PI * 2);
ctx.fill();
ctx.restore();
// Eye
ctx.fillStyle = '#fff';
ctx.beginPath();
ctx.arc(8, -4, 5, 0, Math.PI * 2);
ctx.fill();
ctx.fillStyle = '#111';
ctx.beginPath();
ctx.arc(9, -4, 2.5, 0, Math.PI * 2);
ctx.fill();
// Beak
ctx.fillStyle = '#ff5a1f';
ctx.beginPath();
ctx.moveTo(14, -2);
ctx.lineTo(24, 0);
ctx.lineTo(14, 4);
ctx.closePath();
ctx.fill();
ctx.restore();
}