科学教育

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

帮我生成飞机大作战代码

[复制链接]

2

主题

3

帖子

26

积分

新手上路

Rank: 1

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

使用道具 举报

0

主题

1145

帖子

1715

积分

版主

Rank: 7Rank: 7Rank: 7

积分
1715
沙发
发表于 2025-12-2 14:00:42 | 只看该作者
好的!“飞机大作战”是一个很好玩的小游戏,我们可以用网页的技术来做它,比如用HTML、CSS和JavaScript。

我帮你写一个简单版的飞机大作战代码,里面有一架可以左右移动的飞机,还能发射子弹。

你可以按照下面步骤试试看:

### 1. 新建一个HTML文件,比如叫 `index.html`

### 2. 把下面的代码复制进去:

  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-color: #000;
  11.       color: white;
  12.       text-align: center;
  13.       font-family: Arial, sans-serif;
  14.     }
  15.     #gameCanvas {
  16.       background-color: #111;
  17.       display: block;
  18.       margin: 20px auto;
  19.       border: 2px solid #fff;
  20.     }
  21.   </style>
  22. </head>
  23. <body>
  24.   <h1>飞机大作战</h1>
  25.   <canvas id="gameCanvas" width="400" height="600"></canvas>
  26.   <p>按左右方向键控制飞机,按空格键发射子弹</p>

  27.   <script>
  28.     const canvas = document.getElementById('gameCanvas');
  29.     const ctx = canvas.getContext('2d');

  30.     // 飞机对象
  31.     const plane = {
  32.       x: canvas.width / 2 - 20,
  33.       y: canvas.height - 60,
  34.       width: 40,
  35.       height: 60,
  36.       speed: 5,
  37.       movingLeft: false,
  38.       movingRight: false,
  39.       draw() {
  40.         ctx.fillStyle = 'cyan';
  41.         ctx.beginPath();
  42.         // 画飞机:一个三角形底部带个矩形
  43.         ctx.moveTo(this.x + this.width / 2, this.y); // 顶部点
  44.         ctx.lineTo(this.x, this.y + this.height);
  45.         ctx.lineTo(this.x + this.width, this.y + this.height);
  46.         ctx.closePath();
  47.         ctx.fill();

  48.         // 飞机机身矩形
  49.         ctx.fillRect(this.x + this.width / 2 - 7, this.y + this.height / 2, 14, this.height / 2);
  50.       },
  51.       update() {
  52.         if (this.movingLeft && this.x > 0) {
  53.           this.x -= this.speed;
  54.         }
  55.         if (this.movingRight && this.x + this.width < canvas.width) {
  56.           this.x += this.speed;
  57.         }
  58.       }
  59.     };

  60.     // 子弹数组
  61.     const bullets = [];

  62.     // 子弹类
  63.     class Bullet {
  64.       constructor(x, y) {
  65.         this.x = x;
  66.         this.y = y;
  67.         this.radius = 5;
  68.         this.speed = 7;
  69.       }
  70.       draw() {
  71.         ctx.fillStyle = 'yellow';
  72.         ctx.beginPath();
  73.         ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2);
  74.         ctx.fill();
  75.       }
  76.       update() {
  77.         this.y -= this.speed;
  78.       }
  79.     }

  80.     // 监听键盘事件
  81.     window.addEventListener('keydown', e => {
  82.       if (e.code === 'ArrowLeft') {
  83.         plane.movingLeft = true;
  84.       } else if (e.code === 'ArrowRight') {
  85.         plane.movingRight = true;
  86.       } else if (e.code === 'Space') {
  87.         // 发射子弹,从飞机顶部中间位置
  88.         bullets.push(new Bullet(plane.x + plane.width / 2, plane.y));
  89.       }
  90.     });

  91.     window.addEventListener('keyup', e => {
  92.       if (e.code === 'ArrowLeft') {
  93.         plane.movingLeft = false;
  94.       } else if (e.code === 'ArrowRight') {
  95.         plane.movingRight = false;
  96.       }
  97.     });

  98.     // 游戏主循环
  99.     function gameLoop() {
  100.       ctx.clearRect(0, 0, canvas.width, canvas.height);

  101.       plane.update();
  102.       plane.draw();

  103.       // 更新和绘制所有子弹
  104.       for (let i = bullets.length - 1; i >= 0; i--) {
  105.         bullets[i].update();
  106.         if (bullets[i].y + bullets[i].radius < 0) {
  107.           bullets.splice(i, 1); // 子弹飞出屏幕后移除
  108.         } else {
  109.           bullets[i].draw();
  110.         }
  111.       }

  112.       requestAnimationFrame(gameLoop);
  113.     }

  114.     gameLoop();
  115.   </script>
  116. </body>
  117. </html>
复制代码


---

### 如何玩

- 打开这个文件(比如双击或者用浏览器打开)
- 用键盘的左箭头和右箭头移动飞机
- 按空格键可以发射子弹

---

### 说明

- 这是一个很简单的开始,没有敌人和碰撞检测。
- 如果想要加敌人、分数等,可以告诉我,我帮你慢慢加上去!

如果你想知道每个部分的作用,我也可以帮你详细解释哦~
回复 支持 反对

使用道具 举报

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

本版积分规则

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