方向键控制蛇的移动,吃到食物得分,撞到边界或自己游戏结束
<!DOCTYPE html>
<html>
<head>
<!-- 标题、元信息和CSS样式 -->
</head>
<body>
<h1>贪吃蛇游戏</h1>
<div id="score">分数: 0</div>
<div id="game-container">
<div id="snake"></div>
<div id="food"></div>
</div>
<div class="controls">
<!-- 控制按钮 -->
</div>
<!-- 游戏说明和分析 -->
<script>
<!-- JavaScript代码 -->
</script>
</body>
</html>
// DOM元素引用 const snake, food, gameContainer, scoreElement; // 游戏状态变量 let snakeSize, snakePos, foodPos, direction, gameLoop, score, isPaused;
// 初始化游戏
function initGame() { ... }
// 生成食物
function generateFood() { ... }
// 渲染蛇
function renderSnake() { ... }
// 移动蛇
function moveSnake() { ... }
// 检查碰撞
function checkCollision(head) { ... }
// 游戏控制函数
function startGame() { ... }
function pauseGame() { ... }
function restartGame() { ... }
1. 初始化游戏状态 2. 生成初始食物 3. 启动游戏循环(setInterval) 4. 每次循环中: - 检查游戏是否暂停 - 计算蛇头新位置 - 检查碰撞 - 更新蛇的位置 - 检查是否吃到食物 - 重新渲染蛇
// DOM元素引用 const snake = document.getElementById('snake'); const food = document.getElementById('food'); const gameContainer = document.getElementById('game-container'); const scoreElement = document.getElementById('score'); // 游戏状态变量 let snakeSize = 20; let snakePos = [{ x: 100, y: 100 }]; let foodPos = { x: 0, y: 0 }; let direction = { x: snakeSize, y: 0 }; let gameLoop; let score = 0; let isPaused = false; // 初始化游戏 function initGame() { snakePos = [{ x: 100, y: 100 }]; direction = { x: snakeSize, y: 0 }; score = 0; scoreElement.textContent = `分数: ${score}`; generateFood(); renderSnake(); } // 生成食物 function generateFood() { const maxX = gameContainer.clientWidth - snakeSize; const maxY = gameContainer.clientHeight - snakeSize; foodPos.x = Math.floor(Math.random() * (maxX / snakeSize)) * snakeSize; foodPos.y = Math.floor(Math.random() * (maxY / snakeSize)) * snakeSize; // 避免食物生成在蛇身上 if (snakePos.some(pos => pos.x === foodPos.x && pos.y === foodPos.y)) { generateFood(); } food.style.left = `${foodPos.x}px`; food.style.top = `${foodPos.y}px`; } // 渲染蛇 function renderSnake() { snake.innerHTML = ''; snakePos.forEach((pos, index) => { const segment = document.createElement('div'); segment.style.position = 'absolute'; segment.style.width = `${snakeSize}px`; segment.style.height = `${snakeSize}px`; segment.style.left = `${pos.x}px`; segment.style.top = `${pos.y}px`; segment.style.backgroundColor = index === 0 ? '#4CAF50' : '#8BC34A'; segment.style.borderRadius = '3px'; snake.appendChild(segment); }); } // 移动蛇 function moveSnake() { if (isPaused) return; const head = { x: snakePos[0].x + direction.x, y: snakePos[0].y + direction.y }; // 检查碰撞 if (checkCollision(head)) { clearInterval(gameLoop); alert(`游戏结束!最终分数: ${score}`); return; } snakePos.unshift(head); // 检查是否吃到食物 if (head.x === foodPos.x && head.y === foodPos.y) { score += 10; scoreElement.textContent = `分数: ${score}`; generateFood(); } else { snakePos.pop(); } renderSnake(); } // 检查碰撞 function checkCollision(head) { // 边界碰撞 if (head.x < 0 || head.x >= gameContainer.clientWidth || head.y < 0 || head.y >= gameContainer.clientHeight) { return true; } // 自身碰撞 return snakePos.slice(1).some(pos => pos.x === head.x && pos.y === head.y); } // 控制方向 document.addEventListener('keydown', (e) => { switch (e.key) { case 'ArrowUp': if (direction.y === 0) direction = { x: 0, y: -snakeSize }; break; case 'ArrowDown': if (direction.y === 0) direction = { x: 0, y: snakeSize }; break; case 'ArrowLeft': if (direction.x === 0) direction = { x: -snakeSize, y: 0 }; break; case 'ArrowRight': if (direction.x === 0) direction = { x: snakeSize, y: 0 }; break; } }); // 游戏控制函数 function startGame() { if (gameLoop) clearInterval(gameLoop); isPaused = false; gameLoop = setInterval(moveSnake, 150); } function pauseGame() { isPaused = !isPaused; } function restartGame() { clearInterval(gameLoop); initGame(); } // 初始化 initGame();