-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
1366 lines (1208 loc) · 54.7 KB
/
index.html
File metadata and controls
1366 lines (1208 loc) · 54.7 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>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Microsoft YaHei', sans-serif;
}
body {
background: linear-gradient(135deg, #1a2a6c, #b21f1f, #fdbb2d);
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
color: white;
overflow: hidden;
}
.game-container {
display: flex;
flex-direction: column;
align-items: center;
background: rgba(0, 0, 0, 0.7);
border-radius: 10px;
padding: 20px;
box-shadow: 0 0 30px rgba(0, 0, 0, 0.5);
max-width: 900px;
width: 100%;
}
.game-header {
text-align: center;
margin-bottom: 20px;
width: 100%;
}
.game-title {
font-size: 36px;
margin-bottom: 10px;
text-shadow: 0 0 10px #ff0000;
letter-spacing: 3px;
}
.game-content {
display: flex;
width: 100%;
gap: 20px;
}
.game-canvas-container {
flex: 1;
position: relative;
}
#gameCanvas {
background: #000;
border: 2px solid #444;
border-radius: 5px;
display: block;
}
.game-info {
width: 300px;
background: rgba(30, 30, 50, 0.8);
border-radius: 5px;
padding: 15px;
display: flex;
flex-direction: column;
gap: 15px;
}
.info-section {
background: rgba(0, 0, 0, 0.3);
padding: 10px;
border-radius: 5px;
border-left: 3px solid #ff9900;
}
.info-title {
font-size: 18px;
margin-bottom: 8px;
color: #ff9900;
}
.score-display {
font-size: 24px;
font-weight: bold;
text-align: center;
margin: 5px 0;
}
.rank-display {
font-size: 18px;
text-align: center;
color: #66ccff;
margin: 5px 0;
}
.controls-list {
list-style: none;
padding-left: 10px;
}
.controls-list li {
margin: 8px 0;
font-size: 14px;
}
.key {
display: inline-block;
background: #333;
color: #fff;
padding: 2px 8px;
border-radius: 4px;
margin: 0 3px;
font-family: monospace;
}
.game-footer {
margin-top: 20px;
text-align: center;
font-size: 14px;
color: #aaa;
width: 100%;
}
.menu-screen, .game-over-screen, .pause-screen {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.85);
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
z-index: 10;
border-radius: 5px;
}
.menu-title {
font-size: 42px;
margin-bottom: 30px;
text-shadow: 0 0 15px #ff0000;
}
.menu-options {
display: flex;
flex-direction: column;
gap: 15px;
width: 80%;
max-width: 400px;
}
.menu-option {
background: rgba(50, 50, 100, 0.7);
padding: 15px;
border-radius: 5px;
text-align: center;
cursor: pointer;
transition: all 0.3s;
border: 2px solid transparent;
}
.menu-option:hover {
background: rgba(70, 70, 120, 0.9);
border-color: #ff9900;
transform: scale(1.05);
}
.menu-option.selected {
background: rgba(90, 90, 140, 0.9);
border-color: #ff3300;
box-shadow: 0 0 15px rgba(255, 51, 0, 0.5);
}
.menu-hint {
margin-top: 20px;
color: #aaa;
font-size: 14px;
}
.btn {
background: linear-gradient(to bottom, #ff9900, #ff6600);
color: white;
border: none;
padding: 10px 20px;
border-radius: 5px;
cursor: pointer;
font-size: 16px;
margin: 10px;
transition: all 0.3s;
}
.btn:hover {
transform: scale(1.05);
box-shadow: 0 0 10px rgba(255, 153, 0, 0.7);
}
.hidden {
display: none;
}
.powerup-indicator {
position: absolute;
top: 10px;
right: 10px;
background: rgba(0, 128, 0, 0.5);
padding: 5px 10px;
border-radius: 5px;
font-size: 14px;
display: none;
}
.boss-health-bar {
position: absolute;
top: 10px;
left: 200px;
width: 200px;
height: 20px;
background: rgba(255, 0, 0, 0.3);
border-radius: 10px;
overflow: hidden;
display: none;
}
.boss-health-fill {
height: 100%;
background: linear-gradient(to right, #ff0000, #ff8800);
width: 100%;
transition: width 0.3s;
}
</style>
</head>
<body>
<div class="game-container">
<div class="game-header">
<h1 class="game-title">飞机大战 - 自定义飞机版</h1>
</div>
<div class="game-content">
<div class="game-canvas-container">
<canvas id="gameCanvas" width="600" height="700"></canvas>
<!-- 能量条指示器 -->
<div id="powerupIndicator" class="powerup-indicator">
能量: <span id="powerupValue">0</span>%
</div>
<!-- Boss血条 -->
<div id="bossHealthBar" class="boss-health-bar">
<div id="bossHealthFill" class="boss-health-fill"></div>
</div>
<div id="menuScreen" class="menu-screen">
<h2 class="menu-title">飞机大战 - 自定义飞机版</h2>
<div class="menu-options">
<div class="menu-option selected" data-level="easy">简单的敌人</div>
<div class="menu-option" data-level="normal">普通难度</div>
<div class="menu-option" data-level="hard">困难模式</div>
<div class="menu-option" data-level="insane">疯狂难度</div>
</div>
<p class="menu-hint">使用鼠标选择,点击确定开始游戏</p>
<button id="startBtn" class="btn">开始游戏</button>
</div>
<div id="pauseScreen" class="pause-screen hidden">
<h2 class="menu-title">游戏暂停</h2>
<div class="info-section" style="width: 80%; max-width: 400px;">
<div class="info-title">当前状态</div>
<p>击落敌机:<span id="pauseEnemiesShot">0</span> 架</p>
<p>得 分:<span id="pauseScore">0</span></p>
<p>获得称号:<span id="pauseRank">初级飞行员</span></p>
</div>
<div style="margin-top: 20px;">
<button id="resumeBtn" class="btn">继续游戏</button>
<button id="menuBtn" class="btn">返回菜单</button>
</div>
</div>
<div id="gameOverScreen" class="game-over-screen hidden">
<h2 class="menu-title">游戏结束</h2>
<div class="info-section" style="width: 80%; max-width: 400px;">
<div class="info-title">战果统计</div>
<p>击落敌机:<span id="enemiesShot">0</span> 架</p>
<p>得 分:<span id="finalScore">0</span></p>
<p>获得称号:<span id="finalRank">初级飞行员</span></p>
<p>最高连击:<span id="maxCombo">0</span></p>
</div>
<div style="margin-top: 20px;">
<button id="restartBtn" class="btn">再玩一次</button>
<button id="quitBtn" class="btn">退出游戏</button>
</div>
</div>
</div>
<div class="game-info">
<div class="info-section">
<div class="info-title">游戏状态</div>
<div class="score-display" id="scoreDisplay">0</div>
<div class="rank-display" id="rankDisplay">初级飞行员</div>
<div style="text-align: center; margin-top: 10px;">
连击: <span id="comboDisplay">0</span>
</div>
<div style="text-align: center; margin-top: 5px;">
生命: <span id="livesDisplay">3</span>
</div>
</div>
<div class="info-section">
<div class="info-title">操作方式</div>
<ul class="controls-list">
<li><span class="key">W</span> <span class="key">A</span> <span class="key">S</span> <span class="key">D</span> 控制战机移动</li>
<li><span class="key">K</span> 发射子弹</li>
<li><span class="key">J</span> 释放能量波(需充能)</li>
<li><span class="key">P</span> 暂停游戏</li>
<li><span class="key">E</span> 退出游戏</li>
</ul>
</div>
<div class="info-section">
<div class="info-title">游戏说明</div>
<p>控制你的战机,躲避敌机并击落它们。随着得分增加,敌机会越来越快,难度也会提升。</p>
<p style="margin-top: 10px;">新增功能:</p>
<ul style="margin-left: 20px; font-size: 13px;">
<li>能量波攻击(按J键释放)</li>
<li>连击系统(连续击落敌机加分)</li>
<li>Boss战(每100分出现)</li>
<li>生命系统(3条命)</li>
<li>多种难度选择</li>
</ul>
</div>
</div>
</div>
<div class="game-footer">
<p>HTML版飞机大战 - 自定义飞机版 | 基于原C++游戏改编</p>
</div>
</div>
<script>
// 游戏主类
class PlaneWar {
constructor() {
this.canvas = document.getElementById('gameCanvas');
this.ctx = this.canvas.getContext('2d');
// 游戏状态
this.gameState = 'menu'; // menu, playing, paused, gameOver
this.difficulty = 'easy';
this.lastTime = 0;
this.deltaTime = 0;
// 玩家飞机
this.player = {
x: this.canvas.width / 2 - 30,
y: this.canvas.height - 100,
width: 60,
height: 80,
speed: 6,
bullets: [],
lives: 3,
power: 0,
maxPower: 100,
isPoweredUp: false,
powerDuration: 0,
maxPowerDuration: 180, // 3秒(60帧/秒)
combo: 0,
maxCombo: 0,
invincible: false,
invincibleTimer: 0
};
// 加载自定义飞机图片
this.playerImage = new Image();
this.playerImage.src = 'https://bpic.588ku.com/element_pic/23/04/23/a82d8cfa01546b209494058ccb4284bc.png';
// 敌机
this.enemies = [];
this.enemySpeed = 2;
this.enemySpawnRate = 60; // 帧数
this.enemyTypes = ['normal', 'fast', 'tank'];
// Boss
this.boss = null;
this.bossActive = false;
this.bossSpawnScore = 100;
this.bossNextSpawnScore = 100;
// 道具
this.powerups = [];
// 爆炸效果
this.explosions = [];
// 游戏数据
this.score = 0;
this.rank = '初级飞行员';
this.enemiesShot = 0;
this.frameCount = 0;
// 控制状态
this.keys = {};
this.init();
}
init() {
// 设置事件监听
this.setupEventListeners();
// 开始游戏循环
this.lastTime = performance.now();
this.gameLoop();
}
setupEventListeners() {
// 键盘事件
document.addEventListener('keydown', (e) => {
const key = e.key.toLowerCase();
this.keys[key] = true;
// 处理特殊按键
if (key === 'p') {
this.togglePause();
}
if (key === 'e') {
this.gameOver();
}
if (key === 'j' && this.player.power >= this.player.maxPower && !this.player.isPoweredUp) {
this.activatePowerUp();
}
});
document.addEventListener('keyup', (e) => {
this.keys[e.key.toLowerCase()] = false;
});
// 菜单按钮事件
document.getElementById('startBtn').addEventListener('click', () => {
this.startGame();
});
document.getElementById('resumeBtn').addEventListener('click', () => {
this.togglePause();
});
document.getElementById('menuBtn').addEventListener('click', () => {
this.showMenu();
});
document.getElementById('restartBtn').addEventListener('click', () => {
this.restartGame();
});
document.getElementById('quitBtn').addEventListener('click', () => {
this.showMenu();
});
// 菜单选项事件
const menuOptions = document.querySelectorAll('.menu-option');
menuOptions.forEach(option => {
option.addEventListener('click', () => {
menuOptions.forEach(opt => opt.classList.remove('selected'));
option.classList.add('selected');
this.difficulty = option.getAttribute('data-level');
});
});
}
startGame() {
this.gameState = 'playing';
document.getElementById('menuScreen').classList.add('hidden');
document.getElementById('gameOverScreen').classList.add('hidden');
document.getElementById('pauseScreen').classList.add('hidden');
// 根据难度设置初始参数
this.setDifficultySettings();
// 重置游戏数据
this.score = 0;
this.enemiesShot = 0;
this.rank = '初级飞行员';
this.player.bullets = [];
this.enemies = [];
this.powerups = [];
this.explosions = [];
this.boss = null;
this.bossActive = false;
this.bossNextSpawnScore = this.bossSpawnScore;
this.player.lives = 3;
this.player.power = 0;
this.player.isPoweredUp = false;
this.player.combo = 0;
this.player.maxCombo = 0;
this.player.invincible = false;
this.player.invincibleTimer = 0;
this.frameCount = 0;
// 更新显示
this.updateDisplay();
}
setDifficultySettings() {
switch (this.difficulty) {
case 'easy':
this.enemySpeed = 2;
this.player.speed = 6;
this.bossSpawnScore = 100;
break;
case 'normal':
this.enemySpeed = 3;
this.player.speed = 5;
this.bossSpawnScore = 75;
break;
case 'hard':
this.enemySpeed = 4;
this.player.speed = 4;
this.bossSpawnScore = 50;
break;
case 'insane':
this.enemySpeed = 5;
this.player.speed = 3;
this.bossSpawnScore = 35;
this.player.lives = 1;
break;
default:
this.enemySpeed = 2;
this.player.speed = 6;
this.bossSpawnScore = 100;
}
this.bossNextSpawnScore = this.bossSpawnScore;
}
showMenu() {
this.gameState = 'menu';
document.getElementById('menuScreen').classList.remove('hidden');
document.getElementById('gameOverScreen').classList.add('hidden');
document.getElementById('pauseScreen').classList.add('hidden');
}
restartGame() {
this.startGame();
}
gameOver() {
this.gameState = 'gameOver';
document.getElementById('gameOverScreen').classList.remove('hidden');
// 更新最终统计
document.getElementById('enemiesShot').textContent = this.enemiesShot;
document.getElementById('finalScore').textContent = this.score;
document.getElementById('finalRank').textContent = this.rank;
document.getElementById('maxCombo').textContent = this.player.maxCombo;
}
togglePause() {
if (this.gameState === 'playing') {
this.gameState = 'paused';
document.getElementById('pauseScreen').classList.remove('hidden');
// 更新暂停界面数据
document.getElementById('pauseEnemiesShot').textContent = this.enemiesShot;
document.getElementById('pauseScore').textContent = this.score;
document.getElementById('pauseRank').textContent = this.rank;
} else if (this.gameState === 'paused') {
this.gameState = 'playing';
document.getElementById('pauseScreen').classList.add('hidden');
this.lastTime = performance.now(); // 重置时间以避免deltaTime过大
}
}
updateDisplay() {
document.getElementById('scoreDisplay').textContent = this.score;
document.getElementById('rankDisplay').textContent = this.rank;
document.getElementById('livesDisplay').textContent = this.player.lives;
document.getElementById('comboDisplay').textContent = this.player.combo;
document.getElementById('powerupValue').textContent = Math.min(100, Math.floor(this.player.power / this.player.maxPower * 100));
// 显示/隐藏能量指示器
const powerupIndicator = document.getElementById('powerupIndicator');
if (this.player.power > 0 || this.player.isPoweredUp) {
powerupIndicator.style.display = 'block';
if (this.player.isPoweredUp) {
powerupIndicator.style.background = 'rgba(255, 165, 0, 0.7)';
powerupIndicator.innerHTML = `能量波激活! (${Math.ceil(this.player.powerDuration / 60)}s)`;
} else {
powerupIndicator.style.background = 'rgba(0, 128, 0, 0.5)';
powerupIndicator.innerHTML = `能量: <span id="powerupValue">${Math.min(100, Math.floor(this.player.power / this.player.maxPower * 100))}</span>%`;
}
} else {
powerupIndicator.style.display = 'none';
}
}
updateRank() {
if (this.score >= 800) {
this.rank = '王牌飞行员';
} else if (this.score >= 600) {
this.rank = '精英飞行员';
} else if (this.score >= 450) {
this.rank = '高级飞行员';
} else if (this.score >= 300) {
this.rank = '中级飞行员';
} else if (this.score >= 150) {
this.rank = '初级飞行员';
} else {
this.rank = '新手飞行员';
}
}
handleInput() {
if (this.keys['w'] || this.keys['arrowup']) {
this.player.y = Math.max(10, this.player.y - this.player.speed);
}
if (this.keys['s'] || this.keys['arrowdown']) {
this.player.y = Math.min(this.canvas.height - this.player.height - 10, this.player.y + this.player.speed);
}
if (this.keys['a'] || this.keys['arrowleft']) {
this.player.x = Math.max(10, this.player.x - this.player.speed);
}
if (this.keys['d'] || this.keys['arrowright']) {
this.player.x = Math.min(this.canvas.width - this.player.width - 10, this.player.x + this.player.speed);
}
if (this.keys['k'] && this.frameCount % 10 === 0) { // 限制射击频率
this.shoot();
}
}
shoot() {
// 普通子弹
this.player.bullets.push({
x: this.player.x + this.player.width / 2 - 2,
y: this.player.y,
width: 4,
height: 10,
speed: 10,
damage: 1,
type: 'normal'
});
// 如果处于能量波状态,发射额外子弹
if (this.player.isPoweredUp) {
this.player.bullets.push({
x: this.player.x + this.player.width / 2 - 17,
y: this.player.y + 20,
width: 4,
height: 10,
speed: 10,
damage: 1,
angle: -20,
type: 'power'
});
this.player.bullets.push({
x: this.player.x + this.player.width / 2 + 13,
y: this.player.y + 20,
width: 4,
height: 10,
speed: 10,
damage: 1,
angle: 20,
type: 'power'
});
}
}
activatePowerUp() {
this.player.isPoweredUp = true;
this.player.powerDuration = this.player.maxPowerDuration;
this.player.power = 0;
this.updateDisplay();
}
updateBullets() {
for (let i = this.player.bullets.length - 1; i >= 0; i--) {
const bullet = this.player.bullets[i];
// 更新角度子弹位置
if (bullet.angle) {
const rad = bullet.angle * Math.PI / 180;
bullet.x += Math.sin(rad) * 3;
bullet.y -= Math.cos(rad) * bullet.speed;
} else {
bullet.y -= bullet.speed;
}
// 移除超出屏幕的子弹
if (bullet.y < -bullet.height ||
bullet.x < -bullet.width ||
bullet.x > this.canvas.width) {
this.player.bullets.splice(i, 1);
}
}
}
spawnEnemy() {
if (this.bossActive) return;
if (Math.random() < 0.02) { // 每帧有2%的几率生成敌机
const type = this.weightedRandomEnemyType();
let enemy;
switch (type) {
case 'fast':
enemy = {
x: Math.random() * (this.canvas.width - 30),
y: -30,
width: 30,
height: 30,
speed: this.enemySpeed * 1.8 + Math.random(),
health: 1,
maxHealth: 1,
type: 'fast',
score: 8
};
break;
case 'tank':
enemy = {
x: Math.random() * (this.canvas.width - 50),
y: -50,
width: 50,
height: 50,
speed: this.enemySpeed * 0.7 + Math.random() * 0.5,
health: 3,
maxHealth: 3,
type: 'tank',
score: 15
};
break;
default: // normal
enemy = {
x: Math.random() * (this.canvas.width - 40),
y: -40,
width: 40,
height: 40,
speed: this.enemySpeed + Math.random(),
health: 1,
maxHealth: 1,
type: 'normal',
score: 5
};
}
this.enemies.push(enemy);
}
}
weightedRandomEnemyType() {
const weights = {
easy: { normal: 0.7, fast: 0.2, tank: 0.1 },
normal: { normal: 0.5, fast: 0.3, tank: 0.2 },
hard: { normal: 0.4, fast: 0.3, tank: 0.3 },
insane: { normal: 0.3, fast: 0.4, tank: 0.3 }
};
const currentWeights = weights[this.difficulty] || weights.easy;
const rand = Math.random();
if (rand < currentWeights.normal) return 'normal';
if (rand < currentWeights.normal + currentWeights.fast) return 'fast';
return 'tank';
}
spawnBoss() {
if (this.bossActive || this.score < this.bossNextSpawnScore) return;
this.bossActive = true;
this.bossNextSpawnScore += this.bossSpawnScore;
this.boss = {
x: this.canvas.width / 2 - 40,
y: -80,
width: 80,
height: 80,
speed: this.enemySpeed * 0.5,
health: 20,
maxHealth: 20,
type: 'boss',
score: 50,
movePattern: 'sin', // sin, linear
moveTimer: 0,
shootTimer: 0
};
// 显示Boss血条
document.getElementById('bossHealthBar').style.display = 'block';
this.updateBossHealthBar();
}
updateBossHealthBar() {
if (!this.boss) return;
const healthPercent = (this.boss.health / this.boss.maxHealth) * 100;
document.getElementById('bossHealthFill').style.width = `${healthPercent}%`;
}
updateEnemies() {
// 更新普通敌机
for (let i = this.enemies.length - 1; i >= 0; i--) {
const enemy = this.enemies[i];
enemy.y += enemy.speed;
// 移除超出屏幕的敌机
if (enemy.y > this.canvas.height) {
this.enemies.splice(i, 1);
this.player.combo = 0; // 敌机逃逸,连击中断
}
}
// 更新Boss
if (this.boss) {
this.boss.moveTimer++;
// Boss移动模式
if (this.boss.movePattern === 'sin') {
this.boss.x = this.canvas.width / 2 - 40 + Math.sin(this.boss.moveTimer * 0.05) * 200;
} else {
if (this.boss.moveTimer % 120 < 60) {
this.boss.x += 2;
if (this.boss.x > this.canvas.width - this.boss.width) {
this.boss.movePattern = 'sin';
}
} else {
this.boss.x -= 2;
if (this.boss.x < 0) {
this.boss.movePattern = 'sin';
}
}
}
// Boss射击
this.boss.shootTimer++;
if (this.boss.shootTimer >= 90) {
this.boss.shootTimer = 0;
this.enemies.push({
x: this.boss.x + this.boss.width / 2 - 15,
y: this.boss.y + this.boss.height,
width: 30,
height: 30,
speed: 4,
health: 1,
maxHealth: 1,
type: 'bossBullet',
score: 0
});
}
// Boss到达屏幕位置
if (this.boss.y < 50) {
this.boss.y += this.boss.speed;
}
}
}
spawnPowerup(x, y) {
if (Math.random() < 0.3) { // 30%几率掉落道具
this.powerups.push({
x: x,
y: y,
width: 20,
height: 20,
speed: 2,
type: Math.random() < 0.7 ? 'power' : 'life' // 70%能量, 30%生命
});
}
}
updatePowerups() {
for (let i = this.powerups.length - 1; i >= 0; i--) {
const powerup = this.powerups[i];
powerup.y += powerup.speed;
// 检测玩家拾取
if (this.isColliding(this.player, powerup)) {
if (powerup.type === 'power') {
this.player.power += 25;
if (this.player.power > this.player.maxPower) {
this.player.power = this.player.maxPower;
}
} else if (powerup.type === 'life') {
this.player.lives = Math.min(5, this.player.lives + 1);
}
this.powerups.splice(i, 1);
this.createExplosion(powerup.x + powerup.width / 2, powerup.y + powerup.height / 2, 'powerup');
continue;
}
// 移除超出屏幕的道具
if (powerup.y > this.canvas.height) {
this.powerups.splice(i, 1);
}
}
}
createExplosion(x, y, type = 'normal') {
this.explosions.push({
x: x,
y: y,
radius: type === 'boss' ? 30 : (type === 'powerup' ? 15 : 20),
alpha: 1,
color: type === 'boss' ? '#ff0000' : (type === 'powerup' ? '#ff00ff' : '#ffff00'),
type: type
});
}
updateExplosions() {
for (let i = this.explosions.length - 1; i >= 0; i--) {
this.explosions[i].alpha -= 0.03;
if (this.explosions[i].alpha <= 0) {
this.explosions.splice(i, 1);
}
}
}
checkCollisions() {
// 检查子弹与敌机的碰撞
for (let i = this.player.bullets.length - 1; i >= 0; i--) {
const bullet = this.player.bullets[i];
// 检查与普通敌机的碰撞
for (let j = this.enemies.length - 1; j >= 0; j--) {
const enemy = this.enemies[j];
if (this.isColliding(bullet, enemy)) {
// 造成伤害
enemy.health -= bullet.damage;
// 移除子弹
this.player.bullets.splice(i, 1);
// 检查敌机是否被摧毁
if (enemy.health <= 0) {
// 增加分数和连击
this.score += enemy.score;
this.enemiesShot++;
this.player.combo++;
if (this.player.combo > this.player.maxCombo) {
this.player.maxCombo = this.player.combo;
}
// 根据连击增加额外分数
if (this.player.combo >= 10) {
this.score += Math.floor(this.player.combo / 5);
}
// 创建爆炸效果
this.createExplosion(enemy.x + enemy.width / 2, enemy.y + enemy.height / 2, enemy.type === 'bossBullet' ? 'normal' : enemy.type);
// 可能掉落道具
if (enemy.type !== 'bossBullet') {
this.spawnPowerup(enemy.x + enemy.width / 2, enemy.y + enemy.height / 2);
}
// 移除敌机
this.enemies.splice(j, 1);
}
break;
}
}
// 检查与Boss的碰撞
if (this.boss && this.isColliding(bullet, this.boss)) {
// 造成伤害
this.boss.health -= bullet.damage;
this.updateBossHealthBar();
// 移除子弹
this.player.bullets.splice(i, 1);
// 检查Boss是否被击败
if (this.boss.health <= 0) {
// 增加分数
this.score += this.boss.score;
this.enemiesShot++;
// 创建爆炸效果
this.createExplosion(this.boss.x + this.boss.width / 2, this.boss.y + this.boss.height / 2, 'boss');
// 掉落道具
this.spawnPowerup(this.boss.x + this.boss.width / 2, this.boss.y + this.boss.height / 2);
this.spawnPowerup(this.boss.x + this.boss.width / 4, this.boss.y + this.boss.height / 2);
this.spawnPowerup(this.boss.x + this.boss.width * 3/4, this.boss.y + this.boss.height / 2);
// 移除Boss
this.boss = null;
this.bossActive = false;
document.getElementById('bossHealthBar').style.display = 'none';
}
break;
}
}
// 检查玩家与敌机的碰撞
if (!this.player.invincible) {
for (let i = this.enemies.length - 1; i >= 0; i--) {
if (this.isColliding(this.player, this.enemies[i])) {
// 减少生命
this.player.lives--;
this.player.invincible = true;
this.player.invincibleTimer = 120; // 2秒无敌时间
// 创建爆炸效果
this.createExplosion(this.player.x + this.player.width / 2, this.player.y + this.player.height / 2, 'player');
// 移除敌机
this.enemies.splice(i, 1);
// 检查游戏是否结束
if (this.player.lives <= 0) {
this.gameOver();
return;
}
break;