写一个贪吃蛇的 html
- User
写一个贪吃蛇的 html
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
写一个贪吃蛇的 html
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
以下是一个简单的贪吃蛇游戏的HTML代码:
<!DOCTYPE html>
<html>
<head>
<title>贪吃蛇游戏</title>
<style>
body {
display: flex;
flex-direction: column;
align-items: center;
font-family: Arial, sans-serif;
background-color: #f0f0f0;
}
#game-board {
width: 400px;
height: 400px;
border: 2px solid #333;
background-color: #fff;
position: relative;
}
.snake-part {
width: 20px;
height: 20px;
background-color: #4CAF50;
position: absolute;
border-radius: 3px;
}
.food {
width: 20px;
height: 20px;
background-color: #FF5722;
position: absolute;
border-radius: 50%;
}
#score {
margin-top: 20px;
font-size: 24px;
}
#controls {
margin-top: 20px;
}
button {
padding: 10px 20px;
font-size: 16px;
background-color: #2196F3;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
margin: 0 5px;
}
button:hover {
background-color: #0b7dda;
}
</style>
</head>
<body>
<h1>贪吃蛇游戏</h1>
<div id="game-board"></div>
<div id="score">分数: 0</div>
<div id="controls">
<button id="start-btn">开始游戏</button>
<button id="pause-btn">暂停</button>
</div>
<script>
document.addEventListener('DOMContentLoaded', () => {
const board = document.getElementById('game-board');
const scoreDisplay = document.getElementById('score');
const startBtn = document.getElementById('start-btn');
const pauseBtn = document.getElementById('pause-btn');
const boardWidth = 400;
const boardHeight = 400;
const gridSize = 20;
const gridWidth = boardWidth / gridSize;
const gridHeight = boardHeight / gridSize;
let snake = [
{x: 10, y: 10},
{x: 9, y: 10},
{x: 8, y: 10}
];
let food = {};
let direction = 'right';
let nextDirection = 'right';
let gameInterval;
let score = 0;
let isPaused = false;
let gameStarted = false;
function drawSnake() {
// 清除旧蛇
const oldParts = document.querySelectorAll('.snake-part');
oldParts.forEach(part => part.remove());
// 绘制新蛇
snake.forEach(part => {
const snakePart = document.createElement('div');
snakePart.className = 'snake-part';
snakePart.style.left = part.x * gridSize + 'px';
snakePart.style.top = part.y * gridSize + 'px';
board.appendChild(snakePart);
});
}
function createFood() {
// 移除旧食物
const oldFood = document.querySelector('.food');
if (oldFood) {
oldFood.remove();
}
// 生成新食物位置
let newFood;
do {
newFood = {
x: Math.floor(Math.random() * gridWidth),
y: Math.floor(Math.random() * gridHeight)
};
// 确保食物不会出现在蛇身上
} while (snake.some(part => part.x === newFood.x && part.y === newFood.y));
food = newFood;
// 绘制食物
const foodElement = document.createElement('div');
foodElement.className = 'food';
foodElement.style.left = food.x * gridSize + 'px';
foodElement.style.top = food.y * gridSize + 'px';
board.appendChild(foodElement);
}
function moveSnake() {
direction = nextDirection;
// 计算新的蛇头位置
const head = {...snake[0]};
switch(direction) {
case 'up':
head.y -= 1;
break;
case 'down':
head.y += 1;
break;
case 'left':
head.x -= 1;
break;
case 'right':
head.x += 1;
break;
}
// 检查是否撞墙
if (head.x < 0 || head.x >= gridWidth || head.y < 0 || head.y >= gridHeight) {
gameOver();
return;
}
// 检查是否撞到自己
if (snake.some(part => part.x === head.x && part.y === head.y)) {
gameOver();
return;
}
// 将新头部添加到蛇身
snake.unshift(head);
// 检查是否吃到食物
if (head.x === food.x && head.y === food.y) {
score += 10;
scoreDisplay.textContent = `分数: ${score}`;
createFood();
} else {
// 如果没吃到食物,移除尾部
snake.pop();
}
drawSnake();
}
function gameOver() {
clearInterval(gameInterval);
alert(`游戏结束!你的分数是: ${score}`);
gameStarted = false;
startBtn.textContent = '重新开始';
}
function startGame() {
if (gameStarted) return;
// 重置游戏状态
snake = [
{x: 10, y: 10},
{x: 9, y: 10},
{x: 8, y: 10}
];
direction = 'right';
nextDirection = 'right';
score = 0;
scoreDisplay.textContent = `分数: ${score}`;
isPaused = false;
gameStarted = true;
// 清除旧元素
board.innerHTML = '';
// 初始化游戏
drawSnake();
createFood();
// 开始游戏循环
gameInterval = setInterval(moveSnake, 150);
}
function togglePause() {
if (!gameStarted) return;
if (isPaused) {
gameInterval = setInterval(moveSnake, 150);
pauseBtn.textContent = '暂停';
} else {
clearInterval(gameInterval);
pauseBtn.textContent = '继续';
}
isPaused = !isPaused;
}
// 事件监听
startBtn.addEventListener('click', startGame);
pauseBtn.addEventListener('click', togglePause);
document.addEventListener('keydown', (e) => {
switch(e.key) {
case 'ArrowUp':
if (direction !== 'down') nextDirection = 'up';
break;
case 'ArrowDown':
if (direction !== 'up') nextDirection = 'down';
break;
case 'ArrowLeft':
if (direction !== 'right') nextDirection = 'left';
break;
case 'ArrowRight':
if (direction !== 'left') nextDirection = 'right';
break;
}
});
});
</script>
</body>
</html>
需要我解释或者分解这段代码吗?