科学教育

搜索
查看: 420|回复: 1
打印 上一主题 下一主题

飞机大战网页代码

[复制链接]

1

主题

3

帖子

17

积分

新手上路

Rank: 1

积分
17
跳转到指定楼层
楼主
发表于 2025-12-2 14:02:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
飞机大战网页代码
回复

使用道具 举报

0

主题

1145

帖子

1715

积分

版主

Rank: 7Rank: 7Rank: 7

积分
1715
沙发
发表于 2025-12-2 14:03:24 | 只看该作者
你好!你想做一个“飞机大战”的网页游戏对吧?我来帮你写一个简单的版本,里面有一架可以移动的飞机,还有子弹可以发射,敌人飞机会从上面飞下来,你控制自己的飞机去打敌人。

下面是完整的代码,你可以直接复制到一个叫 **index.html** 的文件里,然后用浏览器打开看效果:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.   <meta charset="UTF-8" />
  5.   <title>简单飞机大战</title>
  6.   <style>
  7.     body {
  8.       margin: 0;
  9.       overflow: hidden;
  10.       background: black;
  11.     }
  12.     canvas {
  13.       display: block;
  14.       margin: 0 auto;
  15.       background: #111;
  16.       border: 1px solid #555;
  17.     }
  18.   </style>
  19. </head>
  20. <body>
  21.   <canvas id="gameCanvas" width="400" height="600"></canvas>

  22.   <script>
  23.     const canvas = document.getElementById('gameCanvas');
  24.     const ctx = canvas.getContext('2d');

  25.     // 飞机对象
  26.     const plane = {
  27.       x: canvas.width / 2 - 20,
  28.       y: canvas.height - 60,
  29.       width: 40,
  30.       height: 40,
  31.       speed: 5,
  32.       movingLeft: false,
  33.       movingRight: false
  34.     };

  35.     // 子弹数组
  36.     const bullets = [];

  37.     // 敌机数组
  38.     const enemies = [];

  39.     // 分数
  40.     let score = 0;

  41.     // 生成敌机
  42.     function createEnemy() {
  43.       const enemyWidth = 40;
  44.       const enemyHeight = 40;
  45.       const x = Math.random() * (canvas.width - enemyWidth);
  46.       const y = -enemyHeight;
  47.       enemies.push({ x, y, width: enemyWidth, height: enemyHeight, speed: 2 + Math.random() * 2 });
  48.     }

  49.     // 绘制飞机
  50.     function drawPlane() {
  51.       ctx.fillStyle = 'skyblue';
  52.       ctx.beginPath();
  53.       ctx.moveTo(plane.x + plane.width / 2, plane.y);
  54.       ctx.lineTo(plane.x, plane.y + plane.height);
  55.       ctx.lineTo(plane.x + plane.width, plane.y + plane.height);
  56.       ctx.closePath();
  57.       ctx.fill();
  58.     }

  59.     // 绘制子弹
  60.     function drawBullets() {
  61.       ctx.fillStyle = 'yellow';
  62.       bullets.forEach(bullet => {
  63.         ctx.fillRect(bullet.x, bullet.y, bullet.width, bullet.height);
  64.       });
  65.     }

  66.     // 绘制敌机
  67.     function drawEnemies() {
  68.       ctx.fillStyle = 'red';
  69.       enemies.forEach(enemy => {
  70.         ctx.fillRect(enemy.x, enemy.y, enemy.width, enemy.height);
  71.       });
  72.     }

  73.     // 画分数
  74.     function drawScore() {
  75.       ctx.fillStyle = 'white';
  76.       ctx.font = '20px Arial';
  77.       ctx.fillText('分数: ' + score, 10, 30);
  78.     }

  79.     // 更新飞机的位置
  80.     function updatePlane() {
  81.       if (plane.movingLeft && plane.x > 0) {
  82.         plane.x -= plane.speed;
  83.       }
  84.       if (plane.movingRight && plane.x + plane.width < canvas.width) {
  85.         plane.x += plane.speed;
  86.       }
  87.     }

  88.     // 更新子弹的位置
  89.     function updateBullets() {
  90.       bullets.forEach((bullet, index) => {
  91.         bullet.y -= bullet.speed;
  92.         if (bullet.y + bullet.height < 0) {
  93.           bullets.splice(index, 1); // 移除超出屏幕的子弹
  94.         }
  95.       });
  96.     }

  97.     // 更新敌机位置
  98.     function updateEnemies() {
  99.       enemies.forEach((enemy, index) => {
  100.         enemy.y += enemy.speed;
  101.         if (enemy.y > canvas.height) {
  102.           enemies.splice(index, 1); // 移除离开底部的敌机
  103.         }
  104.       });
  105.     }

  106.     // 检测碰撞(AABB算法)
  107.     function isColliding(rect1, rect2) {
  108.       return !(
  109.         rect1.x > rect2.x + rect2.width ||
  110.         rect1.x + rect1.width < rect2.x ||
  111.         rect1.y > rect2.y + rect2.height ||
  112.         rect1.y + rect1.height < rect2.y
  113.       );
  114.     }

  115.     // 处理碰撞检测
  116.     function checkCollisions() {
  117.       // 子弹击中敌机
  118.       bullets.forEach((bullet, bIndex) => {
  119.         enemies.forEach((enemy, eIndex) => {
  120.           if (isColliding(bullet, enemy)) {
  121.             bullets.splice(bIndex, 1);
  122.             enemies.splice(eIndex, 1);
  123.             score += 10;
  124.           }
  125.         });
  126.       });

  127.       // 敌机碰到飞机 -> 游戏结束
  128.       for (let enemy of enemies) {
  129.         if (isColliding(enemy, plane)) {
  130.           alert('游戏结束! 你的得分是: ' + score);
  131.           document.location.reload();
  132.           break;
  133.         }
  134.       }
  135.     }

  136.     // 发射子弹
  137.     function shoot() {
  138.       bullets.push({
  139.         x: plane.x + plane.width / 2 - 2,
  140.         y: plane.y,
  141.         width: 4,
  142.         height: 10,
  143.         speed: 7
  144.       });
  145.     }

  146.     // 按键操作
  147.     window.addEventListener('keydown', function(e) {
  148.       if (e.key === 'ArrowLeft') {
  149.         plane.movingLeft = true;
  150.       } else if (e.key === 'ArrowRight') {
  151.         plane.movingRight = true;
  152.       } else if (e.key === ' ') {
  153.         shoot();
  154.       }
  155.     });

  156.     window.addEventListener('keyup', function(e) {
  157.       if (e.key === 'ArrowLeft') {
  158.         plane.movingLeft = false;
  159.       } else if (e.key === 'ArrowRight') {
  160.         plane.movingRight = false;
  161.       }
  162.     });

  163.     // 主循环
  164.     let enemyTimer = 0;
  165.     function gameLoop() {
  166.       ctx.clearRect(0, 0, canvas.width, canvas.height);

  167.       updatePlane();
  168.       updateBullets();
  169.       updateEnemies();

  170.       checkCollisions();

  171.       drawPlane();
  172.       drawBullets();
  173.       drawEnemies();
  174.       drawScore();

  175.       // 每隔一定时间生成新敌机
  176.       enemyTimer++;
  177.       if (enemyTimer > 60) { // 大约每1秒生成一个敌机
  178.         createEnemy();
  179.         enemyTimer = 0;
  180.       }

  181.       requestAnimationFrame(gameLoop);
  182.     }

  183.     // 开始游戏
  184.     gameLoop();
  185.   </script>
  186. </body>
  187. </html>
复制代码


---

### 怎么玩?

- 用键盘的左右方向键(← →)控制你的小飞机左右移动。
- 用空格键(Space)发射子弹,打掉从上面飞下来的红色敌机。
- 打掉敌机会得分,敌机撞到你的飞机游戏就结束了。

---

### 代码讲解:

- 我们用 `<canvas>` 画图,所以程序里用 `ctx` 来画飞机、敌机和子弹。
- 飞机用三角形表示,敌机和子弹用矩形表示,很简单。
- 子弹往上飞,敌机往下飞。
- 用一个函数 `isColliding` 判断两个矩形有没有碰到。
- 游戏不断刷新,飞机和子弹的位置不断更新。
- 敌机不断随机出现,增加难度。
- 游戏结束后会弹出得分,然后刷新页面重新开始。

---

如果你想更有趣,可以试着:

- 改变飞机、敌机的颜色或者形状
- 增加血量,打中几次才死
- 加入音效或者爆炸动画

需要帮忙随时问我哦!祝你编程开心!????✈️
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表