|
|
帮我把字染红:
function initGame() {
snake = [
{x: 5, y: 10},
{x: 4, y: 10},
{x: 3, y: 10}
];
direction = 'right';
nextDirection = 'right';
score = 0;
scoreElement.textContent = score;
generateFood();
gameRunning = true;
gamePaused = false;
gameOverElement.style.display = 'none';
}
function startGame() {
if (!gameRunning) {
initGame();
gameLoop = setInterval(gameUpdate, gameSpeed);
}
}
function pauseGame() {
if (gameRunning) {
if (gamePaused) {
gameLoop = setInterval(gameUpdate, gameSpeed);
gamePaused = false;
} else {
clearInterval(gameLoop);
gamePaused = true;
}
}
}
function resetGame() {
clearInterval(gameLoop);
initGame();
gameLoop = setInterval(gameUpdate, gameSpeed);
}
function gameUpdate() {
if (gamePaused) return;
direction = nextDirection;
const head = {x: snake[0].x, y: snake[0].y};
switch(direction) {
case 'up':
head.y--;
break;
case 'down':
head.y++;
break;
case 'left':
head.x--;
break;
case 'right':
head.x++;
break;
}
if (
head.x < 0 ||
head.x >= tileCount ||
head.y < 0 ||
head.y >= tileCount ||
snake.some(segment => segment.x === head.x && segment.y === head.y)
) {
gameOver();
return;
}
snake.unshift(head);
if (head.x === food.x && head.y === food.y) {
score += 10;
scoreElement.textContent = score;
generateFood();
if (score % 50 === 0 && gameSpeed > 50) {
gameSpeed -= 10;
clearInterval(gameLoop);
gameLoop = setInterval(gameUpdate, gameSpeed);
}
} else {
snake.pop();
}
drawGame();
}
function drawGame() {
ctx.fillStyle = '#ecf0f1';
ctx.fillRect(0, 0, canvas.width, canvas.height);
snake.forEach((segment, index) => {
ctx.fillStyle = index === 0 ? '#2ecc71' : '#27ae60';
ctx.fillRect(segment.x * gridSize, segment.y * gridSize, gridSize - 2, gridSize - 2);
ctx.strokeStyle = '#ecf0f1';
ctx.strokeRect(segment.x * gridSize, segment.y * gridSize, gridSize - 2, gridSize - 2);
});
ctx.fillStyle = '#e74c3c';
ctx.beginPath();
ctx.arc(
food.x * gridSize + gridSize / 2,
food.y * gridSize + gridSize / 2,
gridSize / 2 - 2,
0,
Math.PI * 2
);
ctx.fill();
}
function generateFood() {
food = {
x: Math.floor(Math.random() * tileCount),
y: Math.floor(Math.random() * tileCount)
};
while (snake.some(segment => segment.x === food.x && segment.y === food.y)) {
food = {
x: Math.floor(Math.random() * tileCount),
y: Math.floor(Math.random() * tileCount)
};
}
}
function gameOver() {
clearInterval(gameLoop);
gameRunning = false;
if (score > highScore) {
highScore = score;
highScoreElement.textContent = highScore;
localStorage.setItem('snakeHighScore', highScore);
}
finalScoreElement.textContent = score;
gameOverElement.style.display = 'block';
}
document.addEventListener('keydown', e => {
if (!gameRunning) return;
switch(e.key) {
case 'ArrowUp':
case 'w':
case 'W':
if (direction !== 'down') nextDirection = 'up';
break;
case 'ArrowDown':
case 's':
case 'S':
if (direction !== 'up') nextDirection = 'down';
break;
case 'ArrowLeft':
case 'a':
case 'A':
if (direction !== 'right') nextDirection = 'left';
break;
case 'ArrowRight':
case 'd':
case 'D':
if (direction !== 'left') nextDirection = 'right';
break;
case ' ':
pauseGame();
break;
}
});
drawGame();
</script>
</body>
</html> |
|