|
|
<!DOCTYPE html> |
|
|
<html lang="zh"> |
|
|
<head> |
|
|
<meta charset="UTF-8"> |
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0"> |
|
|
<title>贪吃蛇</title> |
|
|
<link href="https://fonts.googleapis.com/css2?family=Press+Start+2P&display=swap" rel="stylesheet"> |
|
|
<script src="https://cdn.tailwindcss.com"></script> |
|
|
<style> |
|
|
body { |
|
|
font-family: 'Press Start 2P', cursive; |
|
|
display: flex; |
|
|
justify-content: center; |
|
|
align-items: center; |
|
|
min-height: 100vh; |
|
|
background-color: #1a1a1a; |
|
|
color: #ffffff; |
|
|
flex-direction: column; |
|
|
padding: 20px; |
|
|
box-sizing: border-box; |
|
|
} |
|
|
#gameCanvas { |
|
|
background-color: #000000; |
|
|
border: 5px solid #33ff33; |
|
|
display: block; |
|
|
max-width: 100%; |
|
|
height: auto; |
|
|
aspect-ratio: 1 / 1; |
|
|
image-rendering: pixelated; |
|
|
box-shadow: 0 0 20px #33ff33; |
|
|
margin-bottom: 20px; |
|
|
} |
|
|
.game-controls { |
|
|
text-align: center; |
|
|
margin-top: 15px; |
|
|
} |
|
|
.game-button { |
|
|
font-family: 'Press Start 2P', cursive; |
|
|
background-color: #33ff33; |
|
|
color: #000000; |
|
|
border: 2px solid #000000; |
|
|
padding: 10px 20px; |
|
|
margin: 5px; |
|
|
cursor: pointer; |
|
|
text-transform: uppercase; |
|
|
transition: background-color 0.3s, transform 0.1s; |
|
|
box-shadow: 3px 3px 0 #118811; |
|
|
} |
|
|
.game-button:hover { |
|
|
background-color: #11cc11; |
|
|
} |
|
|
.game-button:active { |
|
|
transform: translate(2px, 2px); |
|
|
box-shadow: 1px 1px 0 #118811; |
|
|
} |
|
|
#messageBox { |
|
|
position: absolute; |
|
|
top: 50%; |
|
|
left: 50%; |
|
|
transform: translate(-50%, -50%); |
|
|
background-color: rgba(0, 0, 0, 0.8); |
|
|
color: #ff3333; |
|
|
padding: 30px; |
|
|
border: 3px solid #ff3333; |
|
|
border-radius: 10px; |
|
|
display: none; |
|
|
text-align: center; |
|
|
z-index: 10; |
|
|
box-shadow: 0 0 15px #ff3333; |
|
|
} |
|
|
#messageText { |
|
|
margin-bottom: 15px; |
|
|
} |
|
|
|
|
|
@media (max-width: 600px) { |
|
|
body { |
|
|
padding: 10px; |
|
|
} |
|
|
#gameCanvas { |
|
|
margin-bottom: 15px; |
|
|
} |
|
|
.game-button { |
|
|
padding: 8px 15px; |
|
|
font-size: 0.8em; |
|
|
} |
|
|
#messageBox { |
|
|
padding: 20px; |
|
|
font-size: 0.9em; |
|
|
} |
|
|
} |
|
|
</style> |
|
|
</head> |
|
|
<body> |
|
|
|
|
|
<h1 class="text-2xl mb-4 text-center">贪吃蛇</h1> |
|
|
<div id="score" class="text-lg mb-2">分数: 0</div> |
|
|
|
|
|
<canvas id="gameCanvas"></canvas> |
|
|
|
|
|
<div class="game-controls"> |
|
|
<button id="resetButton" class="game-button">重新开始</button> |
|
|
</div> |
|
|
|
|
|
<div id="messageBox"> |
|
|
<div id="messageText">游戏结束!</div> |
|
|
<button id="closeMessageButton" class="game-button" style="background-color: #ff3333; color: #000;">关闭</button> |
|
|
</div> |
|
|
|
|
|
<script> |
|
|
const canvas = document.getElementById('gameCanvas'); |
|
|
const ctx = canvas.getContext('2d'); |
|
|
const scoreElement = document.getElementById('score'); |
|
|
const resetButton = document.getElementById('resetButton'); |
|
|
const messageBox = document.getElementById('messageBox'); |
|
|
const messageText = document.getElementById('messageText'); |
|
|
const closeMessageButton = document.getElementById('closeMessageButton'); |
|
|
|
|
|
|
|
|
const grid = 20; |
|
|
const canvasSize = 400; |
|
|
canvas.width = canvasSize; |
|
|
canvas.height = canvasSize; |
|
|
|
|
|
|
|
|
let snake; |
|
|
let food; |
|
|
let score; |
|
|
let direction; |
|
|
let nextDirection; |
|
|
let bufferedDirection; |
|
|
let isAligned; |
|
|
let gameLoopTimeout; |
|
|
let isGameOver; |
|
|
|
|
|
|
|
|
function initGame() { |
|
|
clearTimeout(gameLoopTimeout); |
|
|
hideMessage(); |
|
|
isGameOver = false; |
|
|
|
|
|
|
|
|
snake = [ |
|
|
{ x: Math.floor(canvasSize / grid / 2) * grid, y: Math.floor(canvasSize / grid / 2) * grid } |
|
|
]; |
|
|
direction = 'right'; |
|
|
nextDirection = null; |
|
|
bufferedDirection = null; |
|
|
isAligned = false; |
|
|
|
|
|
|
|
|
score = 0; |
|
|
scoreElement.textContent = `分数: ${score}`; |
|
|
|
|
|
|
|
|
placeFood(); |
|
|
|
|
|
|
|
|
gameLoop(); |
|
|
} |
|
|
|
|
|
|
|
|
function placeFood() { |
|
|
let newFoodPosition; |
|
|
do { |
|
|
newFoodPosition = { |
|
|
x: Math.floor(Math.random() * (canvasSize / grid)) * grid, |
|
|
y: Math.floor(Math.random() * (canvasSize / grid)) * grid |
|
|
}; |
|
|
} while (isFoodOnSnake(newFoodPosition)); |
|
|
food = newFoodPosition; |
|
|
} |
|
|
|
|
|
|
|
|
function isFoodOnSnake(position) { |
|
|
for (let i = 0; i < snake.length; i++) { |
|
|
if (snake[i].x === position.x && snake[i].y === position.y) { |
|
|
return true; |
|
|
} |
|
|
} |
|
|
return false; |
|
|
} |
|
|
|
|
|
|
|
|
function gameLoop() { |
|
|
if (isGameOver) { |
|
|
return; |
|
|
} |
|
|
|
|
|
|
|
|
const speed = 150; |
|
|
|
|
|
gameLoopTimeout = setTimeout(() => { |
|
|
|
|
|
ctx.clearRect(0, 0, canvasSize, canvasSize); |
|
|
|
|
|
|
|
|
update(); |
|
|
|
|
|
|
|
|
draw(); |
|
|
|
|
|
|
|
|
requestAnimationFrame(gameLoop); |
|
|
}, speed); |
|
|
} |
|
|
|
|
|
|
|
|
function update() { |
|
|
|
|
|
const head = snake[0]; |
|
|
isAligned = (head.x === food.x || head.y === food.y); |
|
|
|
|
|
|
|
|
if (isAligned) { |
|
|
|
|
|
if (nextDirection) { |
|
|
|
|
|
if (!isOppositeDirection(nextDirection, direction)) { |
|
|
bufferedDirection = nextDirection; |
|
|
} |
|
|
nextDirection = null; |
|
|
} |
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
|
if (bufferedDirection) { |
|
|
|
|
|
if (!isOppositeDirection(bufferedDirection, direction)) { |
|
|
direction = bufferedDirection; |
|
|
} |
|
|
bufferedDirection = null; |
|
|
nextDirection = null; |
|
|
} |
|
|
|
|
|
else if (nextDirection) { |
|
|
|
|
|
if (!isOppositeDirection(nextDirection, direction)) { |
|
|
direction = nextDirection; |
|
|
} |
|
|
nextDirection = null; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
let newHead = { x: head.x, y: head.y }; |
|
|
switch (direction) { |
|
|
case 'up': newHead.y -= grid; break; |
|
|
case 'down': newHead.y += grid; break; |
|
|
case 'left': newHead.x -= grid; break; |
|
|
case 'right': newHead.x += grid; break; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (newHead.x < 0 || newHead.x >= canvasSize || newHead.y < 0 || newHead.y >= canvasSize) { |
|
|
gameOver("撞到墙了!"); |
|
|
return; |
|
|
} |
|
|
|
|
|
for (let i = 1; i < snake.length; i++) { |
|
|
if (newHead.x === snake[i].x && newHead.y === snake[i].y) { |
|
|
gameOver("撞到自己了!"); |
|
|
return; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
snake.unshift(newHead); |
|
|
|
|
|
|
|
|
|
|
|
if (newHead.x === food.x && newHead.y === food.y) { |
|
|
score++; |
|
|
scoreElement.textContent = `分数: ${score}`; |
|
|
placeFood(); |
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!(newHead.x === food.x && newHead.y === food.y)) { |
|
|
snake.pop(); |
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
function draw() { |
|
|
|
|
|
ctx.fillStyle = '#33ff33'; |
|
|
snake.forEach(segment => { |
|
|
ctx.fillRect(segment.x, segment.y, grid, grid); |
|
|
ctx.strokeStyle = '#000000'; |
|
|
ctx.strokeRect(segment.x, segment.y, grid, grid); |
|
|
}); |
|
|
|
|
|
|
|
|
ctx.fillStyle = '#ff3333'; |
|
|
ctx.fillRect(food.x, food.y, grid, grid); |
|
|
ctx.strokeStyle = '#ffffff'; |
|
|
ctx.strokeRect(food.x, food.y, grid, grid); |
|
|
|
|
|
|
|
|
if (isAligned) { |
|
|
ctx.fillStyle = 'rgba(255, 255, 0, 0.3)'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ctx.fillStyle = 'yellow'; |
|
|
ctx.fillRect(snake[0].x, snake[0].y, grid, grid); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
function isOppositeDirection(dir1, dir2) { |
|
|
return (dir1 === 'up' && dir2 === 'down') || |
|
|
(dir1 === 'down' && dir2 === 'up') || |
|
|
(dir1 === 'left' && dir2 === 'right') || |
|
|
(dir1 === 'right' && dir2 === 'left'); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
document.addEventListener('keydown', (e) => { |
|
|
if (isGameOver) return; |
|
|
|
|
|
let requestedDir = null; |
|
|
switch (e.key) { |
|
|
case 'ArrowUp': |
|
|
case 'w': |
|
|
requestedDir = 'up'; |
|
|
break; |
|
|
case 'ArrowDown': |
|
|
case 's': |
|
|
requestedDir = 'down'; |
|
|
break; |
|
|
case 'ArrowLeft': |
|
|
case 'a': |
|
|
requestedDir = 'left'; |
|
|
break; |
|
|
case 'ArrowRight': |
|
|
case 'd': |
|
|
requestedDir = 'right'; |
|
|
break; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const effectiveCurrentDirection = bufferedDirection || direction; |
|
|
if (requestedDir && |
|
|
!isOppositeDirection(requestedDir, effectiveCurrentDirection) && |
|
|
(!nextDirection || !isOppositeDirection(requestedDir, nextDirection))) |
|
|
{ |
|
|
nextDirection = requestedDir; |
|
|
} |
|
|
|
|
|
if (['ArrowUp', 'ArrowDown', 'ArrowLeft', 'ArrowRight'].includes(e.key)) { |
|
|
e.preventDefault(); |
|
|
} |
|
|
}); |
|
|
|
|
|
|
|
|
function gameOver(reason) { |
|
|
isGameOver = true; |
|
|
clearTimeout(gameLoopTimeout); |
|
|
showMessage(`游戏结束! ${reason}<br>最终分数: ${score}`); |
|
|
} |
|
|
|
|
|
|
|
|
function showMessage(msg) { |
|
|
messageText.innerHTML = msg; |
|
|
messageBox.style.display = 'block'; |
|
|
} |
|
|
|
|
|
|
|
|
function hideMessage() { |
|
|
messageBox.style.display = 'none'; |
|
|
} |
|
|
|
|
|
|
|
|
resetButton.addEventListener('click', initGame); |
|
|
closeMessageButton.addEventListener('click', hideMessage); |
|
|
|
|
|
|
|
|
initGame(); |
|
|
|
|
|
</script> |
|
|
|
|
|
</body> |
|
|
</html> |
|
|
|