|
|
好的,为了让“游戏结束!”和“得分”分成两行显示,只需在 `ctx.fillText` 中调用两次,并调整它们的垂直位置。以下是如何修改代码:
### 修改后的代码
- const canvas = document.getElementById('gameCanvas');
- const ctx = canvas.getContext('2d');
- let snake = [{ x: 150, y: 150 }, { x: 140, y: 150 }, { x: 130, 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() {
- ctx.fillStyle = "black";
- ctx.font = "30px Arial";
- // 绘制“游戏结束!”位于中心稍上方
- ctx.fillText("游戏结束!", canvas.width / 2 - 75, canvas.height / 2 - 20);
- // 绘制得分位于中心稍下方
- ctx.fillText("得分: " + score, canvas.width / 2 - 50, canvas.height / 2 + 20);
- document.getElementById('startButton').style.display = 'block';
- }
- function startGame() {
- snake = [{ x: 150, y: 150 }, { x: 140, y: 150 }, { x: 130, y: 150 }];
- score = 0;
- dx = 10;
- dy = 0;
- gameOver = false;
- food = randomFoodPosition();
- document.getElementById('startButton').style.display = 'none';
- main();
- }
- food = randomFoodPosition();
- main();
复制代码
### 解释修改内容:
- **`ctx.fillText("游戏结束!", canvas.width / 2 - 75, canvas.height / 2 - 20);`**:这行代码绘制“游戏结束!”文本,位于画布的水平中央位置,稍微向上调整,使其在画布中央偏上的位置。
- **`ctx.fillText("得分: " + score, canvas.width / 2 - 50, canvas.height / 2 + 20);`**:这行代码绘制得分文本,位于画布的水平中央位置,稍微向下调整,使其在画布中央偏下的位置。
通过这种方法,“游戏结束!”和“得分”会显示在两行,并且在画布的中心位置。
希望这些修改能满足你的需求!如果你有其他问题或需要进一步的帮助,请随时告诉我哦! |
|