科学教育

搜索
查看: 4599|回复: 16
打印 上一主题 下一主题

王桃桃2

[复制链接]

29

主题

107

帖子

348

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
348
跳转到指定楼层
#
发表于 2024-7-16 20:50:58 | 只看该作者 回帖奖励 |正序浏览 |阅读模式
你好,我有一个贪吃蛇的小游戏代码,可以帮我分析和改编一下代码吗?
回复

使用道具 举报

0

主题

1145

帖子

1715

积分

版主

Rank: 7Rank: 7Rank: 7

积分
1715
16#
发表于 2024-7-18 16:56:45 | 只看该作者
ZLJ 发表于 2024-07-18 16:52
你好,我刚学习了代码的控制结构,我想给贪吃蛇增加游 ...


当然可以!在贪吃蛇游戏中增加游戏暂停和继续的功能是一个非常好的练习。我们可以通过添加一个新的按钮来实现这个功能,并在JavaScript代码中控制游戏的暂停和继续状态。

### 修改思路

1. **HTML**:添加一个“暂停/继续”按钮。
2. **CSS**:对新按钮进行样式设置。
3. **JavaScript**:添加控制游戏暂停和继续逻辑的功能。

### HTML部分

在你的HTML中添加一个新的“暂停/继续”按钮:



    贪吃蛇游戏
   


    欢迎来到贪吃蛇游戏!

   
   
   
        游戏结束
        很遗憾!得分: 0
        开始游戏
   
   
     查看规则
   
        游戏规则:
        
            用方向键控制蛇的移动。
            吃到食物得分并延长蛇身。
            撞到墙壁或自己的身体则游戏结束。

   
   
    暂停/继续
   




### CSS部分

对新按钮进行样式设置:

/* styles.css */
body {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    margin: 0;
    background-color: #000;
    flex-direction: column;
}

canvas {
    border: 1px solid #fff;
}

#gameOver {
    position: absolute;
    text-align: center;
    color: white;
}

button {
    padding: 10px 20px;
    font-size: 16px;
    color: black;
    background-color: #9370DB;
    border: none;
    border-radius: 5px;
    cursor: pointer;
}

/* 新增规则样式 */
#gameRules {
    color: white;
    margin-top: 20px;
    background: rgba(0, 0, 0, 0.7);
    padding: 10px;
    border-radius: 5px;
    display: none; /* 默认隐藏 */
}

### JavaScript部分

在JavaScript中添加控制游戏暂停和继续逻辑的功能:

// script.js
const canvas = document.getElementById("gameCanvas");
const ctx = canvas.getContext("2d");

canvas.width = 400;
canvas.height = 400;

const box = 20;
let snake;
let food;
let d;
let score;
let game;
let isPaused = false; // 新增一个状态变量表示游戏是否暂停

// 获取音频元素
const eatSound = document.getElementById("eatSound");

// 播放音效函数
function playEatSound() {
    eatSound.currentTime = 0; // 每次播放前重置播放时间
    eatSound.play();
}

// 初始化游戏
function initGame() {
    snake = ; // 初始蛇的位置
    food = {
        x: Math.floor(Math.random() * 19 + 1) * box,
        y: Math.floor(Math.random() * 19 + 1) * box
    };
    score = 0;
    d = null;
}

// 启动游戏
function startGame() {
    document.getElementById("gameOver").style.display = "none";
    initGame();
    if (game) clearInterval(game);
    game = setInterval(draw, 500); // 设置游戏速度为500毫秒
}

// 暂停/继续游戏
function togglePause() {
    if (isPaused) {
        game = setInterval(draw, 500); // 继续游戏
        isPaused = false;
    } else {
        clearInterval(game); // 暂停游戏
        isPaused = true;
    }
}

// 监听键盘事件
document.addEventListener("keydown", direction);
function direction(event) {
    if (event.keyCode == 37 && d !== "RIGHT") {
        d = "LEFT";
    } else if (event.keyCode == 38 && d !== "DOWN") {
        d = "UP";
    } else if (event.keyCode == 39 && d !== "LEFT") {
        d = "RIGHT";
    } else if (event.keyCode == 40 && d !== "UP") {
        d = "DOWN";
    }
}

function collision(newHead, array) {
    for (let i = 0; i < array.length; i++) {
        if (newHead.x === array.x && newHead.y === array.y) {
            return true;
        }
    }
    return false;
}

// 绘制游戏
function draw() {
    ctx.fillStyle = "#000";
    ctx.fillRect(0, 0, canvas.width, canvas.height);

    for (let i = 0; i < snake.length; i++) {
        ctx.fillStyle = (i === 0) ? "green" : "white";
        ctx.fillRect(snake.x, snake.y, box, box);

        ctx.strokeStyle = "red";
        ctx.strokeRect(snake.x, snake.y, box, box);
    }

    ctx.fillStyle = "red";
    ctx.fillRect(food.x, food.y, box, box);

    let snakeX = snake.x;
    let snakeY = snake.y;

    if (d === "LEFT") snakeX -= box;
    if (d === "UP") snakeY -= box;
    if (d === "RIGHT") snakeX += box;
    if (d === "DOWN") snakeY += box;

    if (snakeX === food.x && snakeY === food.y) {
        score += 1; // 每次吃到食物时分数增加1
        
        // 播放音效
        playEatSound();

        food = {
            x: Math.floor(Math.random() * 19 + 1) * box,
            y: Math.floor(Math.random() * 19 + 1) * box
        };
    } else {
        snake.pop();
    }

    let newHead = { x: snakeX, y: snakeY };

    if (snakeX < 0 || snakeY < 0 || snakeX >= canvas.width || snakeY >= canvas.height || collision(newHead, snake)) {
        clearInterval(game);
        document.getElementById("score").innerText = score;
        document.getElementById("gameOver").style.display = "block";
        return;
    }

    snake.unshift(newHead);

    ctx.fillStyle = "white";
    ctx.font = "45px Changa one";
    ctx.fillText(score, 2 * box, 1.6 * box);
}

// 添加鼠标悬停事件
const viewRulesButton = document.getElementById('viewRulesButton');
const gameRules = document.getElementById('gameRules');

viewRulesButton.addEventListener('mouseenter', () => {
    gameRules.style.display = 'block';
});

viewRulesButton.addEventListener('mouseleave', () => {
    gameRules.style.display = 'none';
});

// 监听“暂停/继续”按钮点击事件
const pauseContinueButton = document.getElementById('pauseContinueButton');
pauseContinueButton.addEventListener('click', togglePause);

// 开始游戏
initGame();
startGame();

### 代码解释

1. **isPaused**:一个布尔值,用于存储游戏是否处于暂停状态。
2. **togglePause**:切换游戏暂停和继续状态的函数。
3. **事件监听器**:在“暂停/继续”按钮上添加一个点击事件监听器。

这样,当你点击“暂停/继续”按钮时,游戏将会暂停或继续。试试看,如果有任何问题或需要进一步的修改,请随时告诉我哦!
回复 支持 反对

使用道具 举报

0

主题

1145

帖子

1715

积分

版主

Rank: 7Rank: 7Rank: 7

积分
1715
15#
发表于 2024-7-18 16:56:00 | 只看该作者
ZLJ 发表于 2024-07-18 16:52
你好,我刚学习了代码的控制结构,我想给贪吃蛇增加游 ...


当然可以!为了实现游戏的暂停和继续功能,我们可以添加一个按钮来切换游戏的状态,以及在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.     <h1>贪吃蛇游戏</h1>

  11.     <p>欢迎来到贪吃蛇游戏!</p>

  12.     <canvas id="gameCanvas"></canvas>

  13.     <div id="gameOver" style="display:none;">
  14.         <h2>游戏结束</h2>
  15.         <p>很遗憾!得分: <span id="score">0</span></p>
  16.         <button onclick="startGame()">开始游戏</button>
  17.     </div>

  18.     <button id="pauseButton">暂停</button>
  19.     <button id="viewRulesButton">查看规则</button>
  20.     <div id="gameRules" style="display:none;">
  21.         <h3>游戏规则:</h3>
  22.         <ul>
  23.             <li>用方向键控制蛇的移动。</li>
  24.             <li>吃到食物得分并延长蛇身。</li>
  25.             <li>撞到墙壁或自己的身体则游戏结束。</li>
  26.         </ul>
  27.     </div>

  28.     <audio id="eatSound" src="audio/eat.mp3"></audio>
  29.     <script src="script.js"></script>

  30. </body>
  31. </html>
复制代码


### CSS部分

CSS部分保持不变,继续使用已有的样式。

  1. /* styles.css */
  2. body {
  3.     display: flex;
  4.     justify-content: center;
  5.     align-items: center;
  6.     height: 100vh;
  7.     margin: 0;
  8.     background-color: #000;
  9.     flex-direction: column;
  10. }

  11. canvas {
  12.     border: 1px solid #fff;
  13. }

  14. #gameOver {
  15.     position: absolute;
  16.     text-align: center;
  17.     color: white;
  18. }

  19. button {
  20.     padding: 10px 20px;
  21.     font-size: 16px;
  22.     color: black;
  23.     background-color: #9370DB;
  24.     border: none;
  25.     border-radius: 5px;
  26.     cursor: pointer;
  27. }

  28. /* 新增规则样式 */
  29. #gameRules {
  30.     color: white;
  31.     margin-top: 20px;
  32.     background: rgba(0, 0, 0, 0.7);
  33.     padding: 10px;
  34.     border-radius: 5px;
  35. }
复制代码


### JavaScript部分

在JavaScript中,添加暂停和继续功能。

  1. // script.js
  2. const canvas = document.getElementById("gameCanvas");
  3. const ctx = canvas.getContext("2d");

  4. canvas.width = 400;
  5. canvas.height = 400;

  6. const box = 20;
  7. let snake;
  8. let food;
  9. let d;
  10. let score;
  11. let game;
  12. let gamePaused = false; // 添加一个变量来表示游戏是否暂停

  13. // 获取音频元素
  14. const eatSound = document.getElementById("eatSound");

  15. // 播放音效函数
  16. function playEatSound() {
  17.     eatSound.currentTime = 0; // 每次播放前重置播放时间
  18.     eatSound.play();
  19. }

  20. // 初始化游戏
  21. function initGame() {
  22.     snake = [{ x: 9 * box, y: 10 * box }]; // 初始蛇的位置
  23.     food = {
  24.         x: Math.floor(Math.random() * 19 + 1) * box,
  25.         y: Math.floor(Math.random() * 19 + 1) * box
  26.     };
  27.     score = 0;
  28.     d = null;
  29. }

  30. // 启动游戏
  31. function startGame() {
  32.     document.getElementById("gameOver").style.display = "none";
  33.     initGame();
  34.     if (game) clearInterval(game);
  35.     game = setInterval(draw, 500); // 设置游戏速度为500毫秒
  36. }

  37. // 暂停/继续游戏
  38. function togglePause() {
  39.     if (gamePaused) {
  40.         game = setInterval(draw, 500); // 恢复游戏
  41.         document.getElementById("pauseButton").innerText = "暂停";
  42.     } else {
  43.         clearInterval(game); // 暂停游戏
  44.         document.getElementById("pauseButton").innerText = "继续";
  45.     }
  46.     gamePaused = !gamePaused;
  47. }

  48. // 监听键盘事件
  49. document.addEventListener("keydown", direction);
  50. function direction(event) {
  51.     if (event.keyCode == 37 && d !== "RIGHT") {
  52.         d = "LEFT";
  53.     } else if (event.keyCode == 38 && d !== "DOWN") {
  54.         d = "UP";
  55.     } else if (event.keyCode == 39 && d !== "LEFT") {
  56.         d = "RIGHT";
  57.     } else if (event.keyCode == 40 && d !== "UP") {
  58.         d = "DOWN";
  59.     }
  60. }

  61. function collision(newHead, array) {
  62.     for (let i = 0; i < array.length; i++) {
  63.         if (newHead.x === array[i].x && newHead.y === array[i].y) {
  64.             return true;
  65.         }
  66.     }
  67.     return false;
  68. }

  69. // 绘制游戏
  70. function draw() {
  71.     ctx.fillStyle = "#000";
  72.     ctx.fillRect(0, 0, canvas.width, canvas.height);

  73.     for (let i = 0; i < snake.length; i++) {
  74.         ctx.fillStyle = (i === 0) ? "green" : "white";
  75.         ctx.fillRect(snake[i].x, snake[i].y, box, box);

  76.         ctx.strokeStyle = "red";
  77.         ctx.strokeRect(snake[i].x, snake[i].y, box, box);
  78.     }

  79.     ctx.fillStyle = "red";
  80.     ctx.fillRect(food.x, food.y, box, box);

  81.     let snakeX = snake[0].x;
  82.     let snakeY = snake[0].y;

  83.     if (d === "LEFT") snakeX -= box;
  84.     if (d === "UP") snakeY -= box;
  85.     if (d === "RIGHT") snakeX += box;
  86.     if (d === "DOWN") snakeY += box;

  87.     if (snakeX === food.x && snakeY === food.y) {
  88.         score += 1; // 每次吃到食物时分数增加1
  89.         
  90.         // 播放音效
  91.         playEatSound();

  92.         food = {
  93.             x: Math.floor(Math.random() * 19 + 1) * box,
  94.             y: Math.floor(Math.random() * 19 + 1) * box
  95.         };
  96.     } else {
  97.         snake.pop();
  98.     }

  99.     let newHead = { x: snakeX, y: snakeY };

  100.     if (snakeX < 0 || snakeY < 0 || snakeX >= canvas.width || snakeY >= canvas.height || collision(newHead, snake)) {
  101.         clearInterval(game);
  102.         document.getElementById("score").innerText = score;
  103.         document.getElementById("gameOver").style.display = "block";
  104.         return;
  105.     }

  106.     snake.unshift(newHead);

  107.     ctx.fillStyle = "white";
  108.     ctx.font = "45px Changa one";
  109.     ctx.fillText(score, 2 * box, 1.6 * box);
  110. }

  111. // 添加鼠标悬停事件
  112. const viewRulesButton = document.getElementById('viewRulesButton');
  113. const gameRules = document.getElementById('gameRules');

  114. viewRulesButton.addEventListener('mouseenter', () => {
  115.     gameRules.style.display = 'block';
  116. });

  117. viewRulesButton.addEventListener('mouseleave', () => {
  118.     gameRules.style.display = 'none';
  119. });

  120. // 添加暂停按钮点击事件
  121. document.getElementById('pauseButton').addEventListener('click', togglePause);

  122. // 开始游戏
  123. initGame();
  124. startGame();
复制代码


### 代码解释

1. **HTML**:
   - 在HTML中添加了一个“暂停”按钮 `<button id="pauseButton">暂停</button>`。

2. **JavaScript**:
   - 添加了一个 `gamePaused` 变量以表示游戏是否处于暂停状态。
   - 定义了 `togglePause` 函数,用来切换游戏的暂停和继续状态:
     - 如果游戏暂停,则重新启动计时器并将按钮文本设置为“暂停”。
     - 如果游戏在运行,则清除计时器并将按钮文本设置为“继续”。
   - 为“暂停”按钮添加点击事件监听器,调用 `togglePause` 函数。

这样,当你点击“暂停”按钮时,游戏会暂停;再次点击该按钮,游戏会继续。试试看,如果有任何问题或进一步的需求,请随时告诉我哦!
回复 支持 反对

使用道具 举报

0

主题

1145

帖子

1715

积分

版主

Rank: 7Rank: 7Rank: 7

积分
1715
14#
发表于 2024-7-18 16:55:45 | 只看该作者
ZLJ 发表于 2024-07-18 16:55
你好,我刚学习了代码的控制结构,我想给贪吃蛇增加游 ...


当然可以!首先,我们要理解如何用代码来实现暂停和继续的功能。其实,这个过程就像是我们在游戏中按下“暂停”按钮,然后用另一个按钮来恢复游戏。

### 步骤一:添加监听键盘事件
我们需要让程序知道什么时候按下了“暂停”键和“继续”键。一般情况下,我们会用键盘的某些特定按键来控制,比如“P”键用来暂停,“R”键用来恢复。

### 步骤二:建立标识符
我们需要一个变量来表示游戏是暂停的还是正在进行的。这就好像在问自己:“现在的游戏状态是暂停还是在运行呢?”

### 步骤三:修改游戏的主循环
我们还要调整贪吃蛇游戏的主循环,让它能够根据我们设定的状态来决定是否继续运行。

下面是一个简单的示例代码,假设你已经有一个基本的贪吃蛇游戏了。我们只需要在你的代码基础上添加这些部分:

  1. // 假设这是你贪吃蛇游戏的已有代码
  2. let isPaused = false; // 游戏是否暂停的标志

  3. // 监听键盘事件
  4. document.addEventListener('keydown', function(event) {
  5.     if (event.key === 'p' || event.key === 'P') {
  6.         // 按下P键,暂停游戏
  7.         isPaused = true;
  8.     } else if (event.key === 'r' || event.key === 'R') {
  9.         // 按下R键,继续游戏
  10.         isPaused = false;
  11.     }
  12. });

  13. // 游戏的主循环
  14. function gameLoop() {
  15.     if (!isPaused) {
  16.         // 只有在游戏未暂停时,才进行更新和绘制
  17.         updateGame(); // 更新蛇的位置和食物等游戏状态
  18.         drawGame(); // 绘制游戏画面
  19.     }
  20.    
  21.     requestAnimationFrame(gameLoop); // 下一帧继续循环
  22. }

  23. // 更新游戏函数
  24. function updateGame() {
  25.     // 更新贪吃蛇的位置、判断碰撞等等
  26. }

  27. // 绘制游戏函数
  28. function drawGame() {
  29.     // 将贪吃蛇和食物绘制到屏幕上
  30. }

  31. // 启动游戏
  32. requestAnimationFrame(gameLoop);
复制代码


在这个示例中,我们做了以下几件事:

1. **设置标识符`isPaused`**:用来标记当前游戏是否处于暂停状态。
2. **监听键盘事件**:当用户按下“P”键时,游戏变为暂停状态;当用户按下“R”键时,游戏恢复运行。
3. **修改游戏主循环`gameLoop`**:如果游戏没有暂停(`isPaused`为false),则更新并绘制游戏。否则不做任何更新或绘制工作。

这样,你的贪吃蛇游戏就可以通过按下特定按键来暂停和继续了。希望这能帮到你!如果你还有其他问题,随时来问我哦!
回复 支持 反对

使用道具 举报

29

主题

107

帖子

348

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
348
13#
 楼主| 发表于 2024-7-18 16:55:12 | 只看该作者
你好,我刚学习了代码的控制结构,我想给贪吃蛇增加游戏暂停和继续的功能,你可以帮我修改吗?@ai
回复 支持 反对

使用道具 举报

29

主题

107

帖子

348

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
348
12#
 楼主| 发表于 2024-7-18 16:52:58 | 只看该作者
kaia 发表于 2024-7-18 15:13
当然可以!我们可以使用鼠标悬停事件来实现这个功能。当鼠标移动到“查看规则”按钮上时,显示游戏规则的 ...

你好,我刚学习了代码的控制结构,我想给贪吃蛇增加游戏暂停和继续的功能,你可以帮我修改吗?
回复 支持 反对

使用道具 举报

0

主题

1145

帖子

1715

积分

版主

Rank: 7Rank: 7Rank: 7

积分
1715
11#
发表于 2024-7-18 15:13:30 | 只看该作者
ZLJ 发表于 2024-07-18 15:12
我还想用鼠标悬停事件实现一个功能,这个功能是当我鼠 ...


当然可以!我们可以使用鼠标悬停事件来实现这个功能。当鼠标移动到“查看规则”按钮上时,显示游戏规则的文字;当鼠标移开时,隐藏这些文字。

### HTML部分

在你的HTML中添加一个新的按钮和一个用于显示游戏规则的`div`。比如,可以在现有的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.     <link rel="stylesheet" href="styles.css">
  7.     <title>贪吃蛇游戏</title>
  8. </head>
  9. <body>
  10.     <h1>贪吃蛇游戏</h1>
  11.     <canvas id="gameCanvas"></canvas>
  12.     <div id="gameOver" style="display:none;">
  13.         <h2>游戏结束</h2>
  14.         <p>很遗憾!得分: <span id="score">0</span></p>
  15.         <button onclick="startGame()">开始游戏</button>
  16.     </div>
  17.    
  18.     <!-- 新增查看规则按钮和规则文字 -->
  19.     <button id="viewRulesButton">查看规则</button>
  20.     <div id="gameRules" style="display:none;">
  21.         <p>游戏规则:</p>
  22.         <ul>
  23.             <li>用方向键控制蛇的移动。</li>
  24.             <li>吃到食物得分并延长蛇身。</li>
  25.             <li>撞到墙壁或自己的身体则游戏结束。</li>
  26.         </ul>
  27.     </div>

  28.     <!-- 增加音效文件 -->
  29.     <audio id="eatSound" src="audio/eat.mp3"></audio>

  30.     <script src="script.js"></script>
  31. </body>
  32. </html>
复制代码


### CSS部分

添加一些CSS以美化显示的规则文字:

  1. /* styles.css */
  2. body {
  3.     display: flex;
  4.     justify-content: center;
  5.     align-items: center;
  6.     height: 100vh;
  7.     margin: 0;
  8.     background-color: #000;
  9.     flex-direction: column;
  10. }

  11. canvas {
  12.     border: 1px solid #fff;
  13. }

  14. #gameOver {
  15.     position: absolute;
  16.     text-align: center;
  17.     color: white;
  18. }

  19. button {
  20.     padding: 10px 20px;
  21.     font-size: 16px;
  22.     color: black;
  23.     background-color: #9370DB;
  24.     border: none;
  25.     border-radius: 5px;
  26.     cursor: pointer;
  27. }

  28. /* 新增规则样式 */
  29. #gameRules {
  30.     color: white;
  31.     margin-top: 20px;
  32.     background: rgba(0, 0, 0, 0.7);
  33.     padding: 10px;
  34.     border-radius: 5px;
  35. }
复制代码


### JavaScript部分

接下来,在JavaScript中添加鼠标悬停事件的监听:

  1. // script.js
  2. const canvas = document.getElementById("gameCanvas");
  3. const ctx = canvas.getContext("2d");

  4. canvas.width = 400;
  5. canvas.height = 400;

  6. const box = 20;
  7. let snake;
  8. let food;
  9. let d;
  10. let score;
  11. let game;

  12. // 获取音频元素
  13. const eatSound = document.getElementById("eatSound");

  14. // 播放音效函数
  15. function playEatSound() {
  16.     eatSound.currentTime = 0; // 每次播放前重置播放时间
  17.     eatSound.play();
  18. }

  19. // 初始化游戏
  20. function initGame() {
  21.     snake = ; // 初始蛇的位置
  22.     food = {
  23.         x: Math.floor(Math.random() * 19 + 1) * box,
  24.         y: Math.floor(Math.random() * 19 + 1) * box
  25.     };
  26.     score = 0;
  27.     d = null;
  28. }

  29. // 启动游戏
  30. function startGame() {
  31.     document.getElementById("gameOver").style.display = "none";
  32.     initGame();
  33.     if (game) clearInterval(game);
  34.     game = setInterval(draw, 500); // 设置游戏速度为500毫秒
  35. }

  36. // 监听键盘事件
  37. document.addEventListener("keydown", direction);
  38. function direction(event) {
  39.     if (event.keyCode == 37 && d !== "RIGHT") {
  40.         d = "LEFT";
  41.     } else if (event.keyCode == 38 && d !== "DOWN") {
  42.         d = "UP";
  43.     } else if (event.keyCode == 39 && d !== "LEFT") {
  44.         d = "RIGHT";
  45.     } else if (event.keyCode == 40 && d !== "UP") {
  46.         d = "DOWN";
  47.     }
  48. }

  49. function collision(newHead, array) {
  50.     for (let i = 0; i < array.length; i++) {
  51.         if (newHead.x === array[i].x && newHead.y === array[i].y) {
  52.             return true;
  53.         }
  54.     }
  55.     return false;
  56. }

  57. // 绘制游戏
  58. function draw() {
  59.     ctx.fillStyle = "#000";
  60.     ctx.fillRect(0, 0, canvas.width, canvas.height);

  61.     for (let i = 0; i < snake.length; i++) {
  62.         ctx.fillStyle = (i === 0) ? "green" : "white";
  63.         ctx.fillRect(snake[i].x, snake[i].y, box, box);

  64.         ctx.strokeStyle = "red";
  65.         ctx.strokeRect(snake[i].x, snake[i].y, box, box);
  66.     }

  67.     ctx.fillStyle = "red";
  68.     ctx.fillRect(food.x, food.y, box, box);

  69.     let snakeX = snake[0].x;
  70.     let snakeY = snake[0].y;

  71.     if (d === "LEFT") snakeX -= box;
  72.     if (d === "UP") snakeY -= box;
  73.     if (d === "RIGHT") snakeX += box;
  74.     if (d === "DOWN") snakeY += box;

  75.     if (snakeX === food.x && snakeY === food.y) {
  76.         score += 1; // 每次吃到食物时分数增加1
  77.         
  78.         // 播放音效
  79.         playEatSound();

  80.         food = {
  81.             x: Math.floor(Math.random() * 19 + 1) * box,
  82.             y: Math.floor(Math.random() * 19 + 1) * box
  83.         };
  84.     } else {
  85.         snake.pop();
  86.     }

  87.     let newHead = { x: snakeX, y: snakeY };

  88.     if (snakeX < 0 || snakeY < 0 || snakeX >= canvas.width || snakeY >= canvas.height || collision(newHead, snake)) {
  89.         clearInterval(game);
  90.         document.getElementById("score").innerText = score;
  91.         document.getElementById("gameOver").style.display = "block";
  92.         return;
  93.     }

  94.     snake.unshift(newHead);

  95.     ctx.fillStyle = "white";
  96.     ctx.font = "45px Changa one";
  97.     ctx.fillText(score, 2 * box, 1.6 * box);
  98. }

  99. // 添加鼠标悬停事件
  100. const viewRulesButton = document.getElementById('viewRulesButton');
  101. const gameRules = document.getElementById('gameRules');

  102. viewRulesButton.addEventListener('mouseenter', () => {
  103.     gameRules.style.display = 'block';
  104. });

  105. viewRulesButton.addEventListener('mouseleave', () => {
  106.     gameRules.style.display = 'none';
  107. });

  108. // 开始游戏
  109. initGame();
  110. startGame();
复制代码


### 代码解释

1. **HTML**:
   - 新增了一个“查看规则”按钮和一个用于显示游戏规则的`div`元素。

2. **CSS**:
   - 为显示规则的`div`添加了一些样式,使其外观更好看。

3. **JavaScript**:
   - 新增了两个事件监听器,分别监听“查看规则”按钮的鼠标进入和离开事件。
   - 当鼠标进入按钮区域时,显示游戏规则;当鼠标离开按钮区域时,隐藏游戏规则。

这样,当你将鼠标移动到“查看规则”按钮上时,就会显示出游戏规则的文字啦!试试看,如果有任何问题或进一步的需求,请随时告诉我哦!
回复 支持 反对

使用道具 举报

29

主题

107

帖子

348

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
348
10#
 楼主| 发表于 2024-7-18 15:12:55 | 只看该作者
kaia 发表于 2024-7-18 14:16
当然可以!我们可以创建一个专门播放音效的函数,然后在蛇吃到食物时调用这个函数。以下是修改后的代码: ...

我还想用鼠标悬停事件实现一个功能,这个功能是当我鼠标移动到一个新的“查看规则”按钮上时,出现游戏规则的文字。
回复 支持 反对

使用道具 举报

0

主题

1145

帖子

1715

积分

版主

Rank: 7Rank: 7Rank: 7

积分
1715
9#
发表于 2024-7-18 14:16:22 | 只看该作者
ZLJ 发表于 2024-07-18 14:15
你好,可以用函数帮我实现蛇吃到食物之后出现音效吗? ...


当然可以!我们可以创建一个专门播放音效的函数,然后在蛇吃到食物时调用这个函数。以下是修改后的代码:

### 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.     <h1>欢迎来到贪吃蛇游戏!</h1>
  11.     <canvas id="gameCanvas"></canvas>
  12.    
  13.     <div id="gameOver" style="display: none;">
  14.         <h2>游戏结束</h2>
  15.         <p>很遗憾!得分: <span id="score">0</span></p>
  16.         <button onclick="startGame()">开始游戏</button>
  17.     </div>

  18.     <audio id="eatSound" src="audio/eat.mp3"></audio>

  19.     <script src="script.js"></script>
  20. </body>
  21. </html>
复制代码


### CSS部分

这里保持不变。

  1. /* styles.css */
  2. body {
  3.     display: flex;
  4.     justify-content: center;
  5.     align-items: center;
  6.     height: 100vh;
  7.     margin: 0;
  8.     background-color: #000;
  9.     flex-direction: column;
  10. }

  11. canvas {
  12.     border: 1px solid #fff;
  13. }

  14. #gameOver {
  15.     position: absolute;
  16.     text-align: center;
  17.     color: white;
  18. }

  19. button {
  20.     padding: 10px 20px;
  21.     font-size: 16px;
  22.     color: black;
  23.     background-color: #9370DB;
  24.     border: none;
  25.     border-radius: 5px;
  26.     cursor: pointer;
  27. }
复制代码


### JavaScript部分

在JavaScript中编写一个函数来播放音效,并在蛇吃到食物时调用它:

  1. // script.js
  2. const canvas = document.getElementById("gameCanvas");
  3. const ctx = canvas.getContext("2d");

  4. canvas.width = 400;
  5. canvas.height = 400;

  6. const box = 20;
  7. let snake;
  8. let food;
  9. let d;
  10. let score;
  11. let game;

  12. // 获取音频元素
  13. const eatSound = document.getElementById("eatSound");

  14. // 播放音效函数
  15. function playEatSound() {
  16.     eatSound.currentTime = 0; // 每次播放前重置播放时间
  17.     eatSound.play();
  18. }

  19. // 初始化游戏
  20. function initGame() {
  21.     snake = [{x: 9 * box, y: 10 * box}]; // 初始蛇的位置
  22.     food = {
  23.         x: Math.floor(Math.random() * 19 + 1) * box,
  24.         y: Math.floor(Math.random() * 19 + 1) * box
  25.     };
  26.     score = 0;
  27.     d = null;
  28. }

  29. // 启动游戏
  30. function startGame() {
  31.     document.getElementById("gameOver").style.display = "none";
  32.     initGame();
  33.     if (game) clearInterval(game);
  34.     game = setInterval(draw, 500); // 设置游戏速度为500毫秒
  35. }

  36. // 监听键盘事件
  37. document.addEventListener("keydown", direction);
  38. function direction(event) {
  39.     if (event.keyCode == 37 && d !== "RIGHT") {
  40.         d = "LEFT";
  41.     } else if (event.keyCode == 38 && d !== "DOWN") {
  42.         d = "UP";
  43.     } else if (event.keyCode == 39 && d !== "LEFT") {
  44.         d = "RIGHT";
  45.     } else if (event.keyCode == 40 && d !== "UP") {
  46.         d = "DOWN";
  47.     }
  48. }

  49. function collision(newHead, array) {
  50.     for (let i = 0; i < array.length; i++) {
  51.         if (newHead.x === array[i].x && newHead.y === array[i].y) {
  52.             return true;
  53.         }
  54.     }
  55.     return false;
  56. }

  57. // 绘制游戏
  58. function draw() {
  59.     ctx.fillStyle = "#000";
  60.     ctx.fillRect(0, 0, canvas.width, canvas.height);

  61.     for (let i = 0; i < snake.length; i++) {
  62.         ctx.fillStyle = (i === 0) ? "green" : "white";
  63.         ctx.fillRect(snake[i].x, snake[i].y, box, box);

  64.         ctx.strokeStyle = "red";
  65.         ctx.strokeRect(snake[i].x, snake[i].y, box, box);
  66.     }

  67.     ctx.fillStyle = "red";
  68.     ctx.fillRect(food.x, food.y, box, box);

  69.     let snakeX = snake[0].x;
  70.     let snakeY = snake[0].y;

  71.     if (d === "LEFT") snakeX -= box;
  72.     if (d === "UP") snakeY -= box;
  73.     if (d === "RIGHT") snakeX += box;
  74.     if (d === "DOWN") snakeY += box;

  75.     if (snakeX === food.x && snakeY === food.y) {
  76.         score += 1; // 每次吃到食物时分数增加1
  77.         
  78.         // 播放音效
  79.         playEatSound();

  80.         food = {
  81.             x: Math.floor(Math.random() * 19 + 1) * box,
  82.             y: Math.floor(Math.random() * 19 + 1) * box
  83.         };
  84.     } else {
  85.         snake.pop();
  86.     }

  87.     let newHead = { x: snakeX, y: snakeY };

  88.     if (snakeX < 0 || snakeY < 0 || snakeX >= canvas.width || snakeY >= canvas.height || collision(newHead, snake)) {
  89.         clearInterval(game);
  90.         document.getElementById("score").innerText = score;
  91.         document.getElementById("gameOver").style.display = "block";
  92.         return;
  93.     }

  94.     snake.unshift(newHead);

  95.     ctx.fillStyle = "white";
  96.     ctx.font = "45px Changa one";
  97.     ctx.fillText(score, 2 * box, 1.6 * box);
  98. }

  99. // 开始游戏
  100. initGame();
  101. startGame();
复制代码


### 代码解释

1. **HTML**:
   - 在`<body>`中添加了一个`<audio>`元素,用于存放吃到食物时的音效。

2. **JavaScript**:
   - 新建了`playEatSound`函数,用于播放吃到食物时的音效。
   - 修改 `draw` 函数,在蛇吃到食物时调用 `playEatSound` 来播放音效。

这样,每当蛇吃到食物时,音效就会被播放。你可以根据需要调整音效和音频文件路径。试试看,如果有任何问题或进一步的需求,请随时告诉我哦!
回复 支持 反对

使用道具 举报

29

主题

107

帖子

348

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
348
8#
 楼主| 发表于 2024-7-18 14:15:45 | 只看该作者
kaia 发表于 2024-7-16 20:59
好的,王桃桃,我来解释一下你的JavaScript代码在游戏开始后的函数调用逻辑链是怎样的。

### 函数调用逻 ...

你好,可以用函数帮我实现蛇吃到食物之后出现音效吗?
回复 支持 反对

使用道具 举报

0

主题

1145

帖子

1715

积分

版主

Rank: 7Rank: 7Rank: 7

积分
1715
7#
发表于 2024-7-16 20:59:24 | 只看该作者
ZLJ 发表于 2024-07-16 20:59
好的,我想知道在js里面的函数调用逻辑链是什么样的? ...


好的,王桃桃,我来解释一下你的JavaScript代码在游戏开始后的函数调用逻辑链是怎样的。

### 函数调用逻辑链

1. **初始化和启动游戏**:
    - 页面加载完成后,`initGame()`和`startGame()`函数被依次调用。这两个函数是在脚本的最底部调用的。
   
  1. initGame();
  2.     startGame();
复制代码


2. **初始化游戏 `initGame()`**:
    - 设置蛇的初始位置和属性。
    - 随机生成食物的位置。
    - 将得分设为0,并初始化方向为`null`。

3. **启动游戏 `startGame()`**:
    - 隐藏“游戏结束”的信息。
    - 初始化游戏状态。
    - 如果之前有游戏在运行,则清除定时器。
    - 使用`setInterval(draw, 500)`每500毫秒调用一次`draw()`函数,这个定时器会让游戏画面不断更新。
   
  1. game = setInterval(draw, 500); // 每500毫秒调用draw()一次
复制代码


4. **绘制游戏 `draw()`**:
    - 清空画布,用黑色填充。
    - 绘制蛇的每一节。
    - 绘制食物。
    - 根据当前方向移动蛇头。
    - 检查碰撞:是否撞墙或者咬到自己。如果发生碰撞,游戏结束,显示得分并停止定时器。
    - 检查是否吃到食物,如果是,则增加得分并生成新的食物。否则,将蛇尾移出。
    - 更新蛇头的位置。
    - 显示当前分数在画布上。

5. **方向控制 `direction(event)`**:
    - 根据按下的键修改蛇移动的方向(上、下、左、右)。
    - 通过事件监听器`document.addEventListener("keydown", direction);`绑定到键盘事件上。

### 更详细的顺序

1. **页面加载**:
    - 浏览器加载HTML、CSS和JavaScript文件。
    - JavaScript文件中的`initGame();`和`startGame();`首先被执行。

2. **initGame()** 被调用:
    - 设置蛇的初始位置。
    - 设置食物随机位置。
    - 初始化分数和方向。

3. **startGame()** 被调用:
    - 隐藏'gameOver'消息。
    - 再次初始化游戏(确保无误)。
    - 使用`setInterval`每500毫秒调用一次`draw()`函数。

4. **第一次调用 draw()**:
    - 用黑色填充整个画布(清理)。
    - 绘制蛇。
    - 绘制食物。
    - 根据当前方向移动蛇头。
    - 检查碰撞情况。
    - 检查是否吃到食物。
    - 在画布上显示当前得分。

5. **之后每500毫秒调用 draw()**:
    - 继续上述绘制操作,不断更新画面。

6. **用户按键事件**:
    - 当用户按下方向键时,`direction(event)`被调用,根据按键改变蛇的方向。

通过以上解释,你就能看到函数之间的调用关系和逻辑链了。如果还有任何具体函数或逻辑上的疑问,请随时告诉我哦,我会进一步帮你解析的!
回复 支持 反对

使用道具 举报

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

本版积分规则

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