| const canvas = document.getElementById('gameCanvas'); const ctx = canvas.getContext('2d'); let snake = [{ x: 50, y: 50 }]; let food = { x: 0, y: 0 }; let dx = 10; let dy = 0; let score = 0; let gameOver = false; // 创建音效对象 const eatSound = new Audio('eat.mp3'); // 创建背景音乐对象 const backgroundMusic = new Audio('background.mp3'); backgroundMusic.loop = true; // 设置循环播放 document.addEventListener("keydown", changeDirection); function randomFoodPosition() { const x = Math.floor(Math.random() * (canvas.width / 10)) * 10; const y = Math.floor(Math.random() * (canvas.height / 10)) * 10; return { x, y }; } function clearCanvas() { ctx.fillStyle = "white"; ctx.fillRect(0, 0, canvas.width, canvas.height); } function drawSnake() { snake.forEach(drawSnakePart); } function drawSnakePart(snakePart) { ctx.fillStyle = 'lightgreen'; ctx.strokeStyle = 'darkgreen'; ctx.fillRect(snakePart.x, snakePart.y, 10, 10); ctx.strokeRect(snakePart.x, snakePart.y, 10, 10); } function drawFood() { ctx.fillStyle = 'lightpink'; ctx.fillRect(food.x, food.y, 10, 10); } function moveSnake() { const head = { x: snake[0].x + dx, y: snake[0].y + dy }; // 检测碰壁 if (head.x < 0 || head.x >= canvas.width || head.y < 0 || head.y >= canvas.height || collision(head)) { gameOver = true; return; } snake.unshift(head); // 检查是否吃到食物 if (head.x === food.x && head.y === food.y) { score++; food = randomFoodPosition(); playEatSound(); // 播放音效 } else { snake.pop(); } } function collision(head) { for (let i = 1; i < snake.length; i++) { if (snake.x === head.x && snake.y === head.y) { return true; } } return false; } // 播放吃食物的音效 function playEatSound() { eatSound.play(); } // 播放背景音乐 function playBackgroundMusic() { backgroundMusic.play(); } // 停止背景音乐 function stopBackgroundMusic() { backgroundMusic.pause(); backgroundMusic.currentTime = 0; // 重置音乐到开头 } function main() { if (gameOver) { displayGameOver(); return; } setTimeout(function onTick() { clearCanvas(); drawFood(); moveSnake(); drawSnake(); main(); }, 100); } function changeDirection(event) { const LEFT_KEY = 37; const RIGHT_KEY = 39; const UP_KEY = 38; const DOWN_KEY = 40; const keyPressed = event.keyCode; const goingUp = dy === -10; const goingDown = dy === 10; const goingRight = dx === 10; const goingLeft = dx === -10; if (keyPressed === LEFT_KEY && !goingRight) { dx = -10; dy = 0; } if (keyPressed === UP_KEY && !goingDown) { dx = 0; dy = -10; } if (keyPressed === RIGHT_KEY && !goingLeft) { dx = 10; dy = 0; } if (keyPressed === DOWN_KEY && !goingUp) { dx = 0; dy = 10; } } // 显示游戏结束 function displayGameOver() { ctx.fillStyle = "pink"; ctx.font = "30px Arial"; // 绘制“游戏结束!”位于中心稍上方 ctx.fillText("很遗憾,游戏结束!", canvas.width / 2 - 115, canvas.height / 2 - 20); // 绘制得分位于中心稍下方 ctx.fillText("得分: " + score, canvas.width / 2 - 50, canvas.height / 2 + 20); document.getElementById("startButton").style.display = "block"; stopBackgroundMusic(); // 停止背景音乐 } function startGame() { snake = [{ x: 50, y: 50 }]; score = 0; dx = 10; dy = 0; gameOver = false; food = randomFoodPosition(); document.getElementById('startButton').style.display = 'none'; playBackgroundMusic(); // 播放背景音乐 main(); } food = randomFoodPosition(); main(); 你好,以上是贪吃蛇游戏的js代码,请修改代码,使游戏的右边出现“游戏规则”悬停按钮,鼠标移到按钮上出现游戏规则。 |
张复礼 发表于 2024-08-12 14:23
body {
display: flex;
flex-direction: colu ...
张复礼 发表于 2024-08-12 14:26
const canvas = document.getElementById('gameCanvas ...
张复礼 发表于 2024-08-12 14:28
请修改游戏,让火柴人不仅可以左右移动,还可以上下移 ...
张复礼 发表于 2024-08-12 14:34
const canvas = document.getElementById('gameCanvas ...
张复礼 发表于 2024-08-12 14:46
我的贪吃蛇游戏好像有问题,无法正常打开,麻烦你帮我 ...
张复礼 发表于 2024-08-12 15:03
贪吃蛇游戏
贪吃蛇游戏
开始游戏
...
张复礼 发表于 2024-08-12 15:10
这是我的js文件,我想让游戏一开始就显示“开始按钮” ...
| 欢迎光临 科学教育 (http://www.sci-edu.net/) | Powered by Discuz! X3.2 |