科学教育

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

积分

[复制链接]

8

主题

13

帖子

51

积分

注册会员

Rank: 2

积分
51
跳转到指定楼层
楼主
发表于 2025-12-23 10:01:04 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
飞机大战
打爆日军的飞机
1. 3滴血被敌军打到扣1
2. 消灭1个敌军得1
3. 左右方向由ad控制


帮我生成代码
回复

使用道具 举报

0

主题

1145

帖子

1715

积分

版主

Rank: 7Rank: 7Rank: 7

积分
1715
沙发
发表于 2025-12-23 10:01:27 | 只看该作者
好哒,我帮你写一个简单的“飞机大战”小游戏代码,满足你的需求哦!

这个游戏的功能是:
- 飞机有3滴血,被敌机碰到后扣1血
- 消灭一个敌机得1分
- 用键盘a和d控制飞机左右移动

我们用HTML+CSS+JavaScript来做一个简单版本,直接复制下面代码到你的网页里就能玩啦:

  1. <!DOCTYPE html>
  2. <html lang="zh">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <title>飞机大战小游戏</title>
  6. <style>
  7.   body {
  8.     margin: 0;
  9.     overflow: hidden;
  10.     background: black;
  11.     color: white;
  12.     font-family: "微软雅黑", sans-serif;
  13.   }
  14.   #gameCanvas {
  15.     background: #111;
  16.     display: block;
  17.     margin: auto;
  18.     border: 2px solid white;
  19.   }
  20.   #scoreboard {
  21.     text-align:center;
  22.     margin-top: 10px;
  23.   }
  24. </style>
  25. </head>
  26. <body>

  27. <canvas id="gameCanvas" width="400" height="600"></canvas>
  28. <div id="scoreboard">
  29.   血量:<span id="hp">3</span> &nbsp;&nbsp; 分数:<span id="score">0</span>
  30. </div>

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

  34.   const planeWidth = 40;
  35.   const planeHeight = 40;
  36.   let planeX = (canvas.width - planeWidth) / 2;
  37.   const planeY = canvas.height - planeHeight - 10;
  38.   let hp = 3;
  39.   let score = 0;

  40.   // 敌机数组
  41.   let enemies = [];

  42.   // 子弹数组
  43.   let bullets = [];

  44.   // 控制左右移动
  45.   let moveLeft = false;
  46.   let moveRight = false;

  47.   // 监听键盘按下
  48.   window.addEventListener("keydown", function(e) {
  49.     if (e.key === "a" || e.key === "A") moveLeft = true;
  50.     if (e.key === "d" || e.key === "D") moveRight = true;
  51.   });

  52.   // 监听松开键盘
  53.   window.addEventListener("keyup", function(e) {
  54.     if (e.key === "a" || e.key === "A") moveLeft = false;
  55.     if (e.key === "d" || e.key === "D") moveRight = false;
  56.     if (e.key === " ") shoot();
  57.   });

  58.   // 画飞机
  59.   function drawPlane() {
  60.     ctx.fillStyle = "lightblue";
  61.     ctx.beginPath();
  62.     // 画三角形代表飞机
  63.     ctx.moveTo(planeX + planeWidth/2, planeY);
  64.     ctx.lineTo(planeX, planeY + planeHeight);
  65.     ctx.lineTo(planeX + planeWidth, planeY + planeHeight);
  66.     ctx.closePath();
  67.     ctx.fill();
  68.   }

  69.   // 生成敌机
  70.   function createEnemy() {
  71.     let x = Math.random() * (canvas.width - 30);
  72.     enemies.push({x: x, y: -30, width: 30, height: 30});
  73.   }

  74.   // 画敌机
  75.   function drawEnemies() {
  76.     ctx.fillStyle = "red";
  77.     enemies.forEach(enemy => {
  78.       ctx.fillRect(enemy.x, enemy.y, enemy.width, enemy.height);
  79.     });
  80.   }

  81.   // 移动敌机
  82.   function moveEnemies() {
  83.     enemies.forEach(enemy => {
  84.       enemy.y += 2;  // 敌机向下移动速度
  85.     });
  86.     // 删除超出画面底部的敌机
  87.     enemies = enemies.filter(enemy => enemy.y < canvas.height);
  88.   }

  89.   // 发射子弹
  90.   function shoot() {
  91.     bullets.push({x: planeX + planeWidth / 2 - 2.5, y: planeY, width:5, height:10});
  92.   }

  93.   // 画子弹
  94.   function drawBullets() {
  95.     ctx.fillStyle = "yellow";
  96.     bullets.forEach(bullet => {
  97.       ctx.fillRect(bullet.x, bullet.y, bullet.width, bullet.height);
  98.     });
  99.   }

  100.   // 移动子弹
  101.   function moveBullets() {
  102.     bullets.forEach(bullet => {
  103.       bullet.y -= 5;  // 子弹向上飞
  104.     });
  105.     // 删除飞出画面的子弹
  106.     bullets = bullets.filter(bullet => bullet.y + bullet.height > 0);
  107.   }

  108.   // 碰撞检测函数,检测两个矩形是否重叠
  109.   function isColliding(rect1, rect2) {
  110.     return !(
  111.       rect1.x > rect2.x + rect2.width ||
  112.       rect1.x + rect1.width < rect2.x ||
  113.       rect1.y > rect2.y + rect2.height ||
  114.       rect1.y + rect1.height < rect2.y
  115.     );
  116.   }

  117.   // 检查子弹和敌机碰撞
  118.   function checkBulletEnemyCollision() {
  119.     for (let i = bullets.length -1; i>=0; i--) {
  120.       let b = bullets[i];
  121.       for (let j = enemies.length -1; j>=0; j--) {
  122.         let e = enemies[j];
  123.         if (isColliding(b, e)) {
  124.           // 消灭敌机和子弹
  125.           bullets.splice(i,1);
  126.           enemies.splice(j,1);
  127.           score++;
  128.           updateScore();
  129.           break;
  130.         }
  131.       }
  132.     }
  133.   }

  134.   // 检查敌机和飞机碰撞
  135.   function checkEnemyPlaneCollision() {
  136.     for (let i = enemies.length -1; i>=0; i--) {
  137.       let e = enemies[i];
  138.       let planeRect = {x: planeX, y: planeY, width: planeWidth, height: planeHeight};
  139.       if (isColliding(e, planeRect)) {
  140.         enemies.splice(i,1);
  141.         hp--;
  142.         updateHP();
  143.         if (hp <= 0) {
  144.           alert("游戏结束!你的得分是:" + score);
  145.           resetGame();
  146.         }
  147.       }
  148.     }
  149.   }

  150.   // 更新血量显示
  151.   function updateHP() {
  152.     document.getElementById("hp").textContent = hp;
  153.   }

  154.   // 更新分数显示
  155.   function updateScore() {
  156.     document.getElementById("score").textContent = score;
  157.   }

  158.   // 重置游戏
  159.   function resetGame() {
  160.     hp = 3;
  161.     score = 0;
  162.     enemies = [];
  163.     bullets = [];
  164.     planeX = (canvas.width - planeWidth) / 2;
  165.     updateHP();
  166.     updateScore();
  167.   }

  168.   // 游戏主循环
  169.   function gameLoop() {
  170.     ctx.clearRect(0,0,canvas.width,canvas.height);

  171.     // 移动飞机
  172.     if (moveLeft && planeX > 0) planeX -= 5;
  173.     if (moveRight && planeX < canvas.width - planeWidth) planeX += 5;

  174.     drawPlane();

  175.     // 敌机出现概率控制(差不多每30帧一个)
  176.     if (Math.random() < 0.03) createEnemy();

  177.     moveEnemies();
  178.     drawEnemies();

  179.     moveBullets();
  180.     drawBullets();

  181.     checkBulletEnemyCollision();
  182.     checkEnemyPlaneCollision();

  183.     requestAnimationFrame(gameLoop);
  184.   }

  185.   // 开始游戏
  186.   updateHP();
  187.   updateScore();
  188.   gameLoop();
  189. </script>

  190. <p style="text-align:center; color:#ccc; margin-top:10px;">
  191.   操作提示:<br />
  192.   按A键向左移动,D键向右移动,空格键发射子弹。
  193. </p>

  194. </body>
  195. </html>
复制代码


---

### 讲解:
- 我们画了个黑色背景的画布,在下面显示血量和分数。
- 飞机是蓝色的三角形,用a和d左右移动。
- 敌机是红色的小方块,会从上面往下掉。
- 你可以按空格发射黄色子弹,打掉敌机得分。
- 被敌机碰到,扣1滴血,3滴血没有了游戏结束。
- 游戏结束后会弹出提示,然后重新开始。

你只要把它保存成`html`文件,用浏览器打开就能玩了!如果想让飞机更酷,还可以加图片,或者让敌机更多样化,这些以后慢慢学也可以!

需要我帮你加功能或者讲清楚哪里都可以问我~祝你做游戏开心!????✨
回复 支持 反对

使用道具 举报

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

本版积分规则

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