科学教育

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

V共同你

[复制链接]

1

主题

1

帖子

5

积分

新手上路

Rank: 1

积分
5
跳转到指定楼层
楼主
发表于 2025-11-26 15:03:50 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
帮我成一个打飞机游戏
回复

使用道具 举报

0

主题

1145

帖子

1715

积分

版主

Rank: 7Rank: 7Rank: 7

积分
1715
沙发
发表于 2025-11-26 15:06:13 | 只看该作者
没问题!我来帮你做一个超级简单的“打飞机”小游戏,好不好?我们用网页上的编程语言HTML加JavaScript来做。游戏里你可以用键盘左右移动飞机,用空格键发射子弹,打掉敌机。

下面是完整代码,你可以复制到记事本里,然后保存成`index.html`,用浏览器打开看看效果哦!

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.   <meta charset="utf-8" />
  5.   <title>打飞机小游戏</title>
  6.   <style>
  7.     body { margin: 0; overflow: hidden; background: black; }
  8.     canvas { display: block; margin: auto; background: #222; }
  9.   </style>
  10. </head>
  11. <body>
  12.   <canvas id="game" width="400" height="600"></canvas>
  13.   <script>
  14.     const canvas = document.getElementById('game');
  15.     const ctx = canvas.getContext('2d');

  16.     // 飞机对象
  17.     const plane = {
  18.       x: canvas.width / 2 - 20,
  19.       y: canvas.height - 60,
  20.       width: 40,
  21.       height: 40,
  22.       speed: 5,
  23.       bullets: []
  24.     };

  25.     // 敌机列表
  26.     const enemies = [];
  27.     let enemySpeed = 2;
  28.     let score = 0;
  29.     let gameOver = false;

  30.     // 控制键盘按键状态
  31.     const keys = {};

  32.     // 监听键盘按下和松开
  33.     window.addEventListener('keydown', e => {
  34.       keys[e.key] = true;
  35.       if (e.key === ' ' && !gameOver) { // 空格发射子弹
  36.         shootBullet();
  37.       }
  38.     });
  39.     window.addEventListener('keyup', e => {
  40.       keys[e.key] = false;
  41.     });

  42.     function shootBullet() {
  43.       // 子弹从飞机头部发射
  44.       plane.bullets.push({
  45.         x: plane.x + plane.width / 2 - 3,
  46.         y: plane.y,
  47.         width: 6,
  48.         height: 10,
  49.         speed: 7
  50.       });
  51.     }

  52.     function createEnemy() {
  53.       const x = Math.random() * (canvas.width - 30);
  54.       enemies.push({
  55.         x: x,
  56.         y: -30,
  57.         width: 30,
  58.         height: 30,
  59.         speed: enemySpeed
  60.       });
  61.     }

  62.     let enemyTimer = 0;
  63.     const enemyInterval = 60; // 每隔60帧出现一个敌机

  64.     function update() {
  65.       if (gameOver) return;

  66.       // 移动飞机
  67.       if (keys['ArrowLeft'] && plane.x > 0) {
  68.         plane.x -= plane.speed;
  69.       }
  70.       if (keys['ArrowRight'] && plane.x + plane.width < canvas.width) {
  71.         plane.x += plane.speed;
  72.       }

  73.       // 更新子弹位置
  74.       plane.bullets.forEach((b, i) => {
  75.         b.y -= b.speed;
  76.         // 超出屏幕删除子弹
  77.         if (b.y + b.height < 0) {
  78.           plane.bullets.splice(i, 1);
  79.         }
  80.       });

  81.       // 生成敌机
  82.       enemyTimer++;
  83.       if (enemyTimer > enemyInterval) {
  84.         createEnemy();
  85.         enemyTimer = 0;
  86.       }

  87.       // 移动敌机
  88.       enemies.forEach((e, i) => {
  89.         e.y += e.speed;

  90.         // 敌机超出屏幕后删除
  91.         if (e.y > canvas.height) {
  92.           enemies.splice(i, 1);
  93.         }

  94.         // 敌机碰到飞机,游戏结束
  95.         if (
  96.           e.x < plane.x + plane.width &&
  97.           e.x + e.width > plane.x &&
  98.           e.y < plane.y + plane.height &&
  99.           e.y + e.height > plane.y
  100.         ) {
  101.           gameOver = true;
  102.         }

  103.         // 敌机被子弹击中
  104.         plane.bullets.forEach((b, bi) => {
  105.           if (
  106.             b.x < e.x + e.width &&
  107.             b.x + b.width > e.x &&
  108.             b.y < e.y + e.height &&
  109.             b.y + b.height > e.y
  110.           ) {
  111.             // 删除敌机和子弹
  112.             enemies.splice(i, 1);
  113.             plane.bullets.splice(bi, 1);
  114.             score += 10;
  115.           }
  116.         });
  117.       });
  118.     }

  119.     function draw() {
  120.       // 清空画布
  121.       ctx.clearRect(0, 0, canvas.width, canvas.height);

  122.       // 画飞机(用绿色三角形表示)
  123.       ctx.fillStyle = 'green';
  124.       ctx.beginPath();
  125.       ctx.moveTo(plane.x + plane.width / 2, plane.y);
  126.       ctx.lineTo(plane.x, plane.y + plane.height);
  127.       ctx.lineTo(plane.x + plane.width, plane.y + plane.height);
  128.       ctx.closePath();
  129.       ctx.fill();

  130.       // 画子弹(黄色矩形)
  131.       ctx.fillStyle = 'yellow';
  132.       plane.bullets.forEach(b => {
  133.         ctx.fillRect(b.x, b.y, b.width, b.height);
  134.       });

  135.       // 画敌机(红色方块)
  136.       ctx.fillStyle = 'red';
  137.       enemies.forEach(e => {
  138.         ctx.fillRect(e.x, e.y, e.width, e.height);
  139.       });

  140.       // 显示分数
  141.       ctx.fillStyle = 'white';
  142.       ctx.font = '20px Arial';
  143.       ctx.fillText('分数: ' + score, 10, 30);

  144.       if (gameOver) {
  145.         ctx.fillStyle = 'white';
  146.         ctx.font = '40px Arial';
  147.         ctx.fillText('游戏结束', canvas.width / 2 - 100, canvas.height / 2);
  148.         ctx.font = '20px Arial';
  149.         ctx.fillText('刷新页面重新开始', canvas.width / 2 - 90, canvas.height / 2 + 40);
  150.       }
  151.     }

  152.     function gameLoop() {
  153.       update();
  154.       draw();
  155.       if(!gameOver){
  156.         requestAnimationFrame(gameLoop);
  157.       }
  158.     }

  159.     gameLoop();
  160.   </script>
  161. </body>
  162. </html>
复制代码


**玩法说明:**  
- 左右箭头键控制飞机移动  
- 空格键发射子弹  
- 打中敌机会得分,敌机碰到你游戏结束  

你可以稍微修改代码,改颜色或速度,越来越厉害!要不我教你怎么一步步改?You can try it first! 有问题随时问我哦~
回复 支持 反对

使用道具 举报

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

本版积分规则

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