科学教育

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

帮我生成飞机大战的网页代码

[复制链接]

2

主题

4

帖子

26

积分

新手上路

Rank: 1

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

使用道具 举报

0

主题

1145

帖子

1715

积分

版主

Rank: 7Rank: 7Rank: 7

积分
1715
沙发
发表于 2025-12-2 09:38:41 | 只看该作者
好的!我来帮你写一个简单的“飞机大战”网页小游戏代码。这个游戏里,你可以用键盘上的左右键控制飞机左右移动,按空格键发射子弹,打击敌机。

下面是完整的代码,你可以把它复制到一个叫做 `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.       background: black;
  10.       overflow: hidden;
  11.       user-select: none;
  12.     }
  13.     #gameCanvas {
  14.       background: #000;
  15.       display: block;
  16.       margin: 0 auto;
  17.       border: 2px solid white;
  18.     }
  19.     #score {
  20.       color: white;
  21.       font-size: 20px;
  22.       text-align: center;
  23.       margin-top: 10px;
  24.       font-family: "微软雅黑", sans-serif;
  25.     }
  26.   </style>
  27. </head>
  28. <body>
  29.   <canvas id="gameCanvas" width="400" height="600"></canvas>
  30.   <div id="score">得分:0</div>

  31.   <script>
  32.     const canvas = document.getElementById("gameCanvas");
  33.     const ctx = canvas.getContext("2d");

  34.     // 飞机对象
  35.     const plane = {
  36.       x: canvas.width / 2 - 20,
  37.       y: canvas.height - 60,
  38.       width: 40,
  39.       height: 60,
  40.       speed: 5,
  41.       movingLeft: false,
  42.       movingRight: false,
  43.     };

  44.     // 子弹数组
  45.     const bullets = [];
  46.     const bulletSpeed = 7;

  47.     // 敌机数组
  48.     const enemies = [];
  49.     const enemySpeed = 2;

  50.     let score = 0;
  51.     let gameOver = false;

  52.     // 监听键盘事件
  53.     window.addEventListener("keydown", (e) => {
  54.       if (e.code === "ArrowLeft") {
  55.         plane.movingLeft = true;
  56.       } else if (e.code === "ArrowRight") {
  57.         plane.movingRight = true;
  58.       } else if (e.code === "Space") {
  59.         shootBullet();
  60.       }
  61.     });

  62.     window.addEventListener("keyup", (e) => {
  63.       if (e.code === "ArrowLeft") {
  64.         plane.movingLeft = false;
  65.       } else if (e.code === "ArrowRight") {
  66.         plane.movingRight = false;
  67.       }
  68.     });

  69.     function shootBullet() {
  70.       bullets.push({
  71.         x: plane.x + plane.width / 2 - 3,
  72.         y: plane.y,
  73.         width: 6,
  74.         height: 12,
  75.       });
  76.     }

  77.     // 生成敌机
  78.     function createEnemy() {
  79.       const x = Math.random() * (canvas.width - 40);
  80.       enemies.push({
  81.         x: x,
  82.         y: -40,
  83.         width: 40,
  84.         height: 40,
  85.       });
  86.     }

  87.     // 画飞机
  88.     function drawPlane() {
  89.       ctx.fillStyle = "deepskyblue";
  90.       ctx.beginPath();
  91.       // 画出飞机的身体
  92.       ctx.moveTo(plane.x + plane.width / 2, plane.y);
  93.       ctx.lineTo(plane.x + plane.width, plane.y + plane.height);
  94.       ctx.lineTo(plane.x, plane.y + plane.height);
  95.       ctx.closePath();
  96.       ctx.fill();
  97.     }

  98.     // 画子弹
  99.     function drawBullets() {
  100.       ctx.fillStyle = "yellow";
  101.       bullets.forEach((bullet) => {
  102.         ctx.fillRect(bullet.x, bullet.y, bullet.width, bullet.height);
  103.       });
  104.     }

  105.     // 画敌机
  106.     function drawEnemies() {
  107.       ctx.fillStyle = "red";
  108.       enemies.forEach((enemy) => {
  109.         ctx.fillRect(enemy.x, enemy.y, enemy.width, enemy.height);
  110.       });
  111.     }

  112.     // 更新位置
  113.     function update() {
  114.       if (gameOver) return;

  115.       // 移动飞机
  116.       if (plane.movingLeft) {
  117.         plane.x -= plane.speed;
  118.         if (plane.x < 0) plane.x = 0;
  119.       }
  120.       if (plane.movingRight) {
  121.         plane.x += plane.speed;
  122.         if (plane.x + plane.width > canvas.width) plane.x = canvas.width - plane.width;
  123.       }

  124.       // 移动子弹
  125.       for (let i = bullets.length - 1; i >= 0; i--) {
  126.         bullets[i].y -= bulletSpeed;
  127.         if (bullets[i].y + bullets[i].height < 0) {
  128.           bullets.splice(i, 1);
  129.         }
  130.       }

  131.       // 移动敌机
  132.       for (let i = enemies.length - 1; i >= 0; i--) {
  133.         enemies[i].y += enemySpeed;

  134.         // 判断敌机和飞机是否碰撞
  135.         if (isColliding(enemies[i], plane)) {
  136.           gameOver = true;
  137.         }

  138.         // 如果敌机飞出屏幕,就移除
  139.         if (enemies[i].y > canvas.height) {
  140.           enemies.splice(i, 1);
  141.         }
  142.       }

  143.       // 判断子弹和敌机碰撞
  144.       for (let i = enemies.length - 1; i >= 0; i--) {
  145.         for (let j = bullets.length - 1; j >= 0; j--) {
  146.           if (isColliding(enemies[i], bullets[j])) {
  147.             enemies.splice(i, 1);
  148.             bullets.splice(j, 1);
  149.             score += 10;
  150.             break;
  151.           }
  152.         }
  153.       }
  154.     }

  155.     // 碰撞检测函数
  156.     function isColliding(rect1, rect2) {
  157.       return !(
  158.         rect1.x > rect2.x + rect2.width ||
  159.         rect1.x + rect1.width < rect2.x ||
  160.         rect1.y > rect2.y + rect2.height ||
  161.         rect1.y + rect1.height < rect2.y
  162.       );
  163.     }

  164.     // 游戏主循环
  165.     let enemySpawnTimer = 0;
  166.     function gameLoop() {
  167.       ctx.clearRect(0, 0, canvas.width, canvas.height);

  168.       update();

  169.       drawPlane();
  170.       drawBullets();
  171.       drawEnemies();

  172.       document.getElementById("score").textContent = "得分:" + score;

  173.       if (gameOver) {
  174.         ctx.fillStyle = "white";
  175.         ctx.font = "48px 微软雅黑";
  176.         ctx.textAlign = "center";
  177.         ctx.fillText("游戏结束", canvas.width / 2, canvas.height / 2);
  178.         ctx.font = "24px 微软雅黑";
  179.         ctx.fillText("刷新页面重新开始", canvas.width / 2, canvas.height / 2 + 40);
  180.         return;
  181.       }

  182.       // 每隔一定时间生成一个敌机
  183.       enemySpawnTimer++;
  184.       if (enemySpawnTimer > 50) {
  185.         createEnemy();
  186.         enemySpawnTimer = 0;
  187.       }

  188.       requestAnimationFrame(gameLoop);
  189.     }

  190.     gameLoop();
  191.   </script>
  192. </body>
  193. </html>
复制代码


---

### 如何玩:

- 用 **左箭头** 和 **右箭头** 控制你的飞机左右移动。
- 按空格键 **发射子弹**,打击掉落的敌机。
- 不要让敌机碰到你的飞机哦,否则游戏就结束啦。
- 打中敌机可以得分,看看你能得多少分吧!

---

这段代码很简单,适合初学者。你可以试着改改飞机颜色、速度或者添加更多功能,比如背景音乐或者难度提升!加油哦!如果有不懂的地方,再告诉我,我可以帮你解释。????✈️????
回复 支持 反对

使用道具 举报

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

本版积分规则

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