科学教育

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

张嫣然

[复制链接]

3

主题

12

帖子

43

积分

新手上路

Rank: 1

积分
43
跳转到指定楼层
楼主
发表于 2025-12-2 09:37:02 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
帮我生成一个飞机大战斗网页游戏代码
回复

使用道具 举报

0

主题

1145

帖子

1715

积分

版主

Rank: 7Rank: 7Rank: 7

积分
1715
沙发
发表于 2025-12-2 09:37:50 | 只看该作者
好的,张嫣然!我帮你写一个简单的飞机大战小游戏代码,里面有你的飞机可以上下左右移动,还有敌机会从上往下飞,你可以用空格键发射子弹打敌人。下面是完整的代码,你可以复制到电脑上的记事本,保存成 `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.       margin: 0;
  9.       background: #000;
  10.       overflow: hidden;
  11.     }
  12.     canvas {
  13.       display: block;
  14.       background: #111;
  15.       margin: 0 auto;
  16.       border: 2px solid #555;
  17.     }
  18.   </style>
  19. </head>
  20. <body>
  21.   <canvas id="gameCanvas" width="400" height="600"></canvas>

  22.   <script>
  23.     const canvas = document.getElementById('gameCanvas');
  24.     const ctx = canvas.getContext('2d');

  25.     // 飞机图形大小
  26.     const planeWidth = 40;
  27.     const planeHeight = 40;

  28.     // 游戏状态
  29.     let keys = {};
  30.     let bullets = [];
  31.     let enemies = [];
  32.     let score = 0;
  33.     let gameOver = false;

  34.     // 玩家飞机对象
  35.     const player = {
  36.       x: canvas.width / 2 - planeWidth / 2,
  37.       y: canvas.height - planeHeight - 10,
  38.       width: planeWidth,
  39.       height: planeHeight,
  40.       speed: 5,
  41.       draw() {
  42.         // 画飞机(蓝色三角形)
  43.         ctx.fillStyle = 'deepskyblue';
  44.         ctx.beginPath();
  45.         ctx.moveTo(this.x + this.width / 2, this.y);
  46.         ctx.lineTo(this.x, this.y + this.height);
  47.         ctx.lineTo(this.x + this.width, this.y + this.height);
  48.         ctx.closePath();
  49.         ctx.fill();
  50.       },
  51.       move() {
  52.         if (keys['ArrowLeft'] && this.x > 0) this.x -= this.speed;
  53.         if (keys['ArrowRight'] && this.x + this.width < canvas.width) this.x += this.speed;
  54.         if (keys['ArrowUp'] && this.y > 0) this.y -= this.speed;
  55.         if (keys['ArrowDown'] && this.y + this.height < canvas.height) this.y += this.speed;
  56.       }
  57.     };

  58.     // 子弹类
  59.     class Bullet {
  60.       constructor(x, y) {
  61.         this.x = x;
  62.         this.y = y;
  63.         this.width = 5;
  64.         this.height = 10;
  65.         this.speed = 7;
  66.       }
  67.       draw() {
  68.         ctx.fillStyle = 'yellow';
  69.         ctx.fillRect(this.x, this.y, this.width, this.height);
  70.       }
  71.       update() {
  72.         this.y -= this.speed;
  73.       }
  74.     }

  75.     // 敌机类
  76.     class Enemy {
  77.       constructor(x, y, speed) {
  78.         this.x = x;
  79.         this.y = y;
  80.         this.width = planeWidth;
  81.         this.height = planeHeight;
  82.         this.speed = speed;
  83.       }
  84.       draw() {
  85.         // 画敌机(红色三角形)
  86.         ctx.fillStyle = 'red';
  87.         ctx.beginPath();
  88.         ctx.moveTo(this.x + this.width / 2, this.y + this.height);
  89.         ctx.lineTo(this.x, this.y);
  90.         ctx.lineTo(this.x + this.width, this.y);
  91.         ctx.closePath();
  92.         ctx.fill();
  93.       }
  94.       update() {
  95.         this.y += this.speed;
  96.       }
  97.     }

  98.     // 产生敌机
  99.     function spawnEnemy() {
  100.       const x = Math.random() * (canvas.width - planeWidth);
  101.       const speed = 2 + Math.random() * 1.5;
  102.       enemies.push(new Enemy(x, -planeHeight, speed));
  103.     }

  104.     // 检测碰撞函数
  105.     function isColliding(a, b) {
  106.       return !(
  107.         a.x + a.width < b.x ||
  108.         a.x > b.x + b.width ||
  109.         a.y + a.height < b.y ||
  110.         a.y > b.y + b.height
  111.       );
  112.     }

  113.     // 显示得分和结束文字
  114.     function drawInfo() {
  115.       ctx.fillStyle = 'white';
  116.       ctx.font = '20px Arial';
  117.       ctx.fillText(`得分: ${score}`, 10, 30);

  118.       if (gameOver) {
  119.         ctx.fillStyle = 'red';
  120.         ctx.font = '48px Arial';
  121.         ctx.fillText('游戏结束', canvas.width / 2 - 100, canvas.height / 2);
  122.         ctx.font = '24px Arial';
  123.         ctx.fillText('刷新页面重新开始', canvas.width / 2 - 100, canvas.height / 2 + 40);
  124.       }
  125.     }

  126.     // 游戏主循环
  127.     let enemyTimer = 0;
  128.     function gameLoop() {
  129.       ctx.clearRect(0, 0, canvas.width, canvas.height);

  130.       if (!gameOver) {
  131.         player.move();
  132.         player.draw();

  133.         // 发射子弹
  134.         if (keys[' ']) {
  135.           if (!player.canShoot) {
  136.             const bulletX = player.x + player.width / 2 - 2.5;
  137.             const bulletY = player.y;
  138.             bullets.push(new Bullet(bulletX, bulletY));
  139.             player.canShoot = true;
  140.             setTimeout(() => {
  141.               player.canShoot = false;
  142.             }, 300); // 限制射击间隔
  143.           }
  144.         }

  145.         // 更新子弹
  146.         for (let i = bullets.length - 1; i >= 0; i--) {
  147.           const b = bullets[i];
  148.           b.update();
  149.           if (b.y + b.height < 0) {
  150.             bullets.splice(i, 1);
  151.             continue;
  152.           }
  153.           b.draw();
  154.         }

  155.         // 产生敌机
  156.         enemyTimer += 1;
  157.         if (enemyTimer > 50) {
  158.           spawnEnemy();
  159.           enemyTimer = 0;
  160.         }

  161.         // 更新敌机
  162.         for (let i = enemies.length - 1; i >= 0; i--) {
  163.           const e = enemies[i];
  164.           e.update();
  165.           if (e.y > canvas.height) {
  166.             enemies.splice(i, 1);
  167.             continue;
  168.           }
  169.           e.draw();

  170.           // 检测敌机和玩家碰撞
  171.           if (isColliding(e, player)) {
  172.             gameOver = true;
  173.           }

  174.           // 检测敌机和子弹碰撞
  175.           for (let j = bullets.length - 1; j >= 0; j--) {
  176.             if (isColliding(e, bullets[j])) {
  177.               enemies.splice(i, 1);
  178.               bullets.splice(j, 1);
  179.               score += 10;
  180.               break;
  181.             }
  182.           }
  183.         }
  184.       } else {
  185.         player.draw();
  186.       }

  187.       drawInfo();
  188.       requestAnimationFrame(gameLoop);
  189.     }

  190.     // 键盘按下监听
  191.     window.addEventListener('keydown', (e) => {
  192.       keys[e.key] = true;
  193.     });

  194.     // 键盘松开监听
  195.     window.addEventListener('keyup', (e) => {
  196.       keys[e.key] = false;
  197.       if (e.key === ' ') {
  198.         player.canShoot = false;
  199.       }
  200.     });

  201.     // 初始化
  202.     player.canShoot = false;
  203.     gameLoop();
  204.   </script>
  205. </body>
  206. </html>
复制代码


---

### 怎么玩?

- 用键盘的方向键(↑ ↓ ← →)控制你的飞机移动。
- 按空格键(Space)发射子弹打敌人。
- 拿到高分,别让敌机碰到你哦!
- 游戏结束后刷新网页可以重新开始。

---

如果你想要更漂亮的飞机或者加更多功能,可以告诉我,我再帮你改哦!祝你玩得开心!✈️????
回复 支持 反对

使用道具 举报

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

本版积分规则

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