科学教育

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

孙浩轩飞机大战

[复制链接]

1

主题

1

帖子

10

积分

新手上路

Rank: 1

积分
10
跳转到指定楼层
楼主
发表于 2025-12-2 15:06:07 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
帮我生成一个飞机大战的代码
回复

使用道具 举报

0

主题

1145

帖子

1715

积分

版主

Rank: 7Rank: 7Rank: 7

积分
1715
沙发
发表于 2025-12-2 15:06:56 | 只看该作者
好的!我来帮你写一个简单的“飞机大战”小游戏,适合小学生学习用。我们用网页编程里的HTML和JavaScript来做,这样你可以直接在浏览器里玩。

这个游戏会有:
- 你的飞机(可以左右移动)
- 敌人的飞机(会自动往下飞)
- 子弹(可以发射击打敌机)
- 计分和结束判断

下面是完整代码,你打开记事本,复制粘贴保存为 `plane_battle.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.   #gameCanvas {
  13.     background: #111;
  14.     display: block;
  15.     margin: 0 auto;
  16.   }
  17.   #scoreBoard {
  18.     color: white;
  19.     font-size: 20px;
  20.     text-align: center;
  21.     margin-top: 5px;
  22.   }
  23. </style>
  24. </head>
  25. <body>

  26. <canvas id="gameCanvas" width="400" height="600"></canvas>
  27. <div id="scoreBoard">得分: 0</div>

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

  31. const planeWidth = 40;
  32. const planeHeight = 60;
  33. let planeX = canvas.width / 2 - planeWidth / 2;
  34. const planeY = canvas.height - planeHeight - 10;

  35. let bullets = []; // 子弹数组
  36. let enemies = []; // 敌机数组
  37. let score = 0;
  38. let gameOver = false;

  39. // 控制飞机左右移动
  40. let leftPressed = false;
  41. let rightPressed = false;

  42. // 监听键盘事件
  43. document.addEventListener('keydown', (e) => {
  44.   if (e.code === 'ArrowLeft') leftPressed = true;
  45.   else if (e.code === 'ArrowRight') rightPressed = true;
  46.   else if (e.code === 'Space') shootBullet();
  47. });

  48. document.addEventListener('keyup', (e) => {
  49.   if (e.code === 'ArrowLeft') leftPressed = false;
  50.   else if (e.code === 'ArrowRight') rightPressed = false;
  51. });

  52. // 飞机画成三角形
  53. function drawPlane() {
  54.   ctx.fillStyle = 'skyblue';
  55.   ctx.beginPath();
  56.   ctx.moveTo(planeX + planeWidth / 2, planeY); // 顶点
  57.   ctx.lineTo(planeX, planeY + planeHeight);
  58.   ctx.lineTo(planeX + planeWidth, planeY + planeHeight);
  59.   ctx.closePath();
  60.   ctx.fill();
  61. }

  62. // 子弹画成细细的红色矩形
  63. function drawBullets() {
  64.   ctx.fillStyle = 'red';
  65.   bullets.forEach(bullet => {
  66.     ctx.fillRect(bullet.x, bullet.y, 4, 10);
  67.   });
  68. }

  69. // 敌机画成绿色矩形
  70. function drawEnemies() {
  71.   ctx.fillStyle = 'lime';
  72.   enemies.forEach(enemy => {
  73.     ctx.fillRect(enemy.x, enemy.y, 40, 30);
  74.   });
  75. }

  76. // 发射子弹,把子弹放到数组里
  77. function shootBullet() {
  78.   if (gameOver) return;
  79.   bullets.push({
  80.     x: planeX + planeWidth / 2 - 2,
  81.     y: planeY
  82.   });
  83. }

  84. // 更新子弹位置
  85. function updateBullets() {
  86.   bullets.forEach((bullet, index) => {
  87.     bullet.y -= 7; // 子弹往上飞
  88.     if (bullet.y < 0) {
  89.       bullets.splice(index, 1); // 超出屏幕就删掉
  90.     }
  91.   });
  92. }

  93. // 生成敌机
  94. function spawnEnemy() {
  95.   if (gameOver) return;
  96.   const x = Math.random() * (canvas.width - 40);
  97.   enemies.push({ x: x, y: 0 });
  98. }

  99. // 更新敌机位置
  100. function updateEnemies() {
  101.   enemies.forEach((enemy, index) => {
  102.     enemy.y += 2;
  103.     // 敌机飞到底部,游戏结束
  104.     if (enemy.y > canvas.height) {
  105.       gameOver = true;
  106.     }
  107.   });
  108. }

  109. // 检查碰撞,子弹和敌机
  110. function checkCollisions() {
  111.   bullets.forEach((bullet, bIndex) => {
  112.     enemies.forEach((enemy, eIndex) => {
  113.       if (
  114.         bullet.x > enemy.x &&
  115.         bullet.x < enemy.x + 40 &&
  116.         bullet.y > enemy.y &&
  117.         bullet.y < enemy.y + 30
  118.       ) {
  119.         // 碰撞了
  120.         bullets.splice(bIndex, 1);
  121.         enemies.splice(eIndex, 1);
  122.         score += 10;
  123.         document.getElementById('scoreBoard').innerText = '得分: ' + score;
  124.       }
  125.     });
  126.   });
  127. }

  128. // 更新飞机位置
  129. function updatePlane() {
  130.   if (leftPressed && planeX > 0) {
  131.     planeX -= 5;
  132.   }
  133.   if (rightPressed && planeX < canvas.width - planeWidth) {
  134.     planeX += 5;
  135.   }
  136. }

  137. // 游戏主循环
  138. function gameLoop() {
  139.   if (gameOver) {
  140.     ctx.fillStyle = 'white';
  141.     ctx.font = '40px Arial';
  142.     ctx.fillText('游戏结束', canvas.width / 2 - 100, canvas.height / 2);
  143.     return;
  144.   }

  145.   // 清空画布
  146.   ctx.clearRect(0, 0, canvas.width, canvas.height);

  147.   updatePlane();
  148.   updateBullets();
  149.   updateEnemies();
  150.   checkCollisions();

  151.   drawPlane();
  152.   drawBullets();
  153.   drawEnemies();

  154.   requestAnimationFrame(gameLoop);
  155. }

  156. // 每隔1秒生成新敌机
  157. setInterval(spawnEnemy, 1000);

  158. // 开始游戏
  159. gameLoop();
  160. </script>

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


### 怎么玩呢?
- 用键盘上的 **←** 和 **→** 控制你的飞机左右移动
- 按空格键 **Space** 发射子弹
- 射击从上方来的敌机
- 如果敌机飞到底部,游戏结束,会显示“游戏结束”
- 右上角有得分板,击中敌机会加分哦

你先试试看,如果想让飞机更漂亮或者增加更多功能,我还能帮你修改。祝你玩得开心!????✈️
回复 支持 反对

使用道具 举报

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

本版积分规则

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