-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshoot3.7t.html
More file actions
832 lines (710 loc) · 31.8 KB
/
shoot3.7t.html
File metadata and controls
832 lines (710 loc) · 31.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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>坦克战游戏</title>
<style>
body {
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #000;
font-family: Arial, sans-serif;
}
canvas {
border: 1px solid #333;
background-color: #111;
}
#gameInfo {
position: absolute;
top: 10px;
left: 10px;
color: white;
font-size: 16px;
background-color: rgba(0,0,0,0.5);
padding: 5px;
border-radius: 5px;
}
</style>
</head>
<body>
<div id="gameInfo">方向键移动,空格射击</div>
<canvas id="gameCanvas" width="640" height="640"></canvas>
<script>
// 游戏常量
const TILE_SIZE = 32; // 一个瓦片的大小
const GRID_WIDTH = 20; // 地图宽度(格子数)
const GRID_HEIGHT = 20; // 地图高度(格子数)
const CANVAS_WIDTH = TILE_SIZE * GRID_WIDTH;
const CANVAS_HEIGHT = TILE_SIZE * GRID_HEIGHT;
// 游戏元素类型
const EMPTY = 0;
const BRICK = 1;
const STEEL = 2;
const BASE = 3;
// 方向常量
const UP = 0;
const RIGHT = 1;
const DOWN = 2;
const LEFT = 3;
// 游戏状态
const GAME_STATE = {
MENU: 0,
PLAYING: 1,
GAME_OVER: 2,
WIN: 3
};
// 获取Canvas和上下文
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
// 游戏类
class Game {
constructor() {
// 初始化游戏状态和地图
this.state = GAME_STATE.MENU;
this.grid = this.createEmptyGrid();
this.player = null;
this.base = null;
this.enemies = [];
this.bullets = [];
this.maxEnemies = 10;
this.enemiesKilled = 0;
this.totalEnemies = 0;
// 键盘控制
this.keys = {};
window.addEventListener('keydown', (e) => {
this.keys[e.keyCode] = true;
// 防止方向键滚动页面
if([32, 37, 38, 39, 40].includes(e.keyCode)) {
e.preventDefault();
}
// 空格键开始游戏或重新开始
if(e.keyCode === 32 && this.state !== GAME_STATE.PLAYING) {
this.resetGame();
}
});
window.addEventListener('keyup', (e) => {
this.keys[e.keyCode] = false;
});
// 初始化游戏
this.showMenu();
// 开始游戏循环
this.lastTime = 0;
requestAnimationFrame(this.gameLoop.bind(this));
}
// 创建空地图
createEmptyGrid() {
const grid = new Array(GRID_HEIGHT);
for(let y = 0; y < GRID_HEIGHT; y++) {
grid[y] = new Array(GRID_WIDTH).fill(EMPTY);
}
return grid;
}
// 重置游戏
resetGame() {
this.grid = this.createEmptyGrid();
this.player = new Tank(
Math.floor(GRID_WIDTH / 2) * TILE_SIZE,
(GRID_HEIGHT - 2) * TILE_SIZE,
true
);
this.base = {
x: Math.floor(GRID_WIDTH / 2) * TILE_SIZE,
y: (GRID_HEIGHT - 1) * TILE_SIZE
};
this.enemies = [];
this.bullets = [];
this.enemiesKilled = 0;
this.totalEnemies = 0;
this.state = GAME_STATE.PLAYING;
// 创建地图
this.createMap();
// 创建初始敌人
this.spawnEnemies(3);
}
// 创建地图
createMap() {
// 放置基地周围的砖墙保护
const baseX = Math.floor(GRID_WIDTH / 2);
const baseY = GRID_HEIGHT - 1;
this.grid[baseY][baseX] = BASE;
// 基地保护墙
this.grid[baseY-1][baseX-1] = BRICK;
this.grid[baseY-1][baseX] = BRICK;
this.grid[baseY-1][baseX+1] = BRICK;
this.grid[baseY][baseX-1] = BRICK;
this.grid[baseY][baseX+1] = BRICK;
// 随机生成砖墙和钢板
for(let y = 0; y < GRID_HEIGHT - 2; y++) {
for(let x = 0; x < GRID_WIDTH; x++) {
// 避开玩家初始位置和上方区域
if((y > GRID_HEIGHT - 5 && x >= baseX - 3 && x <= baseX + 3) ||
(y < 2 && (x < 3 || x > GRID_WIDTH - 4))) {
continue;
}
const rand = Math.random();
if(rand < 0.15) {
this.grid[y][x] = BRICK;
} else if(rand < 0.2) {
this.grid[y][x] = STEEL;
}
}
}
// 确保地图连通性
this.ensureAccessibility();
}
// 确保地图连通性
ensureAccessibility() {
// 确保有通路从顶部到基地
const baseX = Math.floor(GRID_WIDTH / 2);
// 在顶部创建几个入口
for(let x = 0; x < GRID_WIDTH; x += 5) {
this.grid[0][Math.min(x, GRID_WIDTH-1)] = EMPTY;
this.grid[1][Math.min(x, GRID_WIDTH-1)] = EMPTY;
}
// 创建从顶部到基地的几条路径
for(let path = 0; path < 3; path++) {
let x = Math.floor(Math.random() * GRID_WIDTH);
let y = 0;
while(y < GRID_HEIGHT - 2) {
this.grid[y][x] = EMPTY;
// 随机选择下一个方向(倾向于向下移动)
const dir = Math.random() < 0.7 ? DOWN : (Math.random() < 0.5 ? LEFT : RIGHT);
if(dir === DOWN) {
y++;
} else if(dir === LEFT && x > 0) {
x--;
} else if(dir === RIGHT && x < GRID_WIDTH - 1) {
x++;
} else {
y++;
}
}
// 确保路径连接到基地附近
if(x < baseX) {
for(let i = x; i <= baseX; i++) {
this.grid[GRID_HEIGHT-3][i] = EMPTY;
}
} else {
for(let i = baseX; i <= x; i++) {
this.grid[GRID_HEIGHT-3][i] = EMPTY;
}
}
}
// 确保玩家周围有足够空间
const playerX = Math.floor(GRID_WIDTH / 2);
const playerY = GRID_HEIGHT - 2;
this.grid[playerY][playerX] = EMPTY;
this.grid[playerY][playerX-1] = EMPTY;
this.grid[playerY][playerX+1] = EMPTY;
this.grid[playerY-1][playerX] = EMPTY;
}
// 生成敌人
spawnEnemies(count) {
if(this.totalEnemies >= this.maxEnemies) return;
const spawnPoints = [
{x: 0, y: 0},
{x: Math.floor(GRID_WIDTH / 2) * TILE_SIZE, y: 0},
{x: (GRID_WIDTH - 1) * TILE_SIZE, y: 0}
];
let spawned = 0;
for(let i = 0; i < count; i++) {
if(this.totalEnemies + spawned >= this.maxEnemies) break;
const spawnPoint = spawnPoints[Math.floor(Math.random() * spawnPoints.length)];
const enemy = new Tank(spawnPoint.x, spawnPoint.y, false);
// 避免敌人重叠
let canSpawn = true;
for(const existingEnemy of this.enemies) {
if(this.checkCollision(enemy, existingEnemy)) {
canSpawn = false;
break;
}
}
if(canSpawn) {
this.enemies.push(enemy);
spawned++;
this.totalEnemies++;
}
}
}
// 游戏循环
gameLoop(timestamp) {
// 计算时间增量
const deltaTime = timestamp - this.lastTime;
this.lastTime = timestamp;
// 清除画布
ctx.clearRect(0, 0, canvas.width, canvas.height);
if(this.state === GAME_STATE.PLAYING) {
this.update(deltaTime / 1000);
this.render();
} else if(this.state === GAME_STATE.MENU) {
this.showMenu();
} else {
this.showGameEnd();
}
requestAnimationFrame(this.gameLoop.bind(this));
}
// 更新游戏状态
update(dt) {
// 更新玩家
this.updatePlayer(dt);
// 更新敌人
this.updateEnemies(dt);
// 更新子弹
this.updateBullets(dt);
// 检查游戏状态
this.checkGameState();
// 如果敌人数量少于最大值的一半,生成新敌人
if(this.enemies.length < 5 && this.totalEnemies < this.maxEnemies) {
if(Math.random() < 0.02) {
this.spawnEnemies(1);
}
}
}
// 更新玩家
updatePlayer(dt) {
if(!this.player) return;
// 保存旧位置用于碰撞检测
const oldX = this.player.x;
const oldY = this.player.y;
// 处理移动
if(this.keys[38]) { // 上
this.player.direction = UP;
this.player.y -= this.player.speed * dt;
} else if(this.keys[40]) { // 下
this.player.direction = DOWN;
this.player.y += this.player.speed * dt;
} else if(this.keys[37]) { // 左
this.player.direction = LEFT;
this.player.x -= this.player.speed * dt;
} else if(this.keys[39]) { // 右
this.player.direction = RIGHT;
this.player.x += this.player.speed * dt;
}
// 边界检查
this.player.x = Math.max(0, Math.min(CANVAS_WIDTH - TILE_SIZE, this.player.x));
this.player.y = Math.max(0, Math.min(CANVAS_HEIGHT - TILE_SIZE, this.player.y));
// 碰撞检测
if(this.checkMapCollision(this.player)) {
this.player.x = oldX;
this.player.y = oldY;
}
// 射击
if(this.keys[32] && this.player.canShoot()) { // 空格
this.bullets.push(this.player.shoot());
}
// 更新射击冷却
this.player.updateShootCooldown(dt);
}
// 更新敌人
updateEnemies(dt) {
for(let i = 0; i < this.enemies.length; i++) {
const enemy = this.enemies[i];
// 保存旧位置用于碰撞检测
const oldX = enemy.x;
const oldY = enemy.y;
// 更新AI移动
enemy.updateAI(dt);
// 应用移动
switch(enemy.direction) {
case UP:
enemy.y -= enemy.speed * dt;
break;
case DOWN:
enemy.y += enemy.speed * dt;
break;
case LEFT:
enemy.x -= enemy.speed * dt;
break;
case RIGHT:
enemy.x += enemy.speed * dt;
break;
}
// 边界检查
enemy.x = Math.max(0, Math.min(CANVAS_WIDTH - TILE_SIZE, enemy.x));
enemy.y = Math.max(0, Math.min(CANVAS_HEIGHT - TILE_SIZE, enemy.y));
// 碰撞检测与地图
if(this.checkMapCollision(enemy)) {
enemy.x = oldX;
enemy.y = oldY;
enemy.changeDirection();
}
// 检查与其他坦克的碰撞
for(let j = 0; j < this.enemies.length; j++) {
if(i !== j && this.checkCollision(enemy, this.enemies[j])) {
enemy.x = oldX;
enemy.y = oldY;
enemy.changeDirection();
break;
}
}
// 与玩家的碰撞
if(this.player && this.checkCollision(enemy, this.player)) {
enemy.x = oldX;
enemy.y = oldY;
enemy.changeDirection();
}
// 敌人射击
if(enemy.canShoot() && Math.random() < 0.02) {
this.bullets.push(enemy.shoot());
}
// 更新射击冷却
enemy.updateShootCooldown(dt);
}
}
// 更新子弹
updateBullets(dt) {
for(let i = this.bullets.length - 1; i >= 0; i--) {
const bullet = this.bullets[i];
// 更新子弹位置
switch(bullet.direction) {
case UP:
bullet.y -= bullet.speed * dt;
break;
case DOWN:
bullet.y += bullet.speed * dt;
break;
case LEFT:
bullet.x -= bullet.speed * dt;
break;
case RIGHT:
bullet.x += bullet.speed * dt;
break;
}
// 检查子弹是否超出边界
if(bullet.x < 0 || bullet.x > CANVAS_WIDTH ||
bullet.y < 0 || bullet.y > CANVAS_HEIGHT) {
this.bullets.splice(i, 1);
continue;
}
// 检查子弹与地图碰撞
const gridX = Math.floor(bullet.x / TILE_SIZE);
const gridY = Math.floor(bullet.y / TILE_SIZE);
if(gridX >= 0 && gridX < GRID_WIDTH && gridY >= 0 && gridY < GRID_HEIGHT) {
const cell = this.grid[gridY][gridX];
if(cell === BRICK) {
// 销毁砖墙
this.grid[gridY][gridX] = EMPTY;
this.bullets.splice(i, 1);
continue;
} else if(cell === STEEL) {
// 钢板反弹子弹
this.bullets.splice(i, 1);
continue;
} else if(cell === BASE) {
// 击中基地,游戏结束
this.state = GAME_STATE.GAME_OVER;
this.bullets.splice(i, 1);
continue;
}
}
// 检查子弹与坦克碰撞
if(this.player && !bullet.isFromPlayer && this.checkBulletCollision(bullet, this.player)) {
// 玩家被击中,游戏结束
this.state = GAME_STATE.GAME_OVER;
this.bullets.splice(i, 1);
continue;
}
// 检查子弹与敌人碰撞
for(let j = this.enemies.length - 1; j >= 0; j--) {
if(bullet.isFromPlayer && this.checkBulletCollision(bullet, this.enemies[j])) {
// 敌人被击中
this.enemies.splice(j, 1);
this.bullets.splice(i, 1);
this.enemiesKilled++;
break;
}
}
// 检查子弹与子弹碰撞
for(let j = this.bullets.length - 1; j > i; j--) {
if(bullet.isFromPlayer !== this.bullets[j].isFromPlayer &&
Math.abs(bullet.x - this.bullets[j].x) < 5 &&
Math.abs(bullet.y - this.bullets[j].y) < 5) {
// 子弹相撞
this.bullets.splice(j, 1);
this.bullets.splice(i, 1);
break;
}
}
}
}
// 检查游戏状态
checkGameState() {
// 检查游戏胜利条件
if(this.enemiesKilled >= this.maxEnemies && this.enemies.length === 0) {
this.state = GAME_STATE.WIN;
}
}
// 检查两个物体之间的碰撞
checkCollision(obj1, obj2) {
return obj1.x < obj2.x + TILE_SIZE &&
obj1.x + TILE_SIZE > obj2.x &&
obj1.y < obj2.y + TILE_SIZE &&
obj1.y + TILE_SIZE > obj2.y;
}
// 检查物体与地图的碰撞
checkMapCollision(obj) {
// 转换坦克坐标到网格坐标
const gridX1 = Math.floor(obj.x / TILE_SIZE);
const gridY1 = Math.floor(obj.y / TILE_SIZE);
const gridX2 = Math.floor((obj.x + TILE_SIZE - 1) / TILE_SIZE);
const gridY2 = Math.floor((obj.y + TILE_SIZE - 1) / TILE_SIZE);
// 检查每个角落
for(let y = gridY1; y <= gridY2; y++) {
for(let x = gridX1; x <= gridX2; x++) {
if(x >= 0 && x < GRID_WIDTH && y >= 0 && y < GRID_HEIGHT) {
if(this.grid[y][x] !== EMPTY) {
return true;
}
}
}
}
return false;
}
// 检查子弹与物体碰撞
checkBulletCollision(bullet, obj) {
return bullet.x >= obj.x && bullet.x <= obj.x + TILE_SIZE &&
bullet.y >= obj.y && bullet.y <= obj.y + TILE_SIZE;
}
// 渲染
render() {
// 绘制地图
this.renderMap();
// 绘制玩家
if(this.player) {
this.player.render(ctx);
}
// 绘制敌人
for(const enemy of this.enemies) {
enemy.render(ctx);
}
// 绘制子弹
for(const bullet of this.bullets) {
bullet.render(ctx);
}
// 绘制UI
this.renderUI();
}
// 渲染地图
renderMap() {
for(let y = 0; y < GRID_HEIGHT; y++) {
for(let x = 0; x < GRID_WIDTH; x++) {
const cell = this.grid[y][x];
const px = x * TILE_SIZE;
const py = y * TILE_SIZE;
if(cell === BRICK) {
// 砖墙
ctx.fillStyle = '#964B00';
ctx.fillRect(px, py, TILE_SIZE, TILE_SIZE);
// 砖纹
ctx.strokeStyle = '#7D3C00';
ctx.lineWidth = 2;
ctx.strokeRect(px + 2, py + 2, TILE_SIZE/2 - 4, TILE_SIZE/2 - 4);
ctx.strokeRect(px + TILE_SIZE/2 + 2, py + 2, TILE_SIZE/2 - 4, TILE_SIZE/2 - 4);
ctx.strokeRect(px + 2, py + TILE_SIZE/2 + 2, TILE_SIZE/2 - 4, TILE_SIZE/2 - 4);
ctx.strokeRect(px + TILE_SIZE/2 + 2, py + TILE_SIZE/2 + 2, TILE_SIZE/2 - 4, TILE_SIZE/2 - 4);
} else if(cell === STEEL) {
// 钢板
ctx.fillStyle = '#888';
ctx.fillRect(px, py, TILE_SIZE, TILE_SIZE);
// 钢铁纹理
ctx.fillStyle = '#999';
ctx.fillRect(px + 4, py + 4, TILE_SIZE - 8, TILE_SIZE - 8);
ctx.strokeStyle = '#666';
ctx.lineWidth = 1;
ctx.strokeRect(px, py, TILE_SIZE, TILE_SIZE);
} else if(cell === BASE) {
// 基地
ctx.fillStyle = '#080';
ctx.fillRect(px, py, TILE_SIZE, TILE_SIZE);
// 星星标志
ctx.fillStyle = '#FF0';
ctx.beginPath();
const cx = px + TILE_SIZE/2;
const cy = py + TILE_SIZE/2;
const r = TILE_SIZE * 0.4;
for(let i = 0; i < 5; i++) {
const angle = Math.PI/2 + i * Math.PI * 2/5;
const x1 = cx + Math.cos(angle) * r;
const y1 = cy - Math.sin(angle) * r;
if(i === 0) ctx.moveTo(x1, y1);
else ctx.lineTo(x1, y1);
const smallAngle = angle + Math.PI/5;
const x2 = cx + Math.cos(smallAngle) * (r/2);
const y2 = cy - Math.sin(smallAngle) * (r/2);
ctx.lineTo(x2, y2);
}
ctx.closePath();
ctx.fill();
}
}
}
}
// 渲染UI
renderUI() {
ctx.fillStyle = 'white';
ctx.font = '16px Arial';
ctx.fillText(`敌人: ${this.enemies.length} / ${this.maxEnemies - this.enemiesKilled}`, 10, 25);
ctx.fillText(`已消灭: ${this.enemiesKilled}`, 10, 50);
}
// 显示菜单
showMenu() {
ctx.fillStyle = 'black';
ctx.fillRect(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT);
ctx.fillStyle = 'white';
ctx.font = '40px Arial';
ctx.textAlign = 'center';
ctx.fillText('坦克战', CANVAS_WIDTH/2, CANVAS_HEIGHT/2 - 40);
ctx.font = '20px Arial';
ctx.fillText('使用方向键移动,空格键射击', CANVAS_WIDTH/2, CANVAS_HEIGHT/2 + 20);
ctx.fillText('按空格键开始游戏', CANVAS_WIDTH/2, CANVAS_HEIGHT/2 + 60);
}
// 显示游戏结束
showGameEnd() {
ctx.fillStyle = 'rgba(0, 0, 0, 0.7)';
ctx.fillRect(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT);
ctx.fillStyle = 'white';
ctx.font = '40px Arial';
ctx.textAlign = 'center';
if(this.state === GAME_STATE.GAME_OVER) {
ctx.fillText('游戏结束', CANVAS_WIDTH/2, CANVAS_HEIGHT/2 - 40);
} else {
ctx.fillText('胜利!', CANVAS_WIDTH/2, CANVAS_HEIGHT/2 - 40);
}
ctx.font = '20px Arial';
ctx.fillText(`敌人消灭: ${this.enemiesKilled}`, CANVAS_WIDTH/2, CANVAS_HEIGHT/2 + 20);
ctx.fillText('按空格键重新开始', CANVAS_WIDTH/2, CANVAS_HEIGHT/2 + 60);
}
}
// 坦克类
class Tank {
constructor(x, y, isPlayer) {
this.x = x;
this.y = y;
this.isPlayer = isPlayer;
this.direction = isPlayer ? UP : DOWN;
this.speed = isPlayer ? 150 : 100;
this.shootCooldown = 0;
this.shootCooldownMax = isPlayer ? 0.3 : 1;
this.aiTimer = 0;
this.aiMoveTime = 0;
}
// 更新AI行为
updateAI(dt) {
if(this.isPlayer) return;
this.aiTimer += dt;
// 每1-3秒更新一次AI决策
if(this.aiTimer > this.aiMoveTime) {
this.aiTimer = 0;
this.aiMoveTime = 1 + Math.random() * 2;
// 随机选择方向
this.direction = Math.floor(Math.random() * 4);
}
}
// 改变方向(碰到障碍物时)
changeDirection() {
// 简单的AI:随机选择新方向
if(this.isPlayer) return;
this.direction = Math.floor(Math.random() * 4);
this.aiTimer = 0;
}
// 检查是否可以射击
canShoot() {
return this.shootCooldown <= 0;
}
// 更新射击冷却时间
updateShootCooldown(dt) {
if(this.shootCooldown > 0) {
this.shootCooldown -= dt;
}
}
// 射击
shoot() {
this.shootCooldown = this.shootCooldownMax;
let bulletX, bulletY;
// 根据方向计算子弹初始位置
switch(this.direction) {
case UP:
bulletX = this.x + TILE_SIZE/2;
bulletY = this.y;
break;
case DOWN:
bulletX = this.x + TILE_SIZE/2;
bulletY = this.y + TILE_SIZE;
break;
case LEFT:
bulletX = this.x;
bulletY = this.y + TILE_SIZE/2;
break;
case RIGHT:
bulletX = this.x + TILE_SIZE;
bulletY = this.y + TILE_SIZE/2;
break;
}
return new Bullet(bulletX, bulletY, this.direction, this.isPlayer);
}
// 渲染坦克
render(ctx) {
// 坦克主体
ctx.fillStyle = this.isPlayer ? '#0A0' : '#A00';
ctx.fillRect(this.x, this.y, TILE_SIZE, TILE_SIZE);
// 坦克履带
ctx.fillStyle = '#000';
ctx.fillRect(this.x, this.y, 5, TILE_SIZE);
ctx.fillRect(this.x + TILE_SIZE - 5, this.y, 5, TILE_SIZE);
// 坦克炮塔
ctx.fillStyle = this.isPlayer ? '#090' : '#900';
ctx.fillRect(this.x + TILE_SIZE/4, this.y + TILE_SIZE/4, TILE_SIZE/2, TILE_SIZE/2);
// 坦克炮管
ctx.strokeStyle = '#000';
ctx.lineWidth = 3;
ctx.beginPath();
const centerX = this.x + TILE_SIZE/2;
const centerY = this.y + TILE_SIZE/2;
let endX = centerX, endY = centerY;
switch(this.direction) {
case UP:
endY = this.y;
break;
case DOWN:
endY = this.y + TILE_SIZE;
break;
case LEFT:
endX = this.x;
break;
case RIGHT:
endX = this.x + TILE_SIZE;
break;
}
ctx.moveTo(centerX, centerY);
ctx.lineTo(endX, endY);
ctx.stroke();
}
}
// 子弹类
class Bullet {
constructor(x, y, direction, isFromPlayer) {
this.x = x;
this.y = y;
this.direction = direction;
this.isFromPlayer = isFromPlayer;
this.speed = 300;
}
// 渲染子弹
render(ctx) {
ctx.fillStyle = this.isFromPlayer ? '#0F0' : '#F00';
ctx.beginPath();
ctx.arc(this.x, this.y, 3, 0, Math.PI * 2);
ctx.fill();
}
}
// 启动游戏
window.onload = function() {
new Game();
};
</script>
</body>
</html>