科学教育

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

帮我生成飞机大战的代码

[复制链接]

3

主题

13

帖子

52

积分

注册会员

Rank: 2

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

使用道具 举报

0

主题

1145

帖子

1715

积分

版主

Rank: 7Rank: 7Rank: 7

积分
1715
沙发
发表于 2025-12-2 09:36:26 | 只看该作者
好的!我来帮你写一个简单版的“飞机大战”游戏代码,用网页上的HTML和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.       overflow: hidden;
  10.       background: black;
  11.     }
  12.     canvas {
  13.       display: block;
  14.       margin: auto;
  15.       background: #000;
  16.       border: 2px solid white;
  17.     }
  18.   </style>
  19. </head>
  20. <body>
  21.   <canvas id="gameCanvas" width="400" height="600"></canvas>

  22.   <script>
  23.     // 获取画布和上下文
  24.     const canvas = document.getElementById('gameCanvas');
  25.     const ctx = canvas.getContext('2d');

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

  37.     // 子弹数组
  38.     let bullets = [];

  39.     // 敌机数组
  40.     let enemies = [];

  41.     // 分数
  42.     let score = 0;

  43.     // 创建敌机
  44.     function createEnemy() {
  45.       const size = 40;
  46.       const x = Math.random() * (canvas.width - size);
  47.       const y = -size;
  48.       const speed = 2 + Math.random() * 2;
  49.       enemies.push({ x, y, width: size, height: size, speed: speed, color: 'red' });
  50.     }

  51.     // 事件监听:按键按下
  52.     window.addEventListener('keydown', (e) => {
  53.       if (e.code === 'ArrowLeft') plane.movingLeft = true;
  54.       else if (e.code === 'ArrowRight') plane.movingRight = true;
  55.       else if (e.code === 'Space') shootBullet();
  56.     });

  57.     // 事件监听:按键松开
  58.     window.addEventListener('keyup', (e) => {
  59.       if (e.code === 'ArrowLeft') plane.movingLeft = false;
  60.       else if (e.code === 'ArrowRight') plane.movingRight = false;
  61.     });

  62.     // 发射子弹
  63.     function shootBullet() {
  64.       bullets.push({ x: plane.x + plane.width / 2 - 3, y: plane.y, width: 6, height: 10, speed: 7, color: 'yellow' });
  65.     }

  66.     // 检测两个矩形是否相撞
  67.     function isColliding(a, b) {
  68.       return !(a.x + a.width < b.x || a.x > b.x + b.width || a.y + a.height < b.y || a.y > b.y + b.height);
  69.     }

  70.     // 游戏结束
  71.     let gameOver = false;
  72.     function endGame() {
  73.       gameOver = true;
  74.       ctx.fillStyle = 'white';
  75.       ctx.font = '40px Arial';
  76.       ctx.fillText('游戏结束', canvas.width / 2 - 100, canvas.height / 2);
  77.       ctx.font = '20px Arial';
  78.       ctx.fillText('得分: ' + score, canvas.width / 2 - 40, canvas.height / 2 + 40);
  79.     }

  80.     // 更新游戏状态
  81.     function update() {
  82.       if (gameOver) return;

  83.       // 移动飞机
  84.       if (plane.movingLeft && plane.x > 0) plane.x -= plane.speed;
  85.       if (plane.movingRight && plane.x + plane.width < canvas.width) plane.x += plane.speed;

  86.       // 更新子弹位置
  87.       bullets.forEach((bullet, index) => {
  88.         bullet.y -= bullet.speed;
  89.         if (bullet.y + bullet.height < 0) {
  90.           bullets.splice(index, 1);  // 子弹飞出屏幕,删除它
  91.         }
  92.       });

  93.       // 更新敌机位置
  94.       enemies.forEach((enemy, eIndex) => {
  95.         enemy.y += enemy.speed;
  96.         if (enemy.y > canvas.height) {
  97.           // 敌机飞出屏幕,游戏结束
  98.           endGame();
  99.         }
  100.       });

  101.       // 子弹和敌机碰撞检测
  102.       bullets.forEach((bullet, bIndex) => {
  103.         enemies.forEach((enemy, eIndex) => {
  104.           if (isColliding(bullet, enemy)) {
  105.             // 删除子弹和敌机,得分加1
  106.             bullets.splice(bIndex, 1);
  107.             enemies.splice(eIndex, 1);
  108.             score++;
  109.           }
  110.         });
  111.       });

  112.       // 敌机和飞机碰撞检测
  113.       enemies.forEach(enemy => {
  114.         if (isColliding(enemy, plane)) {
  115.           endGame();
  116.         }
  117.       });
  118.     }

  119.     // 绘制游戏画面
  120.     function draw() {
  121.       // 清空画布
  122.       ctx.clearRect(0, 0, canvas.width, canvas.height);

  123.       // 画飞机(三角形+矩形)
  124.       ctx.fillStyle = plane.color;
  125.       // 飞机机身
  126.       ctx.fillRect(plane.x, plane.y + 20, plane.width, plane.height - 20);
  127.       // 飞机头(三角形)
  128.       ctx.beginPath();
  129.       ctx.moveTo(plane.x, plane.y + 20);
  130.       ctx.lineTo(plane.x + plane.width / 2, plane.y);
  131.       ctx.lineTo(plane.x + plane.width, plane.y + 20);
  132.       ctx.closePath();
  133.       ctx.fill();

  134.       // 画子弹
  135.       bullets.forEach(bullet => {
  136.         ctx.fillStyle = bullet.color;
  137.         ctx.fillRect(bullet.x, bullet.y, bullet.width, bullet.height);
  138.       });

  139.       // 画敌机(红色方块)
  140.       enemies.forEach(enemy => {
  141.         ctx.fillStyle = enemy.color;
  142.         ctx.fillRect(enemy.x, enemy.y, enemy.width, enemy.height);
  143.       });

  144.       // 画分数
  145.       ctx.fillStyle = 'white';
  146.       ctx.font = '20px Arial';
  147.       ctx.fillText('得分: ' + score, 10, 30);
  148.     }

  149.     // 生成敌机的定时器
  150.     setInterval(() => {
  151.       if (!gameOver) createEnemy();
  152.     }, 1000);

  153.     // 游戏主循环
  154.     function gameLoop() {
  155.       update();
  156.       draw();
  157.       requestAnimationFrame(gameLoop);
  158.     }

  159.     // 开始游戏
  160.     gameLoop();
  161.   </script>
  162. </body>
  163. </html>
复制代码


---

###怎么玩儿:
- 用键盘左右箭头控制自己的飞机移动。
- 按空格键发射子弹。
- 射中红色的敌机可以得分。
- 敌机碰到飞机或飞到底部,游戏就结束啦。

---

如果你想让飞机变漂亮或者加音乐,可以告诉我,我再帮你改进代码,怎么样?玩得开心!????✈️????
回复 支持 反对

使用道具 举报

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

本版积分规则

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