科学教育

搜索
楼主: ZLJ
打印 上一主题 下一主题

王桃桃

[复制链接]

29

主题

107

帖子

348

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
348
11#
 楼主| 发表于 2024-7-28 11:18:40 | 只看该作者
kaia 发表于 2024-7-28 11:12
当然可以!我们可以通过加载图像并在绘制玩家和敌人的时候替换方块来实现这个功能。下面是具体步骤:

## ...

你好,怎么样可以修改玩家图标的旋转角度?
回复 支持 反对

使用道具 举报

0

主题

1147

帖子

1718

积分

版主

Rank: 7Rank: 7Rank: 7

积分
1718
12#
发表于 2024-7-28 11:19:10 | 只看该作者
ZLJ 发表于 2024-07-28 11:18
你好,怎么样可以修改玩家图标的旋转角度? ...


好的,修改图片的旋转角度确实能让你的游戏看起来更加动态!我们可以通过Canvas的`rotate`方法来实现这个效果,但在使用之前需要一些小技巧,因为`rotate`方法会影响到整个Canvas的绘图状态,所以我们需要保存和恢复绘图状态。

### 实现步骤:
1. **保存上下文状态(`ctx.save()`)**:在开始旋转之前。
2. **平移原点(`ctx.translate`)**:将原点移动到玩家图片的中心,这样旋转时是围绕中心点进行的,而不是默认的左上角。
3. **旋转画布(`ctx.rotate`)**:设置旋转的角度。
4. **绘制图像(`ctx.drawImage`)**:在旋转后的坐标系中绘制图像。
5. **恢复上下文状态(`ctx.restore`)**:防止旋转影响到后续的绘图。

下面是修改后的`drawPlayer`函数,添加了旋转功能:

  1. function drawPlayer(angle) {
  2.     ctx.save(); // 保存当前绘图状态
  3.     ctx.translate(playerX + playerWidth / 2, playerY + playerHeight / 2); // 将原点移动到玩家中心
  4.     ctx.rotate(angle * Math.PI / 180); // 旋转画布,参数为角度,这里将角度转换成弧度
  5.     ctx.drawImage(playerImg, -playerWidth / 2, -playerHeight / 2, playerWidth, playerHeight); // 绘制玩家图片,注意位置偏移
  6.     ctx.restore(); // 恢复到之前的绘图状态
  7. }
复制代码


在你的`update`函数中,需要为`drawPlayer`函数传递一个角度值。例如,如果你想每帧都增加5度的旋转,可以这样:

  1. let playerAngle = 0;

  2. function update() {
  3.     ctx.clearRect(0, 0, canvas.width, canvas.height);
  4.     drawPlayer(playerAngle); // 传入当前角度
  5.     drawBullets();
  6.     drawEnemies();
  7.     detectCollisions();
  8.     generateEnemies();
  9.    
  10.     playerAngle += 5; // 每帧增加5度
  11.     requestAnimationFrame(update);
  12. }

  13. update();
复制代码


当然,你可以根据需求调整旋转角度的变化,比如让玩家按键控制旋转方向。假如你希望在按下左右箭头时,分别逆时针或顺时针旋转,可以在`movePlayer`函数中做如下修改:

  1. let playerAngle = 0;

  2. function movePlayer(e) {
  3.     if (e.key === 'ArrowLeft') {
  4.         playerAngle -= 5; // 向左旋转5度
  5.     }
  6.     if (e.key === 'ArrowRight') {
  7.         playerAngle += 5; // 向右旋转5度
  8.     }
  9.     if (e.key === 'ArrowUp' && playerX > 0) {
  10.         playerX -= playerSpeed;
  11.     }
  12.     if (e.key === 'ArrowDown' && playerX < canvas.width - playerWidth) {
  13.         playerX += playerSpeed;
  14.     }
  15. }
复制代码


记得把之前`update`函数中的角度自动增加删除掉,以免影响用户输入。

这样一来,玩家的飞机就能根据你的设定旋转啦。如果有任何其他需求或者进一步的改进方向,请随时告诉我哦!
回复 支持 反对

使用道具 举报

29

主题

107

帖子

348

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
348
13#
 楼主| 发表于 2024-7-28 11:21:58 | 只看该作者
kaia 发表于 2024-7-28 11:12
当然可以!我们可以通过加载图像并在绘制玩家和敌人的时候替换方块来实现这个功能。下面是具体步骤:

## ...

你好,由于我的图标是倾斜的,怎么样可以给我的玩家图标旋转一定角度,使其符合正常方向呢?
回复 支持 反对

使用道具 举报

0

主题

1147

帖子

1718

积分

版主

Rank: 7Rank: 7Rank: 7

积分
1718
14#
发表于 2024-7-28 11:23:07 | 只看该作者
ZLJ 发表于 2024-07-28 11:21
你好,由于我的图标是倾斜的,怎么样可以给我的玩家图 ...


如果你的玩家飞机图标是倾斜的,而你希望其显示为直立状态以符合游戏中正常的方向,那么你可以指定一个固定的旋转角度来调整这个图标。就像之前解释的一样,我们会使用`ctx.rotate`方法来旋转画布,然后绘制图像在新的旋转坐标系中。

假设你的图标向右倾斜了45度,那么我们需要向左旋转45度来使其正常显示。下面是如何做的代码示例:

  1. function drawPlayer() {
  2.     ctx.save(); // 保存当前绘图状态
  3.     ctx.translate(playerX + playerWidth / 2, playerY + playerHeight / 2); // 将原点移动到玩家中心
  4.     ctx.rotate(-45 * Math.PI / 180); // 旋转画布,参数为角度的负值,这里将-45度角度转换成弧度
  5.     ctx.drawImage(playerImg, -playerWidth / 2, -playerHeight / 2, playerWidth, playerHeight); // 绘制玩家图片,注意位置偏移
  6.     ctx.restore(); // 恢复到之前的绘图状态
  7. }
复制代码


如果你的图标原始倾斜不是45度,比如是30度、60度等,那么相应地调整上述代码中`rotate`方法参数中的角度即可。

记住,任何时候使用`rotate`之前,都要先用`translate`将原点移到需要旋转物体的中心,以确保旋转是围绕自身中心进行的。完成旋转后,使用`restore`来恢复Canvas状态到旋转之前,以免影响到后续绘制其他元素。

现在你的飞机图标应该会按照预期的方向直立显示在游戏中了。尝试一下吧,如有疑问或者进一步的需求,欢迎继续提问!
回复 支持 反对

使用道具 举报

29

主题

107

帖子

348

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
348
15#
 楼主| 发表于 2024-7-28 11:27:44 | 只看该作者
kaia 发表于 2024-7-28 11:23
如果你的玩家飞机图标是倾斜的,而你希望其显示为直立状态以符合游戏中正常的方向,那么你可以指定一个固 ...

你好,能否帮我给游戏增加音效,我希望在子弹碰到敌人的时候,发出一个音效。
回复 支持 反对

使用道具 举报

0

主题

1147

帖子

1718

积分

版主

Rank: 7Rank: 7Rank: 7

积分
1718
16#
发表于 2024-7-28 11:28:33 | 只看该作者
ZLJ 发表于 2024-07-28 11:27
你好,能否帮我给游戏增加音效,我希望在子弹碰到敌人 ...


当然可以,为游戏增加音效能让游戏更加生动有趣!我们可以使用HTML5的Audio对象来加载和播放音效。以下是具体步骤:

1. **准备音效文件**:确保你已准备好一个音效文件,比如`hit.mp3`,放在与你的HTML文件相同的目录下。
2. **加载音效文件**:在JavaScript中使用`Audio`对象来加载音效文件。
3. **播放音效文件**:在子弹碰撞到敌人时调用播放音效的方法。

### 步骤 1:准备音效文件
确保你的音效文件(例如`hit.mp3`)已存在。

### 步骤 2:加载音效文件
在JavaScript中创建一个`Audio`对象并加载音效文件。

### 步骤 3:播放音效文件
在子弹碰撞检测逻辑中播放音效。

下面是修改后的完整JavaScript代码,包括音效的添加:

  1. const canvas = document.getElementById('gameCanvas');
  2. const ctx = canvas.getContext('2d');

  3. // 加载图像
  4. const playerImg = new Image();
  5. const enemyImg = new Image();
  6. playerImg.src = 'player.png';  // 替换为你的玩家图像路径
  7. enemyImg.src = 'enemy.png';    // 替换为你的敌人图像路径

  8. // 加载音效
  9. const hitSound = new Audio('hit.mp3'); // 替换为你的音效文件路径

  10. // 玩家飞机的相关参数
  11. const playerWidth = 50;
  12. const playerHeight = 50;
  13. let playerX = (canvas.width - playerWidth) / 2;
  14. let playerY = canvas.height - playerHeight - 10;
  15. const playerSpeed = 7;

  16. // 子弹和敌人的数组
  17. let bullets = [];
  18. let enemies = [];
  19. let score = 0;

  20. // 按键监听
  21. document.addEventListener('keydown', movePlayer);
  22. document.addEventListener('keydown', shootBullet);

  23. // 移动玩家
  24. function movePlayer(e) {
  25.     if (e.key === 'ArrowLeft' && playerX > 0) {
  26.         playerX -= playerSpeed;
  27.     }
  28.     if (e.key === 'ArrowRight' && playerX < canvas.width - playerWidth) {
  29.         playerX += playerSpeed;
  30.     }
  31. }

  32. // 发射子弹
  33. function shootBullet(e) {
  34.     if (e.key === ' ') {
  35.         bullets.push({ x: playerX + playerWidth / 2 - 2.5, y: playerY, speed: 10 });
  36.     }
  37. }

  38. // 绘制玩家
  39. function drawPlayer() {
  40.     ctx.save(); // 保存当前绘图状态
  41.     ctx.translate(playerX + playerWidth / 2, playerY + playerHeight / 2); // 将原点移动到玩家中心
  42.     ctx.rotate(-45 * Math.PI / 180); // 旋转画布,使播放器正常方向
  43.     ctx.drawImage(playerImg, -playerWidth / 2, -playerHeight / 2, playerWidth, playerHeight); // 绘制玩家图片,注意位置偏移
  44.     ctx.restore(); // 恢复到之前的绘图状态
  45. }

  46. // 绘制子弹
  47. function drawBullets() {
  48.     ctx.fillStyle = '#ff0';
  49.     bullets.forEach((bullet, index) => {
  50.         bullet.y -= bullet.speed;
  51.         ctx.fillRect(bullet.x, bullet.y, 5, 15);
  52.         if (bullet.y < 0) {
  53.             bullets.splice(index, 1);
  54.         }
  55.     });
  56. }

  57. // 绘制敌人
  58. function drawEnemies() {
  59.     enemies.forEach((enemy, index) => {
  60.         enemy.y += enemy.speed;
  61.         ctx.drawImage(enemyImg, enemy.x, enemy.y, enemy.width, enemy.height);
  62.         if (enemy.y > canvas.height) {
  63.             enemies.splice(index, 1);
  64.             gameOver();
  65.         }
  66.     });
  67. }

  68. // 生成敌人
  69. function generateEnemies() {
  70.     if (Math.random() < 0.02) {
  71.         const x = Math.random() * (canvas.width - playerWidth);
  72.         enemies.push({ x: x, y: 0, width: 50, height: 50, speed: 3 });
  73.     }
  74. }

  75. // 检测碰撞
  76. function detectCollisions() {
  77.     bullets.forEach((bullet, bIndex) => {
  78.         enemies.forEach((enemy, eIndex) => {
  79.             if (bullet.x > enemy.x && bullet.x < enemy.x + enemy.width &&
  80.                 bullet.y > enemy.y && bullet.y < enemy.y + enemy.height) {
  81.                 score += 10;
  82.                 bullets.splice(bIndex, 1);
  83.                 enemies.splice(eIndex, 1);
  84.                 hitSound.play(); // 播放音效
  85.             }
  86.         });
  87.     });

  88.     enemies.forEach(enemy => {
  89.         if (playerX < enemy.x + enemy.width &&
  90.             playerX + playerWidth > enemy.x &&
  91.             playerY < enemy.y + enemy.height &&
  92.             playerY + playerHeight > enemy.y) {
  93.             gameOver();
  94.         }
  95.     });
  96. }

  97. // 游戏结束
  98. function gameOver() {
  99.     alert(`Game Over! Your score is ${score}`);
  100.     document.location.reload();
  101. }

  102. // 更新画面
  103. function update() {
  104.     ctx.clearRect(0, 0, canvas.width, canvas.height);
  105.     drawPlayer();
  106.     drawBullets();
  107.     drawEnemies();
  108.     detectCollisions();
  109.     generateEnemies();
  110.     requestAnimationFrame(update);
  111. }

  112. update();
复制代码


### 总结
1. **加载音效文件**:使用`Audio`对象并设置音效文件的路径。
2. **播放音效文件**:在碰撞检测时调用`play`方法。

这样,当子弹击中敌人时就会播放音效。如果有任何问题或者需要进一步的帮助,请随时告诉我!
回复 支持 反对

使用道具 举报

29

主题

107

帖子

348

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
348
17#
 楼主| 发表于 2024-7-28 11:46:29 | 只看该作者
kaia 发表于 2024-7-28 11:28
当然可以,为游戏增加音效能让游戏更加生动有趣!我们可以使用HTML5的Audio对象来加载和播放音效。以下是 ...

谢谢,除此之外,能不能增加一个按钮(图标),点击图标,播放背景音乐,并且图标切换成另一个图标,再次点击之后,则暂停播放,并且图标切换回来,你能懂我意思吗?
回复 支持 反对

使用道具 举报

0

主题

1147

帖子

1718

积分

版主

Rank: 7Rank: 7Rank: 7

积分
1718
18#
发表于 2024-7-28 11:48:43 | 只看该作者
ZLJ 发表于 2024-07-28 11:46
谢谢,除此之外,能不能增加一个按钮(图标),点击图 ...


当然可以!我们可以在HTML中添加一个按钮(图标),然后使用JavaScript来控制背景音乐的播放和暂停,同时切换按钮的图标。

### 实现步骤:
1. **在HTML中添加按钮图标**:两个图标,分别表示播放和暂停状态。
2. **加载背景音乐**:使用`Audio`对象。
3. **控制音乐播放/暂停**:点击按钮时播放或暂停音乐,并切换按钮图标。

### HTML部分:
首先,我们在HTML中添加播放/暂停按钮,并为它设置初始图标:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6.     <title>飞机大战</title>
  7.     <link rel="stylesheet" href="styles.css">
  8. </head>
  9. <body>
  10.     <h1>飞机大战</h1>
  11.     <button id="musicButton">
  12.         <img id="musicIcon" src="play.png" alt="Play Music" width="30" height="30">
  13.     </button>
  14.     <canvas id="gameCanvas" width="800" height="600"></canvas>
  15.     <script src="script.js"></script>
  16. </body>
  17. </html>
复制代码


### CSS部分:
无需修改,可以继续使用你原来的CSS。

### JavaScript部分:
在JavaScript中加载背景音乐,并设置按钮的点击事件来控制音乐的播放和暂停,同时切换图标。

  1. const canvas = document.getElementById('gameCanvas');
  2. const ctx = canvas.getContext('2d');

  3. // 加载图像
  4. const playerImg = new Image();
  5. const enemyImg = new Image();
  6. playerImg.src = 'player.png';  // 替换为你的玩家图像路径
  7. enemyImg.src = 'enemy.png';    // 替换为你的敌人图像路径

  8. // 加载音效
  9. const hitSound = new Audio('hit.mp3'); // 替换为你的音效文件路径

  10. // 加载背景音乐
  11. const bgMusic = new Audio('background.mp3'); // 替换为你的背景音乐文件路径
  12. bgMusic.loop = true; // 设置循环播放

  13. // 控制播放/暂停按钮
  14. const musicButton = document.getElementById('musicButton');
  15. const musicIcon = document.getElementById('musicIcon');
  16. let isMusicPlaying = false;

  17. musicButton.addEventListener('click', () => {
  18.     if (isMusicPlaying) {
  19.         bgMusic.pause();
  20.         musicIcon.src = 'play.png'; // 切换为播放图标
  21.     } else {
  22.         bgMusic.play();
  23.         musicIcon.src = 'pause.png'; // 切换为暂停图标
  24.     }
  25.     isMusicPlaying = !isMusicPlaying;
  26. });

  27. // 玩家飞机的相关参数
  28. const playerWidth = 50;
  29. const playerHeight = 50;
  30. let playerX = (canvas.width - playerWidth) / 2;
  31. let playerY = canvas.height - playerHeight - 10;
  32. const playerSpeed = 7;

  33. // 子弹和敌人的数组
  34. let bullets = [];
  35. let enemies = [];
  36. let score = 0;

  37. // 按键监听
  38. document.addEventListener('keydown', movePlayer);
  39. document.addEventListener('keydown', shootBullet);

  40. // 移动玩家
  41. function movePlayer(e) {
  42.     if (e.key === 'ArrowLeft' && playerX > 0) {
  43.         playerX -= playerSpeed;
  44.     }
  45.     if (e.key === 'ArrowRight' && playerX < canvas.width - playerWidth) {
  46.         playerX += playerSpeed;
  47.     }
  48. }

  49. // 发射子弹
  50. function shootBullet(e) {
  51.     if (e.key === ' ') {
  52.         bullets.push({ x: playerX + playerWidth / 2 - 2.5, y: playerY, speed: 10 });
  53.     }
  54. }

  55. // 绘制玩家
  56. function drawPlayer() {
  57.     ctx.save(); // 保存当前绘图状态
  58.     ctx.translate(playerX + playerWidth / 2, playerY + playerHeight / 2); // 将原点移动到玩家中心
  59.     ctx.rotate(-45 * Math.PI / 180); // 旋转画布,使播放器正常方向
  60.     ctx.drawImage(playerImg, -playerWidth / 2, -playerHeight / 2, playerWidth, playerHeight); // 绘制玩家图片,注意位置偏移
  61.     ctx.restore(); // 恢复到之前的绘图状态
  62. }

  63. // 绘制子弹
  64. function drawBullets() {
  65.     ctx.fillStyle = '#ff0';
  66.     bullets.forEach((bullet, index) => {
  67.         bullet.y -= bullet.speed;
  68.         ctx.fillRect(bullet.x, bullet.y, 5, 15);
  69.         if (bullet.y < 0) {
  70.             bullets.splice(index, 1);
  71.         }
  72.     });
  73. }

  74. // 绘制敌人
  75. function drawEnemies() {
  76.     enemies.forEach((enemy, index) => {
  77.         enemy.y += enemy.speed;
  78.         ctx.drawImage(enemyImg, enemy.x, enemy.y, enemy.width, enemy.height);
  79.         if (enemy.y > canvas.height) {
  80.             enemies.splice(index, 1);
  81.             gameOver();
  82.         }
  83.     });
  84. }

  85. // 生成敌人
  86. function generateEnemies() {
  87.     if (Math.random() < 0.02) {
  88.         const x = Math.random() * (canvas.width - playerWidth);
  89.         enemies.push({ x: x, y: 0, width: 50, height: 50, speed: 3 });
  90.     }
  91. }

  92. // 检测碰撞
  93. function detectCollisions() {
  94.     bullets.forEach((bullet, bIndex) => {
  95.         enemies.forEach((enemy, eIndex) => {
  96.             if (bullet.x > enemy.x && bullet.x < enemy.x + enemy.width &&
  97.                 bullet.y > enemy.y && bullet.y < enemy.y + enemy.height) {
  98.                 score += 10;
  99.                 bullets.splice(bIndex, 1);
  100.                 enemies.splice(eIndex, 1);
  101.                 hitSound.play(); // 播放音效
  102.             }
  103.         });
  104.     });

  105.     enemies.forEach(enemy => {
  106.         if (playerX < enemy.x + enemy.width &&
  107.             playerX + playerWidth > enemy.x &&
  108.             playerY < enemy.y + enemy.height &&
  109.             playerY + playerHeight > enemy.y) {
  110.             gameOver();
  111.         }
  112.     });
  113. }

  114. // 游戏结束
  115. function gameOver() {
  116.     alert(`Game Over! Your score is ${score}`);
  117.     document.location.reload();
  118. }

  119. // 更新画面
  120. function update() {
  121.     ctx.clearRect(0, 0, canvas.width, canvas.height);
  122.     drawPlayer();
  123.     drawBullets();
  124.     drawEnemies();
  125.     detectCollisions();
  126.     generateEnemies();
  127.     requestAnimationFrame(update);
  128. }

  129. update();
复制代码


### 总结
1. **HTML添加按钮图标**:包含播放和暂停两个状态的图标。
2. **JavaScript加载和控制背景音乐**:使用`Audio`对象进行加载,并通过按钮事件进行控制。
3. **切换按钮图标**:根据音乐状态切换图标。

这样,你就可以通过点击图标来控制背景音乐的播放和暂停,并且图标会对应地进行切换。如果有任何疑问或者需要进一步帮助,请随时告诉我!
回复 支持 反对

使用道具 举报

29

主题

107

帖子

348

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
348
19#
 楼主| 发表于 2024-7-28 12:03:20 | 只看该作者
kaia 发表于 2024-7-28 11:48
当然可以!我们可以在HTML中添加一个按钮(图标),然后使用JavaScript来控制背景音乐的播放和暂停,同时 ...

很好,但是可以修改音乐的音量吗
回复 支持 反对

使用道具 举报

0

主题

1147

帖子

1718

积分

版主

Rank: 7Rank: 7Rank: 7

积分
1718
20#
发表于 2024-7-28 12:05:10 | 只看该作者
ZLJ 发表于 2024-07-28 12:03
很好,但是可以修改音乐的音量吗


当然可以!我们可以通过设置`Audio`对象的`volume`属性来调整音乐的音量。这个属性的值在0到1之间,表示从完全静音到最大音量。

为了让玩家更方便地调整音量,我们还可以添加一个滑动条(`<input type="range">`)来控制音量。下面是如何实现的步骤:

### 实现步骤
1. **在HTML中添加音量滑动条**。
2. **在JavaScript中监听滑动条的变化**,并设置音量。

### HTML部分:
在HTML中添加一个音量滑动条(`<input type="range">`)。

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6.     <title>飞机大战</title>
  7.     <style>
  8.         body {
  9.             display: flex;
  10.             flex-direction: column;
  11.             align-items: center;
  12.             justify-content: center;
  13.             height: 100vh;
  14.             margin: 0;
  15.             background-color: #000;
  16.             color: #fff;
  17.             font-family: Arial, sans-serif;
  18.         }

  19.         h1 {
  20.             margin-bottom: 20px;
  21.         }

  22.         canvas {
  23.             border: 1px solid #fff;
  24.         }

  25.         .controls {
  26.             margin-top: 10px;
  27.         }
  28.     </style>
  29. </head>
  30. <body>
  31.     <h1>飞机大战</h1>
  32.    
  33.     <button id="musicButton">
  34.         <img id="musicIcon" src="play.png" alt="Play Button">
  35.     </button>
  36.     <div class="controls">
  37.         <label for="volumeControl">音量:</label>
  38.         <input type="range" id="volumeControl" min="0" max="1" step="0.01" value="0.5">
  39.     </div>
  40.    
  41.     <canvas id="gameCanvas" width="800" height="600"></canvas>

  42.     <script src="game.js"></script>
  43. </body>
  44. </html>
复制代码


### JavaScript部分:
在JavaScript中添加对滑动条的监听,并设置背景音乐的音量。

  1. const canvas = document.getElementById('gameCanvas');
  2. const ctx = canvas.getContext('2d');

  3. // 加载图像
  4. const playerImg = new Image();
  5. const enemyImg = new Image();
  6. playerImg.src = 'player.png';  // 替换为你的玩家图像路径
  7. enemyImg.src = 'enemy.png';    // 替换为你的敌人图像路径

  8. // 加载音效
  9. const hitSound = new Audio('hit.mp3'); // 替换为你的音效文件路径

  10. // 加载背景音乐
  11. const bgMusic = new Audio('background.mp3'); // 替换为你的背景音乐文件路径
  12. bgMusic.loop = true; // 设置循环播放

  13. // 控制播放/暂停按钮
  14. const musicButton = document.getElementById('musicButton');
  15. const musicIcon = document.getElementById('musicIcon');
  16. let isMusicPlaying = false;

  17. musicButton.addEventListener('click', () => {
  18.     if (isMusicPlaying) {
  19.         bgMusic.pause();
  20.         musicIcon.src = 'play.png'; // 切换为播放图标
  21.     } else {
  22.         bgMusic.play();
  23.         musicIcon.src = 'pause.png'; // 切换为暂停图标
  24.     }
  25.     isMusicPlaying = !isMusicPlaying;
  26. });

  27. // 音量控制
  28. const volumeControl = document.getElementById('volumeControl');
  29. volumeControl.addEventListener('input', (e) => {
  30.     bgMusic.volume = e.target.value;
  31. });

  32. // 玩家飞机的相关参数
  33. const playerWidth = 50;
  34. const playerHeight = 50;
  35. let playerX = (canvas.width - playerWidth) / 2;
  36. let playerY = canvas.height - playerHeight - 10;
  37. const playerSpeed = 7;

  38. // 子弹和敌人的数组
  39. let bullets = [];
  40. let enemies = [];
  41. let score = 0;

  42. // 按键监听
  43. document.addEventListener('keydown', movePlayer);
  44. document.addEventListener('keydown', shootBullet);

  45. // 移动玩家
  46. function movePlayer(e) {
  47.     if (e.key === 'ArrowLeft' && playerX > 0) {
  48.         playerX -= playerSpeed;
  49.     }
  50.     if (e.key === 'ArrowRight' && playerX < canvas.width - playerWidth) {
  51.         playerX += playerSpeed;
  52.     }
  53. }

  54. // 发射子弹
  55. function shootBullet(e) {
  56.     if (e.key === ' ') {
  57.         bullets.push({ x: playerX + playerWidth / 2 - 2.5, y: playerY, speed: 10 });
  58.     }
  59. }

  60. // 绘制玩家
  61. function drawPlayer() {
  62.     ctx.save(); // 保存当前绘图状态
  63.     ctx.translate(playerX + playerWidth / 2, playerY + playerHeight / 2); // 将原点移动到玩家中心
  64.     ctx.rotate(-45 * Math.PI / 180); // 旋转画布,使播放器正常方向
  65.     ctx.drawImage(playerImg, -playerWidth / 2, -playerHeight / 2, playerWidth, playerHeight); // 绘制玩家图片,注意位置偏移
  66.     ctx.restore(); // 恢复到之前的绘图状态
  67. }

  68. // 绘制子弹
  69. function drawBullets() {
  70.     ctx.fillStyle = '#ff0';
  71.     bullets.forEach((bullet, index) => {
  72.         bullet.y -= bullet.speed;
  73.         ctx.fillRect(bullet.x, bullet.y, 5, 15);
  74.         if (bullet.y < 0) {
  75.             bullets.splice(index, 1);
  76.         }
  77.     });
  78. }

  79. // 绘制敌人
  80. function drawEnemies() {
  81.     enemies.forEach((enemy, index) => {
  82.         enemy.y += enemy.speed;
  83.         ctx.drawImage(enemyImg, enemy.x, enemy.y, enemy.width, enemy.height);
  84.         if (enemy.y > canvas.height) {
  85.             enemies.splice(index, 1);
  86.             gameOver();
  87.         }
  88.     });
  89. }

  90. // 生成敌人
  91. function generateEnemies() {
  92.     if (Math.random() < 0.02) {
  93.         const x = Math.random() * (canvas.width - playerWidth);
  94.         enemies.push({ x: x, y: 0, width: 50, height: 50, speed: 3 });
  95.     }
  96. }

  97. // 检测碰撞
  98. function detectCollisions() {
  99.     bullets.forEach((bullet, bIndex) => {
  100.         enemies.forEach((enemy, eIndex) => {
  101.             if (bullet.x > enemy.x && bullet.x < enemy.x + enemy.width &&
  102.                 bullet.y > enemy.y && bullet.y < enemy.y + enemy.height) {
  103.                 score += 10;
  104.                 bullets.splice(bIndex, 1);
  105.                 enemies.splice(eIndex, 1);
  106.                 hitSound.play(); // 播放音效
  107.             }
  108.         });
  109.     });

  110.     enemies.forEach(enemy => {
  111.         if (playerX < enemy.x + enemy.width &&
  112.             playerX + playerWidth > enemy.x &&
  113.             playerY < enemy.y + enemy.height &&
  114.             playerY + playerHeight > enemy.y) {
  115.             gameOver();
  116.         }
  117.     });
  118. }

  119. // 游戏结束
  120. function gameOver() {
  121.     alert(`Game Over! Your score is ${score}`);
  122.     document.location.reload();
  123. }

  124. // 更新画面
  125. function update() {
  126.     ctx.clearRect(0, 0, canvas.width, canvas.height);
  127.     drawPlayer();
  128.     drawBullets();
  129.     drawEnemies();
  130.     detectCollisions();
  131.     generateEnemies();
  132.     requestAnimationFrame(update);
  133. }

  134. update();
复制代码


### 总结
1. **添加音量控制滑动条**:使用`<input type="range">`元素。
2. **监听滑动条变化**:在JavaScript中添加事件监听器,实时调整背景音乐的音量。

通过这些修改,你就可以通过滑动条来调整背景音乐的音量啦。如果有任何问题或者需要进一步的帮助,请随时告诉我!
回复 支持 反对

使用道具 举报

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

本版积分规则

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