科学教育

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

zhr飞坤

[复制链接]

3

主题

16

帖子

63

积分

注册会员

Rank: 2

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

使用道具 举报

0

主题

1145

帖子

1715

积分

版主

Rank: 7Rank: 7Rank: 7

积分
1715
沙发
发表于 2025-12-2 15:15:09 | 只看该作者
好的!我来帮你做一个简单的“飞机大战”小游戏网页版本,好玩又容易理解。

### 游戏规则简介:
- 你控制一架飞机,可以用键盘左右移动。
- 上方会有敌机往下飞,你要躲避它们。
- 你可以发射子弹击中敌机,敌机被打中就会消失。

### 代码部分

你只需要新建一个HTML文件,比如叫 `feiji.html`,把下面代码复制进去,然后用浏览器打开就能玩啦!

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4.   <meta charset="UTF-8" />
  5.   <title>飞机大战小游戏</title>
  6.   <style>
  7.     body {
  8.       text-align: center;
  9.       background-color: #000;
  10.       color: white;
  11.       font-family: "微软雅黑", sans-serif;
  12.       margin: 0;
  13.     }
  14.     canvas {
  15.       background: #111;
  16.       display: block;
  17.       margin: 20px auto;
  18.       border: 2px solid #555;
  19.     }
  20.   </style>
  21. </head>
  22. <body>
  23.   <h1>飞机大战小游戏</h1>
  24.   <p>左右方向键控制飞机移动,空格键发射子弹</p>
  25.   <canvas id="game" width="400" height="600"></canvas>

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

  29.     // 飞机对象
  30.     const plane = {
  31.       x: canvas.width / 2 - 25,
  32.       y: canvas.height - 80,
  33.       width: 50,
  34.       height: 60,
  35.       speed: 5,
  36.       movingLeft: false,
  37.       movingRight: false,
  38.     };

  39.     // 子弹数组
  40.     let bullets = [];

  41.     // 敌机数组
  42.     let enemies = [];

  43.     // 分数
  44.     let score = 0;

  45.     // 创建敌机函数
  46.     function createEnemy() {
  47.       const enemyWidth = 40;
  48.       const enemyHeight = 40;
  49.       const x = Math.random() * (canvas.width - enemyWidth);
  50.       enemies.push({
  51.         x: x,
  52.         y: -enemyHeight,
  53.         width: enemyWidth,
  54.         height: enemyHeight,
  55.         speed: 2 + Math.random() * 2,
  56.       });
  57.     }

  58.     // 键盘事件监听
  59.     window.addEventListener('keydown', function(e) {
  60.       if (e.code === 'ArrowLeft') plane.movingLeft = true;
  61.       if (e.code === 'ArrowRight') plane.movingRight = true;
  62.       if (e.code === 'Space') shootBullet();
  63.     });

  64.     window.addEventListener('keyup', function(e) {
  65.       if (e.code === 'ArrowLeft') plane.movingLeft = false;
  66.       if (e.code === 'ArrowRight') plane.movingRight = false;
  67.     });

  68.     // 发射子弹函数
  69.     function shootBullet() {
  70.       bullets.push({
  71.         x: plane.x + plane.width / 2 - 5,
  72.         y: plane.y,
  73.         width: 10,
  74.         height: 20,
  75.         speed: 7,
  76.       });
  77.     }

  78.     // 检测碰撞函数(矩形碰撞)
  79.     function isColliding(obj1, obj2) {
  80.       return !(
  81.         obj1.x + obj1.width < obj2.x ||
  82.         obj1.x > obj2.x + obj2.width ||
  83.         obj1.y + obj1.height < obj2.y ||
  84.         obj1.y > obj2.y + obj2.height
  85.       );
  86.     }

  87.     // 游戏主循环
  88.     function gameLoop() {
  89.       ctx.clearRect(0, 0, canvas.width, canvas.height);

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

  93.       // 画飞机(用三角形表示)
  94.       ctx.fillStyle = 'cyan';
  95.       ctx.beginPath();
  96.       ctx.moveTo(plane.x + plane.width/2, plane.y); // 顶点
  97.       ctx.lineTo(plane.x, plane.y + plane.height);
  98.       ctx.lineTo(plane.x + plane.width, plane.y + plane.height);
  99.       ctx.closePath();
  100.       ctx.fill();

  101.       // 更新和画子弹
  102.       ctx.fillStyle = 'yellow';
  103.       for (let i = bullets.length -1; i >= 0; i--) {
  104.         bullets[i].y -= bullets[i].speed;
  105.         if (bullets[i].y + bullets[i].height < 0) bullets.splice(i, 1);

  106.         else {
  107.           ctx.fillRect(bullets[i].x, bullets[i].y, bullets[i].width, bullets[i].height);
  108.         }
  109.       }

  110.       // 每隔一段时间生成敌机
  111.       if (Math.random() < 0.02) {
  112.         createEnemy();
  113.       }

  114.       // 更新和画敌机
  115.       ctx.fillStyle = 'red';
  116.       for (let i = enemies.length -1; i >= 0; i--) {
  117.         enemies[i].y += enemies[i].speed;

  118.         if (enemies[i].y > canvas.height) {
  119.           enemies.splice(i, 1);
  120.           // 这里可以扣分或者游戏结束,不过为了简单我们忽略
  121.         }
  122.         else {
  123.           ctx.fillRect(enemies[i].x, enemies[i].y, enemies[i].width, enemies[i].height);
  124.         }
  125.       }

  126.       // 检测子弹和敌机的碰撞
  127.       for (let b = bullets.length -1; b >= 0; b--) {
  128.         for (let e = enemies.length -1; e >= 0; e--) {
  129.           if (isColliding(bullets[b], enemies[e])) {
  130.             bullets.splice(b, 1);
  131.             enemies.splice(e, 1);
  132.             score += 10;
  133.             break;
  134.           }
  135.         }
  136.       }

  137.       // 画分数
  138.       ctx.fillStyle = 'white';
  139.       ctx.font = '20px 微软雅黑';
  140.       ctx.fillText('分数: ' + score, 10, 30);

  141.       requestAnimationFrame(gameLoop);
  142.     }

  143.     // 开始游戏
  144.     gameLoop();
  145.   </script>
  146. </body>
  147. </html>
复制代码


---

### 怎么开始玩?

1. 新建一个文本文件,取名叫 `feiji.html`。
2. 把上面代码全部复制粘贴进去,保存。
3. 用浏览器打开这个 `feiji.html` 文件。
4. 按左右方向键控制飞机移动,按空格键发射子弹。
5. 击中红色敌机得分,试试看你能得多少分吧!

---

如果你想要,我可以告诉你怎么一步步改进游戏,添加更多什么功能,比如生命值、敌人不同速度、背景音乐等等。你有兴趣吗?????
回复 支持 反对

使用道具 举报

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

本版积分规则

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