-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.html
More file actions
1070 lines (932 loc) · 28.4 KB
/
game.html
File metadata and controls
1070 lines (932 loc) · 28.4 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="zh-CN">
<head>
<meta charset="UTF-8" />
<title>反应力小测试小游戏</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<style>
:root {
--bg: #050816;
--bg-elevated: rgba(15, 23, 42, 0.9);
--accent: #22c55e;
--accent-soft: rgba(34, 197, 94, 0.15);
--accent-2: #38bdf8;
--danger: #f97373;
--text: #e5e7eb;
--muted: #9ca3af;
--border-subtle: rgba(148, 163, 184, 0.25);
--shadow-soft: 0 18px 45px rgba(15, 23, 42, 0.75);
}
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
background:
radial-gradient(circle at top left, #1f2937 0, transparent 55%),
radial-gradient(circle at bottom right, #111827 0, transparent 55%),
var(--bg);
font-family: system-ui, -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", sans-serif;
color: var(--text);
}
.shell {
width: min(480px, 100% - 32px);
background: radial-gradient(circle at top, rgba(148, 163, 184, 0.08), transparent 60%) padding-box,
linear-gradient(135deg, rgba(56, 189, 248, 0.5), rgba(52, 211, 153, 0.4)) border-box;
border-radius: 24px;
border: 1px solid transparent;
padding: 1px;
box-shadow: var(--shadow-soft);
}
.card {
background: radial-gradient(circle at top, rgba(15, 23, 42, 0.95), rgba(15, 23, 42, 0.98));
border-radius: 23px;
padding: 20px 20px 18px;
border: 1px solid rgba(15, 23, 42, 0.9);
backdrop-filter: blur(18px) saturate(140%);
position: relative;
overflow: hidden;
}
.card::before {
content: "";
position: absolute;
inset: -40%;
background:
radial-gradient(circle at 10% 0%, rgba(56, 189, 248, 0.12), transparent 55%),
radial-gradient(circle at 90% 0%, rgba(52, 211, 153, 0.12), transparent 55%);
opacity: 0.9;
pointer-events: none;
}
.card-inner {
position: relative;
z-index: 1;
}
.header-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 16px;
margin-bottom: 16px;
}
.title {
display: flex;
flex-direction: column;
gap: 4px;
}
.title-main {
display: flex;
align-items: center;
gap: 10px;
}
.logo-dot {
width: 24px;
height: 24px;
border-radius: 999px;
background: radial-gradient(circle at 30% 20%, #e5e7eb, #22c55e);
box-shadow:
0 0 0 1px rgba(34, 197, 94, 0.4),
0 0 25px rgba(34, 197, 94, 0.7);
position: relative;
overflow: hidden;
}
.logo-dot::after {
content: "";
position: absolute;
inset: -80%;
background:
radial-gradient(circle at 10% 0%, rgba(56, 189, 248, 0.18), transparent 60%),
radial-gradient(circle at 90% 90%, rgba(34, 197, 94, 0.2), transparent 60%);
mix-blend-mode: screen;
opacity: 0.9;
}
h1 {
font-size: 1.1rem;
font-weight: 600;
letter-spacing: 0.01em;
color: #e5e7eb;
}
.subtitle {
font-size: 0.8rem;
color: var(--muted);
display: flex;
align-items: center;
gap: 8px;
}
.pill {
padding: 2px 8px;
border-radius: 999px;
border: 1px solid rgba(148, 163, 184, 0.45);
background: radial-gradient(circle at top, rgba(148, 163, 184, 0.3), rgba(15, 23, 42, 0.9));
font-size: 0.7rem;
color: var(--muted);
}
.stats {
display: flex;
gap: 10px;
align-items: center;
}
.stat-pill {
min-width: 82px;
border-radius: 999px;
padding: 6px 10px;
background: radial-gradient(circle at top, rgba(15, 23, 42, 0.95), rgba(15, 23, 42, 0.98));
border: 1px solid var(--border-subtle);
display: flex;
flex-direction: column;
gap: 2px;
}
.stat-label {
font-size: 0.68rem;
color: var(--muted);
}
.stat-value {
font-size: 0.95rem;
font-weight: 600;
display: flex;
align-items: baseline;
gap: 4px;
}
.stat-unit {
font-size: 0.7rem;
color: var(--muted);
}
.mode-toggle {
display: inline-flex;
align-items: center;
border-radius: 999px;
background: radial-gradient(circle at top, rgba(15, 23, 42, 0.9), rgba(15, 23, 42, 0.98));
padding: 2px;
border: 1px solid rgba(148, 163, 184, 0.5);
box-shadow: 0 0 0 1px rgba(15, 23, 42, 0.8);
}
.mode-toggle button {
border: none;
background: transparent;
color: var(--muted);
font-size: 0.7rem;
padding: 4px 10px;
border-radius: 999px;
cursor: pointer;
transition: all 0.15s ease-out;
display: flex;
align-items: center;
gap: 4px;
white-space: nowrap;
}
.mode-toggle button.active {
background: radial-gradient(circle at top, rgba(34, 197, 94, 0.18), rgba(15, 23, 42, 0.96));
color: #dcfce7;
box-shadow:
0 0 18px rgba(34, 197, 94, 0.5),
inset 0 0 0 1px rgba(34, 197, 94, 0.6);
}
.mode-dot {
width: 6px;
height: 6px;
border-radius: 999px;
background: #22c55e;
box-shadow: 0 0 0 4px rgba(34, 197, 94, 0.25);
}
.description {
font-size: 0.8rem;
color: var(--muted);
margin-bottom: 10px;
line-height: 1.5;
}
.tips {
font-size: 0.75rem;
color: rgba(148, 163, 184, 0.9);
margin-bottom: 12px;
display: flex;
align-items: center;
gap: 8px;
}
.tips span.key {
min-width: 18px;
height: 18px;
border-radius: 6px;
border: 1px solid rgba(148, 163, 184, 0.6);
font-size: 0.65rem;
display: flex;
align-items: center;
justify-content: center;
color: var(--muted);
background: radial-gradient(circle at top, rgba(15, 23, 42, 0.98), rgba(15, 23, 42, 1));
box-shadow: 0 3px 0 rgba(15, 23, 42, 0.9);
}
.game-shell {
position: relative;
border-radius: 20px;
padding: 1px;
background: radial-gradient(circle at 10% 0%, rgba(56, 189, 248, 0.7), transparent 50%),
radial-gradient(circle at 90% 100%, rgba(34, 197, 94, 0.7), transparent 50%);
margin-bottom: 10px;
}
.game-shell-inner {
border-radius: 19px;
background: radial-gradient(circle at top, rgba(15, 23, 42, 0.97), rgba(15, 23, 42, 0.98));
padding: 12px;
border: 1px solid rgba(30, 64, 175, 0.6);
box-shadow: inset 0 0 0 1px rgba(15, 23, 42, 0.9);
}
.game-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 8px;
font-size: 0.75rem;
color: var(--muted);
}
.badge {
padding: 2px 8px;
border-radius: 999px;
border: 1px solid rgba(37, 99, 235, 0.6);
background: radial-gradient(circle at top, rgba(37, 99, 235, 0.45), rgba(15, 23, 42, 0.98));
color: #dbeafe;
font-size: 0.7rem;
display: inline-flex;
align-items: center;
gap: 4px;
}
.badge-dot {
width: 6px;
height: 6px;
border-radius: 999px;
background: #38bdf8;
box-shadow: 0 0 10px rgba(56, 189, 248, 0.8);
}
.game-meta {
display: flex;
gap: 10px;
align-items: center;
}
.meta-item {
display: flex;
align-items: center;
gap: 4px;
}
.dot {
width: 4px;
height: 4px;
border-radius: 999px;
background: rgba(148, 163, 184, 0.85);
}
.game-area {
position: relative;
width: 100%;
aspect-ratio: 4 / 3;
min-height: 210px;
border-radius: 14px;
border: 1px solid rgba(30, 64, 175, 0.8);
background:
radial-gradient(circle at 10% 10%, rgba(56, 189, 248, 0.22), transparent 60%),
radial-gradient(circle at 90% 90%, rgba(34, 197, 94, 0.22), transparent 60%),
radial-gradient(circle at center, rgba(15, 23, 42, 0.98), rgba(2, 6, 23, 0.98));
overflow: hidden;
box-shadow:
inset 0 0 40px rgba(0, 0, 0, 0.85),
0 14px 30px rgba(15, 23, 42, 0.9);
}
.grid-overlay {
position: absolute;
inset: 0;
background-image:
linear-gradient(rgba(15, 23, 42, 0.85) 1px, transparent 1px),
linear-gradient(90deg, rgba(15, 23, 42, 0.85) 1px, transparent 1px);
background-size: 22px 22px;
opacity: 0.7;
pointer-events: none;
mix-blend-mode: soft-light;
}
.glow-orbit {
position: absolute;
width: 160%;
height: 160%;
left: -30%;
top: -30%;
border-radius: 999px;
border: 1px dashed rgba(56, 189, 248, 0.18);
transform: rotate(-18deg);
pointer-events: none;
}
.target {
position: absolute;
width: 54px;
height: 54px;
border-radius: 999px;
background: radial-gradient(circle at 25% 20%, #f9fafb, #22c55e 45%, #16a34a 70%, #052e16 100%);
box-shadow:
0 0 0 3px rgba(22, 163, 74, 0.6),
0 0 40px rgba(34, 197, 94, 0.9);
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
color: #052e16;
font-weight: 700;
font-size: 0.8rem;
user-select: none;
transform: translate(-50%, -50%);
transition: transform 0.16s ease-out;
}
.target::after {
content: "";
position: absolute;
inset: -18%;
border-radius: inherit;
border: 1px solid rgba(187, 247, 208, 0.6);
box-shadow: 0 0 25px rgba(22, 163, 74, 0.6);
opacity: 0.8;
pointer-events: none;
}
.target.pulse {
animation: pulse 0.4s ease-out;
}
@keyframes pulse {
0% { transform: translate(-50%, -50%) scale(1); }
50% { transform: translate(-50%, -50%) scale(0.88); }
100% { transform: translate(-50%, -50%) scale(1); }
}
.wave {
position: absolute;
inset: 12%;
border-radius: inherit;
border: 1px solid rgba(56, 189, 248, 0.25);
opacity: 0;
pointer-events: none;
}
.wave.wave-animate {
animation: ripple 0.8s ease-out;
}
@keyframes ripple {
0% {
opacity: 0.5;
transform: scale(1);
}
100% {
opacity: 0;
transform: scale(1.08);
}
}
.center-hint {
position: absolute;
inset: 0;
display: flex;
align-items: center;
justify-content: center;
color: rgba(148, 163, 184, 0.9);
font-size: 0.85rem;
pointer-events: none;
text-align: center;
padding: 0 16px;
}
.center-hint span {
background: radial-gradient(circle at top, rgba(15, 23, 42, 1), rgba(15, 23, 42, 0.98));
padding: 6px 10px;
border-radius: 999px;
border: 1px solid rgba(148, 163, 184, 0.55);
box-shadow: 0 10px 25px rgba(15, 23, 42, 0.9);
display: inline-flex;
align-items: center;
gap: 6px;
}
.center-dot {
width: 6px;
height: 6px;
border-radius: 999px;
background: #22c55e;
box-shadow: 0 0 14px rgba(34, 197, 94, 0.7);
}
.game-footer {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin-top: 6px;
}
.scoreline {
display: flex;
flex-direction: column;
gap: 2px;
font-size: 0.74rem;
}
.score-main {
display: flex;
align-items: baseline;
gap: 8px;
}
.score-main strong {
font-size: 1.15rem;
color: #bbf7d0;
}
.score-sub {
font-size: 0.7rem;
color: rgba(148, 163, 184, 0.9);
}
.btn-primary {
position: relative;
border: none;
border-radius: 999px;
padding: 8px 18px;
background: radial-gradient(circle at top, #22c55e, #16a34a);
color: #022c22;
font-size: 0.82rem;
font-weight: 600;
cursor: pointer;
display: inline-flex;
align-items: center;
gap: 6px;
box-shadow:
0 0 0 1px rgba(22, 163, 74, 1),
0 14px 24px rgba(21, 128, 61, 0.8);
transition: transform 0.1s ease-out, box-shadow 0.1s ease-out, filter 0.1s ease-out;
white-space: nowrap;
}
.btn-primary span.dot {
width: 6px;
height: 6px;
border-radius: 999px;
background: #dcfce7;
box-shadow: 0 0 12px rgba(187, 247, 208, 0.8);
}
.btn-primary:active {
transform: translateY(1px);
box-shadow:
0 0 0 1px rgba(22, 163, 74, 1),
0 8px 18px rgba(21, 128, 61, 0.9);
filter: brightness(0.98);
}
.btn-primary:disabled {
opacity: 0.65;
cursor: default;
box-shadow:
0 0 0 1px rgba(22, 163, 74, 0.7),
0 8px 18px rgba(21, 128, 61, 0.6);
}
.history {
margin-top: 8px;
font-size: 0.75rem;
color: rgba(148, 163, 184, 0.92);
padding-top: 8px;
border-top: 1px dashed rgba(55, 65, 81, 0.8);
display: flex;
justify-content: space-between;
gap: 12px;
align-items: center;
}
.history span.label {
color: rgba(148, 163, 184, 0.98);
}
.history span.value {
color: #e5e7eb;
font-weight: 500;
}
.history-sub {
font-size: 0.7rem;
color: rgba(148, 163, 184, 0.8);
}
.badge-compact {
padding: 2px 8px;
border-radius: 999px;
border: 1px solid rgba(148, 163, 184, 0.7);
background: radial-gradient(circle at top, rgba(148, 163, 184, 0.4), rgba(15, 23, 42, 0.98));
font-size: 0.68rem;
color: rgba(229, 231, 235, 0.95);
display: inline-flex;
align-items: center;
gap: 4px;
white-space: nowrap;
}
.badge-compact .dot {
background: #22c55e;
box-shadow: 0 0 10px rgba(34, 197, 94, 0.8);
}
@media (max-width: 420px) {
.card {
padding: 16px 14px 14px;
}
.header-row {
flex-direction: column;
align-items: flex-start;
}
.stats {
align-self: stretch;
justify-content: space-between;
width: 100%;
}
.game-footer {
flex-direction: column-reverse;
align-items: stretch;
}
.btn-primary {
justify-content: center;
width: 100%;
}
}
</style>
</head>
<body>
<main class="shell">
<section class="card">
<div class="card-inner">
<div class="header-row">
<div class="title">
<div class="title-main">
<div class="logo-dot"></div>
<h1>反应力小测试</h1>
</div>
<div class="subtitle">
<span>在限定时间内尽可能多地点击发光圆点。</span>
<span class="pill">30 秒一局 · 纯前端小游戏</span>
</div>
</div>
<div class="stats">
<div class="stat-pill">
<span class="stat-label">剩余时间</span>
<div class="stat-value">
<span id="timeValue">30.0</span>
<span class="stat-unit">s</span>
</div>
</div>
<div class="stat-pill">
<span class="stat-label">命中次数</span>
<div class="stat-value">
<span id="scoreValue">0</span>
<span class="stat-unit">clicks</span>
</div>
</div>
</div>
</div>
<div class="mode-toggle" aria-label="选择模式">
<button type="button" class="active" data-mode="normal">
<span class="mode-dot"></span>
标准模式
</button>
<button type="button" data-mode="hard">
高速模式
</button>
</div>
<p class="description">
点击绿色发光圆点,每命中一次立刻刷新到随机位置。标准模式适合热身,高速模式更考验手眼协调与反应速度。
</p>
<p class="tips">
<span class="key">R</span> 再来一局,
<span class="key">H</span> 切换难度。
</p>
<div class="game-shell">
<div class="game-shell-inner">
<header class="game-header">
<div class="badge">
<span class="badge-dot"></span>
Click Reflex Arena
</div>
<div class="game-meta">
<div class="meta-item">
<span class="dot"></span>
<span id="modeLabel">标准</span>
</div>
<div class="meta-item">
<span class="dot"></span>
<span id="speedLabel">刷新间隔 600ms</span>
</div>
</div>
</header>
<div class="game-area" id="gameArea">
<div class="grid-overlay"></div>
<div class="glow-orbit"></div>
<div class="wave" id="wave"></div>
<button
type="button"
class="target"
id="target"
aria-label="点击目标"
style="left: 50%; top: 55%;"
>
GO
</button>
<div class="center-hint" id="centerHint">
<span>
<span class="center-dot"></span>
点击绿色圆点开始游戏
</span>
</div>
</div>
<footer class="game-footer">
<div class="scoreline">
<div class="score-main">
<strong id="scoreSummary">0</strong>
<span class="score-sub" id="scoreDetail">命中 0 次 · 平均 0.0 次/秒</span>
</div>
<div class="score-sub" id="statusText">准备就绪,点击圆点开始第一局。</div>
</div>
<button class="btn-primary" id="startBtn" type="button">
<span class="dot"></span>
<span id="startBtnLabel">开始游戏</span>
</button>
</footer>
</div>
</div>
<div class="history">
<div>
<span class="label">最佳成绩:</span>
<span class="value" id="bestScore">--</span>
</div>
<div class="history-sub">
<span class="badge-compact">
<span class="dot"></span>
纯前端 · 离线可玩 · 无任何数据上传
</span>
</div>
</div>
</div>
</section>
</main>
<script>
(function () {
const DURATION = 30_000; // ms
const MODES = {
normal: {
label: "标准",
speedLabel: "刷新间隔 600ms",
minDelay: 450,
maxDelay: 650,
},
hard: {
label: "高速",
speedLabel: "刷新间隔 350ms",
minDelay: 260,
maxDelay: 380,
},
};
const gameArea = document.getElementById("gameArea");
const target = document.getElementById("target");
const wave = document.getElementById("wave");
const centerHint = document.getElementById("centerHint");
const timeValueEl = document.getElementById("timeValue");
const scoreValueEl = document.getElementById("scoreValue");
const scoreSummaryEl = document.getElementById("scoreSummary");
const scoreDetailEl = document.getElementById("scoreDetail");
const statusTextEl = document.getElementById("statusText");
const bestScoreEl = document.getElementById("bestScore");
const startBtn = document.getElementById("startBtn");
const startBtnLabel = document.getElementById("startBtnLabel");
const modeButtons = Array.from(
document.querySelectorAll(".mode-toggle button")
);
const modeLabelEl = document.getElementById("modeLabel");
const speedLabelEl = document.getElementById("speedLabel");
let mode = "normal";
let playing = false;
let clicks = 0;
let startTime = 0;
let timerId = null;
let moveTimeoutId = null;
let best = null;
function updateModeUI() {
const cfg = MODES[mode];
modeLabelEl.textContent = cfg.label;
speedLabelEl.textContent = cfg.speedLabel;
modeButtons.forEach((btn) => {
const isActive = btn.dataset.mode === mode;
btn.classList.toggle("active", isActive);
});
statusTextEl.textContent = playing
? "游戏进行中,加油!"
: `当前为${cfg.label}模式,点击圆点开始。`;
}
function loadBest() {
try {
const key = "reaction-game-best";
const raw = localStorage.getItem(key);
if (!raw) return null;
const parsed = JSON.parse(raw);
if (
parsed &&
typeof parsed.score === "number" &&
typeof parsed.mode === "string"
) {
best = parsed;
}
} catch {
best = null;
}
}
function saveBest() {
if (!best) return;
try {
const key = "reaction-game-best";
localStorage.setItem(key, JSON.stringify(best));
} catch {
// ignore
}
}
function renderBest() {
if (!best) {
bestScoreEl.textContent = "--";
return;
}
const cfg = MODES[best.mode] || MODES.normal;
const avg = best.score / (DURATION / 1000);
bestScoreEl.textContent = `${best.score} 次 (${cfg.label} · ${avg.toFixed(
1
)} 次/秒)`;
}
function resetGameState() {
playing = false;
clicks = 0;
startTime = 0;
clearInterval(timerId);
clearTimeout(moveTimeoutId);
timerId = null;
moveTimeoutId = null;
timeValueEl.textContent = (DURATION / 1000).toFixed(1);
scoreValueEl.textContent = "0";
scoreSummaryEl.textContent = "0";
scoreDetailEl.textContent = "命中 0 次 · 平均 0.0 次/秒";
statusTextEl.textContent = "准备就绪,点击圆点或右侧按钮开始。";
centerHint.style.opacity = "1";
centerHint.style.transform = "translateY(0)";
startBtn.disabled = false;
startBtnLabel.textContent = "开始游戏";
target.textContent = "GO";
target.style.left = "50%";
target.style.top = "55%";
}
function randomPosition() {
const rect = gameArea.getBoundingClientRect();
const padding = 32;
const xPx =
padding + Math.random() * Math.max(10, rect.width - padding * 2);
const yPx =
padding + Math.random() * Math.max(10, rect.height - padding * 2);
const x = (xPx / rect.width) * 100;
const y = (yPx / rect.height) * 100;
return { x, y };
}
function scheduleMove() {
const cfg = MODES[mode];
const delay =
cfg.minDelay +
Math.random() * Math.max(50, cfg.maxDelay - cfg.minDelay);
clearTimeout(moveTimeoutId);
moveTimeoutId = setTimeout(() => {
if (!playing) return;
const { x, y } = randomPosition();
target.style.left = x + "%";
target.style.top = y + "%";
wave.classList.remove("wave-animate");
void wave.offsetWidth;
wave.classList.add("wave-animate");
scheduleMove();
}, delay);
}
function startGame() {
if (playing) return;
playing = true;
clicks = 0;
startTime = performance.now();
clearInterval(timerId);
centerHint.style.opacity = "0";
centerHint.style.transform = "translateY(6px)";
startBtn.disabled = true;
startBtnLabel.textContent = "游戏进行中…";
target.textContent = "GO";
const { x, y } = randomPosition();
target.style.left = x + "%";
target.style.top = y + "%";
statusTextEl.textContent = "开始计时!尽可能快地点击每一次出现的圆点。";
timerId = setInterval(() => {
if (!playing) return;
const elapsed = performance.now() - startTime;
const remain = Math.max(0, DURATION - elapsed);
timeValueEl.textContent = (remain / 1000).toFixed(1);
if (remain <= 0) {
finishGame();
}
}, 50);
scheduleMove();
}
function finishGame() {
if (!playing) return;
playing = false;
clearInterval(timerId);
clearTimeout(moveTimeoutId);
timerId = null;
moveTimeoutId = null;
const totalSeconds = DURATION / 1000;
const avg = clicks / totalSeconds;
timeValueEl.textContent = "0.0";
scoreSummaryEl.textContent = String(clicks);
scoreDetailEl.textContent = `命中 ${clicks} 次 · 平均 ${avg.toFixed(
1
)} 次/秒`;
target.textContent = "再来";
statusTextEl.textContent =
clicks === 0
? "这一局还没热身开局,再试一次吧。"
: avg >= 2.5
? "反应很快!试试再挑战一下自己的极限?"
: avg >= 1.6
? "不错的节奏,稍微提一点速度就能更棒。"
: "可以先从标准模式熟悉节奏,再逐渐提速。";
startBtn.disabled = false;
startBtnLabel.textContent = "再来一局";
const prevBestScore = best ? best.score : -1;
if (clicks > prevBestScore) {
best = { score: clicks, mode };
saveBest();
renderBest();
if (clicks > 0) {
statusTextEl.textContent += " 🎉 刷新了个人最佳!";
}
}
}
function handleHit() {
if (!playing) {
startGame();
return;
}
clicks += 1;
scoreValueEl.textContent = String(clicks);
scoreSummaryEl.textContent = String(clicks);
target.classList.remove("pulse");
void target.offsetWidth;
target.classList.add("pulse");
const rect = gameArea.getBoundingClientRect();
const radius = 100;
const centerX = rect.width / 2;
const centerY = rect.height / 2;
const currentX =
(parseFloat(target.style.left.replace("%", "")) / 100) * rect.width;
const currentY =
(parseFloat(target.style.top.replace("%", "")) / 100) * rect.height;
const dx = currentX - centerX;
const dy = currentY - centerY;
const dist = Math.sqrt(dx * dx + dy * dy) || 1;
const nx = dx / dist;
const ny = dy / dist;
const jitterRadius = 28;