科学教育

搜索
查看: 4778|回复: 13
打印 上一主题 下一主题

林奕锦飞机大战

[复制链接]

8

主题

62

帖子

184

积分

注册会员

Rank: 2

积分
184
跳转到指定楼层
楼主
发表于 2024-7-25 21:25:51 | 只看该作者 回帖奖励 |正序浏览 |阅读模式
请帮我用html、css、js文件生成一个飞机大战游戏,有开始按钮,我们可以用左右键控制飞机左右移动,当按下空格键可以发射炮弹摧毁敌机,敌机会从上方的随机位置出现,并可以实时看见自己的得分,谢谢
回复

使用道具 举报

0

主题

1145

帖子

1715

积分

版主

Rank: 7Rank: 7Rank: 7

积分
1715
推荐
发表于 2024-7-26 13:44:48 | 只看该作者
林奕锦 发表于 2024-07-26 13:44
我想在飞机碰到敌机时结束游戏


好的!让我们来修改一下代码,在飞机碰到敌机时结束游戏。这需要在JavaScript代码中添加一些新的逻辑,确保检测到碰撞时停止游戏并显示结束信息。

### 修改后的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.     <div id="game-container">
  11.         <div id="player"></div>
  12.         <div id="score">得分: 0</div>
  13.         <button id="start-button">开始游戏</button>
  14.         <div id="game-over" style="display: none;">游戏结束</div>
  15.     </div>
  16.     <script src="script.js"></script>
  17. </body>
  18. </html>
复制代码


### 修改后的CSS文件
为游戏结束消息添加样式:

  1. /* styles.css */
  2. body {
  3.     margin: 0;
  4.     overflow: hidden;
  5.     background-color: #000;
  6.     color: white;
  7.     font-family: Arial, sans-serif;
  8. }

  9. #game-container {
  10.     position: relative;
  11.     width: 100vw;
  12.     height: 100vh;
  13.     overflow: hidden;
  14. }

  15. #player {
  16.     position: absolute;
  17.     width: 50px;
  18.     height: 50px;
  19.     background-color: blue;
  20.     bottom: 10px;
  21.     left: calc(50% - 25px);
  22. }

  23. #score {
  24.     position: absolute;
  25.     top: 10px;
  26.     left: 10px;
  27. }

  28. #start-button {
  29.     position: absolute;
  30.     top: 50%;
  31.     left: 50%;
  32.     transform: translate(-50%, -50%);
  33.     padding: 10px 20px;
  34.     font-size: 18px;
  35. }

  36. #game-over {
  37.     position: absolute;
  38.     top: 50%;
  39.     left: 50%;
  40.     transform: translate(-50%, -50%);
  41.     font-size: 24px;
  42.     color: red;
  43. }
复制代码


### 修改后的JavaScript文件
增加碰撞检测和游戏结束逻辑:

  1. // script.js
  2. document.addEventListener("DOMContentLoaded", () => {
  3.     const player = document.getElementById("player");
  4.     const gameContainer = document.getElementById("game-container");
  5.     const scoreDisplay = document.getElementById("score");
  6.     const startButton = document.getElementById("start-button");
  7.     const gameOverDisplay = document.getElementById("game-over");

  8.     let score = 0;
  9.     let gameInterval;
  10.     let bulletInterval;
  11.     let enemyInterval;

  12.     startButton.addEventListener("click", startGame);

  13.     function startGame() {
  14.         startButton.style.display = "none";
  15.         gameOverDisplay.style.display = "none";
  16.         score = 0;
  17.         scoreDisplay.innerText = "得分: " + score;
  18.         gameLoop();
  19.         document.addEventListener("keydown", handleKeys);
  20.     }

  21.     function handleKeys(e) {
  22.         const key = e.code;
  23.         if (key === "ArrowLeft") {
  24.             movePlayer(-10);
  25.         } else if (key === "ArrowRight") {
  26.             movePlayer(10);
  27.         } else if (key === "Space") {
  28.             shootBullet();
  29.         }
  30.     }

  31.     function movePlayer(distance) {
  32.         let currentLeft = parseInt(window.getComputedStyle(player).left);
  33.         let newLeft = currentLeft + distance;
  34.         if (newLeft >= 0 && newLeft <= gameContainer.offsetWidth - player.offsetWidth) {
  35.             player.style.left = newLeft + "px";
  36.         }
  37.     }

  38.     function shootBullet() {
  39.         const bullet = document.createElement("div");
  40.         bullet.className = "bullet";
  41.         bullet.style.left = (parseInt(window.getComputedStyle(player).left) + player.offsetWidth / 2 - 2.5) + "px";
  42.         bullet.style.top = player.offsetTop + "px";
  43.         gameContainer.appendChild(bullet);
  44.         moveBullet(bullet);
  45.     }

  46.     function moveBullet(bullet) {
  47.         const bulletTimer = setInterval(() => {
  48.             const bulletTop = parseInt(window.getComputedStyle(bullet).top);
  49.             if (bulletTop <= 0) {
  50.                 clearInterval(bulletTimer);
  51.                 bullet.remove();
  52.             } else {
  53.                 bullet.style.top = (bulletTop - 10) + "px";
  54.                 checkCollision(bullet);
  55.             }
  56.         }, 30);
  57.     }

  58.     function checkCollision(bullet) {
  59.         const enemies = document.querySelectorAll(".enemy");
  60.         enemies.forEach(enemy => {
  61.             const bulletTop = parseInt(window.getComputedStyle(bullet).top);
  62.             if (isColliding(bullet, enemy)) {
  63.                 clearInterval(bulletTimer);
  64.                 bullet.remove();
  65.                 enemy.remove();
  66.                 score++;
  67.                 scoreDisplay.innerText = "得分: " + score;
  68.             }
  69.         });
  70.     }

  71.     function isColliding(bullet, enemy) {
  72.         const bulletRect = bullet.getBoundingClientRect();
  73.         const enemyRect = enemy.getBoundingClientRect();
  74.         return !(
  75.             bulletRect.top > enemyRect.bottom ||
  76.             bulletRect.right < enemyRect.left ||
  77.             bulletRect.bottom < enemyRect.top ||
  78.             bulletRect.left > enemyRect.right
  79.         );
  80.     }

  81.     function gameLoop() {
  82.         enemyInterval = setInterval(() => {
  83.             createEnemy();
  84.         }, 2000);
  85.     }

  86.     function createEnemy() {
  87.         const enemy = document.createElement("div");
  88.         enemy.className = "enemy";
  89.         enemy.style.left = Math.random() * (gameContainer.offsetWidth - 50) + "px";
  90.         enemy.style.top = "0px";
  91.         gameContainer.appendChild(enemy);
  92.         moveEnemy(enemy);
  93.     }

  94.     function moveEnemy(enemy) {
  95.         const enemyTimer = setInterval(() => {
  96.             const enemyTop = parseInt(window.getComputedStyle(enemy).top);
  97.             if (enemyTop >= gameContainer.offsetHeight) {
  98.                 clearInterval(enemyTimer);
  99.                 enemy.remove();
  100.             } else {
  101.                 enemy.style.top = (enemyTop + 5) + "px";
  102.             }

  103.             if (checkPlayerCollision(enemy)) {
  104.                 endGame();
  105.             }
  106.         }, 30);
  107.     }

  108.     function checkPlayerCollision(enemy) {
  109.         const playerRect = player.getBoundingClientRect();
  110.         const enemyRect = enemy.getBoundingClientRect();
  111.         return !(
  112.             playerRect.top > enemyRect.bottom ||
  113.             playerRect.right < enemyRect.left ||
  114.             playerRect.bottom < enemyRect.top ||
  115.             playerRect.left > enemyRect.right
  116.         );
  117.     }

  118.     function endGame() {
  119.         clearInterval(gameInterval);
  120.         clearInterval(enemyInterval);
  121.         document.querySelectorAll(".bullet").forEach(bullet => clearInterval(bullet._timer));
  122.         document.querySelectorAll(".enemy").forEach(enemy => clearInterval(enemy._timer));
  123.         gameOverDisplay.style.display = "block";
  124.         startButton.style.display = "block";
  125.         document.removeEventListener("keydown", handleKeys);
  126.     }

  127.     const style = document.createElement('style');
  128.     style.innerHTML = `
  129.         .bullet {
  130.             position: absolute;
  131.             width: 5px;
  132.             height: 20px;
  133.             background-color: red;
  134.             top: calc(100% - 60px);
  135.         }
  136.         .enemy {
  137.             position: absolute;
  138.             width: 50px;
  139.             height: 50px;
  140.             background-color: green;
  141.         }
  142.     `;
  143.     document.head.appendChild(style);
  144. });
复制代码


这段代码增加了检测玩家飞机是否与敌机碰撞的逻辑,并在碰撞时结束游戏。游戏结束后会显示“游戏结束”消息,并禁用键盘事件处理,游戏的子弹和敌机移动也都会停止。

现在,你可以再次保存这些代码到相应的文件中,然后在浏览器中打开 `index.html` 文件就可以看到当玩家的飞机撞到敌机时游戏会结束啦!

如果有其他问题或者需要进一步的解释,随时告诉我哦!????✨
回复 支持 1 反对 0

使用道具 举报

0

主题

1145

帖子

1715

积分

版主

Rank: 7Rank: 7Rank: 7

积分
1715
14#
发表于 2024-8-1 16:07:09 | 只看该作者
好的,我们来加一些代码,让我们的游戏更加完美!

我们需要做以下几点改动:

1. 检测玩家的飞机是否与敌机相撞。
2. 在碰撞时结束游戏,并显示“游戏结束”的界面。

### 修改后的JavaScript代码

我们在原来的基础上进行修改和补充:

  1. document.addEventListener("DOMContentLoaded", () => {
  2.     const player = document.getElementById("player");
  3.     const gameContainer = document.getElementById("game-container");
  4.     const scoreDisplay = document.getElementById("score");
  5.     const startButton = document.getElementById("start-button");
  6.     const gameOverDisplay = document.createElement('div');

  7.     let score = 0;
  8.     let gameInterval;
  9.     let bulletInterval;

  10.     // 创建游戏结束的界面
  11.     gameOverDisplay.id = 'game-over';
  12.     gameOverDisplay.innerText = "游戏结束";
  13.     gameOverDisplay.style.display = "none";
  14.     gameContainer.appendChild(gameOverDisplay);

  15.     startButton.addEventListener("click", startGame);

  16.     function startGame() {
  17.         startButton.style.display = "none";
  18.         gameOverDisplay.style.display = "none";
  19.         score = 0;
  20.         scoreDisplay.innerText = "得分: " + score;
  21.         gameLoop();
  22.         document.addEventListener("keydown", handleKeys);
  23.     }

  24.     function handleKeys(e) {
  25.         const key = e.code;
  26.         if (key === "ArrowLeft") {
  27.             movePlayer(-10);
  28.         } else if (key === "ArrowRight") {
  29.             movePlayer(10);
  30.         } else if (key === "Space") {
  31.             shootBullet();
  32.         }
  33.     }

  34.     function movePlayer(distance) {
  35.         let currentLeft = parseInt(window.getComputedStyle(player).left);
  36.         let newLeft = currentLeft + distance;
  37.         if (newLeft >= 0 && newLeft <= gameContainer.offsetWidth - player.offsetWidth) {
  38.             player.style.left = newLeft + "px";
  39.         }
  40.     }

  41.     function shootBullet() {
  42.         const bullet = document.createElement("div");
  43.         bullet.className = "bullet";
  44.         bullet.style.left = parseInt(window.getComputedStyle(player).left) + player.offsetWidth / 2 - 2.5 + "px";
  45.         gameContainer.appendChild(bullet);
  46.         moveBullet(bullet);
  47.     }

  48.     function moveBullet(bullet) {
  49.         const bulletTimer = setInterval(() => {
  50.             const bulletTop = parseInt(window.getComputedStyle(bullet).top);
  51.             if (bulletTop <= 0) {
  52.                 clearInterval(bulletTimer);
  53.                 bullet.remove();
  54.             } else {
  55.                 bullet.style.top = (bulletTop - 10) + "px";
  56.             }

  57.             // 检查子弹是否击中敌机
  58.             document.querySelectorAll(".enemy").forEach(enemy => {
  59.                 if (isColliding(bullet, enemy)) {
  60.                     clearInterval(bulletTimer);
  61.                     bullet.remove();
  62.                     enemy.remove();
  63.                     score++;
  64.                     scoreDisplay.innerText = "得分: " + score;
  65.                 }
  66.             });
  67.         }, 30);
  68.     }

  69.     function isColliding(a, b) {
  70.         const aRect = a.getBoundingClientRect();
  71.         const bRect = b.getBoundingClientRect();
  72.         return !(
  73.             aRect.top > bRect.bottom ||
  74.             aRect.right < bRect.left ||
  75.             aRect.bottom < bRect.top ||
  76.             aRect.left > bRect.right
  77.         );
  78.     }

  79.     function gameLoop() {
  80.         gameInterval = setInterval(() => {
  81.             createEnemy();
  82.         }, 2000);
  83.     }

  84.     function createEnemy() {
  85.         const enemy = document.createElement("div");
  86.         enemy.className = "enemy";
  87.         enemy.style.left = Math.random() * (gameContainer.offsetWidth - 50) + "px";
  88.         enemy.style.top = "0px";
  89.         gameContainer.appendChild(enemy);
  90.         moveEnemy(enemy);
  91.     }

  92.     function moveEnemy(enemy) {
  93.         const enemyTimer = setInterval(() => {
  94.             const enemyTop = parseInt(window.getComputedStyle(enemy).top);
  95.             if (enemyTop >= gameContainer.offsetHeight) {
  96.                 clearInterval(enemyTimer);
  97.                 enemy.remove();
  98.             } else {
  99.                 enemy.style.top = (enemyTop + 5) + "px";
  100.             }

  101.             // 检查玩家是否与敌机相撞
  102.             if (isColliding(player, enemy)) {
  103.                 clearInterval(enemyTimer);
  104.                 endGame();
  105.             }
  106.         }, 30);
  107.     }

  108.     function endGame() {
  109.         clearInterval(gameInterval);
  110.         document.removeEventListener("keydown", handleKeys);
  111.         document.querySelectorAll(".bullet").forEach(bullet => bullet.remove());
  112.         document.querySelectorAll(".enemy").forEach(enemy => enemy.remove());
  113.         gameOverDisplay.style.display = "block";
  114.         startButton.style.display = "block";
  115.     }

  116.     const style = document.createElement('style');
  117.     style.innerHTML = `
  118.         .bullet {
  119.             position: absolute;
  120.             width: 5px;
  121.             height: 20px;
  122.             background-color: red;
  123.             top: calc(100% - 60px);
  124.         }
  125.         .enemy {
  126.             position: absolute;
  127.             width: 50px;
  128.             height: 50px;
  129.             background-color: green;
  130.         }
  131.         #game-over {
  132.             position: absolute;
  133.             top: 50%;
  134.             left: 50%;
  135.             transform: translate(-50%, -50%);
  136.             font-size: 48px;
  137.             color: red;
  138.         }
  139.     `;
  140.     document.head.appendChild(style);
  141. });
复制代码


### 修改后的CSS文件

我们还需要稍微调整一下CSS文件,增加`#game-over`的样式:

  1. /* styles.css */
  2. body {
  3.     margin: 0;
  4.     overflow: hidden;
  5.     background-color: #000;
  6.     color: white;
  7.     font-family: Arial, sans-serif;
  8. }

  9. #game-container {
  10.     position: relative;
  11.     width: 100vw;
  12.     height: 100vh;
  13.     overflow: hidden;
  14. }

  15. #player {
  16.     position: absolute;
  17.     width: 50px;
  18.     height: 50px;
  19.     background-color: blue;
  20.     bottom: 10px;
  21.     left: calc(50% - 25px);
  22. }

  23. #score {
  24.     position: absolute;
  25.     top: 10px;
  26.     left: 10px;
  27. }

  28. #start-button {
  29.     position: absolute;
  30.     top: 50%;
  31.     left: 50%;
  32.     transform: translate(-50%, -50%);
  33.     padding: 10px 20px;
  34.     font-size: 18px;
  35. }

  36. #game-over {
  37.     position: absolute;
  38.     top: 50%;
  39.     left: 50%;
  40.     transform: translate(-50%, -50%);
  41.     font-size: 48px;
  42.     color: red;
  43. }
复制代码


这样,我们就完成了一个更加完善的飞机大战游戏。当玩家的飞机与敌机相撞时,会结束游戏并显示“游戏结束”界面。

希望你喜欢这个游戏!如果有任何问题,随时问我哦!????✨
回复 支持 反对

使用道具 举报

0

主题

1145

帖子

1715

积分

版主

Rank: 7Rank: 7Rank: 7

积分
1715
13#
发表于 2024-7-26 14:11:24 | 只看该作者
林奕锦 发表于 2024-07-26 14:10
为什么敌机依然看见不了?


如果敌机仍然不可见,可能是因为某些原因导致CSS样式或者JavaScript代码没有正确应用到敌机元素上。我们将一步步来确保所有内容设置正确。

### 确认图片路径
首先,请确保 `enemy.png` 图片文件在与你的 HTML 文件同一目录下,并且路径无误。

### 完整代码示例
这是完整的代码示例,确保所有内容正确连接并运行良好。

#### HTML文件 (index.html)

  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.     <link rel="stylesheet" href="styles.css">
  8. </head>
  9. <body>
  10.     <div id="game-container">
  11.         <div id="score">得分: 0</div>
  12.         <button id="start-button">开始游戏</button>
  13.         <div id="game-over">游戏结束</div>
  14.         <div id="player"></div>
  15.     </div>
  16.     <script src="script.js"></script>
  17. </body>
  18. </html>
复制代码


#### CSS文件 (styles.css)

  1. body {
  2.     margin: 0;
  3.     overflow: hidden;
  4.     background-color: #000;
  5.     color: white;
  6.     font-family: Arial, sans-serif;
  7. }

  8. #game-container {
  9.     position: relative;
  10.     width: 100vw;
  11.     height: 100vh;
  12.     overflow: hidden;
  13. }

  14. #player {
  15.     position: absolute;
  16.     width: 50px; /* 调整飞机宽度 */
  17.     height: 50px; /* 调整飞机高度 */
  18.     bottom: 10px;
  19.     left: calc(50% - 25px);
  20.     background-image: url('player.png');
  21.     background-size: cover; /* 使得背景图片填满元素 */
  22. }

  23. #score {
  24.     position: absolute;
  25.     top: 10px;
  26.     left: 10px;
  27. }

  28. #start-button {
  29.     position: absolute;
  30.     top: 50%;
  31.     left: 50%;
  32.     transform: translate(-50%, -50%);
  33.     padding: 10px 20px;
  34.     font-size: 18px;
  35. }

  36. #game-over {
  37.     position: absolute;
  38.     top: 50%;
  39.     left: 50%;
  40.     transform: translate(-50%, -50%);
  41.     display: none;
  42.     font-size: 24px;
  43.     color: red;
  44. }

  45. .bullet {
  46.     position: absolute;
  47.     width: 5px; /* 调整子弹宽度 */
  48.     height: 20px; /* 调整子弹高度 */
  49.     background-image: url('bullet.png');
  50.     background-size: cover; /* 使得背景图片填满元素 */
  51.     top: calc(100% - 60px);
  52. }

  53. .enemy {
  54.     position: absolute;
  55.     width: 50px; /* 调整敌机宽度 */
  56.     height: 50px; /* 调整敌机高度 */
  57.     background-image: url('enemy.png');
  58.     background-size: cover; /* 使得背景图片填满元素 */
  59. }
复制代码


#### JavaScript文件 (script.js)

  1. document.addEventListener("DOMContentLoaded", () => {
  2.     const player = document.getElementById("player");
  3.     const gameContainer = document.getElementById("game-container");
  4.     const scoreDisplay = document.getElementById("score");
  5.     const startButton = document.getElementById("start-button");
  6.     const gameOverDisplay = document.getElementById("game-over");

  7.     let score = 0;
  8.     let gameInterval;
  9.     let bulletInterval;
  10.     let enemyInterval;

  11.     startButton.addEventListener("click", startGame);

  12.     function startGame() {
  13.         startButton.style.display = "none";
  14.         gameOverDisplay.style.display = "none";
  15.         score = 0;
  16.         scoreDisplay.innerText = "得分: " + score;
  17.         gameLoop();
  18.         document.addEventListener("keydown", handleKeys);
  19.     }

  20.     function handleKeys(e) {
  21.         const key = e.code;
  22.         if (key === "ArrowLeft") {
  23.             movePlayer(-10);
  24.         } else if (key === "ArrowRight") {
  25.             movePlayer(10);
  26.         } else if (key === "Space") {
  27.             shootBullet();
  28.         }
  29.     }

  30.     function movePlayer(distance) {
  31.         let currentLeft = parseInt(window.getComputedStyle(player).left);
  32.         let newLeft = currentLeft + distance;
  33.         if (newLeft >= 0 && newLeft < (gameContainer.offsetWidth - player.offsetWidth)) {
  34.             player.style.left = newLeft + "px";
  35.         }
  36.     }

  37.     function shootBullet() {
  38.         const bullet = document.createElement("div");
  39.         bullet.className = "bullet";
  40.         bullet.style.left = (parseInt(window.getComputedStyle(player).left) + player.offsetWidth / 2 - 2.5) + "px";
  41.         bullet.style.top = (parseInt(window.getComputedStyle(player).top) - 20) + "px";
  42.         gameContainer.appendChild(bullet);

  43.         const bulletTimer = setInterval(() => {
  44.             const bulletTop = parseInt(window.getComputedStyle(bullet).top);
  45.             bullet.style.top = (bulletTop - 5) + "px";

  46.             if (bulletTop < 0) {
  47.                 clearInterval(bulletTimer);
  48.                 bullet.remove();
  49.             }

  50.             document.querySelectorAll(".enemy").forEach(enemy => {
  51.                 if (isColliding(bullet, enemy)) {
  52.                     clearInterval(bulletTimer);
  53.                     bullet.remove();
  54.                     enemy.remove();
  55.                     score++;
  56.                     scoreDisplay.innerText = "得分: " + score;
  57.                 }
  58.             });
  59.         }, 30);
  60.         bullet._timer = bulletTimer;
  61.     }

  62.     function isColliding(element1, element2) {
  63.         const rect1 = element1.getBoundingClientRect();
  64.         const rect2 = element2.getBoundingClientRect();
  65.         return !(
  66.             rect1.top > rect2.bottom ||
  67.             rect1.right < rect2.left ||
  68.             rect1.bottom < rect2.top ||
  69.             rect1.left > rect2.right
  70.         );
  71.     }

  72.     function gameLoop() {
  73.         enemyInterval = setInterval(() => {
  74.             createEnemy();
  75.         }, 2000);
  76.     }

  77.     function createEnemy() {
  78.         const enemy = document.createElement("div");
  79.         enemy.className = "enemy";
  80.         enemy.style.left = Math.random() * (gameContainer.offsetWidth - 50) + "px";
  81.         enemy.style.top = "0px";
  82.         gameContainer.appendChild(enemy);
  83.         moveEnemy(enemy);
  84.     }

  85.     function moveEnemy(enemy) {
  86.         const enemyTimer = setInterval(() => {
  87.             const enemyTop = parseInt(window.getComputedStyle(enemy).top);
  88.             if (enemyTop >= gameContainer.offsetHeight) {
  89.                 clearInterval(enemyTimer);
  90.                 enemy.remove();
  91.             } else {
  92.                 enemy.style.top = (enemyTop + 5) + "px";
  93.             }

  94.             if (checkPlayerCollision(enemy)) {
  95.                 endGame();
  96.             }
  97.         }, 30);
  98.         enemy._timer = enemyTimer;
  99.     }

  100.     function checkPlayerCollision(enemy) {
  101.         const playerRect = player.getBoundingClientRect();
  102.         const enemyRect = enemy.getBoundingClientRect();
  103.         return !(
  104.             playerRect.top > enemyRect.bottom ||
  105.             playerRect.right < enemyRect.left ||
  106.             playerRect.bottom < enemyRect.top ||
  107.             playerRect.left > enemyRect.right
  108.         );
  109.     }

  110.     function endGame() {
  111.         clearInterval(gameInterval);
  112.         clearInterval(enemyInterval);
  113.         document.querySelectorAll(".bullet").forEach(bullet => clearInterval(bullet._timer));
  114.         document.querySelectorAll(".enemy").forEach(enemy => clearInterval(enemy._timer));
  115.         gameOverDisplay.style.display = "block";
  116.         startButton.style.display = "block";
  117.         document.removeEventListener("keydown", handleKeys);
  118.     }
  119. });
复制代码


### 检查点
1. **图片路径**:确保图片文件 `player.png`、`enemy.png` 和 `bullet.png` 与 `index.html` 文件在同一目录下。
2. **浏览器开发者工具**:按 `F12` 打开开发者工具,检查控制台是否有任何错误消息。如果有关于图片加载的问题,它会显示相关错误信息。
3. **CSS 样式**:确保 CSS 样式正确应用到敌机上,可以通过开发者工具(右键点击元素选择“检查”)查看敌机元素的样式,确认 `background-image` 是否正确加载。

通过以上步骤,你应该能够找到敌机无法显示的问题所在。如果仍然无法解决,请告诉我,我会进一步帮你排查问题!????✨
回复 支持 反对

使用道具 举报

8

主题

62

帖子

184

积分

注册会员

Rank: 2

积分
184
12#
 楼主| 发表于 2024-7-26 14:10:45 | 只看该作者
kaia 发表于 2024-7-26 13:59
哦哦,可能是因为图片没有正确加载或者CSS设置导致了这些元素不可见。我们来一步步排查问题:

1. **确认 ...

为什么敌机依然看见不了?
回复 支持 反对

使用道具 举报

0

主题

1145

帖子

1715

积分

版主

Rank: 7Rank: 7Rank: 7

积分
1715
11#
发表于 2024-7-26 13:59:44 | 只看该作者
林奕锦 发表于 2024-07-26 13:58
为什么在游戏开始后我看不见飞机和敌机? ...


哦哦,可能是因为图片没有正确加载或者CSS设置导致了这些元素不可见。我们来一步步排查问题:

1. **确认图片路径和文件名**:首先确保你的图片文件(`player.png`、`enemy.png`、`bullet.png`)和HTML文件在同一个文件夹中,并且文件名和扩展名完全匹配。

2. **检查CSS样式**:确保CSS中的路径是正确的,图片背景正确应用到了相应的元素上。

3. **调试通过控制台**:可以通过浏览器的开发者工具(F12)查看控制台输出,一旦有任何错误,控制台应该能给出提示。

以下是经过调整后的完整代码示例,请按照这些代码来确保所有内容都正确:

### HTML文件 (index.html)

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>林奕锦的飞机大战</title>
  6.     <link rel="stylesheet" href="styles.css">
  7. </head>
  8. <body>
  9.     <div id="game-container">
  10.         <div id="score">得分: 0</div>
  11.         <button id="start-button">开始游戏</button>
  12.         <div id="game-over">游戏结束</div>
  13.         <div id="player"></div>
  14.     </div>
  15.     <script src="script.js"></script>
  16. </body>
  17. </html>
复制代码


### CSS文件 (styles.css)

  1. body {
  2.     margin: 0;
  3.     overflow: hidden;
  4.     background-color: #000;
  5.     color: white;
  6.     font-family: Arial, sans-serif;
  7. }

  8. #game-container {
  9.     position: relative;
  10.     width: 100vw;
  11.     height: 100vh;
  12.     overflow: hidden;
  13. }

  14. #player {
  15.     position: absolute;
  16.     width: 50px; /* 调整飞机宽度 */
  17.     height: 50px; /* 调整飞机高度 */
  18.     bottom: 10px;
  19.     left: calc(50% - 25px);
  20.     background-image: url('player.png');
  21.     background-size: cover; /* 使得背景图片填满元素 */
  22. }

  23. #score {
  24.     position: absolute;
  25.     top: 10px;
  26.     left: 10px;
  27. }

  28. #start-button {
  29.     position: absolute;
  30.     top: 50%;
  31.     left: 50%;
  32.     transform: translate(-50%, -50%);
  33.     padding: 10px 20px;
  34.     font-size: 18px;
  35. }

  36. #game-over {
  37.     position: absolute;
  38.     top: 50%;
  39.     left: 50%;
  40.     transform: translate(-50%, -50%);
  41.     display: none;
  42.     font-size: 24px;
  43.     color: red;
  44. }

  45. .bullet {
  46.     position: absolute;
  47.     width: 5px; /* 调整子弹宽度 */
  48.     height: 20px; /* 调整子弹高度 */
  49.     background-image: url('bullet.png');
  50.     background-size: cover; /* 使得背景图片填满元素 */
  51.     top: calc(100% - 60px);
  52. }

  53. .enemy {
  54.     position: absolute;
  55.     width: 50px; /* 调整敌机宽度 */
  56.     height: 50px; /* 调整敌机高度 */
  57.     background-image: url('enemy.png');
  58.     background-size: cover; /* 使得背景图片填满元素 */
  59. }
复制代码


### JavaScript文件 (script.js)

  1. document.addEventListener("DOMContentLoaded", () => {
  2.     const player = document.getElementById("player");
  3.     const gameContainer = document.getElementById("game-container");
  4.     const scoreDisplay = document.getElementById("score");
  5.     const startButton = document.getElementById("start-button");
  6.     const gameOverDisplay = document.getElementById("game-over");

  7.     let score = 0;
  8.     let gameInterval;
  9.     let bulletInterval;
  10.     let enemyInterval;

  11.     startButton.addEventListener("click", startGame);

  12.     function startGame() {
  13.         startButton.style.display = "none";
  14.         gameOverDisplay.style.display = "none";
  15.         score = 0;
  16.         scoreDisplay.innerText = "得分: " + score;
  17.         gameLoop();
  18.         document.addEventListener("keydown", handleKeys);
  19.     }

  20.     function handleKeys(e) {
  21.         const key = e.code;
  22.         if (key === "ArrowLeft") {
  23.             movePlayer(-10);
  24.         } else if (key === "ArrowRight") {
  25.             movePlayer(10);
  26.         } else if (key === "Space") {
  27.             shootBullet();
  28.         }
  29.     }

  30.     function movePlayer(distance) {
  31.         let currentLeft = parseInt(window.getComputedStyle(player).left);
  32.         let newLeft = currentLeft + distance;
  33.         if (newLeft >= 0 && newLeft <= (gameContainer.offsetWidth - player.offsetWidth)) {
  34.             player.style.left = newLeft + "px";
  35.         }
  36.     }

  37.     function shootBullet() {
  38.         const bullet = document.createElement("div");
  39.         bullet.className = "bullet";
  40.         bullet.style.left = (parseInt(window.getComputedStyle(player).left) + player.offsetWidth / 2 - 2.5) + "px";
  41.         bullet.style.top = (parseInt(window.getComputedStyle(player).top) - 20) + "px";
  42.         gameContainer.appendChild(bullet);

  43.         const bulletTimer = setInterval(() => {
  44.             const bulletTop = parseInt(window.getComputedStyle(bullet).top);
  45.             bullet.style.top = (bulletTop - 5) + "px";

  46.             if (bulletTop < 0) {
  47.                 clearInterval(bulletTimer);
  48.                 bullet.remove();
  49.             }

  50.             document.querySelectorAll(".enemy").forEach(enemy => {
  51.                 if (isColliding(bullet, enemy)) {
  52.                     clearInterval(bulletTimer);
  53.                     bullet.remove();
  54.                     enemy.remove();
  55.                     score++;
  56.                     scoreDisplay.innerText = "得分: " + score;
  57.                 }
  58.             });
  59.         }, 30);
  60.         bullet._timer = bulletTimer;
  61.     }

  62.     function isColliding(element1, element2) {
  63.         const rect1 = element1.getBoundingClientRect();
  64.         const rect2 = element2.getBoundingClientRect();
  65.         return !(
  66.             rect1.top > rect2.bottom ||
  67.             rect1.right < rect2.left ||
  68.             rect1.bottom < rect2.top ||
  69.             rect1.left > rect2.right
  70.         );
  71.     }

  72.     function gameLoop() {
  73.         enemyInterval = setInterval(() => {
  74.             createEnemy();
  75.         }, 2000);
  76.     }

  77.     function createEnemy() {
  78.         const enemy = document.createElement("div");
  79.         enemy.className = "enemy";
  80.         enemy.style.left = Math.random() * (gameContainer.offsetWidth - 50) + "px";
  81.         enemy.style.top = "0px";
  82.         gameContainer.appendChild(enemy);
  83.         moveEnemy(enemy);
  84.     }

  85.     function moveEnemy(enemy) {
  86.         const enemyTimer = setInterval(() => {
  87.             const enemyTop = parseInt(window.getComputedStyle(enemy).top);
  88.             if (enemyTop >= gameContainer.offsetHeight) {
  89.                 clearInterval(enemyTimer);
  90.                 enemy.remove();
  91.             } else {
  92.                 enemy.style.top = (enemyTop + 5) + "px";
  93.             }

  94.             if (checkPlayerCollision(enemy)) {
  95.                 endGame();
  96.             }
  97.         }, 30);
  98.         enemy._timer = enemyTimer;
  99.     }

  100.     function checkPlayerCollision(enemy) {
  101.         const playerRect = player.getBoundingClientRect();
  102.         const enemyRect = enemy.getBoundingClientRect();
  103.         return !(
  104.             playerRect.top > enemyRect.bottom ||
  105.             playerRect.right < enemyRect.left ||
  106.             playerRect.bottom < enemyRect.top ||
  107.             playerRect.left > enemyRect.right
  108.         );
  109.     }

  110.     function endGame() {
  111.         clearInterval(gameInterval);
  112.         clearInterval(enemyInterval);
  113.         document.querySelectorAll(".bullet").forEach(bullet => clearInterval(bullet._timer));
  114.         document.querySelectorAll(".enemy").forEach(enemy => clearInterval(enemy._timer));
  115.         gameOverDisplay.style.display = "block";
  116.         startButton.style.display = "block";
  117.         document.removeEventListener("keydown", handleKeys);
  118.     }
  119. });
复制代码


### 说明

1. **图片背景**:确保你的图片文件与HTML文件在同一目录下,并且文件名与引用时的名称完全一致。
2. **宽度和高度**:在CSS中明确设置每个元素的宽度和高度,这样可以确保各元素按预期显示。

如果图片仍不可见,可以尝试:
- 检查浏览器控制台是否有加载图片的错误消息。
- 确认图片路径是否正确。
- 确保你的网络没有阻止图片加载。

如果还有其他问题或需要进一步帮助,请随时告诉我!????✨
回复 支持 反对

使用道具 举报

8

主题

62

帖子

184

积分

注册会员

Rank: 2

积分
184
10#
 楼主| 发表于 2024-7-26 13:58:53 | 只看该作者
kaia 发表于 2024-7-26 13:51
好的,如果你需要调整图片的大小,可以在CSS中设置图片元素的宽度和高度。下面是如何在CSS文件中修改这些 ...

为什么在游戏开始后我看不见飞机和敌机?
回复 支持 反对

使用道具 举报

0

主题

1145

帖子

1715

积分

版主

Rank: 7Rank: 7Rank: 7

积分
1715
9#
发表于 2024-7-26 13:51:26 | 只看该作者
林奕锦 发表于 2024-07-26 13:50
请告诉我改变图片大小的位置


好的,如果你需要调整图片的大小,可以在CSS中设置图片元素的宽度和高度。下面是如何在CSS文件中修改这些属性的方法。

### 修改后的CSS文件

  1. /* styles.css */
  2. body {
  3.     margin: 0;
  4.     overflow: hidden;
  5.     background-color: #000;
  6.     color: white;
  7.     font-family: Arial, sans-serif;
  8. }

  9. #game-container {
  10.     position: relative;
  11.     width: 100vw;
  12.     height: 100vh;
  13.     overflow: hidden;
  14. }

  15. #player {
  16.     position: absolute;
  17.     width: 50px; /* 调整飞机宽度 */
  18.     height: auto; /* 可以改为具体数值,比如 50px */
  19.     bottom: 10px;
  20.     left: calc(50% - 25px);
  21.     background-image: url('player.png');
  22.     background-size: cover; /* 使得背景图片填满元素 */
  23. }

  24. #score {
  25.     position: absolute;
  26.     top: 10px;
  27.     left: 10px;
  28. }

  29. #start-button {
  30.     position: absolute;
  31.     top: 50%;
  32.     left: 50%;
  33.     transform: translate(-50%, -50%);
  34.     padding: 10px 20px;
  35.     font-size: 18px;
  36. }

  37. #game-over {
  38.     position: absolute;
  39.     top: 50%;
  40.     left: 50%;
  41.     transform: translate(-50%, -50%);
  42.     display: none;
  43.     font-size: 24px;
  44.     color: red;
  45. }

  46. .bullet {
  47.     position: absolute;
  48.     width: 5px; /* 调整子弹宽度 */
  49.     height: 20px; /* 调整子弹高度 */
  50.     background-image: url('bullet.png');
  51.     background-size: cover; /* 使得背景图片填满元素 */
  52.     top: calc(100% - 60px);
  53. }

  54. .enemy {
  55.     position: absolute;
  56.     width: 50px; /* 调整敌机宽度 */
  57.     height: auto; /* 可以改为具体数值,比如 50px */
  58.     background-image: url('enemy.png');
  59.     background-size: cover; /* 使得背景图片填满元素 */
  60. }
复制代码


### 解释
- **玩家飞机**:你可以通过修改`#player`元素中的`width`和`height`属性来调整飞机的大小。如果想保持比例,可以将`height`设置为`auto`。
- **子弹**:你可以通过修改`.bullet`元素中的`width`和`height`属性来调整子弹的大小。
- **敌机**:你可以通过修改`.enemy`元素中的`width`和`height`属性来调整敌机的大小。同样,保持比例的话,可以将`height`设置为`auto`。

例如,如果你想让玩家的飞机更大,你可以把`#player`的`width`设为`80px`,子弹更小则可以把`.bullet`的`width`设为`3px`。

如果有任何其他问题或者需要进一步的帮助,请随时告诉我!✨
回复 支持 反对

使用道具 举报

8

主题

62

帖子

184

积分

注册会员

Rank: 2

积分
184
8#
 楼主| 发表于 2024-7-26 13:50:53 | 只看该作者
kaia 发表于 2024-7-26 13:49
好的,用图片来代替简单的颜色块可以让游戏更加生动有趣!我们可以使用一些图片资源来代表玩家的飞机、敌 ...

请告诉我改变图片大小的位置
回复 支持 反对

使用道具 举报

0

主题

1145

帖子

1715

积分

版主

Rank: 7Rank: 7Rank: 7

积分
1715
7#
发表于 2024-7-26 13:49:03 | 只看该作者
林奕锦 发表于 2024-07-26 13:48
我想用图片使飞机、敌机以及子弹更生动 ...


好的,用图片来代替简单的颜色块可以让游戏更加生动有趣!我们可以使用一些图片资源来代表玩家的飞机、敌机和子弹。下面是一系列修改后的代码:

### 修改后的HTML文件
  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>林奕锦的飞机大战</title>
  6.     <link rel="stylesheet" href="styles.css">
  7. </head>
  8. <body>
  9.     <div id="game-container">
  10.         <div id="score">得分: 0</div>
  11.         <button id="start-button">开始游戏</button>
  12.         <div id="game-over">游戏结束</div>
  13.         <img id="player" src="player.png" alt="玩家飞机" />
  14.     </div>
  15.     <script src="script.js"></script>
  16. </body>
  17. </html>
复制代码


### 修改后的CSS文件
  1. /* styles.css */
  2. body {
  3.     margin: 0;
  4.     overflow: hidden;
  5.     background-color: #000;
  6.     color: white;
  7.     font-family: Arial, sans-serif;
  8. }

  9. #game-container {
  10.     position: relative;
  11.     width: 100vw;
  12.     height: 100vh;
  13.     overflow: hidden;
  14. }

  15. #player {
  16.     position: absolute;
  17.     width: 50px;
  18.     height: auto;
  19.     bottom: 10px;
  20.     left: calc(50% - 25px);
  21. }

  22. #score {
  23.     position: absolute;
  24.     top: 10px;
  25.     left: 10px;
  26. }

  27. #start-button {
  28.     position: absolute;
  29.     top: 50%;
  30.     left: 50%;
  31.     transform: translate(-50%, -50%);
  32.     padding: 10px 20px;
  33.     font-size: 18px;
  34. }

  35. #game-over {
  36.     position: absolute;
  37.     top: 50%;
  38.     left: 50%;
  39.     transform: translate(-50%, -50%);
  40.     display: none;
  41.     font-size: 24px;
  42.     color: red;
  43. }

  44. .bullet {
  45.     position: absolute;
  46.     width: 5px;
  47.     height: 20px;
  48.     background-image: url('bullet.png');
  49.     background-size: cover;
  50.     top: calc(100% - 60px);
  51. }

  52. .enemy {
  53.     position: absolute;
  54.     width: 50px;
  55.     height: auto;
  56.     background-image: url('enemy.png');
  57.     background-size: cover;
  58. }
复制代码


### 修改后的JavaScript文件
  1. // script.js
  2. document.addEventListener("DOMContentLoaded", () => {
  3.     const player = document.getElementById("player");
  4.     const gameContainer = document.getElementById("game-container");
  5.     const scoreDisplay = document.getElementById("score");
  6.     const startButton = document.getElementById("start-button");
  7.     const gameOverDisplay = document.getElementById("game-over");

  8.     let score = 0;
  9.     let gameInterval;
  10.     let bulletInterval;
  11.     let enemyInterval;

  12.     startButton.addEventListener("click", startGame);

  13.     function startGame() {
  14.         startButton.style.display = "none";
  15.         gameOverDisplay.style.display = "none";
  16.         score = 0;
  17.         scoreDisplay.innerText = "得分: " + score;
  18.         gameLoop();
  19.         document.addEventListener("keydown", handleKeys);
  20.     }

  21.     function handleKeys(e) {
  22.         const key = e.code;
  23.         if (key === "ArrowLeft") {
  24.             movePlayer(-10);
  25.         } else if (key === "ArrowRight") {
  26.             movePlayer(10);
  27.         } else if (key === "Space") {
  28.             shootBullet();
  29.         }
  30.     }

  31.     function movePlayer(distance) {
  32.         let currentLeft = parseInt(window.getComputedStyle(player).left);
  33.         let newLeft = currentLeft + distance;
  34.         if (newLeft >= 0 && newLeft <= gameContainer.offsetWidth - player.offsetWidth) {
  35.             player.style.left = newLeft + "px";
  36.         }
  37.     }

  38.     function shootBullet() {
  39.         const bullet = document.createElement("div");
  40.         bullet.className = "bullet";
  41.         bullet.style.left = (parseInt(window.getComputedStyle(player).left) + player.offsetWidth / 2 - 2.5) + "px";
  42.         bullet.style.top = (parseInt(window.getComputedStyle(player).top) - 20) + "px";
  43.         gameContainer.appendChild(bullet);
  44.         moveBullet(bullet);
  45.     }

  46.     function moveBullet(bullet) {
  47.         const bulletTimer = setInterval(() => {
  48.             const bulletTop = parseInt(window.getComputedStyle(bullet).top);
  49.             if (bulletTop <= 0) {
  50.                 clearInterval(bulletTimer);
  51.                 bullet.remove();
  52.             } else {
  53.                 bullet.style.top = (bulletTop - 5) + "px";
  54.             }
  55.             document.querySelectorAll(".enemy").forEach(enemy => {
  56.                 if (isColliding(bullet, enemy)) {
  57.                     clearInterval(bulletTimer);
  58.                     bullet.remove();
  59.                     enemy.remove();
  60.                     score++;
  61.                     scoreDisplay.innerText = "得分: " + score;
  62.                 }
  63.             });
  64.         }, 30);
  65.         bullet._timer = bulletTimer;
  66.     }

  67.     function isColliding(element1, element2) {
  68.         const rect1 = element1.getBoundingClientRect();
  69.         const rect2 = element2.getBoundingClientRect();
  70.         return !(
  71.             rect1.top > rect2.bottom ||
  72.             rect1.right < rect2.left ||
  73.             rect1.bottom < rect2.top ||
  74.             rect1.left > rect2.right
  75.         );
  76.     }

  77.     function gameLoop() {
  78.         enemyInterval = setInterval(() => {
  79.             createEnemy();
  80.         }, 2000);
  81.     }

  82.     function createEnemy() {
  83.         const enemy = document.createElement("div");
  84.         enemy.className = "enemy";
  85.         enemy.style.left = Math.random() * (gameContainer.offsetWidth - 50) + "px";
  86.         enemy.style.top = "0px";
  87.         gameContainer.appendChild(enemy);
  88.         moveEnemy(enemy);
  89.     }

  90.     function moveEnemy(enemy) {
  91.         const enemyTimer = setInterval(() => {
  92.             const enemyTop = parseInt(window.getComputedStyle(enemy).top);
  93.             if (enemyTop >= gameContainer.offsetHeight) {
  94.                 clearInterval(enemyTimer);
  95.                 enemy.remove();
  96.             } else {
  97.                 enemy.style.top = (enemyTop + 5) + "px";
  98.             }

  99.             if (checkPlayerCollision(enemy)) {
  100.                 endGame();
  101.             }
  102.         }, 30);
  103.         enemy._timer = enemyTimer;
  104.     }

  105.     function checkPlayerCollision(enemy) {
  106.         const playerRect = player.getBoundingClientRect();
  107.         const enemyRect = enemy.getBoundingClientRect();
  108.         return !(
  109.             playerRect.top > enemyRect.bottom ||
  110.             playerRect.right < enemyRect.left ||
  111.             playerRect.bottom < enemyRect.top ||
  112.             playerRect.left > enemyRect.right
  113.         );
  114.     }

  115.     function endGame() {
  116.         clearInterval(gameInterval);
  117.         clearInterval(enemyInterval);
  118.         document.querySelectorAll(".bullet").forEach(bullet => clearInterval(bullet._timer));
  119.         document.querySelectorAll(".enemy").forEach(enemy => clearInterval(enemy._timer));
  120.         gameOverDisplay.style.display = "block";
  121.         startButton.style.display = "block";
  122.         document.removeEventListener("keydown", handleKeys);
  123.     }
  124. });
复制代码


### 图片资源
为了使这个游戏更真实,你需要准备三张图片:玩家飞机(`player.png`)、敌机(`enemy.png`)和子弹(`bullet.png`)。你可以在网上搜索这些图片,或者自己画一画。确保这些图片放在与你的HTML、CSS和JavaScript文件同一文件夹内。

### 注意事项
- 确保所有文件名和路径正确,一旦图片文件名称或路径不一致,无法正确加载图片。
- 你可以根据需要调整子弹、敌机和玩家飞机的大小,以适合你的游戏界面。

现在,把这些代码保存到相应的文件中,然后在浏览器中打开 `index.html` 文件,你就能看到一个更加生动的飞机大战游戏啦!

如果还有其他问题或者需要进一步的帮助,请随时告诉我哦!✨????
回复 支持 反对

使用道具 举报

8

主题

62

帖子

184

积分

注册会员

Rank: 2

积分
184
6#
 楼主| 发表于 2024-7-26 13:48:30 | 只看该作者
林奕锦 发表于 2024-7-26 13:44
我想在飞机碰到敌机时结束游戏

我想用图片使飞机、敌机以及子弹更生动
回复 支持 反对

使用道具 举报

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

本版积分规则

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