科学教育

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

朱语汐

[复制链接]

2

主题

4

帖子

18

积分

新手上路

Rank: 1

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

使用道具 举报

0

主题

1145

帖子

1715

积分

版主

Rank: 7Rank: 7Rank: 7

积分
1715
沙发
发表于 2025-11-26 15:00:27 | 只看该作者
好的!我帮你做一个简单的“飞机大战”网页游戏,里面有一架小飞机,可以用方向键移动,按空格键发射子弹,敌机会从上方飞下来,你要用子弹打中它们。代码会包含HTML、CSS和JavaScript,直接复制到一个`.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: #000;
  11.     }
  12.     canvas {
  13.       display: block;
  14.       margin: 0 auto;
  15.       background: #111;
  16.       border: 2px solid #444;
  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 plane = {
  27.       x: canvas.width / 2 - 20,
  28.       y: canvas.height - 80,
  29.       width: 40,
  30.       height: 60,
  31.       speed: 5,
  32.       movingLeft: false,
  33.       movingRight: false,
  34.       movingUp: false,
  35.       movingDown: false,
  36.     };

  37.     // 子弹数组
  38.     const bullets = [];

  39.     // 敌机数组
  40.     const enemies = [];
  41.     let enemySpawnTimer = 0; // 生成敌机计时

  42.     // 分数
  43.     let score = 0;
  44.     let gameOver = false;

  45.     // 监听键盘
  46.     document.addEventListener('keydown', (e) => {
  47.       if (e.code === 'ArrowLeft') plane.movingLeft = true;
  48.       else if (e.code === 'ArrowRight') plane.movingRight = true;
  49.       else if (e.code === 'ArrowUp') plane.movingUp = true;
  50.       else if (e.code === 'ArrowDown') plane.movingDown = true;
  51.       else if (e.code === 'Space') shootBullet();
  52.     });

  53.     document.addEventListener('keyup', (e) => {
  54.       if (e.code === 'ArrowLeft') plane.movingLeft = false;
  55.       else if (e.code === 'ArrowRight') plane.movingRight = false;
  56.       else if (e.code === 'ArrowUp') plane.movingUp = false;
  57.       else if (e.code === 'ArrowDown') plane.movingDown = false;
  58.     });

  59.     // 发射子弹
  60.     function shootBullet() {
  61.       if (gameOver) return;
  62.       bullets.push({
  63.         x: plane.x + plane.width / 2 - 3,
  64.         y: plane.y,
  65.         width: 6,
  66.         height: 12,
  67.         speed: 7,
  68.       });
  69.     }

  70.     // 生成敌机
  71.     function spawnEnemy() {
  72.       const size = 40;
  73.       enemies.push({
  74.         x: Math.random() * (canvas.width - size),
  75.         y: -size,
  76.         width: size,
  77.         height: size,
  78.         speed: 2 + Math.random() * 1.5,
  79.       });
  80.     }

  81.     // 碰撞检测
  82.     function isColliding(a, b) {
  83.       return !(
  84.         a.x + a.width < b.x ||
  85.         a.x > b.x + b.width ||
  86.         a.y + a.height < b.y ||
  87.         a.y > b.y + b.height
  88.       );
  89.     }

  90.     // 更新游戏状态
  91.     function update() {
  92.       if (gameOver) return;

  93.       // 移动飞机
  94.       if (plane.movingLeft && plane.x > 0) plane.x -= plane.speed;
  95.       if (plane.movingRight && plane.x < canvas.width - plane.width) plane.x += plane.speed;
  96.       if (plane.movingUp && plane.y > 0) plane.y -= plane.speed;
  97.       if (plane.movingDown && plane.y < canvas.height - plane.height) plane.y += plane.speed;

  98.       // 移动子弹
  99.       bullets.forEach((b, i) => {
  100.         b.y -= b.speed;
  101.         if (b.y + b.height < 0) {
  102.           bullets.splice(i, 1);
  103.         }
  104.       });

  105.       // 生成敌机
  106.       enemySpawnTimer++;
  107.       if (enemySpawnTimer > 50) { // 每50帧生成一个敌机
  108.         spawnEnemy();
  109.         enemySpawnTimer = 0;
  110.       }

  111.       // 移动敌机
  112.       enemies.forEach((e, i) => {
  113.         e.y += e.speed;
  114.         if (e.y > canvas.height) {
  115.           enemies.splice(i, 1);
  116.           // 敌机到底部,游戏结束
  117.           gameOver = true;
  118.         }
  119.       });

  120.       // 子弹和敌机碰撞检测
  121.       bullets.forEach((b, bi) => {
  122.         enemies.forEach((e, ei) => {
  123.           if (isColliding(b, e)) {
  124.             bullets.splice(bi, 1);
  125.             enemies.splice(ei, 1);
  126.             score += 10;
  127.           }
  128.         });
  129.       });

  130.       // 敌机和飞机碰撞检测
  131.       enemies.forEach((e) => {
  132.         if (isColliding(e, plane)) {
  133.           gameOver = true;
  134.         }
  135.       });
  136.     }

  137.     // 画飞机(用三角形表示)
  138.     function drawPlane() {
  139.       ctx.fillStyle = '#00f';
  140.       ctx.beginPath();
  141.       ctx.moveTo(plane.x + plane.width/2, plane.y);
  142.       ctx.lineTo(plane.x, plane.y + plane.height);
  143.       ctx.lineTo(plane.x + plane.width, plane.y + plane.height);
  144.       ctx.closePath();
  145.       ctx.fill();
  146.     }

  147.     // 画子弹
  148.     function drawBullets() {
  149.       ctx.fillStyle = '#ff0';
  150.       bullets.forEach(b => {
  151.         ctx.fillRect(b.x, b.y, b.width, b.height);
  152.       });
  153.     }

  154.     // 画敌机(用红色矩形表示)
  155.     function drawEnemies() {
  156.       ctx.fillStyle = '#f00';
  157.       enemies.forEach(e => {
  158.         ctx.fillRect(e.x, e.y, e.width, e.height);
  159.       });
  160.     }

  161.     // 画分数和游戏结束提示
  162.     function drawUI() {
  163.       ctx.fillStyle = '#fff';
  164.       ctx.font = '20px Arial';
  165.       ctx.fillText('分数: ' + score, 10, 30);

  166.       if (gameOver) {
  167.         ctx.fillStyle = 'rgba(0,0,0,0.7)';
  168.         ctx.fillRect(0, canvas.height/2 - 40, canvas.width, 80);
  169.         ctx.fillStyle = '#fff';
  170.         ctx.font = '36px Arial';
  171.         ctx.textAlign = 'center';
  172.         ctx.fillText('游戏结束', canvas.width/2, canvas.height/2);
  173.         ctx.font = '20px Arial';
  174.         ctx.fillText('刷新页面重新开始', canvas.width/2, canvas.height/2 + 30);
  175.       }
  176.     }

  177.     // 游戏主循环
  178.     function loop() {
  179.       ctx.clearRect(0, 0, canvas.width, canvas.height);
  180.       update();
  181.       drawPlane();
  182.       drawBullets();
  183.       drawEnemies();
  184.       drawUI();
  185.       requestAnimationFrame(loop);
  186.     }

  187.     loop();
  188.   </script>
  189. </body>
  190. </html>
复制代码


### 怎么玩?
- 用键盘的 **方向键** 上下左右移动飞机
- 按 **空格键** 发射子弹
- 打中红色的敌机得分
- 如果敌机碰到你或者敌机飞到屏幕底部,游戏结束
- 想再玩就刷新网页

如果你想,让我帮你加更多功能,比如更炫的图片、音效或者更难的关卡,也可以告诉我哦!祝你游戏快乐!????✈️
回复 支持 反对

使用道具 举报

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

本版积分规则

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