科学教育

标题: zhangwanyu张婉玗18 [打印本页]

作者: 张婉玗18    时间: 2025-12-2 09:44
标题: zhangwanyu张婉玗18
帮我生成飞机大战的网页代码html

作者: kaia    时间: 2025-12-2 09:45
你好!我来帮你做一个简单的“飞机大战”小游戏网页代码哦。这个游戏里你可以用键盘左右箭头控制飞机移动,用空格键发射子弹,打中敌机会得分。下面是完整的代码,你可以复制到一个叫`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.       overflow: hidden;
  10.       background: black;
  11.     }
  12.     canvas {
  13.       display: block;
  14.       margin: 0 auto;
  15.       background: #111;
  16.       border: 2px solid white;
  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: 180,
  28.       y: 520,
  29.       width: 40,
  30.       height: 40,
  31.       speed: 5,
  32.       color: 'cyan',
  33.     };

  34.     // 子弹数组
  35.     let bullets = [];

  36.     // 敌机数组
  37.     let enemies = [];

  38.     // 分数
  39.     let score = 0;

  40.     // 监听按键状态
  41.     const keys = {
  42.       left: false,
  43.       right: false,
  44.       space: false,
  45.     };

  46.     // 绑定键盘事件
  47.     window.addEventListener('keydown', function(e) {
  48.       if (e.code === 'ArrowLeft') keys.left = true;
  49.       else if (e.code === 'ArrowRight') keys.right = true;
  50.       else if (e.code === 'Space') keys.space = true;
  51.     });
  52.     window.addEventListener('keyup', function(e) {
  53.       if (e.code === 'ArrowLeft') keys.left = false;
  54.       else if (e.code === 'ArrowRight') keys.right = false;
  55.       else if (e.code === 'Space') keys.space = false;
  56.     });

  57.     // 生成敌机
  58.     function createEnemy() {
  59.       const x = Math.random() * (canvas.width - 40);
  60.       enemies.push({x, y: -40, width: 40, height: 40, speed: 2 + Math.random() * 2, color: 'red'});
  61.     }

  62.     // 画飞机
  63.     function drawPlane() {
  64.       ctx.fillStyle = plane.color;
  65.       ctx.beginPath();
  66.       // 简单飞机形状(三角形)
  67.       ctx.moveTo(plane.x + plane.width / 2, plane.y);
  68.       ctx.lineTo(plane.x, plane.y + plane.height);
  69.       ctx.lineTo(plane.x + plane.width, plane.y + plane.height);
  70.       ctx.closePath();
  71.       ctx.fill();
  72.     }

  73.     // 画子弹
  74.     function drawBullets() {
  75.       ctx.fillStyle = 'yellow';
  76.       bullets.forEach(bullet => {
  77.         ctx.fillRect(bullet.x, bullet.y, 4, 10);
  78.       });
  79.     }

  80.     // 画敌机
  81.     function drawEnemies() {
  82.       enemies.forEach(enemy => {
  83.         ctx.fillStyle = enemy.color;
  84.         ctx.fillRect(enemy.x, enemy.y, enemy.width, enemy.height);
  85.       });
  86.     }

  87.     // 更新飞机位置
  88.     function updatePlane() {
  89.       if (keys.left && plane.x > 0) {
  90.         plane.x -= plane.speed;
  91.       }
  92.       if (keys.right && plane.x < canvas.width - plane.width) {
  93.         plane.x += plane.speed;
  94.       }
  95.     }

  96.     // 更新子弹位置
  97.     function updateBullets() {
  98.       bullets.forEach((bullet, index) => {
  99.         bullet.y -= 7;
  100.         if (bullet.y < 0) {
  101.           bullets.splice(index, 1); // 删除飞出屏幕的子弹
  102.         }
  103.       });
  104.     }

  105.     // 更新敌机位置
  106.     function updateEnemies() {
  107.       enemies.forEach((enemy, index) => {
  108.         enemy.y += enemy.speed;
  109.         if (enemy.y > canvas.height) {
  110.           enemies.splice(index, 1); // 删除飞出屏幕底部的敌机
  111.         }
  112.       });
  113.     }

  114.     // 发射子弹
  115.     let canShoot = true;
  116.     function shootBullet() {
  117.       if (keys.space && canShoot) {
  118.         bullets.push({x: plane.x + plane.width/2 - 2, y: plane.y, width:4, height:10});
  119.         canShoot = false;
  120.         setTimeout(() => { canShoot = true; }, 300); // 每300毫秒才能射击一次
  121.       }
  122.     }

  123.     // 碰撞检测
  124.     function isColliding(a, b) {
  125.       return !(
  126.         a.x + a.width < b.x ||
  127.         a.x > b.x + b.width ||
  128.         a.y + a.height < b.y ||
  129.         a.y > b.y + b.height
  130.       );
  131.     }

  132.     // 检测子弹和敌机的碰撞
  133.     function checkCollisions() {
  134.       bullets.forEach((bullet, bIndex) => {
  135.         enemies.forEach((enemy, eIndex) => {
  136.           if (isColliding(bullet, enemy)) {
  137.             bullets.splice(bIndex, 1);
  138.             enemies.splice(eIndex, 1);
  139.             score += 10;
  140.           }
  141.         });
  142.       });
  143.     }

  144.     // 检测敌机和飞机碰撞
  145.     function checkGameOver() {
  146.       for (let enemy of enemies) {
  147.         if (isColliding(enemy, plane)) {
  148.           alert('游戏结束!你的得分: ' + score);
  149.           document.location.reload();
  150.           break;
  151.         }
  152.       }
  153.     }

  154.     let enemyTimer = 0;
  155.     function gameLoop() {
  156.       ctx.clearRect(0, 0, canvas.width, canvas.height);

  157.       updatePlane();
  158.       shootBullet();
  159.       updateBullets();
  160.       updateEnemies();
  161.       checkCollisions();
  162.       checkGameOver();

  163.       drawPlane();
  164.       drawBullets();
  165.       drawEnemies();

  166.       // 定时生成敌机,差不多每秒两个
  167.       enemyTimer++;
  168.       if (enemyTimer > 30) {
  169.         createEnemy();
  170.         enemyTimer = 0;
  171.       }

  172.       // 显示分数
  173.       ctx.fillStyle = 'white';
  174.       ctx.font = '20px Arial';
  175.       ctx.fillText('得分: ' + score, 10, 30);

  176.       requestAnimationFrame(gameLoop);
  177.     }

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


### 怎么玩的:
- 用键盘左右箭头键让飞机左右移动
- 按空格键发射子弹
- 把红色的敌机打掉,得分会增加
- 敌机碰到你的飞机游戏就结束啦!

如果你想加功能或者有不懂的地方,可以随时问我哦!祝你编程快乐!????✈️????




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