科学教育

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

马紫萱

[复制链接]

1

主题

1

帖子

5

积分

新手上路

Rank: 1

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

使用道具 举报

0

主题

1145

帖子

1715

积分

版主

Rank: 7Rank: 7Rank: 7

积分
1715
沙发
发表于 2025-11-26 15:12:51 | 只看该作者
你好,马紫萱!我很乐意帮你做一个简单的“飞机大战”网页游戏哦!我会一步步告诉你代码里都做了什么,这样你学起来更容易理解。

下面是一个用HTML和JavaScript写的简单飞机大战游戏,你可以把它复制到一个叫做`plane_battle.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: black;
  10.       overflow: hidden;
  11.     }
  12.     canvas {
  13.       display: block;
  14.       margin: 0 auto;
  15.       background: #222;
  16.       border: 2px solid white;
  17.     }
  18.   </style>
  19. </head>
  20. <body>
  21.   <canvas id="game" width="400" height="600"></canvas>

  22.   <script>
  23.     // 先拿到画布和画笔
  24.     const canvas = document.getElementById('game');
  25.     const ctx = canvas.getContext('2d');

  26.     // 飞机的属性
  27.     const plane = {
  28.       x: canvas.width / 2 - 20, // 飞机初始x坐标
  29.       y: canvas.height - 80,     // 飞机初始y坐标
  30.       width: 40,
  31.       height: 60,
  32.       speed: 5
  33.     };

  34.     // 子弹数组,存放所有发射出的子弹
  35.     const bullets = [];

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

  38.     // 用来控制键盘操作
  39.     const keys = {};

  40.     // 记录分数
  41.     let score = 0;

  42.     // 游戏是否结束
  43.     let gameOver = false;

  44.     // 监听键盘按下和松开事件
  45.     window.addEventListener('keydown', function(e){
  46.       keys[e.key] = true;
  47.     });

  48.     window.addEventListener('keyup', function(e){
  49.       keys[e.key] = false;
  50.     });

  51.     // 定义子弹类
  52.     class Bullet {
  53.       constructor(x, y) {
  54.         this.x = x;
  55.         this.y = y;
  56.         this.width = 5;
  57.         this.height = 10;
  58.         this.speed = 7;
  59.       }
  60.       update() {
  61.         this.y -= this.speed;
  62.       }
  63.       draw() {
  64.         ctx.fillStyle = 'yellow';
  65.         ctx.fillRect(this.x, this.y, this.width, this.height);
  66.       }
  67.     }

  68.     // 定义敌机类
  69.     class Enemy {
  70.       constructor(x, y, speed) {
  71.         this.x = x;
  72.         this.y = y;
  73.         this.width = 40;
  74.         this.height = 40;
  75.         this.speed = speed;
  76.       }
  77.       update() {
  78.         this.y += this.speed;
  79.       }
  80.       draw() {
  81.         ctx.fillStyle = 'red';
  82.         ctx.fillRect(this.x, this.y, this.width, this.height);
  83.       }
  84.     }

  85.     // 每隔一段时间创造敌机
  86.     function createEnemy() {
  87.       const x = Math.random() * (canvas.width - 40);
  88.       const speed = 2 + Math.random() * 2;
  89.       enemies.push(new Enemy(x, -40, speed));
  90.     }

  91.     setInterval(createEnemy, 1500); // 每1.5秒创建一个敌机

  92.     // 控制飞机移动和开火
  93.     function handlePlane() {
  94.       if (keys['ArrowLeft'] && plane.x > 0) {
  95.         plane.x -= plane.speed;
  96.       }
  97.       if (keys['ArrowRight'] && plane.x + plane.width < canvas.width) {
  98.         plane.x += plane.speed;
  99.       }
  100.       if (keys[' ']) { // 空格键开火
  101.         if (Date.now() - lastBulletTime > 300) { // 控制发射速度
  102.           bullets.push(new Bullet(plane.x + plane.width/2 - 2.5, plane.y));
  103.           lastBulletTime = Date.now();
  104.         }
  105.       }
  106.     }

  107.     let lastBulletTime = 0;

  108.     // 检测碰撞的函数(两个矩形有没有重叠)
  109.     function isCollide(a, b) {
  110.       return !(
  111.         a.x + a.width < b.x ||
  112.         a.x > b.x + b.width ||
  113.         a.y + a.height < b.y ||
  114.         a.y > b.y + b.height
  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 - 50, canvas.height/2 + 40);
  125.         return;
  126.       }

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

  128.       // 画飞机
  129.       ctx.fillStyle = 'blue';
  130.       ctx.fillRect(plane.x, plane.y, plane.width, plane.height);

  131.       // 控制飞机移动和射击
  132.       handlePlane();

  133.       // 更新并画子弹
  134.       for (let i = bullets.length - 1; i >= 0; i--) {
  135.         const b = bullets[i];
  136.         b.update();
  137.         if (b.y + b.height < 0) {
  138.           bullets.splice(i, 1); // 超出画面就删掉子弹
  139.           continue;
  140.         }
  141.         b.draw();
  142.       }

  143.       // 更新并画敌机
  144.       for (let i = enemies.length - 1; i >= 0; i--) {
  145.         const e = enemies[i];
  146.         e.update();

  147.         if (e.y > canvas.height) {
  148.           enemies.splice(i, 1); // 敌机飞出画面删掉
  149.           continue;
  150.         }

  151.         e.draw();

  152.         // 检查敌机是否撞到飞机,游戏结束
  153.         if (isCollide(e, plane)) {
  154.           gameOver = true;
  155.         }

  156.         // 敌机和子弹碰撞检测
  157.         for (let j = bullets.length - 1; j >= 0; j--) {
  158.           if (isCollide(e, bullets[j])) {
  159.             enemies.splice(i, 1);
  160.             bullets.splice(j, 1);
  161.             score += 10;
  162.             break;
  163.           }
  164.         }
  165.       }

  166.       // 显示分数
  167.       ctx.fillStyle = 'white';
  168.       ctx.font = '20px Arial';
  169.       ctx.fillText('得分: '+score, 10, 30);

  170.       requestAnimationFrame(gameLoop);
  171.     }

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


---

### 这段代码做了什么?

1. **画布(canvas)**:游戏画面就是在一个黑色背景的画布上画出来的。
2. **飞机**:用一个蓝色矩形表示,可以左右移动。
3. **子弹**:按空格键发射黄色子弹,子弹会往上飞。
4. **敌机**:红色的矩形,从上往下自动出现,速度不一样。
5. **碰撞检测**:子弹打中敌机,敌机消失并加分;敌机撞到飞机,游戏结束。
6. **分数**:右上角显示打掉敌机得到的分数。
7. **游戏结束**:当飞机被撞到时,停止游戏并显示“游戏结束”和你的得分。

---

### 怎么玩?

- 用 **方向键左** 和 **方向键右** 移动飞机。
- 按 **空格键** 射击子弹。
- 尽量射击敌机,避免被敌机撞到。

---

如果你想让飞机、子弹或者敌机变得更漂亮,可以以后学习如何使用图片替代矩形。现在,先玩玩这段代码,熟悉一下基本的网页游戏编程吧!有什么问题随时问我哦!????????✈️
回复 支持 反对

使用道具 举报

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

本版积分规则

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