科学教育

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

帮我生成飞机大战的代码

[复制链接]

3

主题

13

帖子

52

积分

注册会员

Rank: 2

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

使用道具 举报

0

主题

1145

帖子

1715

积分

版主

Rank: 7Rank: 7Rank: 7

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

下面是完整的HTML+CSS+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: #000;
  10.       overflow: hidden;
  11.       user-select: none;
  12.     }
  13.     canvas {
  14.       display: block;
  15.       margin: 0 auto;
  16.       background: #111;
  17.       border: 2px solid #555;
  18.     }
  19.     #score {
  20.       color: #fff;
  21.       font-family: "微软雅黑", sans-serif;
  22.       font-size: 20px;
  23.       text-align: center;
  24.       margin-top: 10px;
  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 - 80,
  38.       width: 40,
  39.       height: 60,
  40.       speed: 5,
  41.       movingLeft: false,
  42.       movingRight: false,
  43.       movingUp: false,
  44.       movingDown: false,
  45.       bullets: []
  46.     };

  47.     // 敌机数组
  48.     const enemies = [];
  49.     const enemyWidth = 40;
  50.     const enemyHeight = 40;
  51.     let enemySpeed = 2;

  52.     // 游戏数据
  53.     let score = 0;
  54.     let gameOver = false;

  55.     // 监听键盘按下
  56.     window.addEventListener("keydown", function(e) {
  57.       if (e.code === "ArrowLeft") plane.movingLeft = true;
  58.       else if (e.code === "ArrowRight") plane.movingRight = true;
  59.       else if (e.code === "ArrowUp") plane.movingUp = true;
  60.       else if (e.code === "ArrowDown") plane.movingDown = true;
  61.       else if (e.code === "Space") shootBullet();
  62.     });

  63.     // 监听键盘松开
  64.     window.addEventListener("keyup", function(e) {
  65.       if (e.code === "ArrowLeft") plane.movingLeft = false;
  66.       else if (e.code === "ArrowRight") plane.movingRight = false;
  67.       else if (e.code === "ArrowUp") plane.movingUp = false;
  68.       else if (e.code === "ArrowDown") plane.movingDown = false;
  69.     });

  70.     // 发射子弹
  71.     function shootBullet() {
  72.       if(gameOver) return;
  73.       plane.bullets.push({
  74.         x: plane.x + plane.width / 2 - 3,
  75.         y: plane.y,
  76.         width: 6,
  77.         height: 12,
  78.         speed: 7
  79.       });
  80.     }

  81.     // 生成敌机
  82.     function createEnemy() {
  83.       if(gameOver) return;
  84.       const x = Math.random() * (canvas.width - enemyWidth);
  85.       enemies.push({ x, y: -enemyHeight, width: enemyWidth, height: enemyHeight });
  86.     }

  87.     // 更新游戏状态
  88.     function update() {
  89.       if(gameOver) return;
  90.       // 移动飞机
  91.       if (plane.movingLeft) plane.x -= plane.speed;
  92.       if (plane.movingRight) plane.x += plane.speed;
  93.       if (plane.movingUp) plane.y -= plane.speed;
  94.       if (plane.movingDown) plane.y += plane.speed;

  95.       // 限制飞机不跑出画布
  96.       plane.x = Math.max(0, Math.min(canvas.width - plane.width, plane.x));
  97.       plane.y = Math.max(0, Math.min(canvas.height - plane.height, plane.y));

  98.       // 移动子弹
  99.       plane.bullets.forEach((bullet, index) => {
  100.         bullet.y -= bullet.speed;
  101.         // 子弹飞出画布就删除
  102.         if (bullet.y + bullet.height < 0) {
  103.           plane.bullets.splice(index, 1);
  104.         }
  105.       });

  106.       // 移动敌机
  107.       enemies.forEach((enemy, index) => {
  108.         enemy.y += enemySpeed;
  109.         // 如果敌机碰到飞机,游戏结束
  110.         if (isColliding(enemy, plane)) {
  111.           gameOver = true;
  112.         }
  113.         // 如果敌机飞出下面,游戏结束(你也可以改成减分或别的)
  114.         if (enemy.y > canvas.height) {
  115.           gameOver = true;
  116.         }
  117.       });

  118.       // 子弹和敌机碰撞检测
  119.       for(let i = enemies.length -1; i >= 0; i--) {
  120.         for(let j = plane.bullets.length -1; j >= 0; j--) {
  121.           if (isColliding(enemies[i], plane.bullets[j])) {
  122.             enemies.splice(i, 1);
  123.             plane.bullets.splice(j, 1);
  124.             score += 10;
  125.             break;
  126.           }
  127.         }
  128.       }
  129.     }

  130.     // 碰撞检测函数(矩形碰撞)
  131.     function isColliding(a, b) {
  132.       return !(
  133.         a.x + a.width < b.x ||
  134.         a.x > b.x + b.width ||
  135.         a.y + a.height < b.y ||
  136.         a.y > b.y + b.height
  137.       );
  138.     }

  139.     // 画出游戏内容
  140.     function draw() {
  141.       // 清屏
  142.       ctx.clearRect(0, 0, canvas.width, canvas.height);

  143.       // 画飞机(用蓝色三角形表示)
  144.       ctx.fillStyle = "deepskyblue";
  145.       ctx.beginPath();
  146.       ctx.moveTo(plane.x + plane.width / 2, plane.y);
  147.       ctx.lineTo(plane.x, plane.y + plane.height);
  148.       ctx.lineTo(plane.x + plane.width, plane.y + plane.height);
  149.       ctx.closePath();
  150.       ctx.fill();

  151.       // 画子弹(黄色小矩形)
  152.       ctx.fillStyle = "yellow";
  153.       plane.bullets.forEach(bullet => {
  154.         ctx.fillRect(bullet.x, bullet.y, bullet.width, bullet.height);
  155.       });

  156.       // 画敌机(红色方块)
  157.       ctx.fillStyle = "red";
  158.       enemies.forEach(enemy => {
  159.         ctx.fillRect(enemy.x, enemy.y, enemy.width, enemy.height);
  160.       });

  161.       // 画分数
  162.       document.getElementById("score").textContent = "得分: " + score;

  163.       // 游戏结束提示
  164.       if(gameOver) {
  165.         ctx.fillStyle = "white";
  166.         ctx.font = "bold 48px 微软雅黑";
  167.         ctx.textAlign = "center";
  168.         ctx.fillText("游戏结束", canvas.width / 2, canvas.height / 2);
  169.         ctx.font = "24px 微软雅黑";
  170.         ctx.fillText("刷新页面重新开始", canvas.width / 2, canvas.height / 2 + 40);
  171.       }
  172.     }

  173.     // 游戏主循环
  174.     function loop() {
  175.       update();
  176.       draw();
  177.       requestAnimationFrame(loop);
  178.     }

  179.     // 每隔一段时间生成敌机
  180.     setInterval(() => {
  181.       if(!gameOver){
  182.         createEnemy();
  183.       }
  184.     }, 1500);

  185.     // 开始游戏
  186.     loop();
  187.   </script>
  188. </body>
  189. </html>
复制代码


---

### 怎么玩?

- 用键盘的箭头键控制你的飞机上下左右移动。
- 按空格键发射子弹。
- 尽量打掉飞过来的红色敌机。
- 如果敌机碰到你,或者飞过了底部,游戏就结束啦!
- 游戏界面上会显示你的得分。

---

如果你想让飞机更好看,可以往里面加图片;或者加更多不同的敌机和奖励物品。开始先玩这个版本,理解了就试着自己改改代码,好不好玩就靠你啦!

有问题随时问我哦~祝你编程开心!????✈️????
回复 支持 反对

使用道具 举报

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

本版积分规则

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