科学教育

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

,

[复制链接]

3

主题

3

帖子

17

积分

新手上路

Rank: 1

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

使用道具 举报

0

主题

1145

帖子

1715

积分

版主

Rank: 7Rank: 7Rank: 7

积分
1715
沙发
发表于 2025-12-2 14:02:42 | 只看该作者
你好!我给你写一个简单的“飞机大战”小游戏网页代码,这个游戏里你可以用键盘控制飞机左右移动,按空格键发射子弹,打中敌机就得分。代码不复杂,很适合初学者学习哦!

你可以把下面代码复制到一个叫 `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: black;
  10.       overflow: hidden;
  11.       font-family: Arial, sans-serif;
  12.       color: white;
  13.       user-select: none; /* 防止选中文字 */
  14.     }
  15.     #gameCanvas {
  16.       display: block;
  17.       margin: 20px auto;
  18.       background: #000;
  19.       border: 2px solid white;
  20.     }
  21.     #score {
  22.       text-align: center;
  23.       font-size: 24px;
  24.       margin-bottom: 10px;
  25.     }
  26.   </style>
  27. </head>
  28. <body>
  29.   <div id="score">得分: 0</div>
  30.   <canvas id="gameCanvas" width="400" height="600"></canvas>

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

  34.     // 飞机对象
  35.     const plane = {
  36.       x: canvas.width / 2 - 20,
  37.       y: canvas.height - 60,
  38.       width: 40,
  39.       height: 60,
  40.       speed: 5,
  41.       movingLeft: false,
  42.       movingRight: false,
  43.     };

  44.     // 子弹数组
  45.     const bullets = [];

  46.     // 敌机数组
  47.     const enemies = [];

  48.     // 得分
  49.     let score = 0;

  50.     // 创建敌机函数
  51.     function createEnemy() {
  52.       // 敌机大小固定
  53.       const enemyWidth = 40;
  54.       const enemyHeight = 40;
  55.       // 随机敌机纵向位置(从顶部出现)
  56.       const x = Math.random() * (canvas.width - enemyWidth);
  57.       enemies.push({ x, y: -enemyHeight, width: enemyWidth, height: enemyHeight, speed: 2 + Math.random() * 2 });
  58.     }

  59.     // 画飞机
  60.     function drawPlane() {
  61.       ctx.fillStyle = 'skyblue';
  62.       ctx.beginPath();
  63.       // 简单飞机形状:一个三角形加一个矩形
  64.       ctx.moveTo(plane.x + plane.width / 2, plane.y);
  65.       ctx.lineTo(plane.x, plane.y + plane.height);
  66.       ctx.lineTo(plane.x + plane.width, plane.y + plane.height);
  67.       ctx.closePath();
  68.       ctx.fill();

  69.       // 飞机机身
  70.       ctx.fillRect(plane.x + plane.width / 3, plane.y + plane.height / 2, plane.width / 3, plane.height / 2);
  71.     }

  72.     // 画子弹
  73.     function drawBullets() {
  74.       ctx.fillStyle = 'yellow';
  75.       bullets.forEach(bullet => {
  76.         ctx.fillRect(bullet.x, bullet.y, bullet.width, bullet.height);
  77.       });
  78.     }

  79.     // 画敌机
  80.     function drawEnemies() {
  81.       ctx.fillStyle = 'red';
  82.       enemies.forEach(enemy => {
  83.         ctx.fillRect(enemy.x, enemy.y, enemy.width, enemy.height);
  84.       });
  85.     }

  86.     // 更新游戏状态
  87.     function update() {
  88.       // 移动飞机
  89.       if (plane.movingLeft && plane.x > 0) {
  90.         plane.x -= plane.speed;
  91.       }
  92.       if (plane.movingRight && plane.x + plane.width < canvas.width) {
  93.         plane.x += plane.speed;
  94.       }

  95.       // 移动子弹
  96.       bullets.forEach((bullet, index) => {
  97.         bullet.y -= bullet.speed;
  98.         if (bullet.y + bullet.height < 0) {
  99.           bullets.splice(index, 1);
  100.         }
  101.       });

  102.       // 移动敌机
  103.       enemies.forEach((enemy, index) => {
  104.         enemy.y += enemy.speed;
  105.         // 如果敌机到底了,游戏结束
  106.         if (enemy.y > canvas.height) {
  107.           alert('游戏结束!你的得分是:' + score);
  108.           document.location.reload();
  109.         }
  110.       });

  111.       // 检测子弹和敌机碰撞
  112.       bullets.forEach((bullet, bIndex) => {
  113.         enemies.forEach((enemy, eIndex) => {
  114.           if (
  115.             bullet.x < enemy.x + enemy.width &&
  116.             bullet.x + bullet.width > enemy.x &&
  117.             bullet.y < enemy.y + enemy.height &&
  118.             bullet.y + bullet.height > enemy.y
  119.           ) {
  120.             // 撞上了,删除敌机和子弹,加分
  121.             enemies.splice(eIndex, 1);
  122.             bullets.splice(bIndex, 1);
  123.             score += 10;
  124.             document.getElementById('score').textContent = '得分: ' + score;
  125.           }
  126.         });
  127.       });
  128.     }

  129.     // 画全部元素
  130.     function draw() {
  131.       ctx.clearRect(0, 0, canvas.width, canvas.height);

  132.       drawPlane();
  133.       drawBullets();
  134.       drawEnemies();
  135.     }

  136.     // 游戏主循环
  137.     function gameLoop() {
  138.       update();
  139.       draw();
  140.       requestAnimationFrame(gameLoop);
  141.     }

  142.     // 控制输入
  143.     window.addEventListener('keydown', e => {
  144.       if (e.key === 'ArrowLeft') {
  145.         plane.movingLeft = true;
  146.       } else if (e.key === 'ArrowRight') {
  147.         plane.movingRight = true;
  148.       } else if (e.key === ' ') {
  149.         // 空格发射子弹
  150.         bullets.push({
  151.           x: plane.x + plane.width / 2 - 2,
  152.           y: plane.y,
  153.           width: 4,
  154.           height: 10,
  155.           speed: 7,
  156.         });
  157.       }
  158.     });

  159.     window.addEventListener('keyup', e => {
  160.       if (e.key === 'ArrowLeft') {
  161.         plane.movingLeft = false;
  162.       } else if (e.key === 'ArrowRight') {
  163.         plane.movingRight = false;
  164.       }
  165.     });

  166.     // 每1.5秒创建一个敌机
  167.     setInterval(createEnemy, 1500);

  168.     // 开始游戏
  169.     gameLoop();
  170.   </script>
  171. </body>
  172. </html>
复制代码


---

### 游戏怎么玩?

- 飞机会在底部,中间的蓝色三角形;
- 用键盘左、右箭头控制飞机左右移动;
- 按空格键发射黄色子弹,子弹会往上飞;
- 红色方块是敌机,敌机会往下移;
- 子弹碰到敌机,敌机消失,你得10分;
- 敌机如果到底部了,游戏结束,会弹框显示分数。

---

你可以自己试试改改代码,比如:

- 改变飞机或敌机的颜色;
- 改变速度让游戏更难;
- 增加更炫的飞机图案。

如果你想知道怎么做这些改动,可以再问我哦!祝你编程快乐!????✈️
回复 支持 反对

使用道具 举报

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

本版积分规则

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