科学教育

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

许睿洋飞机

[复制链接]

4

主题

9

帖子

49

积分

新手上路

Rank: 1

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

使用道具 举报

0

主题

1145

帖子

1715

积分

版主

Rank: 7Rank: 7Rank: 7

积分
1715
沙发
发表于 2025-12-2 15:20:04 | 只看该作者
好的!我帮你做一个简单的“飞机大战”网页小游戏。这个游戏里,你可以用键盘控制一架小飞机左右移动,按空格键发射子弹,击中敌机得分。

下面是完整的代码,你可以复制到记事本里,保存成 **plane_game.html**,然后用浏览器打开玩玩看!

  1. <!DOCTYPE html>
  2. <html>
  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.       background: #111;
  14.       display: block;
  15.       margin: 0 auto;
  16.       border: 2px solid #555;
  17.     }
  18.   </style>
  19. </head>
  20. <body>
  21.   <canvas id="gameCanvas" width="400" height="600"></canvas>
  22.   <script>
  23.     const canvas = document.getElementById('gameCanvas');
  24.     const ctx = canvas.getContext('2d');

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

  37.     // 敌机数组
  38.     const enemies = [];
  39.     const enemySpeed = 2;
  40.     const enemySize = 40;

  41.     let score = 0;
  42.     let gameOver = false;

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

  48.     // 监听键盘事件
  49.     document.addEventListener('keydown', e => {
  50.       if (e.code === 'ArrowLeft') plane.moveLeft = true;
  51.       if (e.code === 'ArrowRight') plane.moveRight = true;
  52.       if (e.code === 'Space') shootBullet();
  53.     });
  54.     document.addEventListener('keyup', e => {
  55.       if (e.code === 'ArrowLeft') plane.moveLeft = false;
  56.       if (e.code === 'ArrowRight') plane.moveRight = false;
  57.     });

  58.     // 射击子弹
  59.     function shootBullet() {
  60.       plane.bullets.push({
  61.         x: plane.x + plane.width / 2 - 3,
  62.         y: plane.y,
  63.         width: 6,
  64.         height: 12,
  65.         color: 'yellow',
  66.         speed: 7
  67.       });
  68.     }

  69.     // 碰撞检测
  70.     function isColliding(a, b) {
  71.       return !(
  72.         a.x + a.width < b.x ||
  73.         a.x > b.x + b.width ||
  74.         a.y + a.height < b.y ||
  75.         a.y > b.y + b.height
  76.       );
  77.     }

  78.     // 游戏主循环
  79.     function gameLoop() {
  80.       if (gameOver) {
  81.         ctx.fillStyle = 'white';
  82.         ctx.font = '30px Arial';
  83.         ctx.fillText('游戏结束! 得分: ' + score, 50, canvas.height / 2);
  84.         ctx.font = '20px Arial';
  85.         ctx.fillText('刷新页面重新开始', 100, canvas.height / 2 + 40);
  86.         return;
  87.       }

  88.       // 清屏
  89.       ctx.clearRect(0, 0, canvas.width, canvas.height);

  90.       // 移动飞机
  91.       if (plane.moveLeft && plane.x > 0) {
  92.         plane.x -= plane.speed;
  93.       }
  94.       if (plane.moveRight && plane.x + plane.width < canvas.width) {
  95.         plane.x += plane.speed;
  96.       }

  97.       // 画飞机
  98.       ctx.fillStyle = plane.color;
  99.       ctx.beginPath();
  100.       ctx.moveTo(plane.x + plane.width / 2, plane.y); // 飞机头
  101.       ctx.lineTo(plane.x, plane.y + plane.height);
  102.       ctx.lineTo(plane.x + plane.width, plane.y + plane.height);
  103.       ctx.closePath();
  104.       ctx.fill();

  105.       // 更新和画子弹
  106.       for (let i = plane.bullets.length - 1; i >= 0; i--) {
  107.         let b = plane.bullets[i];
  108.         b.y -= b.speed;
  109.         if (b.y + b.height < 0) {
  110.           plane.bullets.splice(i, 1);
  111.           continue;
  112.         }
  113.         ctx.fillStyle = b.color;
  114.         ctx.fillRect(b.x, b.y, b.width, b.height);
  115.       }

  116.       // 每隔一定时间生成敌机
  117.       if (Math.random() < 0.02) {
  118.         createEnemy();
  119.       }

  120.       // 更新敌机位置 并画敌机
  121.       for (let i = enemies.length - 1; i >= 0; i--) {
  122.         let e = enemies[i];
  123.         e.y += enemySpeed;

  124.         // 敌机碰到飞机 游戏结束
  125.         if (isColliding(e, plane)) {
  126.           gameOver = true;
  127.         }

  128.         // 敌机飞出屏幕 删除敌机
  129.         if (e.y > canvas.height) {
  130.           enemies.splice(i, 1);
  131.           continue;
  132.         }

  133.         // 画敌机(红色三角形)
  134.         ctx.fillStyle = e.color;
  135.         ctx.beginPath();
  136.         ctx.moveTo(e.x + e.width / 2, e.y + e.height);
  137.         ctx.lineTo(e.x, e.y);
  138.         ctx.lineTo(e.x + e.width, e.y);
  139.         ctx.closePath();
  140.         ctx.fill();

  141.         // 子弹打中敌机
  142.         for (let j = plane.bullets.length - 1; j >= 0; j--) {
  143.           if (isColliding(plane.bullets[j], e)) {
  144.             enemies.splice(i, 1); // 敌机消失
  145.             plane.bullets.splice(j, 1); // 子弹消失
  146.             score += 10;
  147.             break;
  148.           }
  149.         }
  150.       }

  151.       // 显示得分
  152.       ctx.fillStyle = 'white';
  153.       ctx.font = '20px Arial';
  154.       ctx.fillText('得分: ' + score, 10, 25);

  155.       requestAnimationFrame(gameLoop);
  156.     }

  157.     // 启动游戏
  158.     gameLoop();

  159.   </script>
  160. </body>
  161. </html>
复制代码


---

### 怎么玩?

- 用键盘左箭头和右箭头控制飞机左右移动。
- 按空格键发射子弹,射击红色的敌机。
- 敌机如果碰到你的飞机,游戏结束。
- 击落敌机得分。

希望你喜欢这个小游戏!有问题随时问我哦!????
回复 支持 反对

使用道具 举报

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

本版积分规则

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