科学教育

标题: 黄思齐小飞棍 [打印本页]

作者: 黄思齐是你爹    时间: 2025-12-2 13:58
标题: 黄思齐小飞棍
帮我生成一个飞机大战的网页代码

作者: kaia    时间: 2025-12-2 13:59
好的!我来帮你写一个简单的“飞机大战”小游戏网页代码,里面会有小飞机可以左右移动,按空格键发射子弹,打掉敌机。代码用的是HTML、CSS和JavaScript,比较适合初学者理解。

你可以把下面的代码直接复制到一个叫 `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.     background: #000;
  9.     margin: 0;
  10.     overflow: hidden;
  11.   }
  12.   canvas {
  13.     background: #111;
  14.     display: block;
  15.     margin: 0 auto;
  16.   }
  17. </style>
  18. </head>
  19. <body>
  20. <canvas id="gameCanvas" width="400" height="600"></canvas>

  21. <script>
  22. // 获取画布和绘图环境
  23. const canvas = document.getElementById('gameCanvas');
  24. const ctx = canvas.getContext('2d');

  25. const CANVAS_WIDTH = canvas.width;
  26. const CANVAS_HEIGHT = canvas.height;

  27. // 玩家飞机对象
  28. const player = {
  29.   x: CANVAS_WIDTH / 2 - 20,
  30.   y: CANVAS_HEIGHT - 60,
  31.   width: 40,
  32.   height: 40,
  33.   speed: 5,
  34.   bullets: []
  35. };

  36. // 敌人飞机数组
  37. let enemies = [];
  38. const enemyWidth = 40;
  39. const enemyHeight = 40;
  40. let enemySpeed = 1;
  41. let enemySpawnInterval = 1000; // 毫秒

  42. // 控制变量
  43. let keys = {};
  44. let score = 0;
  45. let gameOver = false;

  46. // 监听键盘按下
  47. window.addEventListener('keydown', function(e){
  48.   keys[e.key] = true;

  49.   // 按空格发射子弹
  50.   if(e.key === ' ' || e.key === 'Spacebar'){
  51.     fireBullet();
  52.   }
  53. });
  54. // 监听键盘松开
  55. window.addEventListener('keyup', function(e){
  56.   keys[e.key] = false;
  57. });

  58. // 发射子弹函数
  59. function fireBullet(){
  60.   // 给子弹添加一个对象,位置在飞机正上方中间
  61.   player.bullets.push({
  62.     x: player.x + player.width / 2 - 3,
  63.     y: player.y,
  64.     width: 6,
  65.     height: 12,
  66.     speed: 7
  67.   });
  68. }

  69. // 生成敌机
  70. function spawnEnemy(){
  71.   const x = Math.random() * (CANVAS_WIDTH - enemyWidth);
  72.   enemies.push({
  73.     x: x,
  74.     y: -enemyHeight,
  75.     width: enemyWidth,
  76.     height: enemyHeight,
  77.     speed: enemySpeed
  78.   });
  79. }

  80. // 更新游戏状态
  81. function update(){
  82.   if(gameOver) return;

  83.   // 玩家左右移动
  84.   if(keys['ArrowLeft'] && player.x > 0){
  85.     player.x -= player.speed;
  86.   }
  87.   if(keys['ArrowRight'] && player.x < CANVAS_WIDTH - player.width){
  88.     player.x += player.speed;
  89.   }

  90.   // 更新子弹位置
  91.   player.bullets.forEach((bullet, index) => {
  92.     bullet.y -= bullet.speed;
  93.     // 离开画面就删除
  94.     if(bullet.y + bullet.height < 0){
  95.       player.bullets.splice(index, 1);
  96.     }
  97.   });

  98.   // 更新敌机位置
  99.   enemies.forEach((enemy, enemyIndex) => {
  100.     enemy.y += enemy.speed;

  101.     // 敌机碰到地面,游戏结束
  102.     if(enemy.y > CANVAS_HEIGHT){
  103.       gameOver = true;
  104.     }

  105.     // 检测敌机和子弹碰撞
  106.     player.bullets.forEach((bullet, bulletIndex) => {
  107.       if(isColliding(bullet, enemy)){
  108.         // 移除敌机和子弹
  109.         enemies.splice(enemyIndex, 1);
  110.         player.bullets.splice(bulletIndex, 1);
  111.         score += 10;
  112.       }
  113.     });

  114.     // 检测敌机和玩家飞机碰撞
  115.     if(isColliding(enemy, player)){
  116.       gameOver = true;
  117.     }
  118.   });
  119. }

  120. // 碰撞检测函数
  121. function isColliding(a, b){
  122.   return a.x < b.x + b.width &&
  123.          a.x + a.width > b.x &&
  124.          a.y < b.y + b.height &&
  125.          a.y + a.height > b.y;
  126. }

  127. // 绘制游戏元素
  128. function draw(){
  129.   // 清空画布
  130.   ctx.clearRect(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT);

  131.   // 画玩家飞机(绿色三角形)
  132.   ctx.fillStyle = 'green';
  133.   ctx.beginPath();
  134.   ctx.moveTo(player.x + player.width/2, player.y);
  135.   ctx.lineTo(player.x, player.y + player.height);
  136.   ctx.lineTo(player.x + player.width, player.y + player.height);
  137.   ctx.closePath();
  138.   ctx.fill();

  139.   // 画子弹(黄色矩形)
  140.   ctx.fillStyle = 'yellow';
  141.   player.bullets.forEach(bullet => {
  142.     ctx.fillRect(bullet.x, bullet.y, bullet.width, bullet.height);
  143.   });

  144.   // 画敌机(红色方块)
  145.   ctx.fillStyle = 'red';
  146.   enemies.forEach(enemy => {
  147.     ctx.fillRect(enemy.x, enemy.y, enemy.width, enemy.height);
  148.   });

  149.   // 画分数
  150.   ctx.fillStyle = 'white';
  151.   ctx.font = '20px Arial';
  152.   ctx.fillText('分数: ' + score, 10, 30);

  153.   if(gameOver){
  154.     ctx.fillStyle = 'white';
  155.     ctx.font = '40px Arial';
  156.     ctx.fillText('游戏结束', CANVAS_WIDTH/2 - 100, CANVAS_HEIGHT/2);
  157.   }
  158. }

  159. // 游戏主循环
  160. function gameLoop(){
  161.   update();
  162.   draw();
  163.   if(!gameOver){
  164.     requestAnimationFrame(gameLoop);
  165.   }
  166. }

  167. // 开始生成敌机
  168. setInterval(() => {
  169.   if(!gameOver){
  170.     spawnEnemy();
  171.   }
  172. }, enemySpawnInterval);

  173. // 开始游戏
  174. gameLoop();
  175. </script>
  176. </body>
  177. </html>
复制代码


---

### 这个游戏怎么玩?

- **左右方向键** 控制你的绿色小飞机左右移动。
- **空格键** 发射黄色子弹。
- 敌人是红色的小方块,会从上往下掉下来。
- 用子弹打掉敌人得分。
- 如果敌人撞到你的飞机或者飞到底部,游戏结束。

---

如果你以后想让飞机变帅一点,或者加更多功能,比如“生命值”、“音效”,可以再告诉我,我帮你改进!怎么样?是不是很酷呢?试试看吧!????✈️????




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