|
|
当然可以!我们可以创建一个专门播放音效的函数,然后在蛇吃到食物时调用这个函数。以下是修改后的代码:
### HTML部分
在HTML中添加一个音频元素,放置在适当的位置:
- <!DOCTYPE html>
- <html lang="zh-CN">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>贪吃蛇游戏</title>
- <link rel="stylesheet" href="styles.css">
- </head>
- <body>
- <h1>欢迎来到贪吃蛇游戏!</h1>
- <canvas id="gameCanvas"></canvas>
-
- <div id="gameOver" style="display: none;">
- <h2>游戏结束</h2>
- <p>很遗憾!得分: <span id="score">0</span></p>
- <button onclick="startGame()">开始游戏</button>
- </div>
- <audio id="eatSound" src="audio/eat.mp3"></audio>
- <script src="script.js"></script>
- </body>
- </html>
复制代码
### 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;
- }
复制代码
### 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;
- // 获取音频元素
- const eatSound = document.getElementById("eatSound");
- // 播放音效函数
- function playEatSound() {
- eatSound.currentTime = 0; // 每次播放前重置播放时间
- eatSound.play();
- }
- // 初始化游戏
- function initGame() {
- snake = [{x: 9 * box, y: 10 * box}]; // 初始蛇的位置
- 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毫秒
- }
- // 监听键盘事件
- 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[i].x && newHead.y === array[i].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[i].x, snake[i].y, box, box);
- ctx.strokeStyle = "red";
- ctx.strokeRect(snake[i].x, snake[i].y, box, box);
- }
- ctx.fillStyle = "red";
- ctx.fillRect(food.x, food.y, box, box);
- let snakeX = snake[0].x;
- let snakeY = snake[0].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);
- }
- // 开始游戏
- initGame();
- startGame();
复制代码
### 代码解释
1. **HTML**:
- 在`<body>`中添加了一个`<audio>`元素,用于存放吃到食物时的音效。
2. **JavaScript**:
- 新建了`playEatSound`函数,用于播放吃到食物时的音效。
- 修改 `draw` 函数,在蛇吃到食物时调用 `playEatSound` 来播放音效。
这样,每当蛇吃到食物时,音效就会被播放。你可以根据需要调整音效和音频文件路径。试试看,如果有任何问题或进一步的需求,请随时告诉我哦! |
|