|
|
好的,让我们在游戏结束时更清晰地显示“很遗憾!游戏结束”,并显示得分。我们会在HTML中添加一个用于显示游戏结束信息的元素,并在JavaScript中更新其内容。
### 修改后的 HTML 代码
首先,更新你的 `index.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="style.css">
- </head>
- <body>
- <canvas id="gameCanvas" width="400" height="400"></canvas>
- <div id="gameOverText" class="hidden">很遗憾!游戏结束<br>得分: <span id="score"></span></div>
- <button id="startButton" onclick="startGame()">开始游戏</button>
- <script src="script.js"></script>
- </body>
- </html>
复制代码
这里我们使用了 `<span id="score"></span>` 来动态显示得分。
### 修改后的 CSS 代码
更新你的 `style.css` 文件,使其如下所示(没有太大变化,只是确保样式合理):
- body {
- display: flex;
- flex-direction: column;
- align-items: center;
- justify-content: center;
- height: 100vh;
- background-color: #e0e0e0;
- margin: 0;
- }
- canvas {
- border: 1px solid black;
- background-color: #f5f5f5;
- }
- #startButton {
- display: none; /* 默认隐藏 */
- padding: 10px 20px;
- font-size: 20px;
- margin-top: 10px;
- cursor: pointer;
- background-color: #4CAF50; /* 绿色按钮 */
- color: white;
- border: none;
- border-radius: 5px;
- }
- #startButton:hover {
- background-color: #45a049; /* 更深的绿色 */
- }
- #gameOverText {
- font-size: 24px;
- color: red;
- text-align: center;
- margin-top: 20px;
- display: none; /* 默认隐藏 */
- }
复制代码
### 修改后的 JavaScript 代码
接下来,更新 `script.js` 文件,使其如下所示:
- const canvas = document.getElementById('gameCanvas');
- const ctx = canvas.getContext('2d');
- let snake = [{ x: 150, y: 150 }, { x: 140, y: 150 }, { x: 130, y: 150 }, { x: 120, y: 150 }, { x: 110, y: 150 }];
- let food = { x: 0, y: 0 };
- let dx = 10;
- let dy = 0;
- let score = 0;
- let gameOver = false;
- 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 = 'red';
- 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();
- } else {
- snake.pop();
- }
- }
- function collision(head) {
- for (let i = 1; i < snake.length; i++) {
- if (snake[i].x === head.x && snake[i].y === head.y) {
- return true;
- }
- }
- return false;
- }
- 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() {
- document.getElementById('gameOverText').classList.remove('hidden');
- document.getElementById('score').innerText = score;
- document.getElementById('startButton').style.display = 'block';
- }
- function startGame() {
- snake = [{ x: 150, y: 150 }, { x: 140, y: 150 }, { x: 130, y: 150 }, { x: 120, y: 150 }, { x: 110, y: 150 }];
- score = 0;
- dx = 10;
- dy = 0;
- gameOver = false;
- food = randomFoodPosition();
- document.getElementById('gameOverText').classList.add('hidden');
- document.getElementById('startButton').style.display = 'none';
- main();
- }
- food = randomFoodPosition();
- main();
复制代码
### 解释调整内容:
1. **HTML**:
- 添加了一个用于显示“很遗憾!游戏结束”和得分信息的 `<span id="score"></span>`。
2. **CSS**:
- 添加了 `#gameOverText` 的样式,确保它在需要的时候显示,并且居中显示文本。
3. **JavaScript**:
- 在 `displayGameOver` 函数中更新了 `gameOverText` 的内容以包含得分。
- 确保游戏结束时,“游戏结束!”信息和按钮一同显示。
现在,当游戏结束时,会显示“很遗憾!游戏结束”并显示得分,同时“开始游戏”按钮也会出现在下方。希望这些修改能满足你的需求!如果还有其他问题,请随时告诉我哦! |
|