科学教育

标题: 飞机 [打印本页]

作者: 张葉彬    时间: 2025-12-2 09:37
标题: 飞机
帮我生成一个飞机大战的网络代码,html,css,js

作者: kaia    时间: 2025-12-2 09:38
好哒!我来帮你写一个简单的“飞机大战”小游戏代码,用HTML、CSS和JS来做。我们会做一个可以用键盘左右移动的小飞机,能够发射子弹,打掉敌机。

下面是一份完整的代码,你可以把它复制到一个新的`.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.     #game {
  14.       position: relative;
  15.       width: 400px;
  16.       height: 600px;
  17.       margin: 20px auto;
  18.       background: url('https://i.ibb.co/Y3x1R1k/bg-star.jpg') repeat-y;
  19.       background-size: cover;
  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.ibb.co/4VMnPBX/player-plane.png') no-repeat center;
  31.       background-size: contain;
  32.     }
  33.     /* 子弹样式 */
  34.     .bullet {
  35.       position: absolute;
  36.       width: 6px;
  37.       height: 14px;
  38.       background: yellow;
  39.       border-radius: 3px;
  40.     }
  41.     /* 敌机样式 */
  42.     .enemy {
  43.       position: absolute;
  44.       width: 40px;
  45.       height: 40px;
  46.       background: url('https://i.ibb.co/gT86hw1/enemy-plane.png') no-repeat center;
  47.       background-size: contain;
  48.     }
  49.     /* 分数显示 */
  50.     #scoreBoard {
  51.       position: absolute;
  52.       top: 5px;
  53.       left: 10px;
  54.       color: white;
  55.       font-weight: bold;
  56.       font-size: 20px;
  57.       z-index: 1000;
  58.       font-family: "微软雅黑", Arial, sans-serif;
  59.     }
  60.   </style>
  61. </head>
  62. <body>
  63.   <div id="game">
  64.     <div id="scoreBoard">得分: 0</div>
  65.     <div id="player"></div>
  66.   </div>

  67.   <script>
  68.     const game = document.getElementById('game');
  69.     const player = document.getElementById('player');
  70.     const scoreBoard = document.getElementById('scoreBoard');

  71.     const gameWidth = game.clientWidth;
  72.     const gameHeight = game.clientHeight;

  73.     let playerX = 180; // 飞机的水平位置
  74.     let speed = 5; // 飞机移动速度
  75.     let bullets = [];
  76.     let enemies = [];
  77.     let score = 0;

  78.     // 监听键盘事件
  79.     const keys = {};
  80.     window.addEventListener('keydown', e => {
  81.       keys[e.code] = true;
  82.     });
  83.     window.addEventListener('keyup', e => {
  84.       keys[e.code] = false;
  85.     });

  86.     // 发射子弹
  87.     function shoot() {
  88.       const bullet = document.createElement('div');
  89.       bullet.classList.add('bullet');
  90.       bullet.style.left = (playerX + 17) + 'px'; // 子弹在飞机正中稍偏上
  91.       bullet.style.top = (gameHeight - 80) + 'px';
  92.       game.appendChild(bullet);
  93.       bullets.push(bullet);
  94.     }

  95.     // 每隔一段时间自动射击
  96.     setInterval(() => {
  97.       shoot();
  98.     }, 300);

  99.     // 创建敌人
  100.     function createEnemy() {
  101.       const enemy = document.createElement('div');
  102.       enemy.classList.add('enemy');
  103.       enemy.style.left = Math.random() * (gameWidth - 40) + 'px';
  104.       enemy.style.top = '-40px';
  105.       game.appendChild(enemy);
  106.       enemies.push(enemy);
  107.     }
  108.     // 每一秒产生一个敌人
  109.     setInterval(() => {
  110.       createEnemy();
  111.     }, 1000);

  112.     // 游戏主循环,更新游戏状态
  113.     function gameLoop() {
  114.       // 控制飞机左右移动
  115.       if (keys['ArrowLeft']) {
  116.         playerX = Math.max(0, playerX - speed);
  117.       }
  118.       if (keys['ArrowRight']) {
  119.         playerX = Math.min(gameWidth - 40, playerX + speed);
  120.       }
  121.       player.style.left = playerX + 'px';

  122.       // 更新子弹位置
  123.       for (let i = bullets.length -1; i >= 0; i--) {
  124.         const bullet = bullets[i];
  125.         let top = parseInt(bullet.style.top);
  126.         top -= 10; // 子弹移动速度
  127.         if (top < -14) {
  128.           bullet.remove();
  129.           bullets.splice(i, 1);
  130.         } else {
  131.           bullet.style.top = top + 'px';
  132.         }
  133.       }

  134.       // 更新敌机位置
  135.       for (let i = enemies.length -1; i >= 0; i--) {
  136.         const enemy = enemies[i];
  137.         let top = parseInt(enemy.style.top);
  138.         top += 2; // 敌机移动速度
  139.         if (top > gameHeight) {
  140.           // 敌机飞出屏幕,删除它
  141.           enemy.remove();
  142.           enemies.splice(i, 1);
  143.           continue;
  144.         } else {
  145.           enemy.style.top = top + 'px';
  146.         }

  147.         // 检测敌机和飞机碰撞
  148.         if (isCollide(player, enemy)) {
  149.           alert('游戏结束!你的得分是: ' + score);
  150.           window.location.reload();
  151.         }

  152.         // 检测敌机和子弹碰撞
  153.         for (let j = bullets.length -1; j >= 0; j--) {
  154.           const bullet = bullets[j];
  155.           if (isCollide(bullet, enemy)) {
  156.             // 击中过敌机,删除敌机和子弹,加分
  157.             enemy.remove();
  158.             enemies.splice(i, 1);
  159.             bullet.remove();
  160.             bullets.splice(j, 1);
  161.             score += 10;
  162.             scoreBoard.textContent = '得分: ' + score;
  163.             break;
  164.           }
  165.         }
  166.       }

  167.       requestAnimationFrame(gameLoop);
  168.     }

  169.     // 碰撞检测函数,两个元素是否相撞
  170.     function isCollide(a, b) {
  171.       const aRect = a.getBoundingClientRect();
  172.       const bRect = b.getBoundingClientRect();

  173.       return !(
  174.         aRect.top > bRect.bottom ||
  175.         aRect.bottom < bRect.top ||
  176.         aRect.right < bRect.left ||
  177.         aRect.left > bRect.right
  178.       );
  179.     }

  180.     gameLoop();
  181.   </script>
  182. </body>
  183. </html>
复制代码


---

### 讲解:

- **HTML**部分定义了游戏区域`#game`,里面有我们的飞机`#player`和分数显示。
- **CSS**给飞机、敌机、子弹和背景设置了样式,让它们看起来更像游戏里真实的角色。
- **JavaScript**是游戏的核心:
  - 用键盘左右键控制飞机左右移动。
  - 飞机每隔300毫秒自动射击(发射黄色小子弹)。
  - 每隔1秒生成一个敌机,从上方往下飞。
  - 不断更新子弹和敌机的位置,判断子弹有没有击中敌机。
  - 如果敌机撞到了飞机,游戏结束并弹出分数。

---

### 怎么玩?

- 把上面的代码保存成`plane-game.html`文件。
- 用浏览器打开它。
- 用键盘左箭头和右箭头控制飞机移动。
- 飞机会自动射击,打掉飞过来的敌机。
- 射爆的敌机会加10分,敌机撞到你游戏就结束啦!

如果你想改进这个游戏,可以告诉我,我帮你再加功能,比如音效、更多敌机类型或者更炫酷的特效哦!

祝你玩得开心!????✈️????




欢迎光临 科学教育 (http://www.sci-edu.net/) Powered by Discuz! X3.2