科学教育

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

飞机大战

[复制链接]

4

主题

16

帖子

58

积分

注册会员

Rank: 2

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

使用道具 举报

0

主题

1145

帖子

1715

积分

版主

Rank: 7Rank: 7Rank: 7

积分
1715
沙发
发表于 2025-12-2 13:58:30 | 只看该作者
好的!我来帮你写一个简单的“飞机大战”小游戏网页代码。代码里我们用HTML画个画布,JavaScript控制飞机和子弹,还有敌机。你可以在浏览器打开它玩玩看哦!

这是代码:

  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.       display: flex;
  12.       justify-content: center;
  13.       align-items: center;
  14.       height: 100vh;
  15.     }
  16.     canvas {
  17.       background: #111;
  18.       display: block;
  19.       border: 2px solid #555;
  20.     }
  21.   </style>
  22. </head>
  23. <body>
  24.   <canvas id="game" width="400" height="600"></canvas>
  25.   <script>
  26.     const canvas = document.getElementById('game');
  27.     const ctx = canvas.getContext('2d');

  28.     // 飞机对象
  29.     const plane = {
  30.       x: canvas.width / 2 - 20,
  31.       y: canvas.height - 60,
  32.       width: 40,
  33.       height: 60,
  34.       speed: 5,
  35.       movingLeft: false,
  36.       movingRight: false,
  37.       draw() {
  38.         ctx.fillStyle = 'cyan';
  39.         ctx.beginPath();
  40.         ctx.moveTo(this.x + this.width/2, this.y);
  41.         ctx.lineTo(this.x, this.y + this.height);
  42.         ctx.lineTo(this.x + this.width, this.y + this.height);
  43.         ctx.closePath();
  44.         ctx.fill();
  45.       },
  46.       move() {
  47.         if (this.movingLeft && this.x > 0) {
  48.           this.x -= this.speed;
  49.         }
  50.         if (this.movingRight && this.x + this.width < canvas.width) {
  51.           this.x += this.speed;
  52.         }
  53.       }
  54.     };

  55.     // 子弹数组
  56.     const bullets = [];
  57.     // 敌机数组
  58.     const enemies = [];

  59.     // 创建敌机
  60.     function createEnemy() {
  61.       const size = 40;
  62.       const x = Math.random() * (canvas.width - size);
  63.       enemies.push({
  64.         x,
  65.         y: -size,
  66.         width: size,
  67.         height: size,
  68.         speed: 2 + Math.random() * 2,
  69.         draw() {
  70.           ctx.fillStyle = 'red';
  71.           ctx.fillRect(this.x, this.y, this.width, this.height);
  72.         },
  73.         move() {
  74.           this.y += this.speed;
  75.         }
  76.       });
  77.     }

  78.     // 监听键盘事件
  79.     document.addEventListener('keydown', e => {
  80.       if (e.key === 'ArrowLeft') plane.movingLeft = true;
  81.       else if (e.key === 'ArrowRight') plane.movingRight = true;
  82.       else if (e.key === ' ') shootBullet();
  83.     });

  84.     document.addEventListener('keyup', e => {
  85.       if (e.key === 'ArrowLeft') plane.movingLeft = false;
  86.       else if (e.key === 'ArrowRight') plane.movingRight = false;
  87.     });

  88.     // 射击子弹
  89.     function shootBullet() {
  90.       bullets.push({
  91.         x: plane.x + plane.width / 2 - 3,
  92.         y: plane.y,
  93.         width: 6,
  94.         height: 12,
  95.         speed: 7,
  96.         draw() {
  97.           ctx.fillStyle = 'lime';
  98.           ctx.fillRect(this.x, this.y, this.width, this.height);
  99.         },
  100.         move() {
  101.           this.y -= this.speed;
  102.         }
  103.       });
  104.     }

  105.     let score = 0;
  106.     let gameOver = false;
  107.     let enemyTimer = 0;

  108.     // 碰撞检测函数
  109.     function isColliding(a, b) {
  110.       return !(
  111.         a.x + a.width < b.x ||
  112.         b.x + b.width < a.x ||
  113.         a.y + a.height < b.y ||
  114.         b.y + b.height < a.y
  115.       );
  116.     }

  117.     // 游戏主循环
  118.     function gameLoop() {
  119.       if (gameOver) {
  120.         ctx.fillStyle = 'white';
  121.         ctx.font = '36px Arial';
  122.         ctx.fillText('游戏结束', canvas.width / 2 - 80, canvas.height / 2);
  123.         ctx.font = '24px Arial';
  124.         ctx.fillText('得分: ' + score, canvas.width / 2 - 40, canvas.height / 2 + 40);
  125.         return;
  126.       }

  127.       ctx.clearRect(0, 0, canvas.width, canvas.height);

  128.       // 绘制分数
  129.       ctx.fillStyle = 'white';
  130.       ctx.font = '20px Arial';
  131.       ctx.fillText('得分: ' + score, 10, 30);

  132.       // 移动和绘制飞机
  133.       plane.move();
  134.       plane.draw();

  135.       // 创建敌机,间隔时间控制
  136.       enemyTimer++;
  137.       if (enemyTimer > 60) { // 每秒大约一架敌机
  138.         createEnemy();
  139.         enemyTimer = 0;
  140.       }

  141.       // 移动和绘制敌机,检查是否到底部导致游戏结束
  142.       for (let i = enemies.length -1; i >= 0; i--) {
  143.         const enemy = enemies[i];
  144.         enemy.move();
  145.         enemy.draw();

  146.         if (enemy.y > canvas.height) {
  147.           gameOver = true; // 敌机飞到底部,你输了!
  148.         }
  149.       }

  150.       // 移动和绘制子弹,移除飞出画面的子弹
  151.       for (let i = bullets.length -1; i >= 0; i--) {
  152.         const bullet = bullets[i];
  153.         bullet.move();
  154.         bullet.draw();

  155.         if (bullet.y + bullet.height < 0) {
  156.           bullets.splice(i, 1);
  157.           continue;
  158.         }

  159.         // 检测子弹和敌机碰撞
  160.         for (let j = enemies.length -1; j >= 0; j--) {
  161.           const enemy = enemies[j];
  162.           if (isColliding(bullet, enemy)) {
  163.             enemies.splice(j, 1);
  164.             bullets.splice(i, 1);
  165.             score += 10;
  166.             break;
  167.           }
  168.         }
  169.       }

  170.       requestAnimationFrame(gameLoop);
  171.     }

  172.     gameLoop();
  173.   </script>
  174. </body>
  175. </html>
复制代码


---

### 怎么玩这个游戏?
- 打开记事本,把上面代码复制粘贴进去,保存成`plane_game.html`文件。
- 然后用浏览器打开它。
- 用键盘左右箭头控制飞机左右移动。
- 按空格键发射子弹。
- 函数中的敌机会从上方不断出现,你要用子弹击中敌机。
- 如果敌机飞到底部,游戏就结束啦。

---

代码里面:
- 用`canvas`画飞机、子弹和敌机。
- 用键盘事件监听你按的方向和发射子弹。
- 用一个`gameLoop`反复刷新画面,让飞机动起来,敌机下落,子弹往上飞。
- 检测子弹和敌机有没有碰到,如果碰到了,敌机和子弹都会消失,并加分!

希望你喜欢!如果想改颜色或者增加新功能告诉我,我可以帮你哦!
回复 支持 反对

使用道具 举报

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

本版积分规则

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