|
|
当然可以!为了让游戏结束界面更加生动,我们可以添加一些动画效果和更具吸引力的文本显示。以下是一个示例,其中包含了更多的CSS样式和JavaScript来实现这一效果。
### 1. 修改HTML文件
我们需要为游戏结束界面添加一个隐藏的元素,用于显示结束信息。更新后的HTML如下:
- <!DOCTYPE html>
- <html lang="zh-CN">
- <head>
- <meta charset="UTF-8">
- <title>贪吃蛇游戏</title>
- <link rel="stylesheet" href="styles.css">
- </head>
- <body>
- <h1>贪吃蛇游戏</h1>
- <button id="startButton">开始游戏</button>
- <canvas id="gameCanvas" width="400" height="400"></canvas>
- <div id="gameOver" class="hidden">
- <h2>游戏结束!</h2>
- <p>得分: <span id="finalScore"></span></p>
- <button id="restartButton">重新开始</button>
- </div>
- <script src="snake_game.js"></script>
- </body>
- </html>
复制代码
### 2. 修改CSS文件
更新CSS文件,以添加结束游戏界面的样式和动画效果:
- /* 样式之前已设置 */
- canvas {
- background-color: #ADD8E6; /* 浅蓝色背景 */
- display: block;
- margin: 0 auto;
- }
- /* 添加基本页面样式 */
- body {
- display: flex;
- flex-direction: column;
- align-items: center;
- justify-content: center;
- height: 100vh;
- margin: 0;
- }
- h1 {
- color: #333;
- }
- #startButton, #restartButton {
- padding: 10px 20px;
- font-size: 16px;
- margin-bottom: 20px;
- background-color: #4CAF50; /* 绿色背景 */
- color: white;
- border: none;
- border-radius: 5px;
- cursor: pointer;
- }
- #startButton:hover, #restartButton:hover {
- background-color: #45a049; /* 鼠标悬停时颜色变深 */
- }
- /* 结束游戏界面 */
- #gameOver {
- position: absolute;
- top: 50%;
- left: 50%;
- transform: translate(-50%, -50%);
- text-align: center;
- background-color: rgba(255, 255, 255, 0.9);
- padding: 20px;
- border: 2px solid #333;
- border-radius: 10px;
- box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
- animation: fadeIn 1s;
- }
- .hidden {
- display: none;
- }
- @keyframes fadeIn {
- from {
- opacity: 0;
- }
- to {
- opacity: 1;
- }
- }
复制代码
### 3. 修改JavaScript文件
在JavaScript文件中,我们需要添加一些代码来显示和隐藏结束游戏界面,并处理重新开始游戏的逻辑:
- const canvas = document.getElementById('gameCanvas');
- const ctx = canvas.getContext('2d');
- let snake = [{x: 200, y: 200}];
- let foodX;
- let foodY;
- let dx = 10;
- let dy = 0;
- let isGameRunning = false; // 初始状态改为未运行
- let score = 0;
- // 获取页面元素
- const startButton = document.getElementById('startButton');
- const gameOverDiv = document.getElementById('gameOver');
- const finalScore = document.getElementById('finalScore');
- const restartButton = document.getElementById('restartButton');
- // 开始游戏按钮点击事件
- startButton.addEventListener('click', startGame);
- restartButton.addEventListener('click', startGame);
- function startGame() {
- isGameRunning = true;
- snake = [{x: 200, y: 200}];
- dx = 10;
- dy = 0;
- score = 0;
- createFood();
- gameOverDiv.classList.add('hidden');
- main();
- }
- // 主游戏循环
- function main() {
- if (isGameRunning) {
- setTimeout(function onTick() {
- clearCanvas();
- moveSnake();
- drawSnake();
- drawFood();
- checkGameOver();
- updateScore();
- main();
- }, 100);
- }
- }
- // 清除画布
- function clearCanvas() {
- ctx.fillStyle = "black";
- ctx.fillRect(0, 0, canvas.width, canvas.height);
- }
- // 绘制蛇
- function drawSnake() {
- snake.forEach(snakePart => {
- ctx.fillStyle = 'lightgreen';
- ctx.strokestyle = 'darkgreen';
- ctx.fillRect(snakePart.x, snakePart.y, 10, 10);
- ctx.strokeRect(snakePart.x, snakePart.y, 10, 10);
- });
- }
- // 移动蛇
- function moveSnake() {
- const head = {x: snake[0].x + dx, y: snake[0].y + dy};
- snake.unshift(head);
- if (snake[0].x === foodX && snake[0].y === foodY) {
- score += 10;
- createFood();
- } else {
- snake.pop();
- }
- }
- // 随机生成食物位置
- function createFood() {
- foodX = Math.floor((Math.random() * canvas.width) / 10) * 10;
- foodY = Math.floor((Math.random() * canvas.height) / 10) * 10;
- snake.forEach(snakePart => {
- if (snakePart.x === foodX && snakePart.y === foodY) {
- createFood();
- }
- });
- }
- // 绘制食物
- function drawFood() {
- ctx.fillStyle = 'red';
- ctx.strokestyle = 'darkred';
- ctx.fillRect(foodX, foodY, 10, 10);
- ctx.strokeRect(foodX, foodY, 10, 10);
- }
- // 检查游戏是否结束
- function checkGameOver() {
- for (let i = 4; i < snake.length; i++) {
- if (snake[0].x === snake[i].x && snake[0].y === snake[i].y) {
- endGame();
- }
- }
- const hitLeftWall = snake[0].x < 0;
- const hitRightWall = snake[0].x >= canvas.width;
- const hitTopWall = snake[0].y < 0;
- const hitBottomWall = snake[0].y >= canvas.height;
- if (hitLeftWall || hitRightWall || hitTopWall || hitBottomWall) {
- endGame();
- }
- }
- // 处理游戏结束
- function endGame() {
- isGameRunning = false;
- finalScore.textContent = score;
- gameOverDiv.classList.remove('hidden');
- }
- // 更改方向
- document.addEventListener("keydown", changeDirection);
- 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 updateScore() {
- document.getElementById('score').textContent = '得分: ' + score;
- }
- // 初始化游戏
- createFood();
复制代码
### 解释
- **HTML部分**:增加了一个隐藏的`gameOver` div,当游戏结束时会显示。
- **CSS部分**:为游戏结束界面添加了样式,使其居中并带有淡入动画效果。
- **JavaScript部分**:增加了`startGame`函数,用于初始化游戏,处理开始和重新开始游戏的逻辑。`endGame`函数用于处理游戏结束显示和分数更新。
你可以按上述步骤将新样式和代码添加到你的项目中,以使游戏结束界面更加生动。如果你有其它问题或需要进一步帮助,请告诉我! |
|