这是一个基础的贪吃蛇游戏实现,代码结构清晰。以下是完整的游戏代码和优化建议: ```javascript // 游戏初始化部分保持不变... // 生成食物位置 function generateFood() { const foodX = Math.floor(Math.random() * (canvas.width / boxSize)) * boxSize; const foodY = Math.floor(Math.random() * (canvas.height / boxSize)) * boxSize; // 确保食物不会出现在蛇身上 const onSnake = snake.some(segment => segment.x === foodX && segment.y === foodY); return onSnake ? generateFood() : { x: foodX, y: foodY }; } // 绘制食物 function drawFood() { ctx.fillStyle = "red"; ctx.fillRect(food.x, food.y, boxSize, boxSize); } // 移动蛇 function moveSnake() { const head = { ...snake[0] }; switch (direction) { case "UP": head.y -= boxSize; break; case "DOWN": head.y += boxSize; break; case "LEFT": head.x -= boxSize; break; case "RIGHT": head.x += boxSize; break; } snake.unshift(head); // 检查是否吃到食物 if (head.x === food.x && head.y === food.y) { score += 10; scoreElement.textContent = `分数: ${score}`; food = generateFood(); } else { snake.pop(); // 没吃到食物则移除尾部 } } // 绘制蛇 function drawSnake() { snake.forEach((segment, index) => { ctx.fillStyle = index === 0 ? "#4CAF50" : "#8BC34A"; ctx.fillRect(segment.x, segment.y, boxSize, boxSize); }); } // 碰撞检测 function checkCollision() { const head = snake[0]; // 撞墙检测 if (head.x < 0 || head.x >= canvas.width || head.y < 0 || head.y >= canvas.height) { gameOver(); } // 撞自身检测(从第1节开始检查) for (let i = 1; i < snake.length; i++) { if (head.x === snake[i].x && head.y === snake[i].y) { gameOver(); } } } // 游戏结束 function gameOver() { gameRunning = false; ctx.fillStyle = "white"; ctx.font = "30px Arial"; ctx.textAlign = "center"; ctx.fillText("游戏结束!", canvas.width/2, canvas.height/2); } // 改变方向 function changeDirection(event) { const key = event.keyCode; const goingUp = direction === "UP"; const goingDown = direction === "DOWN"; const goingLeft = direction === "LEFT"; const goingRight = direction === "RIGHT"; if (key === 37 && !goingRight) direction = "LEFT"; if (key === 38 && !goingDown) direction = "UP"; if (key === 39 && !goingLeft) direction = "RIGHT"; if (key === 40 && !goingUp) direction = "DOWN"; } // 重新开始游戏 function restartGame() { snake = [{ x: 9 * boxSize, y: 10 * boxSize }]; direction = "RIGHT"; food = generateFood(); score = 0; scoreElement.textContent = `分数: ${score}`; gameRunning = true; clearCanvas(); gameLoop(); } // 开始游戏 gameLoop(); ``` 优化建议: 1. 食物生成函数使用递归确保不会出现在蛇身上 2. 蛇头使用不同颜分 3. 方向控制加入防止180度转弯的逻辑 需要注意的逻辑点: 1. 蛇移动时先创建新头部再检查是否吃到食物 2. 碰撞检测需要区分墙壁和自身碰撞 3. 游戏循环使用setTimeout而非requestAnimationFrame,适合这种基于网格的游戏 [2025-05-03 22:07:44 | AI写代码神器 | 891点数解答]