-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
1207 lines (1097 loc) · 63 KB
/
index.html
File metadata and controls
1207 lines (1097 loc) · 63 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">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>贪吃蛇游戏</title>
<script src="https://cdn.tailwindcss.com"></script>
<link href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css" rel="stylesheet">
<script>
tailwind.config = {
theme: {
extend: {
colors: {
primary: '#4ade80',
primary2: '#3b82f6',
secondary: '#a3e635',
food: '#ef4444',
special: '#f59e0b',
poison: '#a855f7',
bgDark: '#1e293b',
bgLight: '#334155',
textPrimary: '#f8fafc',
textSecondary: '#cbd5e1',
accent: '#60a5fa',
},
fontFamily: {
game: ['"Press Start 2P"', 'cursive', 'system-ui'],
},
}
}
}
</script>
<style type="text/tailwindcss">
@layer utilities {
.text-shadow { text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5); }
.game-shadow { box-shadow: 0 0 20px rgba(74, 222, 128, 0.5); }
.btn-hover { @apply transition-all duration-300 hover:scale-105 hover:shadow-lg active:scale-95; }
.bg-overlay { background-color: rgba(30, 41, 59, 0.85); }
.skin-option.active { @apply ring-2 ring-primary ring-offset-2 ring-offset-bgDark; }
}
</style>
<style>
@import url('https://fonts.googleapis.com/css2?family=Press+Start+2P&display=swap');
@keyframes pulse { 0%, 100% { transform: scale(1); opacity: 1; } 50% { transform: scale(1.1); opacity: 0.8; } }
.food-pulse { animation: pulse 1s infinite; }
.effect-notification {
position: absolute; color: white; font-weight: bold; text-shadow: 2px 2px 4px rgba(0,0,0,0.8);
pointer-events: none; z-index: 20;
}
.control-btn { -webkit-tap-highlight-color: transparent; }
body { background-image: url('https://picsum.photos/id/1015/1920/1080'); background-size: cover; background-position: center; background-attachment: fixed; }
/* 皮肤预览 */
.skin-preview { width: 100%; aspect-ratio: 1/1; border-radius: 8px; display: flex; align-items: center; justify-content: center; position: relative; }
.skin-snake-head { width: 20px; height: 20px; border-radius: 4px; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); }
.skin-snake-body { width: 20px; height: 20px; border-radius: 4px; position: absolute; top: 50%; left: 30%; transform: translate(-50%, -50%); }
.skin-food { width: 20px; height: 20px; border-radius: 50%; position: absolute; top: 50%; left: 70%; transform: translate(-50%, -50%); }
</style>
</head>
<body class="min-h-screen flex flex-col items-center justify-center p-4 text-textPrimary font-sans">
<div class="max-w-6xl w-full mx-auto">
<div class="text-center mb-8">
<h1 class="text-4xl font-game text-primary mb-4 text-shadow">贪吃蛇游戏</h1>
<p class="text-textSecondary" id="gameDescription">控制你的蛇,吃到更多食物,并避免碰撞!</p>
</div>
<div class="flex flex-col lg:flex-row gap-6 items-start">
<!-- 左侧游戏信息和控制区域 -->
<div class="w-full lg:w-80 flex flex-col gap-4">
<div class="bg-overlay backdrop-blur-sm rounded-xl p-4 game-shadow">
<h2 class="text-xl font-game text-primary text-center mb-4 text-shadow">游戏状态</h2>
<div class="flex flex-col gap-3">
<!-- 单人模式分数 (默认显示) -->
<div id="singleScoreRow" class="flex justify-between items-center">
<span class="text-textSecondary"><i class="fa fa-user mr-2 text-primary"></i>分数</span>
<span id="score" class="text-primary font-bold text-xl">0</span>
</div>
<!-- 单人模式最高分 -->
<div id="singleHighScoreRow" class="flex justify-between items-center">
<span class="text-textSecondary"><i class="fa fa-trophy mr-2 text-yellow-400"></i>最高分</span>
<span id="highScore" class="text-yellow-400 font-bold text-xl">0</span>
</div>
<!-- 双人模式分数 (默认隐藏) -->
<div id="player1ScoreRow" class="flex justify-between items-center hidden">
<span class="text-textSecondary"><i class="fa fa-user mr-2 text-primary"></i>玩家1 (绿蛇)</span>
<span id="score1" class="text-primary font-bold text-xl">0</span>
</div>
<div id="player2ScoreRow" class="flex justify-between items-center hidden">
<span class="text-textSecondary"><i class="fa fa-user mr-2 text-primary2"></i>玩家2 (蓝蛇)</span>
<span id="score2" class="text-primary2 font-bold text-xl">0</span>
</div>
<!-- 双人模式最高分 -->
<div id="player1HighScoreRow" class="flex justify-between items-center hidden">
<span class="text-textSecondary"><i class="fa fa-trophy mr-2 text-yellow-400"></i>最高分 (P1)</span>
<span id="highScore1" class="text-yellow-400 font-bold text-xl">0</span>
</div>
<div class="flex justify-between items-center">
<span class="text-textSecondary"><i class="fa fa-clock-o mr-2 text-accent"></i>游戏时间</span>
<span id="time" class="text-accent font-bold text-xl">0s</span>
</div>
<div class="flex justify-between items-center">
<span class="text-textSecondary"><i class="fa fa-gamepad mr-2 text-special"></i>游戏模式</span>
<span id="gameModeDisplay" class="text-special font-bold text-sm">经典单人模式</span>
</div>
</div>
</div>
<div class="bg-overlay backdrop-blur-sm rounded-xl p-4 game-shadow">
<h2 class="text-lg font-bold mb-3 text-center text-textPrimary">控制面板</h2>
<div class="flex flex-col gap-3 mb-4">
<div class="flex justify-between">
<button id="startBtn" class="flex-1 bg-primary text-bgDark font-bold py-3 px-4 rounded-lg btn-hover">
<i class="fa fa-play mr-2"></i> 开始
</button>
<button id="pauseBtn" class="flex-1 bg-yellow-500 text-bgDark font-bold py-3 px-4 rounded-lg btn-hover hidden">
<i class="fa fa-pause mr-2"></i> 暂停
</button>
</div>
<button id="restartBtn" class="w-full bg-red-500 text-white font-bold py-3 px-4 rounded-lg btn-hover">
<i class="fa fa-refresh mr-2"></i> 重新开始
</button>
<!-- 设置按钮 -->
<button id="settingsBtn" class="w-full bg-gray-600 text-white font-bold py-3 px-4 rounded-lg btn-hover">
<i class="fa fa-cog mr-2"></i> 设置
</button>
</div>
<div class="border-t border-gray-700 pt-3">
<h3 class="text-sm font-semibold text-textSecondary mb-2">控制说明</h3>
<ul class="text-sm text-textSecondary space-y-1" id="controlInstructions">
<!-- 内容将通过JavaScript动态生成 -->
</ul>
</div>
</div>
</div>
<!-- 中间游戏区域 -->
<div class="flex-1 flex flex-col gap-6">
<div class="relative w-full bg-bgDark rounded-xl overflow-hidden game-shadow" id="gameContainer">
<canvas id="gameCanvas" class="w-full"></canvas>
<div id="startScreen" class="absolute inset-0 bg-bgDark/90 backdrop-blur-sm flex flex-col items-center justify-center gap-6 z-10">
<h2 class="text-3xl font-game text-primary text-center text-shadow" id="startScreenTitle">贪吃蛇</h2>
<p class="text-textSecondary text-center" id="startScreenDesc">控制你的蛇,吃到更多食物,并避免碰撞!</p>
<button id="startScreenBtn" class="bg-primary text-bgDark font-bold py-3 px-8 rounded-lg text-lg btn-hover">
开始游戏
</button>
</div>
<div id="pauseScreen" class="absolute inset-0 bg-bgDark/80 backdrop-blur-sm flex flex-col items-center justify-center gap-4 z-10 hidden">
<h2 class="text-2xl font-game text-yellow-400 text-center">游戏暂停</h2>
<button id="resumeBtn" class="bg-primary text-bgDark font-bold py-2 px-6 rounded-lg btn-hover">
继续游戏
</button>
</div>
<div id="gameOverScreen" class="absolute inset-0 bg-bgDark/80 backdrop-blur-sm flex flex-col items-center justify-center gap-4 z-10 hidden">
<h2 class="text-2xl font-game text-red-500 text-center">游戏结束</h2>
<p id="winnerText" class="text-textSecondary text-xl"></p>
<!-- 单人分数 -->
<p id="finalScoreSingleRow" class="text-textSecondary">得分: <span id="finalScore" class="text-primary font-bold">0</span></p>
<!-- 双人分数 -->
<p id="finalScore1Row" class="text-textSecondary hidden">玩家1得分: <span id="finalScore1" class="text-primary font-bold">0</span></p>
<p id="finalScore2Row" class="text-textSecondary hidden">玩家2得分: <span id="finalScore2" class="text-primary2 font-bold">0</span></p>
<button id="playAgainBtn" class="bg-primary text-bgDark font-bold py-2 px-6 rounded-lg btn-hover">
再玩一次
</button>
</div>
</div>
</div>
</div>
</div>
<!-- 设置模态框 -->
<div id="settingsModal" class="fixed inset-0 bg-black/70 backdrop-blur-sm hidden items-center justify-center z-20 p-4">
<div class="bg-overlay rounded-xl p-6 max-w-3xl w-full max-h-[90vh] overflow-y-auto">
<h2 class="text-2xl font-game text-primary mb-6 text-center"><i class="fa fa-cog mr-2"></i>游戏设置</h2>
<!-- 1. 皮肤选择 -->
<div class="mb-6">
<h3 class="text-lg font-semibold mb-3 text-textPrimary">选择皮肤</h3>
<div class="grid grid-cols-2 md:grid-cols-3 gap-3" id="settingsSkinOptions"></div>
<div class="mt-3 bg-bgLight rounded-lg p-4">
<div class="text-center mb-2 text-sm text-textSecondary">当前皮肤预览</div>
<div id="currentSkinPreview" class="skin-preview mb-2"></div>
<p id="currentSkinName" class="text-center font-medium text-primary text-lg"></p>
</div>
</div>
<!-- 2. 游戏设置 -->
<div class="mb-6">
<h3 class="text-lg font-semibold mb-3 text-textPrimary">游戏设置</h3>
<div class="mb-4">
<label for="settingsSnakeSpeed" class="block text-textSecondary text-sm mb-2">基础速度</label>
<input type="range" id="settingsSnakeSpeed" min="50" max="500" value="150" class="w-full h-2 bg-bgLight rounded-lg appearance-none cursor-pointer">
<div class="flex justify-between text-xs text-textSecondary mt-1">
<span>很快</span><span id="speedValue">中</span><span>很慢</span>
</div>
</div>
<div class="mb-4">
<label for="settingsGridSize" class="block text-textSecondary text-sm mb-2">网格尺寸</label>
<select id="settingsGridSize" class="w-full bg-bgLight text-textPrimary rounded-lg p-2 focus:outline-none focus:ring-2 focus:ring-primary">
<option value="15">15×15 (简单)</option>
<option value="20" selected>20×20 (中等)</option>
<option value="25">25×25 (困难)</option>
<option value="30">30×30 (专家)</option>
</select>
</div>
<div class="mb-4">
<label for="settingsSpecialFoodMode" class="block text-textSecondary text-sm mb-2">特殊食物模式</label>
<select id="settingsSpecialFoodMode" class="w-full bg-bgLight text-textPrimary rounded-lg p-2 focus:outline-none focus:ring-2 focus:ring-primary">
<option value="normal">普通模式 (少量特殊食物)</option>
<option value="frequent" selected>频繁模式 (较多特殊食物)</option>
<option value="intense">激烈模式 (大量特殊食物)</option>
</select>
</div>
<div>
<label for="gameModeSelect" class="block text-textSecondary text-sm mb-2">游戏模式</label>
<select id="gameModeSelect" class="w-full bg-bgLight text-textPrimary rounded-lg p-2 focus:outline-none focus:ring-2 focus:ring-primary">
<option value="single" selected>经典单人模式</option>
<option value="competitive">双人对战模式</option>
<option value="cooperative">双人合作模式</option>
</select>
</div>
</div>
<!-- 3. 控制说明 -->
<div class="mb-6">
<h3 class="text-lg font-semibold text-textPrimary cursor-pointer mb-3">控制说明</h3>
<div class="text-sm text-textSecondary space-y-2 bg-bgLight/30 p-3 rounded" id="settingsControlInstructions">
<!-- 内容将通过JavaScript动态生成 -->
</div>
</div>
<div class="flex justify-end">
<button id="closeSettingsBtn" class="bg-primary text-bgDark font-bold py-2 px-6 rounded-lg btn-hover">完成并应用</button>
</div>
</div>
</div>
<footer class="mt-8 text-sm text-black text-center">
开发人员:郑博翰 江宥贤 丘晋宇<br>
支持单人/双人模式
</footer>
<script>
// --- 常量定义 ---
const FoodType = {
NORMAL: 'normal', SPEED_UP: 'speedUp', SPEED_DOWN: 'speedDown',
LENGTH_UP: 'lengthUp', DOUBLE_SCORE: 'doubleScore', POISON: 'poison'
};
const foodConfig = {
[FoodType.NORMAL]: { name: '普通食物', score: 10, color: '#ef4444', shape: 'circle', effectDuration: 0, probability: 0.6 },
[FoodType.SPEED_UP]: { name: '加速食物', score: 20, color: '#f59e0b', shape: 'diamond', effectDuration: 5000, probability: 0.1 },
[FoodType.SPEED_DOWN]: { name: '减速食物', score: 20, color: '#fbbf24', shape: 'triangle', effectDuration: 5000, probability: 0.08 },
[FoodType.LENGTH_UP]: { name: '加长食物', score: 30, color: '#10b981', shape: 'pentagon', effectDuration: 0, probability: 0.07 },
[FoodType.DOUBLE_SCORE]: { name: '得分加倍', score: 15, color: '#60a5fa', shape: 'star', effectDuration: 3, probability: 0.05 },
[FoodType.POISON]: { name: '毒物', score: -5, color: '#a855f7', shape: 'skull', effectDuration: 0, probability: 0.1 }
};
const specialFoodModeConfig = {
normal: { name: '普通模式', foodProbabilities: { [FoodType.NORMAL]: 0.7, [FoodType.SPEED_UP]: 0.08, [FoodType.SPEED_DOWN]: 0.06, [FoodType.LENGTH_UP]: 0.05, [FoodType.DOUBLE_SCORE]: 0.04, [FoodType.POISON]: 0.07 } },
frequent: { name: '频繁模式', foodProbabilities: { [FoodType.NORMAL]: 0.5, [FoodType.SPEED_UP]: 0.12, [FoodType.SPEED_DOWN]: 0.1, [FoodType.LENGTH_UP]: 0.1, [FoodType.DOUBLE_SCORE]: 0.08, [FoodType.POISON]: 0.1 } },
intense: { name: '激烈模式', foodProbabilities: { [FoodType.NORMAL]: 0.3, [FoodType.SPEED_UP]: 0.15, [FoodType.SPEED_DOWN]: 0.15, [FoodType.LENGTH_UP]: 0.15, [FoodType.DOUBLE_SCORE]: 0.1, [FoodType.POISON]: 0.15 } }
};
// 游戏模式常量
const GameMode = {
SINGLE: 'single', // 经典单人模式
COMPETITIVE: 'competitive', // 双人对战
COOPERATIVE: 'cooperative' // 双人合作
};
const skins = {
classic: { name: "经典主题", headColor: "#4ade80", bodyColor: "#a3e635", speedMultiplier: 1.0 },
ocean: { name: "海洋主题", headColor: "#3b82f6", bodyColor: "#60a5fa", speedMultiplier: 1.1 },
fire: { name: "火焰主题", headColor: "#f97316", bodyColor: "#f59e0b", speedMultiplier: 1.2 },
dark: { name: "暗黑主题", headColor: "#ffffff", bodyColor: "#9ca3af", speedMultiplier: 0.9 },
candy: { name: "糖果主题", headColor: "#ec4899", bodyColor: "#f472b6", speedMultiplier: 1.0 },
forest: { name: "森林主题", headColor: "#22c55e", bodyColor: "#84cc16", speedMultiplier: 0.8 }
};
const config = { gridSize: 20, minSpeed: 50, maxSpeed: 500, speedIncrease: 2 };
// --- 游戏状态 ---
let gameState = {
// 模式相关状态
currentGameMode: localStorage.getItem('snakeGameMode') || GameMode.SINGLE, // 默认为单人
// 蛇的状态(保留两条蛇的数据结构,但根据模式决定使用哪条)
snake1: [], direction1: 'right', nextDirection1: 'right',
snake2: [], direction2: 'left', nextDirection2: 'left',
score: 0, // 单人模式通用分数
score1: 0, score2: 0, // 双人模式保留独立分数
highScore: localStorage.getItem('snakeHighScore') || 0,
highScore1: localStorage.getItem('snakeHighScore1') || 0,
highScore2: localStorage.getItem('snakeHighScore2') || 0,
// 游戏通用状态
food: null, foodType: FoodType.NORMAL,
gameLoop: null, isRunning: false, isPaused: false,
gameTime: 0, timer: null,
baseSpeed: parseInt(localStorage.getItem('snakeBaseSpeed')) || 150,
currentSpeed: 150,
currentSkin: localStorage.getItem('snakeSkin') || 'classic',
gridSize: parseInt(localStorage.getItem('snakeGridSize')) || 20,
specialFoodMode: localStorage.getItem('snakeSpecialFoodMode') || 'frequent',
// 特殊效果状态
activeEffects: { speedUp: { active: false, endTime: 0 }, speedDown: { active: false, endTime: 0 }, doubleScore: { active: false, count: 0 } },
effectTimers: []
};
// --- DOM 元素 ---
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const scoreElement = document.getElementById('score');
const score1Element = document.getElementById('score1');
const score2Element = document.getElementById('score2');
const highScoreElement = document.getElementById('highScore');
const highScore1Element = document.getElementById('highScore1');
const timeElement = document.getElementById('time');
const gameModeDisplayElement = document.getElementById('gameModeDisplay');
const startBtn = document.getElementById('startBtn');
const pauseBtn = document.getElementById('pauseBtn');
const restartBtn = document.getElementById('restartBtn');
const settingsBtn = document.getElementById('settingsBtn');
const startScreen = document.getElementById('startScreen');
const startScreenBtn = document.getElementById('startScreenBtn');
const startScreenTitle = document.getElementById('startScreenTitle');
const startScreenDesc = document.getElementById('startScreenDesc');
const pauseScreen = document.getElementById('pauseScreen');
const resumeBtn = document.getElementById('resumeBtn');
const gameOverScreen = document.getElementById('gameOverScreen');
const winnerTextElement = document.getElementById('winnerText');
const finalScoreElement = document.getElementById('finalScore');
const finalScore1Element = document.getElementById('finalScore1');
const finalScore2Element = document.getElementById('finalScore2');
const playAgainBtn = document.getElementById('playAgainBtn');
const gameDescription = document.getElementById('gameDescription');
const controlInstructions = document.getElementById('controlInstructions');
// 设置界面元素
const settingsModal = document.getElementById('settingsModal');
const closeSettingsBtn = document.getElementById('closeSettingsBtn');
const settingsSkinOptions = document.getElementById('settingsSkinOptions');
const currentSkinPreview = document.getElementById('currentSkinPreview');
const currentSkinName = document.getElementById('currentSkinName');
const snakeSpeedSlider = document.getElementById('settingsSnakeSpeed');
const speedValueElement = document.getElementById('speedValue');
const gridSizeSelect = document.getElementById('settingsGridSize');
const specialFoodModeSelect = document.getElementById('settingsSpecialFoodMode');
const gameModeSelect = document.getElementById('gameModeSelect');
const settingsControlInstructions = document.getElementById('settingsControlInstructions');
// --- 核心游戏函数 ---
function resizeCanvas() {
const container = document.getElementById('gameContainer');
if (!container) return;
const containerWidth = container.clientWidth;
const containerHeight = 400;
const cellSize = Math.min(Math.floor(containerWidth / config.gridSize), Math.floor(containerHeight / config.gridSize));
canvas.width = cellSize * config.gridSize;
canvas.height = cellSize * config.gridSize;
if (gameState.isRunning && !gameState.isPaused) draw();
}
function initGame() {
const centerX = Math.floor(config.gridSize / 2);
const centerY = Math.floor(config.gridSize / 2);
// 重置分数和效果
gameState.score = 0;
gameState.score1 = 0; gameState.score2 = 0;
gameState.activeEffects = { speedUp: { active: false, endTime: 0 }, speedDown: { active: false, endTime: 0 }, doubleScore: { active: false, count: 0 } };
gameState.effectTimers.forEach(timer => clearTimeout(timer));
gameState.effectTimers = [];
if (gameState.currentGameMode === GameMode.SINGLE) {
// 单人模式:只初始化蛇1,放在中央
gameState.snake1 = [ { x: centerX, y: centerY }, { x: centerX - 1, y: centerY }, { x: centerX - 2, y: centerY } ];
gameState.direction1 = 'right'; gameState.nextDirection1 = 'right';
gameState.snake2 = []; // 清空蛇2
} else {
// 双人模式:初始化两条蛇,位置分开
gameState.snake1 = [ { x: centerX - 5, y: centerY }, { x: centerX - 6, y: centerY }, { x: centerX - 7, y: centerY } ];
gameState.direction1 = 'right'; gameState.nextDirection1 = 'right';
gameState.snake2 = [ { x: centerX + 5, y: centerY }, { x: centerX + 6, y: centerY }, { x: centerX + 7, y: centerY } ];
gameState.direction2 = 'left'; gameState.nextDirection2 = 'left';
}
recalculateCurrentSpeed();
generateFood();
updateUIForGameMode();
draw();
}
function generateFood() {
let newFood, onSnake;
do {
onSnake = false;
newFood = { x: Math.floor(Math.random() * config.gridSize), y: Math.floor(Math.random() * config.gridSize) };
// 根据模式构建要检查的蛇数组
let snakesToCheck = [...gameState.snake1];
if (gameState.currentGameMode !== GameMode.SINGLE) {
snakesToCheck = snakesToCheck.concat(...gameState.snake2);
}
for (let seg of snakesToCheck) {
if (seg.x === newFood.x && seg.y === newFood.y) { onSnake = true; break; }
}
} while (onSnake);
const mode = gameState.specialFoodMode;
const probs = specialFoodModeConfig[mode].foodProbabilities;
const rand = Math.random(); let cumProb = 0;
for (const [type, prob] of Object.entries(probs)) {
cumProb += prob;
if (rand <= cumProb) { gameState.foodType = type; break; }
}
gameState.food = newFood;
}
function applyFoodEffect(foodType, snakeNumber) {
const foodInfo = foodConfig[foodType];
let score = foodInfo.score;
if (gameState.activeEffects.doubleScore.active) {
score *= 2;
gameState.activeEffects.doubleScore.count--;
if (gameState.activeEffects.doubleScore.count <= 0) gameState.activeEffects.doubleScore.active = false;
}
if (gameState.currentGameMode === GameMode.SINGLE) {
// 单人模式:更新通用分数
gameState.score = Math.max(0, gameState.score + score);
if (gameState.score > gameState.highScore) {
gameState.highScore = gameState.score;
localStorage.setItem('snakeHighScore', gameState.highScore);
}
} else {
// 双人模式:更新对应玩家的分数
if (snakeNumber === 1) {
gameState.score1 = Math.max(0, gameState.score1 + score);
if (gameState.score1 > gameState.highScore1) {
gameState.highScore1 = gameState.score1;
localStorage.setItem('snakeHighScore1', gameState.highScore1);
}
} else {
gameState.score2 = Math.max(0, gameState.score2 + score);
if (gameState.score2 > gameState.highScore2) {
gameState.highScore2 = gameState.score2;
localStorage.setItem('snakeHighScore2', gameState.highScore2);
}
}
}
updateScores();
showEffectNotification(foodInfo.name, score, snakeNumber);
// 特殊效果处理(总是对蛇1生效,可根据需要扩展)
if (snakeNumber === 1) {
switch (foodType) {
case FoodType.SPEED_UP:
if (gameState.activeEffects.speedDown.active) {
clearTimeout(gameState.activeEffects.speedDown.endTime);
gameState.activeEffects.speedDown.active = false;
}
gameState.activeEffects.speedUp.active = true;
gameState.activeEffects.speedUp.endTime = Date.now() + foodInfo.effectDuration;
const timer1 = setTimeout(() => {
gameState.activeEffects.speedUp.active = false;
recalculateCurrentSpeed();
if (gameState.isRunning && !gameState.isPaused) {
clearInterval(gameState.gameLoop);
gameState.gameLoop = setInterval(gameUpdate, gameState.currentSpeed);
}
}, foodInfo.effectDuration);
gameState.effectTimers.push(timer1);
break;
case FoodType.SPEED_DOWN:
if (gameState.activeEffects.speedUp.active) {
clearTimeout(gameState.activeEffects.speedUp.endTime);
gameState.activeEffects.speedUp.active = false;
}
gameState.activeEffects.speedDown.active = true;
gameState.activeEffects.speedDown.endTime = Date.now() + foodInfo.effectDuration;
const timer2 = setTimeout(() => {
gameState.activeEffects.speedDown.active = false;
recalculateCurrentSpeed();
if (gameState.isRunning && !gameState.isPaused) {
clearInterval(gameState.gameLoop);
gameState.gameLoop = setInterval(gameUpdate, gameState.currentSpeed);
}
}, foodInfo.effectDuration);
gameState.effectTimers.push(timer2);
break;
case FoodType.LENGTH_UP:
for (let i = 0; i < 3; i++) {
const lastSeg = gameState.snake1[gameState.snake1.length - 1];
gameState.snake1.push({ x: lastSeg.x, y: lastSeg.y });
}
break;
case FoodType.DOUBLE_SCORE:
gameState.activeEffects.doubleScore.active = true;
gameState.activeEffects.doubleScore.count = foodInfo.effectDuration;
break;
case FoodType.POISON:
if (gameState.snake1.length > 1) gameState.snake1.pop();
break;
case FoodType.NORMAL:
const skin = skins[gameState.currentSkin];
const speedInc = config.speedIncrease * skin.speedMultiplier;
gameState.baseSpeed = Math.max(config.minSpeed, gameState.baseSpeed - speedInc);
recalculateCurrentSpeed();
snakeSpeedSlider.value = gameState.baseSpeed;
updateSpeedDisplay(gameState.baseSpeed);
break;
}
recalculateCurrentSpeed();
}
}
function showEffectNotification(effectName, score, playerNum) {
const cellSize = canvas.width / config.gridSize;
const x = gameState.food.x * cellSize + cellSize / 2;
const y = gameState.food.y * cellSize;
const notification = document.createElement('div');
notification.className = 'effect-notification';
let playerText = '';
if (gameState.currentGameMode === GameMode.SINGLE) {
playerText = '';
} else {
playerText = `P${playerNum}: `;
}
notification.textContent = `${playerText}${effectName} ${score > 0 ? '+' : ''}${score}`;
notification.style.left = `${x}px`;
notification.style.top = `${y}px`;
if (gameState.currentGameMode === GameMode.SINGLE) {
notification.style.color = '#4ade80';
} else {
notification.style.color = playerNum === 1 ? '#4ade80' : '#3b82f6';
}
document.getElementById('gameContainer').appendChild(notification);
let opacity = 1;
const fadeOut = setInterval(() => {
opacity -= 0.05;
notification.style.opacity = opacity;
notification.style.top = `${y - (1 - opacity) * 50}px`;
if (opacity <= 0) {
clearInterval(fadeOut);
notification.remove();
}
}, 50);
}
function checkCollision(head, snakeArray, otherSnakeArray = []) {
// 撞墙
if (head.x < 0 || head.x >= config.gridSize || head.y < 0 || head.y >= config.gridSize) return true;
// 自撞
for (let i = 1; i < snakeArray.length; i++) {
if (head.x === snakeArray[i].x && head.y === snakeArray[i].y) return true;
}
// 与另一条蛇相撞
for (let seg of otherSnakeArray) {
if (head.x === seg.x && head.y === seg.y) return true;
}
return false;
}
function moveSnake() {
if (gameState.currentGameMode === GameMode.SINGLE) {
// ---------- 单人模式逻辑 ----------
gameState.direction1 = gameState.nextDirection1;
const head1 = { ...gameState.snake1[0] };
switch (gameState.direction1) {
case 'up': head1.y--; break;
case 'down': head1.y++; break;
case 'left': head1.x--; break;
case 'right': head1.x++; break;
}
// 单人碰撞检测:只检测边界和自撞
if (checkCollision(head1, gameState.snake1, [])) {
gameOver(0);
return;
}
gameState.snake1.unshift(head1);
if (head1.x === gameState.food.x && head1.y === gameState.food.y) {
applyFoodEffect(gameState.foodType, 1);
generateFood();
if (gameState.isRunning && !gameState.isPaused) {
clearInterval(gameState.gameLoop);
gameState.gameLoop = setInterval(gameUpdate, gameState.currentSpeed);
}
} else {
gameState.snake1.pop();
}
} else {
// ---------- 双人模式逻辑 ----------
gameState.direction1 = gameState.nextDirection1;
gameState.direction2 = gameState.nextDirection2;
// 移动蛇1
const head1 = { ...gameState.snake1[0] };
switch (gameState.direction1) {
case 'up': head1.y--; break;
case 'down': head1.y++; break;
case 'left': head1.x--; break;
case 'right': head1.x++; break;
}
// 双人碰撞检测
const otherSnakeFor1 = (gameState.currentGameMode === GameMode.COMPETITIVE) ? gameState.snake2 : [];
if (checkCollision(head1, gameState.snake1, otherSnakeFor1)) {
gameOver(2);
return;
}
gameState.snake1.unshift(head1);
if (head1.x === gameState.food.x && head1.y === gameState.food.y) {
applyFoodEffect(gameState.foodType, 1);
generateFood();
if (gameState.isRunning && !gameState.isPaused) {
clearInterval(gameState.gameLoop);
gameState.gameLoop = setInterval(gameUpdate, gameState.currentSpeed);
}
} else {
gameState.snake1.pop();
}
// 移动蛇2
const head2 = { ...gameState.snake2[0] };
switch (gameState.direction2) {
case 'up': head2.y--; break;
case 'down': head2.y++; break;
case 'left': head2.x--; break;
case 'right': head2.x++; break;
}
const otherSnakeFor2 = (gameState.currentGameMode === GameMode.COMPETITIVE) ? gameState.snake1 : [];
if (checkCollision(head2, gameState.snake2, otherSnakeFor2)) {
gameOver(1);
return;
}
gameState.snake2.unshift(head2);
if (head2.x === gameState.food.x && head2.y === gameState.food.y) {
applyFoodEffect(gameState.foodType, 2);
generateFood();
if (gameState.isRunning && !gameState.isPaused) {
clearInterval(gameState.gameLoop);
gameState.gameLoop = setInterval(gameUpdate, gameState.currentSpeed);
}
} else {
gameState.snake2.pop();
}
}
draw();
}
function draw() {
const cellSize = canvas.width / config.gridSize;
const skin = skins[gameState.currentSkin];
const foodInfo = foodConfig[gameState.foodType];
// 清空与绘制背景
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = '#2d3748';
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.strokeStyle = '#4a5568';
ctx.lineWidth = 0.5;
for (let i = 0; i <= config.gridSize; i++) {
ctx.beginPath();
ctx.moveTo(0, i * cellSize);
ctx.lineTo(canvas.width, i * cellSize);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(i * cellSize, 0);
ctx.lineTo(i * cellSize, canvas.height);
ctx.stroke();
}
// 绘制蛇1 (应用当前皮肤)
drawSingleSnake(gameState.snake1, gameState.direction1, skin.headColor, skin.bodyColor, cellSize);
// 仅在双人模式下绘制蛇2
if (gameState.currentGameMode !== GameMode.SINGLE) {
drawSingleSnake(gameState.snake2, gameState.direction2, '#3b82f6', '#60a5fa', cellSize);
}
// 绘制食物
if (gameState.food) {
const foodX = gameState.food.x * cellSize + cellSize / 2;
const foodY = gameState.food.y * cellSize + cellSize / 2;
const foodRadius = cellSize / 2 - 2;
ctx.fillStyle = foodInfo.color;
ctx.shadowColor = foodInfo.color;
ctx.shadowBlur = 10;
ctx.beginPath();
switch (foodInfo.shape) {
case 'circle':
ctx.arc(foodX, foodY, foodRadius, 0, Math.PI * 2);
break;
case 'diamond':
ctx.moveTo(foodX, foodY - foodRadius);
ctx.lineTo(foodX + foodRadius, foodY);
ctx.lineTo(foodX, foodY + foodRadius);
ctx.lineTo(foodX - foodRadius, foodY);
ctx.closePath();
break;
case 'triangle':
ctx.moveTo(foodX, foodY - foodRadius);
ctx.lineTo(foodX + foodRadius, foodY + foodRadius);
ctx.lineTo(foodX - foodRadius, foodY + foodRadius);
ctx.closePath();
break;
case 'pentagon':
for (let i = 0; i < 5; i++) {
const angle = (i * 2 * Math.PI) / 5 - Math.PI / 2;
const x = foodX + foodRadius * Math.cos(angle);
const y = foodY + foodRadius * Math.sin(angle);
if (i === 0) ctx.moveTo(x, y);
else ctx.lineTo(x, y);
}
ctx.closePath();
break;
case 'star':
for (let i = 0; i < 10; i++) {
const angle = (i * Math.PI) / 5;
const radius = i % 2 === 0 ? foodRadius : foodRadius * 0.5;
const x = foodX + radius * Math.cos(angle);
const y = foodY + radius * Math.sin(angle);
if (i === 0) ctx.moveTo(x, y);
else ctx.lineTo(x, y);
}
ctx.closePath();
break;
case 'skull':
ctx.arc(foodX, foodY, foodRadius, 0, Math.PI * 2);
ctx.moveTo(foodX - foodRadius * 0.3, foodY - foodRadius * 0.2);
ctx.arc(foodX - foodRadius * 0.3, foodY - foodRadius * 0.2, foodRadius * 0.1, 0, Math.PI * 2);
ctx.moveTo(foodX + foodRadius * 0.3, foodY - foodRadius * 0.2);
ctx.arc(foodX + foodRadius * 0.3, foodY - foodRadius * 0.2, foodRadius * 0.1, 0, Math.PI * 2);
ctx.moveTo(foodX - foodRadius * 0.4, foodY + foodRadius * 0.2);
ctx.quadraticCurveTo(foodX, foodY + foodRadius * 0.4, foodX + foodRadius * 0.4, foodY + foodRadius * 0.2);
break;
default:
ctx.arc(foodX, foodY, foodRadius, 0, Math.PI * 2);
}
ctx.fill();
ctx.shadowBlur = 0;
}
}
function drawSingleSnake(snake, direction, headColor, bodyColor, cellSize) {
if (snake.length === 0) return; // 如果蛇为空,不绘制
const head = snake[0];
// 画蛇头
ctx.fillStyle = headColor;
ctx.shadowColor = headColor;
ctx.shadowBlur = 10;
ctx.beginPath();
ctx.roundRect(head.x * cellSize + 1, head.y * cellSize + 1, cellSize - 2, cellSize - 2, 6);
ctx.fill();
ctx.shadowBlur = 0;
// 画眼睛
ctx.fillStyle = '#1e293b';
const eyeSize = cellSize * 0.2;
switch (direction) {
case 'right':
ctx.beginPath();
ctx.arc(head.x * cellSize + cellSize - eyeSize * 2, head.y * cellSize + eyeSize * 2, eyeSize, 0, Math.PI * 2);
ctx.arc(head.x * cellSize + cellSize - eyeSize * 2, head.y * cellSize + cellSize - eyeSize * 2, eyeSize, 0, Math.PI * 2);
ctx.fill();
break;
case 'left':
ctx.beginPath();
ctx.arc(head.x * cellSize + eyeSize * 2, head.y * cellSize + eyeSize * 2, eyeSize, 0, Math.PI * 2);
ctx.arc(head.x * cellSize + eyeSize * 2, head.y * cellSize + cellSize - eyeSize * 2, eyeSize, 0, Math.PI * 2);
ctx.fill();
break;
case 'up':
ctx.beginPath();
ctx.arc(head.x * cellSize + eyeSize * 2, head.y * cellSize + eyeSize * 2, eyeSize, 0, Math.PI * 2);
ctx.arc(head.x * cellSize + cellSize - eyeSize * 2, head.y * cellSize + eyeSize * 2, eyeSize, 0, Math.PI * 2);
ctx.fill();
break;
case 'down':
ctx.beginPath();
ctx.arc(head.x * cellSize + eyeSize * 2, head.y * cellSize + cellSize - eyeSize * 2, eyeSize, 0, Math.PI * 2);
ctx.arc(head.x * cellSize + cellSize - eyeSize * 2, head.y * cellSize + cellSize - eyeSize * 2, eyeSize, 0, Math.PI * 2);
ctx.fill();
break;
}
// 画蛇身
for (let i = 1; i < snake.length; i++) {
const seg = snake[i];
const colorRatio = 1 - (i / snake.length) * 0.3;
if (bodyColor.startsWith('#')) {
const r = parseInt(bodyColor.slice(1, 3), 16),
g = parseInt(bodyColor.slice(3, 5), 16),
b = parseInt(bodyColor.slice(5, 7), 16);
ctx.fillStyle = `rgba(${r}, ${g}, ${b}, ${colorRatio})`;
} else {
ctx.fillStyle = bodyColor;
}
ctx.beginPath();
ctx.roundRect(seg.x * cellSize + 1, seg.y * cellSize + 1, cellSize - 2, cellSize - 2, 4);
ctx.fill();
}
}
function gameUpdate() {
moveSnake();
}
function startGame() {
if (gameState.isRunning) return;
startScreen.classList.add('hidden');
startBtn.classList.add('hidden');
pauseBtn.classList.remove('hidden');
gameState.isRunning = true;
gameState.isPaused = false;
recalculateCurrentSpeed();
gameState.gameLoop = setInterval(gameUpdate, gameState.currentSpeed);
gameState.timer = setInterval(() => {
gameState.gameTime++;
timeElement.textContent = `${gameState.gameTime}s`;
}, 1000);
}
function pauseGame() {
if (!gameState.isRunning || gameState.isPaused) return;
clearInterval(gameState.gameLoop);
clearInterval(gameState.timer);
gameState.isPaused = true;
pauseScreen.classList.remove('hidden');
pauseBtn.classList.add('hidden');
startBtn.classList.remove('hidden');
startBtn.innerHTML = '<i class="fa fa-play mr-2"></i> 继续';
}
function resumeGame() {
if (!gameState.isRunning || !gameState.isPaused) return;
pauseScreen.classList.add('hidden');
recalculateCurrentSpeed();
gameState.gameLoop = setInterval(gameUpdate, gameState.currentSpeed);
gameState.timer = setInterval(() => {
gameState.gameTime++;
timeElement.textContent = `${gameState.gameTime}s`;
}, 1000);
gameState.isPaused = false;
startBtn.classList.add('hidden');
pauseBtn.classList.remove('hidden');
}
function gameOver(winner) { // winner: 0=单人模式结束, 1=蛇1赢, 2=蛇2赢
clearInterval(gameState.gameLoop);
clearInterval(gameState.timer);
gameState.effectTimers.forEach(timer => clearTimeout(timer));
gameState.effectTimers = [];
gameState.isRunning = false;
// 更新结束画面显示
const isSingle = gameState.currentGameMode === GameMode.SINGLE;
document.getElementById('finalScoreSingleRow').classList.toggle('hidden', !isSingle);
document.getElementById('finalScore1Row').classList.toggle('hidden', isSingle);
document.getElementById('finalScore2Row').classList.toggle('hidden', isSingle);
if (isSingle) {
document.getElementById('finalScore').textContent = gameState.score;
winnerTextElement.innerHTML = '游戏结束!';
} else {
document.getElementById('finalScore1').textContent = gameState.score1;
document.getElementById('finalScore2').textContent = gameState.score2;
let winnerMsg = '';
if (winner === 1) winnerMsg = '<span class="text-primary">玩家1 (绿蛇) 获胜!</span>';
else if (winner === 2) winnerMsg = '<span class="text-primary2">玩家2 (蓝蛇) 获胜!</span>';
winnerTextElement.innerHTML = winnerMsg;
}
gameOverScreen.classList.remove('hidden');
pauseBtn.classList.add('hidden');
startBtn.classList.remove('hidden');
startBtn.innerHTML = '<i class="fa fa-play mr-2"></i> 开始';
}
function restartGame() {
clearInterval(gameState.gameLoop);
clearInterval(gameState.timer);
gameState.effectTimers.forEach(timer => clearTimeout(timer));
gameState.effectTimers = [];
pauseScreen.classList.add('hidden');
gameOverScreen.classList.add('hidden');
initGame();
startBtn.classList.remove('hidden');
pauseBtn.classList.add('hidden');
startBtn.innerHTML = '<i class="fa fa-play mr-2"></i> 开始';
gameState.isRunning = false;
gameState.isPaused = false;
}
// --- UI更新函数 ---
function updateUIForGameMode() {
const isSingle = gameState.currentGameMode === GameMode.SINGLE;
// 显示/隐藏分数行
document.getElementById('singleScoreRow').classList.toggle('hidden', !isSingle);
document.getElementById('singleHighScoreRow').classList.toggle('hidden', !isSingle);
document.getElementById('player1ScoreRow').classList.toggle('hidden', isSingle);
document.getElementById('player2ScoreRow').classList.toggle('hidden', isSingle);
document.getElementById('player1HighScoreRow').classList.toggle('hidden', isSingle);
// 更新开始屏幕
if (isSingle) {
startScreenTitle.textContent = '贪吃蛇 - 单人模式';
startScreenDesc.textContent = '控制你的蛇,吃到更多食物,并避免碰撞!';
gameDescription.textContent = '控制你的蛇,吃到更多食物,并避免碰撞!';
} else if (gameState.currentGameMode === GameMode.COMPETITIVE) {
startScreenTitle.textContent = '贪吃蛇 - 双人对战';
startScreenDesc.textContent = '控制两条蛇,看谁得分更高!小心不要撞到自己、对方或墙。';
gameDescription.textContent = '控制两条蛇,看谁得分更高!小心不要撞到自己、对方或墙。';
} else {
startScreenTitle.textContent = '贪吃蛇 - 双人合作';
startScreenDesc.textContent = '控制两条蛇合作得分,小心不要撞到自己!';
gameDescription.textContent = '控制两条蛇合作得分,小心不要撞到自己!';
}
// 更新控制说明
updateControlInstructions();
// 更新游戏模式显示
updateGameModeDisplay();
// 更新分数显示
updateScores();
}
function updateControlInstructions() {
const isSingle = gameState.currentGameMode === GameMode.SINGLE;
if (isSingle) {
controlInstructions.innerHTML = `
<li><i class="fa fa-user text-primary mr-2"></i><span class="font-medium">玩家控制</span>: 方向键 ↑↓←→ 控制移动</li>
<li><i class="fa fa-space-shuttle mr-2 text-accent"></i>空格 - 暂停/继续游戏</li>
<li><i class="fa fa-refresh mr-2 text-accent"></i>R - 重新开始游戏</li>`;
settingsControlInstructions.innerHTML = `
<p><span class="font-medium text-primary">玩家控制</span>: 方向键 ↑ ↓ ← → 控制移动。</p>
<p><span class="font-medium text-accent">通用控制</span>: 空格键暂停/继续,R键重新开始。</p>`;
} else {
controlInstructions.innerHTML = `
<li><i class="fa fa-user text-primary mr-2"></i><span class="font-medium">玩家1 (绿蛇)</span>: 方向键 ↑↓←→ 控制移动</li>
<li><i class="fa fa-user text-primary2 mr-2"></i><span class="font-medium">玩家2 (蓝蛇)</span>: WASD 键控制移动</li>
<li><i class="fa fa-space-shuttle mr-2 text-accent"></i>空格 - 暂停/继续游戏</li>
<li><i class="fa fa-refresh mr-2 text-accent"></i>R - 重新开始游戏</li>`;
settingsControlInstructions.innerHTML = `
<p><span class="font-medium text-primary">玩家1 (绿蛇)</span>: 方向键 ↑ ↓ ←→ 控制移动。</p>
<p><span class="font-medium text-primary2">玩家2 (蓝蛇)</span>: W A S D 键控制移动。</p>
<p><span class="font-medium text-accent">通用控制</span>: 空格键暂停/继续,R键重新开始。</p>`;
}
}
function updateScores() {
const isSingle = gameState.currentGameMode === GameMode.SINGLE;
if (isSingle) {
scoreElement.textContent = gameState.score;
highScoreElement.textContent = gameState.highScore;
} else {
score1Element.textContent = gameState.score1;
score2Element.textContent = gameState.score2;
highScore1Element.textContent = gameState.highScore1;
}
}
function updateGameModeDisplay() {
const modeText = {
[GameMode.SINGLE]: '经典单人模式',
[GameMode.COMPETITIVE]: '双人对战模式',
[GameMode.COOPERATIVE]: '双人合作模式'
};