科学教育

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

飞机

[复制链接]

1

主题

1

帖子

5

积分

新手上路

Rank: 1

积分
5
跳转到指定楼层
楼主
发表于 2025-12-2 09:40:11 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

帮我生成一个飞机大战的网络代码,html,css,js

回复

使用道具 举报

0

主题

1145

帖子

1715

积分

版主

Rank: 7Rank: 7Rank: 7

积分
1715
沙发
发表于 2025-12-2 09:41:42 | 只看该作者
好的!我帮你做一个简单的“飞机大战”小游戏,里面有一架小飞机可以左右移动,还能发射子弹击中敌机。代码分成三部分:HTML(页面结构)、CSS(样式)、JavaScript(游戏功能)。你可以把它们放在同一个文件里,也可以分开文件。

---

### 完整代码(直接复制到一个html文件里运行)

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4.   <meta charset="UTF-8" />
  5.   <title>简单飞机大战</title>
  6.   <style>
  7.     * {
  8.       margin: 0; padding: 0; box-sizing: border-box;
  9.     }
  10.     body {
  11.       background: #000;
  12.       overflow: hidden;
  13.     }
  14.     #game {
  15.       position: relative;
  16.       width: 400px;
  17.       height: 600px;
  18.       margin: 20px auto;
  19.       background: linear-gradient(to top, #010130, #020260);
  20.       border: 2px solid #fff;
  21.       overflow: hidden;
  22.     }

  23.     /* 玩家飞机 */
  24.     #player {
  25.       position: absolute;
  26.       bottom: 20px;
  27.       left: 180px; /* 在中间 */
  28.       width: 40px;
  29.       height: 60px;
  30.       background: url('https://i.postimg.cc/Y0QbkGGc/player-plane.png') no-repeat center / contain;
  31.       /* 如果图片没法加载,会显示一个蓝色三角形 */
  32.       background-color: dodgerblue;
  33.       clip-path: polygon(50% 0%, 100% 100%, 0% 100%);
  34.     }

  35.     /* 敌机 */
  36.     .enemy {
  37.       position: absolute;
  38.       width: 40px;
  39.       height: 40px;
  40.       background: url('https://i.postimg.cc/rFZq1XmF/enemy-plane.png') no-repeat center / contain;
  41.       background-color: crimson;
  42.       clip-path: polygon(50% 0%, 90% 80%, 10% 80%);
  43.     }

  44.     /* 子弹 */
  45.     .bullet {
  46.       position: absolute;
  47.       width: 6px;
  48.       height: 14px;
  49.       background: yellow;
  50.       border-radius: 3px;
  51.     }

  52.     /* 文字提示 */
  53.     #score {
  54.       position: absolute;
  55.       top: 10px;
  56.       left: 10px;
  57.       color: white;
  58.       font-size: 18px;
  59.       font-family: 'Microsoft Yahei', sans-serif;
  60.     }
  61.   </style>
  62. </head>
  63. <body>
  64.   <div id="game">
  65.     <div id="player"></div>
  66.     <div id="score">得分:0</div>
  67.   </div>

  68.   <script>
  69.     // 游戏区域
  70.     const game = document.getElementById('game');
  71.     const player = document.getElementById('player');
  72.     const scoreDisplay = document.getElementById('score');

  73.     const gameWidth = 400;
  74.     const gameHeight = 600;

  75.     let playerX = 180; // 初始位置
  76.     const playerSpeed = 10;

  77.     let bullets = []; // 存放子弹
  78.     let enemies = []; // 存放敌机
  79.     let score = 0;

  80.     // 键盘控制
  81.     const keys = {
  82.       left: false,
  83.       right: false,
  84.       space: false,
  85.     };

  86.     window.addEventListener('keydown', (e) => {
  87.       if (e.code === 'ArrowLeft') keys.left = true;
  88.       if (e.code === 'ArrowRight') keys.right = true;
  89.       if (e.code === 'Space') keys.space = true;
  90.     });

  91.     window.addEventListener('keyup', (e) => {
  92.       if (e.code === 'ArrowLeft') keys.left = false;
  93.       if (e.code === 'ArrowRight') keys.right = false;
  94.       if (e.code === 'Space') keys.space = false;
  95.     });

  96.     // 发射子弹函数
  97.     function shootBullet() {
  98.       const bullet = document.createElement('div');
  99.       bullet.className = 'bullet';
  100.       bullet.style.left = playerX + 17 + 'px'; // 子弹从飞机中间位置射出
  101.       bullet.style.bottom = '80px';
  102.       game.appendChild(bullet);
  103.       bullets.push(bullet);
  104.     }

  105.     let canShoot = true;

  106.     // 创建敌机函数
  107.     function createEnemy() {
  108.       const enemy = document.createElement('div');
  109.       enemy.className = 'enemy';
  110.       enemy.style.left = Math.random() * (gameWidth - 40) + 'px';
  111.       enemy.style.top = '-40px'; // 从顶部外面开始
  112.       game.appendChild(enemy);
  113.       enemies.push(enemy);
  114.     }

  115.     // 游戏循环
  116.     function gameLoop() {
  117.       // 移动玩家
  118.       if (keys.left) {
  119.         playerX -= playerSpeed;
  120.         if (playerX < 0) playerX = 0;
  121.       }
  122.       if (keys.right) {
  123.         playerX += playerSpeed;
  124.         if (playerX > gameWidth - 40) playerX = gameWidth - 40;
  125.       }
  126.       player.style.left = playerX + 'px';

  127.       // 发射子弹(限制频率)
  128.       if (keys.space && canShoot) {
  129.         shootBullet();
  130.         canShoot = false;
  131.         setTimeout(() => { canShoot = true }, 300); // 300毫秒发射一次
  132.       }

  133.       // 移动子弹
  134.       for (let i = bullets.length - 1; i >= 0; i--) {
  135.         const b = bullets[i];
  136.         let bottom = parseInt(b.style.bottom);
  137.         bottom += 15;
  138.         if (bottom > gameHeight) {
  139.           // 离开屏幕了就移除
  140.           game.removeChild(b);
  141.           bullets.splice(i, 1);
  142.           continue;
  143.         }
  144.         b.style.bottom = bottom + 'px';
  145.       }

  146.       // 移动敌机
  147.       for (let i = enemies.length - 1; i >= 0; i--) {
  148.         const e = enemies[i];
  149.         let top = parseInt(e.style.top);
  150.         top += 3; // 敌机速度
  151.         if (top > gameHeight) {
  152.           // 敌机飞出底部,游戏结束
  153.           alert('游戏结束! 得分:' + score);
  154.           window.location.reload();
  155.           return;
  156.         }
  157.         e.style.top = top + 'px';
  158.       }

  159.       // 检测子弹和敌机碰撞
  160.       for (let i = enemies.length - 1; i >= 0; i--) {
  161.         const e = enemies[i];
  162.         const eRect = e.getBoundingClientRect();

  163.         for (let j = bullets.length - 1; j >= 0; j--) {
  164.           const b = bullets[j];
  165.           const bRect = b.getBoundingClientRect();

  166.           // 简单矩形碰撞检测
  167.           if (
  168.             bRect.left < eRect.right &&
  169.             bRect.right > eRect.left &&
  170.             bRect.top < eRect.bottom &&
  171.             bRect.bottom > eRect.top
  172.           ) {
  173.             // 击中
  174.             // 删除敌机和子弹
  175.             game.removeChild(e);
  176.             enemies.splice(i, 1);
  177.             game.removeChild(b);
  178.             bullets.splice(j, 1);
  179.             score++;
  180.             scoreDisplay.textContent = '得分:' + score;
  181.             break;
  182.           }
  183.         }
  184.       }

  185.       requestAnimationFrame(gameLoop);
  186.     }

  187.     // 每两秒生成一个敌机
  188.     setInterval(createEnemy, 2000);

  189.     // 启动游戏循环
  190.     gameLoop();
  191.   </script>
  192. </body>
  193. </html>
复制代码


---

### 说明:

- 用键盘左右方向键控制飞机左右移动。
- 按空格键发射黄色子弹。
- 敌机从屏幕顶部随机位置出现,往下飞。
- 子弹碰到敌机会消失,敌机也被击毁,得分+1。
- 如果敌机飞到底部没被打掉,游戏结束,弹窗显示得分,然后刷新页面重新开始。
- 飞机、敌机用CSS画的三角形,如果想换成图片可以修改background-url。

---

如果你不太懂,可以问我任何问题哦,我可以帮你一步步学会怎么写!
回复 支持 反对

使用道具 举报

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

本版积分规则

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