科学教育

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

帮我生成飞机大战斗的网页代码

[复制链接]

1

主题

2

帖子

11

积分

新手上路

Rank: 1

积分
11
跳转到指定楼层
楼主
发表于 2025-11-27 11:56:04 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
帮我生成飞机大战斗的网页代码
回复

使用道具 举报

0

主题

1145

帖子

1715

积分

版主

Rank: 7Rank: 7Rank: 7

积分
1715
沙发
发表于 2025-11-27 11:56:25 | 只看该作者
好的!我来帮你做一个简单的“飞机大战”网页小游戏。这个游戏里,你可以用键盘控制一个小飞机,发射子弹去打敌机。代码会用到HTML、CSS和JavaScript,都是网页制作里面常用的东西。

下面是完整的代码,你可以复制到一个名字叫`index.html`的文件里,然后用浏览器打开就能玩啦!

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4.   <meta charset="UTF-8" />
  5.   <title>简单飞机大战</title>
  6.   <style>
  7.     body {
  8.       margin: 0;
  9.       background: #000;
  10.       overflow: hidden;
  11.     }
  12.     canvas {
  13.       display: block;
  14.       margin: 0 auto;
  15.       background: #111;
  16.       border: 2px solid #444;
  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 player = {
  27.       x: canvas.width / 2 - 20,
  28.       y: canvas.height - 60,
  29.       width: 40,
  30.       height: 60,
  31.       speed: 5,
  32.       movingLeft: false,
  33.       movingRight: false,
  34.       bullets: []
  35.     };

  36.     // 敌机列表
  37.     const enemies = [];
  38.     let enemySpawnTimer = 0;
  39.     const enemySpawnInterval = 100; // 每100帧产生一个敌机

  40.     // 子弹类
  41.     class Bullet {
  42.       constructor(x, y) {
  43.         this.x = x;
  44.         this.y = y;
  45.         this.width = 5;
  46.         this.height = 10;
  47.         this.speed = 7;
  48.         this.alive = true;
  49.       }
  50.       update() {
  51.         this.y -= this.speed;
  52.         if (this.y < 0) this.alive = false;
  53.       }
  54.       draw() {
  55.         ctx.fillStyle = 'yellow';
  56.         ctx.fillRect(this.x, this.y, this.width, this.height);
  57.       }
  58.     }

  59.     // 敌机类
  60.     class Enemy {
  61.       constructor(x, y) {
  62.         this.x = x;
  63.         this.y = y;
  64.         this.width = 40;
  65.         this.height = 40;
  66.         this.speed = 2;
  67.         this.alive = true;
  68.       }
  69.       update() {
  70.         this.y += this.speed;
  71.         if (this.y > canvas.height) this.alive = false;
  72.       }
  73.       draw() {
  74.         ctx.fillStyle = 'red';
  75.         ctx.fillRect(this.x, this.y, this.width, this.height);
  76.       }
  77.     }

  78.     // 监听键盘按下
  79.     window.addEventListener('keydown', e => {
  80.       if (e.code === 'ArrowLeft') player.movingLeft = true;
  81.       if (e.code === 'ArrowRight') player.movingRight = true;
  82.       if (e.code === 'Space') shootBullet();
  83.     });

  84.     // 监听键盘松开
  85.     window.addEventListener('keyup', e => {
  86.       if (e.code === 'ArrowLeft') player.movingLeft = false;
  87.       if (e.code === 'ArrowRight') player.movingRight = false;
  88.     });

  89.     // 发射子弹
  90.     function shootBullet() {
  91.       const bulletX = player.x + player.width / 2 - 2.5;
  92.       const bulletY = player.y;
  93.       player.bullets.push(new Bullet(bulletX, bulletY));
  94.     }

  95.     // 检测碰撞的函数
  96.     function isColliding(a, b) {
  97.       return !(
  98.         a.x + a.width < b.x ||
  99.         a.x > b.x + b.width ||
  100.         a.y + a.height < b.y ||
  101.         a.y > b.y + b.height
  102.       );
  103.     }

  104.     // 游戏主循环
  105.     function gameLoop() {
  106.       ctx.clearRect(0, 0, canvas.width, canvas.height);

  107.       // 控制飞机移动
  108.       if (player.movingLeft && player.x > 0) player.x -= player.speed;
  109.       if (player.movingRight && player.x + player.width < canvas.width) player.x += player.speed;

  110.       // 画飞机(用蓝色矩形代替简单的飞机)
  111.       ctx.fillStyle = 'deepskyblue';
  112.       ctx.fillRect(player.x, player.y, player.width, player.height);

  113.       // 更新和画子弹
  114.       player.bullets.forEach((bullet, index) => {
  115.         bullet.update();
  116.         if (!bullet.alive) {
  117.           player.bullets.splice(index, 1);
  118.         } else {
  119.           bullet.draw();
  120.         }
  121.       });

  122.       // 产生敌机
  123.       enemySpawnTimer++;
  124.       if (enemySpawnTimer >= enemySpawnInterval) {
  125.         enemySpawnTimer = 0;
  126.         const x = Math.random() * (canvas.width - 40);
  127.         enemies.push(new Enemy(x, -40));
  128.       }

  129.       // 更新和画敌机
  130.       enemies.forEach((enemy, index) => {
  131.         enemy.update();
  132.         if (!enemy.alive) {
  133.           enemies.splice(index, 1);
  134.         } else {
  135.           enemy.draw();
  136.         }
  137.       });

  138.       // 检测子弹和敌机碰撞
  139.       player.bullets.forEach((bullet, bIndex) => {
  140.         enemies.forEach((enemy, eIndex) => {
  141.           if (isColliding(bullet, enemy)) {
  142.             bullet.alive = false;
  143.             enemy.alive = false;
  144.           }
  145.         });
  146.       });

  147.       // 检测敌机和飞机碰撞,游戏结束
  148.       for (let enemy of enemies) {
  149.         if (isColliding(enemy, player)) {
  150.           alert('游戏结束!你被敌机撞到了!');
  151.           window.location.reload();
  152.           return;
  153.         }
  154.       }

  155.       requestAnimationFrame(gameLoop);
  156.     }

  157.     gameLoop();
  158.   </script>
  159. </body>
  160. </html>
复制代码


---

### 怎么玩呢?

- 用键盘左右箭头键控制飞机左右移动。
- 按空格键(Space)发射子弹。
- 打飞所有从上方掉下来的红色敌机。
- 如果敌机撞到你,游戏结束,会弹出提示并重新开始。

---

### 小知识点:

- 这个游戏用到了**Canvas**,它是一块画布,可以让我们用代码画图形。
- 游戏中飞机和敌机都是用简单的矩形代替,这样代码更简单。
- 用`requestAnimationFrame`来让浏览器不停地刷新画面,形成动画效果。
- 监听键盘事件,做出响应。
- 判断两个矩形是否碰撞,就是检查它们的位置和大小有没有交叉。

---

你可以把这段代码保存下来,打开看看,自己还能加点特效,比如声音、分数或者更漂亮的飞机图片哦!如果有不懂的地方,可以继续问我~
回复 支持 反对

使用道具 举报

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

本版积分规则

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