科学教育

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

帮我生成飞机大战的网页代码

[复制链接]

1

主题

2

帖子

9

积分

新手上路

Rank: 1

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

使用道具 举报

0

主题

1145

帖子

1715

积分

版主

Rank: 7Rank: 7Rank: 7

积分
1715
沙发
发表于 2025-12-2 09:47:50 | 只看该作者
好的!我帮你写一个简单的飞机大战网页游戏代码。你可以在浏览器里打开它玩玩看哦!

代码里有:
- 一个可以用键盘左右移动的小飞机
- 飞下来的敌机
- 发射子弹打敌机
- 打中敌机会消失,得分增加

你只需要复制下面代码,保存成一个 `.html` 文件,比如 `feiji.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: black;
  10.     overflow: hidden;
  11.   }
  12.   #gameCanvas {
  13.     background: #111;
  14.     display: block;
  15.     margin: 0 auto;
  16.     border: 2px solid white;
  17.   }
  18.   #score {
  19.     color: white;
  20.     font-size: 20px;
  21.     text-align: center;
  22.     margin-top: 5px;
  23.     font-family: "微软雅黑", Arial, sans-serif;
  24.   }
  25. </style>
  26. </head>
  27. <body>

  28. <canvas id="gameCanvas" width="400" height="600"></canvas>
  29. <div id="score">得分: 0</div>

  30. <script>
  31.   const canvas = document.getElementById('gameCanvas');
  32.   const ctx = canvas.getContext('2d');

  33.   const planeWidth = 40;
  34.   const planeHeight = 40;
  35.   let planeX = (canvas.width - planeWidth) / 2;
  36.   const planeY = canvas.height - planeHeight - 10;

  37.   const bulletWidth = 5;
  38.   const bulletHeight = 10;
  39.   const bulletSpeed = 7;

  40.   const enemyWidth = 40;
  41.   const enemyHeight = 40;
  42.   const enemySpeed = 2;

  43.   let bullets = [];
  44.   let enemies = [];

  45.   let leftPressed = false;
  46.   let rightPressed = false;
  47.   const planeSpeed = 5;

  48.   let score = 0;

  49.   // 监听键盘按键事件
  50.   document.addEventListener('keydown', e => {
  51.     if(e.code === 'ArrowLeft') leftPressed = true;
  52.     if(e.code === 'ArrowRight') rightPressed = true;
  53.     if(e.code === 'Space') shootBullet();
  54.   });

  55.   document.addEventListener('keyup', e => {
  56.     if(e.code === 'ArrowLeft') leftPressed = false;
  57.     if(e.code === 'ArrowRight') rightPressed = false;
  58.   });

  59.   // 发射子弹函数
  60.   function shootBullet() {
  61.     bullets.push({
  62.       x: planeX + planeWidth / 2 - bulletWidth / 2,
  63.       y: planeY,
  64.       width: bulletWidth,
  65.       height: bulletHeight,
  66.     });
  67.   }

  68.   // 创建敌机函数
  69.   function createEnemy() {
  70.     const x = Math.random() * (canvas.width - enemyWidth);
  71.     enemies.push({
  72.       x: x,
  73.       y: -enemyHeight,
  74.       width: enemyWidth,
  75.       height: enemyHeight,
  76.     });
  77.   }

  78.   // 检测碰撞
  79.   function isCollide(a, b) {
  80.     return !(
  81.       a.x + a.width < b.x ||
  82.       a.x > b.x + b.width ||
  83.       a.y + a.height < b.y ||
  84.       a.y > b.y + b.height
  85.     );
  86.   }

  87.   // 更新游戏状态
  88.   function update() {
  89.     // 移动飞机
  90.     if(leftPressed) {
  91.       planeX -= planeSpeed;
  92.       if(planeX < 0) planeX = 0;
  93.     }
  94.     if(rightPressed) {
  95.       planeX += planeSpeed;
  96.       if(planeX > canvas.width - planeWidth) planeX = canvas.width - planeWidth;
  97.     }

  98.     // 移动子弹
  99.     bullets = bullets.filter(bullet => bullet.y + bullet.height > 0);
  100.     bullets.forEach(bullet => {
  101.       bullet.y -= bulletSpeed;
  102.     });

  103.     // 移动敌机
  104.     enemies = enemies.filter(enemy => enemy.y < canvas.height);
  105.     enemies.forEach(enemy => {
  106.       enemy.y += enemySpeed;
  107.     });

  108.     // 子弹和敌机碰撞检测
  109.     for(let i = bullets.length - 1; i >= 0; i--) {
  110.       for(let j = enemies.length - 1; j >= 0; j--) {
  111.         if(isCollide(bullets[i], enemies[j])) {
  112.           // 碰撞,删除子弹和敌机,得分加一
  113.           bullets.splice(i, 1);
  114.           enemies.splice(j, 1);
  115.           score++;
  116.           document.getElementById('score').innerText = '得分: ' + score;
  117.           break;
  118.         }
  119.       }
  120.     }

  121.     // 敌机碰到飞机也算游戏结束(这个版本还没做,可以自己试试加)
  122.   }

  123.   // 画出飞机
  124.   function drawPlane() {
  125.     ctx.fillStyle = 'lime';
  126.     ctx.beginPath();
  127.     // 画一个三角形表示飞机
  128.     ctx.moveTo(planeX + planeWidth/2, planeY);
  129.     ctx.lineTo(planeX, planeY + planeHeight);
  130.     ctx.lineTo(planeX + planeWidth, planeY + planeHeight);
  131.     ctx.closePath();
  132.     ctx.fill();
  133.   }

  134.   // 画子弹
  135.   function drawBullets() {
  136.     ctx.fillStyle = 'yellow';
  137.     bullets.forEach(bullet => {
  138.       ctx.fillRect(bullet.x, bullet.y, bullet.width, bullet.height);
  139.     });
  140.   }

  141.   // 画敌机
  142.   function drawEnemies() {
  143.     ctx.fillStyle = 'red';
  144.     enemies.forEach(enemy => {
  145.       ctx.fillRect(enemy.x, enemy.y, enemy.width, enemy.height);
  146.     });
  147.   }

  148.   // 主循环
  149.   function gameLoop() {
  150.     ctx.clearRect(0, 0, canvas.width, canvas.height);
  151.     update();
  152.     drawPlane();
  153.     drawBullets();
  154.     drawEnemies();

  155.     requestAnimationFrame(gameLoop);
  156.   }

  157.   // 每隔1秒生成一个敌机
  158.   setInterval(createEnemy, 1000);

  159.   // 开始游戏
  160.   gameLoop();

  161. </script>
  162. </body>
  163. </html>
复制代码


玩的时候:
- 用键盘左右箭头控制飞机左右移动
- 按空格发射子弹
- 打掉飞下来的红色敌机得分

如果你学会了,可以尝试自己改改颜色、速度或者加个“游戏结束”功能哦!需要帮助再找我哈~
回复 支持 反对

使用道具 举报

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

本版积分规则

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