科学教育

标题: 601陈若冰 [打印本页]

作者: 陈若冰    时间: 2025-11-27 12:10
标题: 601陈若冰
做一个飞机大战的网页代码

作者: kaia    时间: 2025-11-27 12:10
你好!我来帮你做一个简单的“飞机大战”小游戏网页代码哦。我们可以用HTML + CSS + JavaScript来做,这样你在电脑上打开网页就能玩了!

下面是一个简单版的飞机大战游戏代码,里面有你的飞机,可以左右移动,还能发射子弹打毁敌机哦!

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <title>简单飞机大战游戏</title>
  6. <style>
  7.   body { margin: 0; background: black; overflow: hidden; }
  8.   canvas { display: block; margin: 0 auto; background: #111; }
  9. </style>
  10. </head>
  11. <body>

  12. <canvas id="gameCanvas" width="400" height="600"></canvas>

  13. <script>
  14. // 获取画布和上下文
  15. const canvas = document.getElementById('gameCanvas');
  16. const ctx = canvas.getContext('2d');

  17. const planeWidth = 40;
  18. const planeHeight = 60;
  19. let planeX = canvas.width / 2 - planeWidth / 2;
  20. const planeY = canvas.height - planeHeight - 10;

  21. const bullets = [];
  22. const enemies = [];
  23. let keys = {};

  24. let score = 0;
  25. let gameOver = false;

  26. // 飞机绘制
  27. function drawPlane() {
  28.   ctx.fillStyle = 'cyan';
  29.   ctx.beginPath();
  30.   ctx.moveTo(planeX + planeWidth / 2, planeY);
  31.   ctx.lineTo(planeX, planeY + planeHeight);
  32.   ctx.lineTo(planeX + planeWidth, planeY + planeHeight);
  33.   ctx.closePath();
  34.   ctx.fill();
  35. }

  36. // 子弹类
  37. class Bullet {
  38.   constructor(x, y) {
  39.     this.x = x;
  40.     this.y = y;
  41.     this.speed = 8;
  42.     this.width = 4;
  43.     this.height = 10;
  44.   }
  45.   move() {
  46.     this.y -= this.speed;
  47.   }
  48.   draw() {
  49.     ctx.fillStyle = 'yellow';
  50.     ctx.fillRect(this.x, this.y, this.width, this.height);
  51.   }
  52. }

  53. // 敌机类
  54. class Enemy {
  55.   constructor(x, y) {
  56.     this.x = x;
  57.     this.y = y;
  58.     this.speed = 2;
  59.     this.width = 40;
  60.     this.height = 30;
  61.   }
  62.   move() {
  63.     this.y += this.speed;
  64.   }
  65.   draw() {
  66.     ctx.fillStyle = 'red';
  67.     ctx.fillRect(this.x, this.y, this.width, this.height);
  68.   }
  69. }

  70. // 生成敌机,每隔一段时间出现
  71. function spawnEnemy() {
  72.   if (gameOver) return;
  73.   const x = Math.random() * (canvas.width - 40);
  74.   enemies.push(new Enemy(x, -30));
  75. }
  76. setInterval(spawnEnemy, 1500);

  77. // 监听键盘按键
  78. window.addEventListener('keydown', function(e) {
  79.   keys[e.key] = true;
  80.   if (e.key === ' ') {  // 空格发射子弹
  81.     bullets.push(new Bullet(planeX + planeWidth / 2 - 2, planeY));
  82.   }
  83. });
  84. window.addEventListener('keyup', function(e) {
  85.   keys[e.key] = false;
  86. });

  87. // 碰撞检测,判断两个矩形是否相交
  88. function collides(rect1, rect2) {
  89.   return !(rect2.x > rect1.x + rect1.width ||
  90.            rect2.x + rect2.width < rect1.x ||
  91.            rect2.y > rect1.y + rect1.height ||
  92.            rect2.y + rect2.height < rect1.y);
  93. }

  94. // 游戏主循环
  95. function gameLoop() {
  96.   if (gameOver) {
  97.     ctx.fillStyle = 'white';
  98.     ctx.font = '30px Arial';
  99.     ctx.fillText('游戏结束!得分:' + score, 50, canvas.height / 2);
  100.     return;
  101.   }
  102.   
  103.   // 清屏
  104.   ctx.clearRect(0, 0, canvas.width, canvas.height);
  105.   
  106.   // 移动飞机
  107.   if (keys['ArrowLeft'] && planeX > 0) {
  108.     planeX -= 5;
  109.   }
  110.   if (keys['ArrowRight'] && planeX < canvas.width - planeWidth) {
  111.     planeX += 5;
  112.   }
  113.   
  114.   drawPlane();
  115.   
  116.   // 移动和绘制子弹
  117.   for (let i = bullets.length -1; i >= 0; i--) {
  118.     bullets[i].move();
  119.     if (bullets[i].y < -10) {
  120.       bullets.splice(i, 1);
  121.       continue;
  122.     }
  123.     bullets[i].draw();
  124.   }
  125.   
  126.   // 移动和绘制敌机
  127.   for (let i = enemies.length - 1; i >= 0; i--) {
  128.     enemies[i].move();
  129.     enemies[i].draw();
  130.    
  131.     // 如果敌机碰到飞机,游戏结束
  132.     if (collides(
  133.       {x: planeX, y: planeY, width: planeWidth, height: planeHeight},
  134.       enemies[i]
  135.     )) {
  136.       gameOver = true;
  137.     }
  138.    
  139.     // 敌机到底部则移除(不结束游戏)
  140.     if (enemies[i].y > canvas.height) {
  141.       enemies.splice(i, 1);
  142.     }
  143.   }
  144.   
  145.   // 检测子弹与敌机碰撞
  146.   for (let i = bullets.length -1; i >= 0; i--) {
  147.     for (let j = enemies.length -1; j >=0; j--) {
  148.       if (collides(bullets[i], enemies[j])) {
  149.         // 击中,移除子弹和敌机,增加分数
  150.         bullets.splice(i, 1);
  151.         enemies.splice(j, 1);
  152.         score += 10;
  153.         break;
  154.       }
  155.     }
  156.   }
  157.   
  158.   // 显示分数
  159.   ctx.fillStyle = 'white';
  160.   ctx.font = '20px Arial';
  161.   ctx.fillText('得分: ' + score, 10, 25);
  162.   
  163.   requestAnimationFrame(gameLoop);
  164. }

  165. gameLoop();

  166. </script>

  167. </body>
  168. </html>
复制代码


---

**怎么玩?**

- 打开这个网页后,你会看到黑色背景,中间有个蓝色小飞机。
- 用键盘左右箭头键控制飞机左右移动。
- 按空格键发射黄色的子弹。
- 红色的方块是敌机,它们会从上往下飞。
- 用子弹打掉敌机可以得分,敌机如果碰到你的飞机,游戏结束。

---

你可以把这段代码复制到记事本里,然后保存为`plane.html`文件。在浏览器里打开,就能玩啦!是不是很酷?

如果想让飞机更漂亮,或者加更多功能,可以告诉我,我教你继续改进!加油!✈️????




欢迎光临 科学教育 (http://www.sci-edu.net/) Powered by Discuz! X3.2