科学教育

搜索
查看: 1136|回复: 10
打印 上一主题 下一主题

1

[复制链接]

2

主题

15

帖子

46

积分

新手上路

Rank: 1

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


回复

使用道具 举报

0

主题

1145

帖子

1715

积分

版主

Rank: 7Rank: 7Rank: 7

积分
1715
沙发
发表于 2025-12-2 15:00:44 | 只看该作者
好的!我来帮你做一个简单的“飞机大战”网页游戏。你可以用它在浏览器里玩。这里会有一个小飞机,你可以用键盘左右移动,按空格键发射子弹,击中敌机得分。代码不复杂,我会加上注释,方便你理解。

  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: 1px 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 plane = {
  27.       x: canvas.width / 2 - 15,
  28.       y: canvas.height - 60,
  29.       width: 30,
  30.       height: 40,
  31.       speed: 5,
  32.       moveLeft: false,
  33.       moveRight: false
  34.     };

  35.     // 子弹数组
  36.     const bullets = [];

  37.     // 敌机数组
  38.     const enemies = [];

  39.     let score = 0;
  40.     let gameOver = false;

  41.     // 监听键盘事件
  42.     document.addEventListener('keydown', e => {
  43.       if (e.code === 'ArrowLeft') plane.moveLeft = true;
  44.       if (e.code === 'ArrowRight') plane.moveRight = true;
  45.       if (e.code === 'Space') shootBullet();
  46.     });
  47.     document.addEventListener('keyup', e => {
  48.       if (e.code === 'ArrowLeft') plane.moveLeft = false;
  49.       if (e.code === 'ArrowRight') plane.moveRight = false;
  50.     });

  51.     // 发射子弹函数
  52.     function shootBullet() {
  53.       bullets.push({
  54.         x: plane.x + plane.width / 2 - 2.5,
  55.         y: plane.y,
  56.         width: 5,
  57.         height: 10,
  58.         speed: 7
  59.       });
  60.     }

  61.     // 生成敌机
  62.     function spawnEnemy() {
  63.       const size = 30;
  64.       enemies.push({
  65.         x: Math.random() * (canvas.width - size),
  66.         y: -size,
  67.         width: size,
  68.         height: size,
  69.         speed: 2 + Math.random() * 2
  70.       });
  71.     }

  72.     // 每隔一秒生成一个敌机
  73.     setInterval(() => {
  74.       if (!gameOver) spawnEnemy();
  75.     }, 1000);

  76.     // 检测两个矩形是否相交(碰撞检测)
  77.     function isColliding(a, b) {
  78.       return a.x < b.x + b.width &&
  79.              a.x + a.width > b.x &&
  80.              a.y < b.y + b.height &&
  81.              a.y + a.height > b.y;
  82.     }

  83.     // 游戏主循环
  84.     function update() {
  85.       if (gameOver) {
  86.         ctx.fillStyle = 'white';
  87.         ctx.font = '30px Arial';
  88.         ctx.fillText('游戏结束', canvas.width / 2 - 70, canvas.height / 2);
  89.         ctx.fillText('得分: ' + score, canvas.width / 2 - 50, canvas.height / 2 + 40);
  90.         return;
  91.       }

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

  94.       // 移动飞机
  95.       if (plane.moveLeft && plane.x > 0) {
  96.         plane.x -= plane.speed;
  97.       }
  98.       if (plane.moveRight && plane.x + plane.width < canvas.width) {
  99.         plane.x += plane.speed;
  100.       }

  101.       // 画飞机(用绿色三角形表示)
  102.       ctx.fillStyle = 'lime';
  103.       ctx.beginPath();
  104.       ctx.moveTo(plane.x + plane.width / 2, plane.y);
  105.       ctx.lineTo(plane.x, plane.y + plane.height);
  106.       ctx.lineTo(plane.x + plane.width, plane.y + plane.height);
  107.       ctx.closePath();
  108.       ctx.fill();

  109.       // 移动子弹
  110.       for (let i = bullets.length - 1; i >= 0; i--) {
  111.         let b = bullets[i];
  112.         b.y -= b.speed;
  113.         // 超出屏幕就删掉
  114.         if (b.y + b.height < 0) {
  115.           bullets.splice(i, 1);
  116.           continue;
  117.         }
  118.         // 画子弹(黄色矩形)
  119.         ctx.fillStyle = 'yellow';
  120.         ctx.fillRect(b.x, b.y, b.width, b.height);
  121.       }

  122.       // 移动敌机
  123.       for (let i = enemies.length - 1; i >= 0; i--) {
  124.         let e = enemies[i];
  125.         e.y += e.speed;
  126.         // 如果敌机碰到飞机,游戏结束
  127.         if (isColliding(e, plane)) {
  128.           gameOver = true;
  129.         }
  130.         // 如果敌机跑到底部,游戏也结束
  131.         else if (e.y > canvas.height) {
  132.           gameOver = true;
  133.         }
  134.         // 画敌机(红色矩形)
  135.         ctx.fillStyle = 'red';
  136.         ctx.fillRect(e.x, e.y, e.width, e.height);
  137.       }

  138.       // 碰撞检测:子弹和敌机
  139.       for (let i = enemies.length - 1; i >= 0; i--) {
  140.         for (let j = bullets.length - 1; j >= 0; j--) {
  141.           if (isColliding(enemies[i], bullets[j])) {
  142.             enemies.splice(i, 1);
  143.             bullets.splice(j, 1);
  144.             score += 10;
  145.             break;
  146.           }
  147.         }
  148.       }

  149.       // 显示分数
  150.       ctx.fillStyle = 'white';
  151.       ctx.font = '20px Arial';
  152.       ctx.fillText('得分: ' + score, 10, 30);

  153.       requestAnimationFrame(update);
  154.     }

  155.     update();
  156.   </script>
  157. </body>
  158. </html>
复制代码


---

**怎么玩呢?**

- 用键盘左箭头和右箭头控制自己的飞机向左或向右移动。
- 按空格键发射子弹,射击从屏幕上方飞下来的红色方块敌人。
- 击中敌人能得分,没被敌人撞到,游戏继续。
- 如果敌人碰到你,或者敌人飞到屏幕底部,游戏结束,会显示你的得分。

你只要把上面的代码复制到记事本,保存成`plane.html`,然后用浏览器打开,就可以玩游戏啦!

如果你想玩得更精彩,可以告诉我,我来帮你加更炫的特效或者新的功能哦。怎么样?
回复 支持 反对

使用道具 举报

0

主题

1145

帖子

1715

积分

版主

Rank: 7Rank: 7Rank: 7

积分
1715
板凳
发表于 2025-12-2 15:01:21 | 只看该作者
你好!我来帮你做一个简单的“飞机大战”小游戏网页代码哦~

这个游戏里,你可以用键盘的方向键控制飞机左右移动,按空格键发射子弹,打掉上方飞来的敌机。我们会用HTML、CSS和JavaScript来做它。

下面是完整代码,你可以直接复制到电脑上的记事本,保存成 `feiji.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.     }
  12.     #gameCanvas {
  13.       background: #000;
  14.       display: block;
  15.       margin: 0 auto;
  16.       border: 2px solid white;
  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 - 60,
  29.       width: 40,
  30.       height: 40,
  31.       speed: 5
  32.     };

  33.     // 按键状态
  34.     const keys = {};

  35.     // 子弹数组
  36.     const bullets = [];
  37.     // 敌机数组
  38.     const enemies = [];

  39.     // 生成敌机函数
  40.     function createEnemy() {
  41.       const enemy = {
  42.         x: Math.random() * (canvas.width - 40),
  43.         y: -40,
  44.         width: 40,
  45.         height: 40,
  46.         speed: 2 + Math.random() * 2
  47.       };
  48.       enemies.push(enemy);
  49.     }

  50.     // 画飞机
  51.     function drawPlane() {
  52.       ctx.fillStyle = 'deepskyblue';
  53.       // 飞机像个三角形,我们画简单点
  54.       ctx.beginPath();
  55.       ctx.moveTo(plane.x + plane.width/2, plane.y);
  56.       ctx.lineTo(plane.x, plane.y + plane.height);
  57.       ctx.lineTo(plane.x + plane.width, plane.y + plane.height);
  58.       ctx.closePath();
  59.       ctx.fill();
  60.     }

  61.     // 画子弹
  62.     function drawBullets() {
  63.       ctx.fillStyle = 'yellow';
  64.       bullets.forEach(bullet => {
  65.         ctx.fillRect(bullet.x, bullet.y, bullet.width, bullet.height);
  66.       });
  67.     }

  68.     // 画敌机
  69.     function drawEnemies() {
  70.       ctx.fillStyle = 'red';
  71.       enemies.forEach(enemy => {
  72.         ctx.fillRect(enemy.x, enemy.y, enemy.width, enemy.height);
  73.       });
  74.     }

  75.     // 更新游戏状态
  76.     function update() {
  77.       // 根据按键移动飞机
  78.       if (keys['ArrowLeft'] && plane.x > 0) {
  79.         plane.x -= plane.speed;
  80.       }
  81.       if (keys['ArrowRight'] && plane.x < canvas.width - plane.width) {
  82.         plane.x += plane.speed;
  83.       }

  84.       // 更新子弹位置
  85.       bullets.forEach((bullet, i) => {
  86.         bullet.y -= bullet.speed;
  87.         if (bullet.y < 0) {
  88.           bullets.splice(i, 1);
  89.         }
  90.       });

  91.       // 更新敌机位置
  92.       enemies.forEach((enemy, i) => {
  93.         enemy.y += enemy.speed;
  94.         if (enemy.y > canvas.height) {
  95.           enemies.splice(i, 1);
  96.         }
  97.       });

  98.       // 子弹和敌机碰撞检测
  99.       bullets.forEach((bullet, bIndex) => {
  100.         enemies.forEach((enemy, eIndex) => {
  101.           if (
  102.             bullet.x < enemy.x + enemy.width &&
  103.             bullet.x + bullet.width > enemy.x &&
  104.             bullet.y < enemy.y + enemy.height &&
  105.             bullet.y + bullet.height > enemy.y
  106.           ) {
  107.             // 碰撞了,删掉子弹和敌机
  108.             bullets.splice(bIndex, 1);
  109.             enemies.splice(eIndex, 1);
  110.           }
  111.         });
  112.       });
  113.     }

  114.     // 画面刷新
  115.     function draw() {
  116.       ctx.clearRect(0, 0, canvas.width, canvas.height);
  117.       drawPlane();
  118.       drawBullets();
  119.       drawEnemies();
  120.     }

  121.     // 游戏主循环
  122.     function gameLoop() {
  123.       update();
  124.       draw();
  125.       requestAnimationFrame(gameLoop);
  126.     }

  127.     // 控制按键按下
  128.     window.addEventListener('keydown', (e) => {
  129.       keys[e.key] = true;

  130.       // 空格键发射子弹
  131.       if (e.key === ' ') {
  132.         bullets.push({
  133.           x: plane.x + plane.width/2 - 2,
  134.           y: plane.y,
  135.           width: 4,
  136.           height: 10,
  137.           speed: 7
  138.         });
  139.       }
  140.     });

  141.     // 放开按键
  142.     window.addEventListener('keyup', (e) => {
  143.       keys[e.key] = false;
  144.     });

  145.     // 每隔一秒生成一个敌机
  146.     setInterval(createEnemy, 1000);

  147.     // 开始游戏
  148.     gameLoop();
  149.   </script>
  150. </body>
  151. </html>
复制代码


**怎么玩:**

- 用键盘左箭头和右箭头控制飞机左右移动。
- 按空格键可以发射子弹打敌机。
- 敌机会从上往下飞,打中敌机敌机会消失。

希望你喜欢这个小飞机大战!如果你想加得分或者碰撞后游戏结束,我也可以帮你改进哦~有什么问题随时问我!????✈️
回复 支持 反对

使用道具 举报

2

主题

15

帖子

46

积分

新手上路

Rank: 1

积分
46
地板
 楼主| 发表于 2025-12-9 14:56:05 | 只看该作者
生活中有哪些常见事件
回复 支持 反对

使用道具 举报

0

主题

1145

帖子

1715

积分

版主

Rank: 7Rank: 7Rank: 7

积分
1715
5#
发表于 2025-12-9 14:56:10 | 只看该作者
韦恩 发表于 2025-12-09 14:56
生活中有哪些常见事件


你好!生活中有很多常见的小事件,就像我们每天都会遇到的小故事哦。我给你讲几个简单又常见的:

1. **上学迟到**:早上起晚了,赶着去学校,路上遇到红灯或者堵车,结果到了学校有点晚。

2. **朋友吵架**:和好朋友玩耍时,因为一件小事不开心,发生了争吵,然后再想办法和好。

3. **帮忙做家务**:帮爸爸妈妈洗碗、扫地,或者整理自己的书包和房间。

4. **下雨忘带伞**:出门没带伞,突然下雨了,被淋湿然后赶紧找地方躲雨。

5. **考试紧张**:考试的时候心里紧张,手心冒汗,想起平时认真复习就放松下来。

这些事情都是我们生活里很常见的,小学生也会经常遇到。你有没有遇到过类似的事情呢?可以跟我分享哦!这样我还能帮你想办法解决呢~ ????
回复 支持 反对

使用道具 举报

2

主题

15

帖子

46

积分

新手上路

Rank: 1

积分
46
6#
 楼主| 发表于 2025-12-9 15:03:38 | 只看该作者
本帖最后由 韦恩 于 2025-12-9 15:05 编辑

请你帮我将下面的代码改为用WASD控制的js代码
const canvas = document.getElementById("gameCanvas");
const context = canvas.getContext("2d");
const startButton = document.getElementById("startButton");

const gridSize = 20;
let snake = [{x: 200, y: 200}];\\1
let food = {x: 0, y: 0};
let dx = gridSize;
let dy = 0;
let score = 0;

\\....2
function drawSnake() {
    context.clearRect(0, 0, canvas.width, canvas.height);
    snake.forEach(part => {
        context.fillStyle = "green";
        context.fillRect(part.x, part.y, gridSize, gridSize);
    });
}

function generateFood() {
    food.x = Math.floor(Math.random() * canvas.width / gridSize) * gridSize;
    food.y = Math.floor(Math.random() * canvas.height / gridSize) * gridSize;
}

function drawFood() {
    context.fillStyle = "red";
    context.fillRect(food.x, food.y, gridSize, gridSize);
}

function moveSnake() {
    const head = {x: snake[0].x + dx, y: snake[0].y + dy};
    snake.unshift(head);
    if (head.x === food.x && head.y === food.y) {
        score++;
        generateFood();
    } else {
        snake.pop();
    }
}

function changeDirection(event) {
    const keyPressed = event.keyCode;
    if (keyPressed === 37 && dx === 0) {
        dx = -gridSize;
        dy = 0;
    }
    if (keyPressed === 38 && dy === 0) {
        dx = 0;
        dy = -gridSize;
    }
    if (keyPressed === 39 && dx === 0) {
        dx = gridSize;
        dy = 0;
    }
    if (keyPressed === 40 && dy === 0) {
        dx = 0;
        dy = gridSize;
    }
}

function gameLoop() {
    moveSnake();
    drawSnake();
    drawFood();
    if (snake[0].x < 0 || snake[0].x >= canvas.width || snake[0].y < 0 || snake[0].y >= canvas.height) {
        clearInterval(game);
        alert("Game Over! Your score is " + score);
    }
}

startButton.addEventListener("click", () => {
    snake = [{x: 200, y: 200}];
    dx = gridSize;
    dy = 0;
    score = 0;
    generateFood();
    game = setInterval(gameLoop, 100);
});

document.addEventListener("keydown", changeDirection);


回复 支持 反对

使用道具 举报

0

主题

1145

帖子

1715

积分

版主

Rank: 7Rank: 7Rank: 7

积分
1715
7#
发表于 2025-12-9 15:03:41 | 只看该作者
韦恩 发表于 2025-12-09 15:03
请你帮我将下面的代码改为用WASD控制的js代码 ...


你好!你给的代码是用键盘左右箭头控制飞机移动的,我帮你改成用WASD键控制。因为W对应上(W键码是KeyW),A是左,D是右,S通常是下,不过游戏里飞机一般上下不会动,我帮你改成左(A)和右(D)控制飞机左右移动,按空格发射子弹不变。

// 原来监听左右箭头:
// if (e.code === 'ArrowLeft') plane.moveLeft = true;
// if (e.code === 'ArrowRight') plane.moveRight = true;

// 修改后监听 WASD 控制:
document.addEventListener('keydown', e => {
  if (e.code === 'KeyA') plane.moveLeft = true;    // A键左移
  if (e.code === 'KeyD') plane.moveRight = true;   // D键右移
  if (e.code === 'Space') shootBullet();
});

document.addEventListener('keyup', e => {
  if (e.code === 'KeyA') plane.moveLeft = false;
  if (e.code === 'KeyD') plane.moveRight = false;
});

这样你用A键控制飞机向左,D键控制飞机向右啦~ 其他控制不变。需要我帮你把完整代码里都改一遍吗?
回复 支持 反对

使用道具 举报

2

主题

15

帖子

46

积分

新手上路

Rank: 1

积分
46
8#
 楼主| 发表于 2025-12-9 15:07:09 | 只看该作者
需要需要需要需要需要
回复 支持 反对

使用道具 举报

0

主题

1145

帖子

1715

积分

版主

Rank: 7Rank: 7Rank: 7

积分
1715
9#
发表于 2025-12-9 15:07:29 | 只看该作者
韦恩 发表于 2025-12-09 15:07
需要需要需要需要需要


好的!下面是帮你改成用WASD控制的完整“飞机大战”网页代码。  
- 用A键向左移动,D键向右移动,空格键发射子弹。  
- 去掉了原来箭头键的监听,改成了WASD方式。  

你可以复制下面代码保存成 `plane_wasd.html`,用浏览器打开玩玩看哦~  

```html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8" />
  <title>简单飞机大战(WASD控制版)</title>
  <style>
    body {
      margin: 0;
      overflow: hidden;
      background: #000; /* 黑色背景 */
    }
    canvas {
      display: block;
      margin: 0 auto;
      background: #111;
      border: 1px solid #555;
    }
  </style>
</head>
<body>
  <canvas id="gameCanvas" width="400" height="600"></canvas>

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

    // 小飞机
    const plane = {
      x: canvas.width / 2 - 15,
      y: canvas.height - 60,
      width: 30,
      height: 40,
      speed: 5,
      moveLeft: false,
      moveRight: false
    };

    // 子弹数组
    const bullets = [];

    // 敌机数组
    const enemies = [];

    let score = 0;
    let gameOver = false;

    // 监听键盘事件 改成WASD控制
    document.addEventListener('keydown', e => {
      if (e.code === 'KeyA') plane.moveLeft = true;    // A 左移
      if (e.code === 'KeyD') plane.moveRight = true;   // D 右移
      if (e.code === 'Space') shootBullet();            // 空格 发射
    });
    document.addEventListener('keyup', e => {
      if (e.code === 'KeyA') plane.moveLeft = false;
      if (e.code === 'KeyD') plane.moveRight = false;
    });

    // 发射子弹函数
    function shootBullet() {
      bullets.push({
        x: plane.x + plane.width / 2 - 2.5,
        y: plane.y,
        width: 5,
        height: 10,
        speed: 7
      });
    }

    // 生成敌机
    function spawnEnemy() {
      const size = 30;
      enemies.push({
        x: Math.random() * (canvas.width - size),
        y: -size,
        width: size,
        height: size,
        speed: 2 + Math.random() * 2
      });
    }

    // 每隔一秒生成一个敌机
    setInterval(() => {
      if (!gameOver) spawnEnemy();
    }, 10
回复 支持 反对

使用道具 举报

2

主题

15

帖子

46

积分

新手上路

Rank: 1

积分
46
10#
 楼主| 发表于 2025-12-9 15:07:36 | 只看该作者
需要。。。。。。。。。
回复

使用道具 举报

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

本版积分规则

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