- BC20270270's blog
贪吃蛇大便
- 2025-6-7 15:27:48 @
使用上下左右键玩
点击"开始游戏"按钮开始
使用键盘方向键或屏幕上的方向按钮控制蛇的移动
吃到红色食物增加10分和蛇的长度
吃到蓝色特殊食物增加50分并缩短蛇身
避免撞墙或自己的身体
游戏结束后点击"再来一次"重新开始
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>贪吃蛇大冒险</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
body {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
background: linear-gradient(135deg, #1a1a2e, #16213e, #0f3460);
overflow: hidden;
color: #fff;
}
.game-container {
display: flex;
flex-direction: column;
align-items: center;
padding: 20px;
max-width: 1000px;
width: 100%;
}
.header {
text-align: center;
margin-bottom: 20px;
width: 100%;
}
h1 {
font-size: 3.5rem;
margin-bottom: 10px;
color: #e94560;
text-shadow: 0 0 10px rgba(233, 69, 96, 0.7);
letter-spacing: 3px;
}
.stats-container {
display: flex;
justify-content: space-between;
width: 100%;
background: rgba(255, 255, 255, 0.1);
backdrop-filter: blur(10px);
border-radius: 15px;
padding: 15px 25px;
margin-bottom: 20px;
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
}
.stat-box {
text-align: center;
}
.stat-title {
font-size: 1.1rem;
color: #a9a9a9;
margin-bottom: 5px;
}
.stat-value {
font-size: 2.2rem;
font-weight: bold;
color: #4dccbd;
}
.game-board {
position: relative;
width: 600px;
height: 450px;
margin: 20px auto;
border-radius: 15px;
overflow: hidden;
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
}
canvas {
background: rgba(13, 19, 33, 0.9);
display: block;
}
.controls {
display: flex;
gap: 15px;
margin-top: 20px;
flex-wrap: wrap;
justify-content: center;
}
button {
padding: 12px 25px;
font-size: 1.1rem;
font-weight: 600;
border: none;
border-radius: 50px;
cursor: pointer;
transition: all 0.3s ease;
background: linear-gradient(to right, #e94560, #ff6b81);
color: white;
box-shadow: 0 5px 15px rgba(233, 69, 96, 0.4);
}
button:hover {
transform: translateY(-3px);
box-shadow: 0 8px 20px rgba(233, 69, 96, 0.6);
}
button:active {
transform: translateY(1px);
}
.difficulty {
display: flex;
gap: 10px;
margin-top: 15px;
}
.difficulty-btn {
padding: 8px 20px;
font-size: 0.9rem;
background: rgba(255, 255, 255, 0.1);
}
.difficulty-btn.active {
background: linear-gradient(to right, #4dccbd, #3aafa9);
box-shadow: 0 5px 15px rgba(77, 204, 189, 0.4);
}
.overlay {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.85);
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
z-index: 10;
border-radius: 15px;
}
.overlay h2 {
font-size: 3rem;
margin-bottom: 20px;
color: #e94560;
}
.overlay p {
font-size: 1.5rem;
margin-bottom: 30px;
text-align: center;
max-width: 80%;
}
.instructions {
background: rgba(255, 255, 255, 0.1);
border-radius: 15px;
padding: 20px;
margin-top: 20px;
width: 100%;
max-width: 600px;
}
.instructions h3 {
color: #4dccbd;
margin-bottom: 10px;
text-align: center;
}
.instructions ul {
padding-left: 20px;
}
.instructions li {
margin-bottom: 8px;
line-height: 1.5;
}
.key {
display: inline-block;
background: rgba(255, 255, 255, 0.15);
padding: 2px 8px;
border-radius: 5px;
font-family: monospace;
}
.mobile-controls {
display: none;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: repeat(2, 1fr);
gap: 10px;
margin-top: 20px;
width: 200px;
height: 200px;
}
.mobile-btn {
display: flex;
justify-content: center;
align-items: center;
background: rgba(255, 255, 255, 0.15);
border-radius: 10px;
font-size: 24px;
font-weight: bold;
user-select: none;
}
.up { grid-column: 2; grid-row: 1; }
.left { grid-column: 1; grid-row: 2; }
.right { grid-column: 3; grid-row: 2; }
.down { grid-column: 2; grid-row: 3; }
@media (max-width: 650px) {
.game-board {
width: 95vw;
height: 70vw;
}
h1 {
font-size: 2.5rem;
}
.stat-value {
font-size: 1.8rem;
}
.mobile-controls {
display: grid;
}
}
</style>
</head>
<body>
<div class="game-container">
<div class="header">
<h1>贪吃蛇大冒险</h1>
</div>
<div class="stats-container">
<div class="stat-box">
<div class="stat-title">当前分数</div>
<div class="stat-value" id="score">0</div>
</div>
<div class="stat-box">
<div class="stat-title">最高分数</div>
<div class="stat-value" id="high-score">0</div>
</div>
<div class="stat-box">
<div class="stat-title">游戏速度</div>
<div class="stat-value" id="speed">5</div>
</div>
</div>
<div class="game-board">
<canvas id="game-canvas" width="600" height="450"></canvas>
<div class="overlay" id="start-screen">
<h2>贪吃蛇大冒险</h2>
<p>使用方向键控制贪吃蛇,吃到食物获得分数!</p>
<button id="start-btn">开始游戏</button>
</div>
<div class="overlay" id="game-over" style="display: none;">
<h2>游戏结束!</h2>
<p>你的分数: <span id="final-score">0</span></p>
<button id="restart-btn">再来一次</button>
</div>
</div>
<div class="controls">
<button id="pause-btn">暂停</button>
<button id="reset-btn">重新开始</button>
</div>
<div class="difficulty">
<button class="difficulty-btn active" data-speed="120">简单</button>
<button class="difficulty-btn" data-speed="80">中等</button>
<button class="difficulty-btn" data-speed="50">困难</button>
<button class="difficulty-btn" data-speed="30">地狱</button>
</div>
<div class="mobile-controls">
<div class="mobile-btn up">↑</div>
<div class="mobile-btn left">←</div>
<div class="mobile-btn right">→</div>
<div class="mobile-btn down">↓</div>
</div>
<div class="instructions">
<h3>游戏说明</h3>
<ul>
<li>使用 <span class="key">方向键</span> 或 <span class="key">WASD</span> 控制贪吃蛇移动</li>
<li>吃到 <span style="color: #ff6b6b">红色食物</span> 得10分,蛇身变长</li>
<li>吃到 <span style="color: #4dccbd">特殊食物</span> 得50分,蛇身缩短</li>
<li>撞到墙壁或自己的身体游戏结束</li>
<li>难度越高,蛇移动速度越快</li>
</ul>
</div>
</div>
<script>
// 游戏主逻辑
document.addEventListener('DOMContentLoaded', () => {
const canvas = document.getElementById('game-canvas');
const ctx = canvas.getContext('2d');
const scoreElement = document.getElementById('score');
const highScoreElement = document.getElementById('high-score');
const speedElement = document.getElementById('speed');
const startScreen = document.getElementById('start-screen');
const gameOverScreen = document.getElementById('game-over');
const startBtn = document.getElementById('start-btn');
const pauseBtn = document.getElementById('pause-btn');
const resetBtn = document.getElementById('reset-btn');
const restartBtn = document.getElementById('restart-btn');
const finalScoreElement = document.getElementById('final-score');
const difficultyBtns = document.querySelectorAll('.difficulty-btn');
const mobileControls = document.querySelectorAll('.mobile-btn');
// 游戏变量
const gridSize = 20;
const gridWidth = canvas.width / gridSize;
const gridHeight = canvas.height / gridSize;
let snake = [];
let food = {};
let specialFood = {};
let direction = 'right';
let nextDirection = 'right';
let score = 0;
let highScore = localStorage.getItem('snakeHighScore') || 0;
let gameSpeed = 120; // 毫秒
let gameInterval;
let gameRunning = false;
let gamePaused = false;
let specialFoodActive = false;
let specialFoodTimer = 0;
// 初始化游戏
function initGame() {
// 初始化蛇
snake = [
{x: 5, y: 10},
{x: 4, y: 10},
{x: 3, y: 10}
];
// 生成食物
generateFood();
specialFoodActive = false;
specialFoodTimer = 0;
// 重置分数和方向
score = 0;
direction = 'right';
nextDirection = 'right';
updateScore();
// 隐藏游戏结束界面
gameOverScreen.style.display = 'none';
// 设置最高分
highScoreElement.textContent = highScore;
// 绘制游戏
draw();
}
// 生成食物
function generateFood() {
food = {
x: Math.floor(Math.random() * gridWidth),
y: Math.floor(Math.random() * gridHeight)
};
// 确保食物不出现在蛇身上
for (let segment of snake) {
if (segment.x === food.x && segment.y === food.y) {
return generateFood();
}
}
}
// 生成特殊食物
function generateSpecialFood() {
if (!specialFoodActive && Math.random() > 0.7) {
specialFood = {
x: Math.floor(Math.random() * gridWidth),
y: Math.floor(Math.random() * gridHeight)
};
// 确保特殊食物不出现在蛇身上或普通食物上
for (let segment of snake) {
if (segment.x === specialFood.x && segment.y === specialFood.y) {
return generateSpecialFood();
}
}
if (specialFood.x === food.x && specialFood.y === food.y) {
return generateSpecialFood();
}
specialFoodActive = true;
specialFoodTimer = 300; // 特殊食物存在300帧
}
}
// 绘制游戏
function draw() {
// 清空画布
ctx.clearRect(0, 0, canvas.width, canvas.height);
// 绘制网格背景
drawGrid();
// 绘制蛇
drawSnake();
// 绘制食物
drawFood();
// 绘制特殊食物
if (specialFoodActive) {
drawSpecialFood();
}
}
// 绘制网格
function drawGrid() {
ctx.strokeStyle = 'rgba(255, 255, 255, 0.05)';
ctx.lineWidth = 0.5;
// 垂直线
for (let x = 0; x < canvas.width; x += gridSize) {
ctx.beginPath();
ctx.moveTo(x, 0);
ctx.lineTo(x, canvas.height);
ctx.stroke();
}
// 水平线
for (let y = 0; y < canvas.height; y += gridSize) {
ctx.beginPath();
ctx.moveTo(0, y);
ctx.lineTo(canvas.width, y);
ctx.stroke();
}
}
// 绘制蛇
function drawSnake() {
// 绘制蛇身
for (let i = 0; i < snake.length; i++) {
const segment = snake[i];
// 蛇头
if (i === 0) {
ctx.fillStyle = '#4dccbd';
ctx.fillRect(segment.x * gridSize, segment.y * gridSize, gridSize, gridSize);
// 蛇头高光
ctx.fillStyle = '#7bdef0';
ctx.fillRect(segment.x * gridSize + 4, segment.y * gridSize + 4, gridSize - 8, gridSize - 8);
// 眼睛
ctx.fillStyle = '#1a1a2e';
if (direction === 'right') {
ctx.fillRect(segment.x * gridSize + 12, segment.y * gridSize + 5, 4, 4);
ctx.fillRect(segment.x * gridSize + 12, segment.y * gridSize + 11, 4, 4);
} else if (direction === 'left') {
ctx.fillRect(segment.x * gridSize + 4, segment.y * gridSize + 5, 4, 4);
ctx.fillRect(segment.x * gridSize + 4, segment.y * gridSize + 11, 4, 4);
} else if (direction === 'up') {
ctx.fillRect(segment.x * gridSize + 5, segment.y * gridSize + 4, 4, 4);
ctx.fillRect(segment.x * gridSize + 11, segment.y * gridSize + 4, 4, 4);
} else if (direction === 'down') {
ctx.fillRect(segment.x * gridSize + 5, segment.y * gridSize + 12, 4, 4);
ctx.fillRect(segment.x * gridSize + 11, segment.y * gridSize + 12, 4, 4);
}
}
// 蛇身
else {
ctx.fillStyle = '#3aafa9';
ctx.fillRect(segment.x * gridSize, segment.y * gridSize, gridSize, gridSize);
// 蛇身内部
ctx.fillStyle = '#4dccbd';
ctx.fillRect(segment.x * gridSize + 4, segment.y * gridSize + 4, gridSize - 8, gridSize - 8);
}
}
}
// 绘制食物
function drawFood() {
// 食物主体
ctx.fillStyle = '#ff6b6b';
ctx.beginPath();
ctx.arc(
food.x * gridSize + gridSize / 2,
food.y * gridSize + gridSize / 2,
gridSize / 2 - 2,
0,
Math.PI * 2
);
ctx.fill();
// 食物高光
ctx.fillStyle = '#ff9e9e';
ctx.beginPath();
ctx.arc(
food.x * gridSize + gridSize / 3,
food.y * gridSize + gridSize / 3,
gridSize / 6,
0,
Math.PI * 2
);
ctx.fill();
}
// 绘制特殊食物
function drawSpecialFood() {
// 特殊食物主体
ctx.fillStyle = '#4dccbd';
ctx.beginPath();
ctx.arc(
specialFood.x * gridSize + gridSize / 2,
specialFood.y * gridSize + gridSize / 2,
gridSize / 2 - 2,
0,
Math.PI * 2
);
ctx.fill();
// 特殊食物闪烁效果
if (Math.floor(specialFoodTimer / 10) % 2 === 0) {
ctx.fillStyle = '#7bdef0';
ctx.beginPath();
ctx.arc(
specialFood.x * gridSize + gridSize / 2,
specialFood.y * gridSize + gridSize / 2,
gridSize / 4,
0,
Math.PI * 2
);
ctx.fill();
}
}
// 更新游戏状态
function update() {
if (gamePaused) return;
// 更新特殊食物计时器
if (specialFoodActive) {
specialFoodTimer--;
if (specialFoodTimer <= 0) {
specialFoodActive = false;
}
} else {
generateSpecialFood();
}
// 更新方向
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.y < 0 ||
head.x >= gridWidth ||
head.y >= gridHeight ||
checkCollision(head)
) {
gameOver();
return;
}
// 添加新蛇头
snake.unshift(head);
// 检查是否吃到食物
if (head.x === food.x && head.y === food.y) {
// 增加分数
score += 10;
updateScore();
// 生成新食物
generateFood();
}
// 检查是否吃到特殊食物
else if (specialFoodActive && head.x === specialFood.x && head.y === specialFood.y) {
// 增加分数
score += 50;
updateScore();
// 缩短蛇身(最多保留3节)
if (snake.length > 3) {
snake.pop();
if (snake.length > 3) snake.pop();
}
specialFoodActive = false;
}
// 没吃到食物,移除蛇尾
else {
snake.pop();
}
// 绘制游戏
draw();
}
// 检查碰撞
function checkCollision(position) {
for (let i = 1; i < snake.length; i++) {
if (snake[i].x === position.x && snake[i].y === position.y) {
return true;
}
}
return false;
}
// 更新分数显示
function updateScore() {
scoreElement.textContent = score;
speedElement.textContent = Math.floor(1500 / gameSpeed);
// 更新最高分
if (score > highScore) {
highScore = score;
highScoreElement.textContent = highScore;
localStorage.setItem('snakeHighScore', highScore);
}
}
// 游戏结束
function gameOver() {
clearInterval(gameInterval);
gameRunning = false;
finalScoreElement.textContent = score;
gameOverScreen.style.display = 'flex';
}
// 开始游戏
function startGame() {
if (gameRunning) return;
initGame();
gameRunning = true;
gamePaused = false;
startScreen.style.display = 'none';
pauseBtn.textContent = '暂停';
gameInterval = setInterval(update, gameSpeed);
}
// 暂停游戏
function togglePause() {
if (!gameRunning) return;
gamePaused = !gamePaused;
pauseBtn.textContent = gamePaused ? '继续' : '暂停';
}
// 重置游戏
function resetGame() {
clearInterval(gameInterval);
startGame();
}
// 键盘控制
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 ' ':
togglePause();
break;
}
});
// 移动端控制
mobileControls.forEach(btn => {
btn.addEventListener('click', () => {
if (!gameRunning) return;
const directionMap = {
'up': () => { if (direction !== 'down') nextDirection = 'up'; },
'down': () => { if (direction !== 'up') nextDirection = 'down'; },
'left': () => { if (direction !== 'right') nextDirection = 'left'; },
'right': () => { if (direction !== 'left') nextDirection = 'right'; }
};
directionMap[btn.classList[1]]();
});
});
// 难度选择
difficultyBtns.forEach(btn => {
btn.addEventListener('click', () => {
// 更新活动按钮
difficultyBtns.forEach(b => b.classList.remove('active'));
btn.classList.add('active');
// 更新游戏速度
gameSpeed = parseInt(btn.dataset.speed);
speedElement.textContent = Math.floor(1500 / gameSpeed);
// 如果游戏正在运行,更新游戏速度
if (gameRunning) {
clearInterval(gameInterval);
gameInterval = setInterval(update, gameSpeed);
}
});
});
// 按钮事件监听
startBtn.addEventListener('click', startGame);
pauseBtn.addEventListener('click', togglePause);
resetBtn.addEventListener('click', resetGame);
restartBtn.addEventListener('click', startGame);
// 初始化
initGame();
highScoreElement.textContent = highScore;
});
</script>
</body>
</html>