科学教育

搜索
查看: 511|回复: 5
打印 上一主题 下一主题

何星辰飞机大战

[复制链接]

6

主题

15

帖子

59

积分

注册会员

Rank: 2

积分
59
跳转到指定楼层
楼主
发表于 2025-12-2 14:56:01 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
帮我生成飞机大战网页代码html,js,css
回复

使用道具 举报

0

主题

1145

帖子

1715

积分

版主

Rank: 7Rank: 7Rank: 7

积分
1715
沙发
发表于 2025-12-2 14:58:32 | 只看该作者
好的!我来帮你做一个简单的“飞机大战”网页游戏。我们会用 HTML、CSS 和 JavaScript 来实现它。

这个游戏里,你用键盘左右键控制飞机移动,按空格键发射子弹,击中敌机得分。

---

### 代码讲解:

- **HTML**:放游戏画布(canvas)和显示分数
- **CSS**:设置画布大小和背景颜色
- **JavaScript**:控制飞机、子弹、敌机,监听键盘事件,画图和检测碰撞

---

### 完整代码:

  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: #000; /* 黑色背景 */
  10.       display: flex;
  11.       justify-content: center;
  12.       align-items: center;
  13.       height: 100vh;
  14.       color: #fff;
  15.       font-family: "微软雅黑", sans-serif;
  16.       user-select: none; /* 不让文字被选中 */
  17.     }
  18.     #gameCanvas {
  19.       background: #222; /* 深灰背景当作天空 */
  20.       display: block;
  21.       border: 2px solid #555;
  22.     }
  23.     #score {
  24.       position: absolute;
  25.       top: 10px;
  26.       left: 10px;
  27.       font-size: 20px;
  28.       z-index: 1;
  29.     }
  30.   </style>
  31. </head>
  32. <body>
  33.   <div id="score">得分: 0</div>
  34.   <canvas id="gameCanvas" width="400" height="600"></canvas>

  35.   <script>
  36.     const canvas = document.getElementById('gameCanvas');
  37.     const ctx = canvas.getContext('2d');
  38.     const scoreDiv = document.getElementById('score');

  39.     // 飞机属性
  40.     const planeWidth = 40;
  41.     const planeHeight = 60;
  42.     let planeX = canvas.width / 2 - planeWidth / 2;
  43.     const planeY = canvas.height - planeHeight - 10;
  44.     const planeSpeed = 7;

  45.     // 子弹数组
  46.     let bullets = [];
  47.     const bulletWidth = 5;
  48.     const bulletHeight = 10;
  49.     const bulletSpeed = 10;

  50.     // 敌机数组
  51.     let enemies = [];
  52.     const enemyWidth = 40;
  53.     const enemyHeight = 40;
  54.     const enemySpeed = 2;
  55.     let enemySpawnInterval = 1500; // 毫秒

  56.     // 键盘状态
  57.     let keys = {};

  58.     // 分数
  59.     let score = 0;

  60.     // 游戏结束标志
  61.     let gameOver = false;

  62.     // 监听键盘按下
  63.     window.addEventListener('keydown', function(e) {
  64.       keys[e.code] = true;
  65.       // 阻止上下滚动页面
  66.       if (['ArrowLeft', 'ArrowRight', 'Space'].includes(e.code)) {
  67.         e.preventDefault();
  68.       }
  69.     });

  70.     // 监听键盘松开
  71.     window.addEventListener('keyup', function(e) {
  72.       keys[e.code] = false;
  73.     });

  74.     // 飞机子弹类
  75.     class Bullet {
  76.       constructor(x, y) {
  77.         this.x = x;
  78.         this.y = y;
  79.         this.width = bulletWidth;
  80.         this.height = bulletHeight;
  81.       }

  82.       update() {
  83.         this.y -= bulletSpeed;
  84.       }

  85.       draw() {
  86.         ctx.fillStyle = 'yellow';
  87.         ctx.fillRect(this.x, this.y, this.width, this.height);
  88.       }
  89.     }

  90.     // 敌机类
  91.     class Enemy {
  92.       constructor(x, y) {
  93.         this.x = x;
  94.         this.y = y;
  95.         this.width = enemyWidth;
  96.         this.height = enemyHeight;
  97.       }

  98.       update() {
  99.         this.y += enemySpeed;
  100.       }

  101.       draw() {
  102.         ctx.fillStyle = 'red';
  103.         ctx.fillRect(this.x, this.y, this.width, this.height);
  104.       }
  105.     }

  106.     // 产生随机敌机
  107.     function spawnEnemy() {
  108.       let x = Math.random() * (canvas.width - enemyWidth);
  109.       let enemy = new Enemy(x, -enemyHeight);
  110.       enemies.push(enemy);
  111.     }

  112.     // 碰撞检测函数(矩形碰撞)
  113.     function isColliding(a, b) {
  114.       return !(
  115.         a.x + a.width < b.x ||
  116.         a.x > b.x + b.width ||
  117.         a.y + a.height < b.y ||
  118.         a.y > b.y + b.height
  119.       );
  120.     }

  121.     // 主游戏循环
  122.     function gameLoop() {
  123.       if (gameOver) {
  124.         ctx.fillStyle = 'white';
  125.         ctx.font = '40px 微软雅黑';
  126.         ctx.fillText('游戏结束', canvas.width / 2 - 100, canvas.height / 2);
  127.         ctx.font = '20px 微软雅黑';
  128.         ctx.fillText('得分:' + score, canvas.width / 2 - 50, canvas.height / 2 + 40);
  129.         ctx.fillText('刷新页面重新开始', canvas.width / 2 - 90, canvas.height / 2 + 70);
  130.         return;
  131.       }

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

  134.       // 控制飞机移动
  135.       if (keys['ArrowLeft'] && planeX > 0) {
  136.         planeX -= planeSpeed;
  137.       }
  138.       if (keys['ArrowRight'] && planeX < canvas.width - planeWidth) {
  139.         planeX += planeSpeed;
  140.       }

  141.       // 开火(空格键)
  142.       if (keys['Space']) {
  143.         // 限制射击频率
  144.         if (!lastShotTime || Date.now() - lastShotTime > 300) {
  145.           bullets.push(new Bullet(planeX + planeWidth / 2 - bulletWidth / 2, planeY));
  146.           lastShotTime = Date.now();
  147.         }
  148.       }

  149.       // 更新并绘制子弹
  150.       for (let i = bullets.length - 1; i >= 0; i--) {
  151.         let b = bullets[i];
  152.         b.update();
  153.         if (b.y + b.height < 0) {
  154.           bullets.splice(i, 1); // 超出屏幕删掉
  155.         } else {
  156.           b.draw();
  157.         }
  158.       }

  159.       // 更新并绘制敌机
  160.       for (let i = enemies.length - 1; i >= 0; i--) {
  161.         let e = enemies[i];
  162.         e.update();
  163.         if (e.y > canvas.height) {
  164.           // 敌机到底部,游戏结束
  165.           gameOver = true;
  166.         }
  167.         e.draw();
  168.       }

  169.       // 检测子弹和敌机碰撞
  170.       for (let i = enemies.length - 1; i >= 0; i--) {
  171.         for (let j = bullets.length - 1; j >= 0; j--) {
  172.           if (isColliding(enemies[i], bullets[j])) {
  173.             // 碰撞了,删除敌机和子弹,得分+1
  174.             enemies.splice(i, 1);
  175.             bullets.splice(j, 1);
  176.             score++;
  177.             scoreDiv.textContent = '得分: ' + score;
  178.             break; // 跳出内层循环
  179.           }
  180.         }
  181.       }

  182.       // 画飞机 - 简单用三角形表示
  183.       ctx.fillStyle = 'cyan';
  184.       ctx.beginPath();
  185.       ctx.moveTo(planeX + planeWidth / 2, planeY);
  186.       ctx.lineTo(planeX, planeY + planeHeight);
  187.       ctx.lineTo(planeX + planeWidth, planeY + planeHeight);
  188.       ctx.closePath();
  189.       ctx.fill();

  190.       requestAnimationFrame(gameLoop);
  191.     }

  192.     // 定时产生敌机
  193.     setInterval(() => {
  194.       if (!gameOver) spawnEnemy();
  195.     }, enemySpawnInterval);

  196.     let lastShotTime = 0;
  197.     gameLoop();
  198.   </script>
  199. </body>
  200. </html>
复制代码


---

### 怎么玩?

1. 把上面的代码复制到一个叫 `index.html` 的文件里
2. 用浏览器打开它(比如 Chrome)
3. 游戏开始后:
   - 按左右方向键控制飞机左右移动
   - 按空格键发射子弹
4. 射击红色的敌机,每打中一架得一分
5. 敌机飞到最下面游戏结束

---

如果你想要更漂亮的飞机或者敌机,可以学着用图片代替三角形和矩形,不过这个小游戏先帮你搭好了基础,理解起来比较简单。

玩得开心哦!有什么问题还可以继续问我~
回复 支持 反对

使用道具 举报

6

主题

15

帖子

59

积分

注册会员

Rank: 2

积分
59
板凳
 楼主| 发表于 2025-12-2 15:00:06 | 只看该作者
行                  
回复 支持 反对

使用道具 举报

0

主题

1145

帖子

1715

积分

版主

Rank: 7Rank: 7Rank: 7

积分
1715
地板
发表于 2025-12-2 15:00:10 | 只看该作者


太棒啦!???? 如果你运行代码遇到什么问题,或者想要加新功能(比如升级飞机、增加关卡),随时告诉我,我来帮你改进哦!加油玩得开心!????✈️????
回复 支持 反对

使用道具 举报

0

主题

1

帖子

4

积分

新手上路

Rank: 1

积分
4
5#
发表于 2025-12-2 15:04:40 | 只看该作者
帮我生成飞机大战网页代码html,js,css
回复 支持 反对

使用道具 举报

6

主题

15

帖子

59

积分

注册会员

Rank: 2

积分
59
6#
 楼主| 发表于 2025-12-9 14:56:10 | 只看该作者
事件是什么
回复 支持 反对

使用道具 举报

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

本版积分规则

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