科学教育

搜索
查看: 4782|回复: 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
沙发
发表于 2024-7-25 21:26:23 | 只看该作者
好的,小朋友!我们来一起做一个有趣的飞机大战游戏。这个游戏会用到HTML、CSS和JavaScript三种语言。别担心,我会一步一步地教你,保证你能理解每一部分内容哦!

### 1. HTML文件
首先,我们先写个简单的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="player"></div>
  12.         <div id="score">得分: 0</div>
  13.         <button id="start-button">开始游戏</button>
  14.     </div>

  15.     <script src="script.js"></script>
  16. </body>
  17. </html>
复制代码


这个文件中,我们定义了游戏的容器(`game-container`),玩家的飞机(`player`),得分显示(`score`)和开始按钮(`start-button`)。

### 2. 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. }
复制代码


这段CSS代码让背景变黑,飞机是蓝色的,并且设置了一个得分显示的位置。

### 3. JavaScript文件
最后,我们来编写控制游戏逻辑的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.     let score = 0;
  8.     let gameInterval;
  9.     let bulletInterval;

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

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

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

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

  35.     function shootBullet() {
  36.         const bullet = document.createElement("div");
  37.         bullet.className = "bullet";
  38.         bullet.style.left = player.style.left;
  39.         gameContainer.appendChild(bullet);
  40.         moveBullet(bullet);
  41.     }

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

  54.     function checkCollision(bullet, bulletTimer) {
  55.         const enemies = document.querySelectorAll(".enemy");
  56.         enemies.forEach(enemy => {
  57.             if (isColliding(bullet, enemy)) {
  58.                 clearInterval(bulletTimer);
  59.                 bullet.remove();
  60.                 enemy.remove();
  61.                 score++;
  62.                 scoreDisplay.innerText = "得分: " + score;
  63.             }
  64.         });
  65.     }

  66.     function isColliding(bullet, enemy) {
  67.         const bulletRect = bullet.getBoundingClientRect();
  68.         const enemyRect = enemy.getBoundingClientRect();
  69.         return !(
  70.             bulletRect.top > enemyRect.bottom ||
  71.             bulletRect.right < enemyRect.left ||
  72.             bulletRect.bottom < enemyRect.top ||
  73.             bulletRect.left > enemyRect.right
  74.         );
  75.     }

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

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

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

  100.     const style = document.createElement('style');
  101.     style.innerHTML = `
  102.         .bullet {
  103.             position: absolute;
  104.             width: 5px;
  105.             height: 20px;
  106.             background-color: red;
  107.             top: calc(100% - 60px);
  108.         }
  109.         .enemy {
  110.             position: absolute;
  111.             width: 50px;
  112.             height: 50px;
  113.             background-color: green;
  114.         }
  115.     `;
  116.     document.head.appendChild(style);
  117. });
复制代码


这个JavaScript代码实现了以下功能:
- 按下“开始游戏”按钮时,游戏开始。
- 玩家可以用左右箭头键移动飞机,用空格键发射子弹。
- 子弹会向上飞,并摧毁敌机。
- 敌机会从上方随机位置出现并向下移动。
- 玩家得分会实时更新。

这样我们就完成了一个简单的飞机大战游戏啦!你可以把这些代码分别保存到三个文件(`index.html`、`styles.css`、`script.js`)中,然后在浏览器中打开`index.html`文件,就可以开始玩了。

有问题随时问我哦!祝你玩得开心!????✨
回复 支持 反对

使用道具 举报

8

主题

62

帖子

184

积分

注册会员

Rank: 2

积分
184
板凳
 楼主| 发表于 2024-7-25 22:11:55 | 只看该作者
kaia 发表于 2024-7-25 21:26
好的,小朋友!我们来一起做一个有趣的飞机大战游戏。这个游戏会用到HTML、CSS和JavaScript三种语言。别担 ...

我想在飞机碰到敌机时结束游戏,并显示游戏结束界面,谢谢!
回复 支持 反对

使用道具 举报

8

主题

62

帖子

184

积分

注册会员

Rank: 2

积分
184
地板
 楼主| 发表于 2024-7-26 13:44:25 | 只看该作者
kaia 发表于 2024-7-25 21:26
好的,小朋友!我们来一起做一个有趣的飞机大战游戏。这个游戏会用到HTML、CSS和JavaScript三种语言。别担 ...

我想在飞机碰到敌机时结束游戏
回复 支持 反对

使用道具 举报

8

主题

62

帖子

184

积分

注册会员

Rank: 2

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

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

使用道具 举报

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

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

使用道具 举报

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

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

使用道具 举报

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

本版积分规则

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