431 lines
13 KiB
HTML
431 lines
13 KiB
HTML
<!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>
|
|
body {
|
|
margin: 0;
|
|
padding: 20px;
|
|
background-color: #333;
|
|
color: white;
|
|
font-family: Arial, sans-serif;
|
|
display: flex;
|
|
flex-direction: column;
|
|
align-items: center;
|
|
min-height: 100vh;
|
|
}
|
|
|
|
h1 {
|
|
margin-bottom: 20px;
|
|
color: #4CAF50;
|
|
}
|
|
|
|
.game-container {
|
|
background-color: #222;
|
|
border: 2px solid #4CAF50;
|
|
border-radius: 10px;
|
|
padding: 20px;
|
|
box-shadow: 0 0 20px rgba(76, 175, 80, 0.3);
|
|
}
|
|
|
|
.game-info {
|
|
display: flex;
|
|
justify-content: space-between;
|
|
margin-bottom: 10px;
|
|
font-size: 18px;
|
|
}
|
|
|
|
.score {
|
|
color: #FFD700;
|
|
}
|
|
|
|
.high-score {
|
|
color: #FF6B6B;
|
|
}
|
|
|
|
#gameCanvas {
|
|
border: 2px solid #4CAF50;
|
|
background-color: #111;
|
|
display: block;
|
|
margin: 0 auto;
|
|
}
|
|
|
|
.controls {
|
|
margin-top: 20px;
|
|
text-align: center;
|
|
}
|
|
|
|
.control-buttons {
|
|
display: grid;
|
|
grid-template-columns: repeat(3, 1fr);
|
|
gap: 10px;
|
|
max-width: 200px;
|
|
margin: 20px auto;
|
|
}
|
|
|
|
.control-btn {
|
|
background-color: #4CAF50;
|
|
color: white;
|
|
border: none;
|
|
padding: 15px;
|
|
border-radius: 5px;
|
|
cursor: pointer;
|
|
font-size: 16px;
|
|
transition: background-color 0.3s;
|
|
}
|
|
|
|
.control-btn:hover {
|
|
background-color: #45a049;
|
|
}
|
|
|
|
.control-btn:active {
|
|
background-color: #3d8b40;
|
|
}
|
|
|
|
.control-btn:nth-child(2) { grid-column: 2; }
|
|
.control-btn:nth-child(3) { grid-column: 1; grid-row: 2; }
|
|
.control-btn:nth-child(4) { grid-column: 2; grid-row: 2; }
|
|
.control-btn:nth-child(5) { grid-column: 3; grid-row: 2; }
|
|
|
|
.game-over {
|
|
position: absolute;
|
|
top: 50%;
|
|
left: 50%;
|
|
transform: translate(-50%, -50%);
|
|
background-color: rgba(0, 0, 0, 0.9);
|
|
color: white;
|
|
padding: 30px;
|
|
border-radius: 10px;
|
|
text-align: center;
|
|
display: none;
|
|
border: 2px solid #4CAF50;
|
|
}
|
|
|
|
.restart-btn {
|
|
background-color: #4CAF50;
|
|
color: white;
|
|
border: none;
|
|
padding: 10px 20px;
|
|
border-radius: 5px;
|
|
cursor: pointer;
|
|
font-size: 16px;
|
|
margin-top: 15px;
|
|
}
|
|
|
|
.restart-btn:hover {
|
|
background-color: #45a049;
|
|
}
|
|
|
|
.instructions {
|
|
margin-top: 20px;
|
|
text-align: center;
|
|
color: #ccc;
|
|
font-size: 14px;
|
|
}
|
|
|
|
@media (max-width: 600px) {
|
|
body {
|
|
padding: 10px;
|
|
}
|
|
|
|
.game-container {
|
|
padding: 10px;
|
|
}
|
|
|
|
#gameCanvas {
|
|
max-width: 100%;
|
|
height: auto;
|
|
}
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<h1>🐍 贪吃蛇小游戏</h1>
|
|
|
|
<div class="game-container">
|
|
<div class="game-info">
|
|
<div class="score">得分: <span id="score">0</span></div>
|
|
<div class="high-score">最高分: <span id="highScore">0</span></div>
|
|
</div>
|
|
|
|
<canvas id="gameCanvas" width="400" height="400"></canvas>
|
|
|
|
<div class="controls">
|
|
<div class="control-buttons">
|
|
<button class="control-btn" onclick="changeDirection('up')">↑</button>
|
|
<button class="control-btn" onclick="changeDirection('left')">←</button>
|
|
<button class="control-btn" onclick="changeDirection('down')">↓</button>
|
|
<button class="control-btn" onclick="changeDirection('right')">→</button>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="instructions">
|
|
<p>使用方向键或点击按钮控制蛇的移动</p>
|
|
<p>按空格键暂停/继续游戏</p>
|
|
</div>
|
|
</div>
|
|
|
|
<div id="gameOver" class="game-over">
|
|
<h2>游戏结束!</h2>
|
|
<p>最终得分: <span id="finalScore">0</span></p>
|
|
<button class="restart-btn" onclick="restartGame()">重新开始</button>
|
|
</div>
|
|
|
|
<script>
|
|
// 游戏变量
|
|
const canvas = document.getElementById('gameCanvas');
|
|
const ctx = canvas.getContext('2d');
|
|
const scoreElement = document.getElementById('score');
|
|
const highScoreElement = document.getElementById('highScore');
|
|
const gameOverElement = document.getElementById('gameOver');
|
|
const finalScoreElement = document.getElementById('finalScore');
|
|
|
|
// 游戏设置
|
|
const gridSize = 20;
|
|
const tileCount = canvas.width / gridSize;
|
|
|
|
let snake = [
|
|
{x: 10, y: 10}
|
|
];
|
|
let food = {};
|
|
let dx = 0;
|
|
let dy = 0;
|
|
let score = 0;
|
|
let highScore = localStorage.getItem('snakeHighScore') || 0;
|
|
let gameRunning = true;
|
|
let gamePaused = false;
|
|
|
|
// 初始化游戏
|
|
function init() {
|
|
generateFood();
|
|
highScoreElement.textContent = highScore;
|
|
gameLoop();
|
|
}
|
|
|
|
// 游戏主循环
|
|
function gameLoop() {
|
|
if (gameRunning && !gamePaused) {
|
|
clearCanvas();
|
|
moveSnake();
|
|
drawFood();
|
|
drawSnake();
|
|
checkCollision();
|
|
checkFoodCollision();
|
|
}
|
|
|
|
if (gameRunning) {
|
|
setTimeout(gameLoop, 100);
|
|
}
|
|
}
|
|
|
|
// 清空画布
|
|
function clearCanvas() {
|
|
ctx.fillStyle = '#111';
|
|
ctx.fillRect(0, 0, canvas.width, canvas.height);
|
|
}
|
|
|
|
// 生成食物
|
|
function generateFood() {
|
|
food = {
|
|
x: Math.floor(Math.random() * tileCount),
|
|
y: Math.floor(Math.random() * tileCount)
|
|
};
|
|
|
|
// 确保食物不在蛇身上
|
|
for (let segment of snake) {
|
|
if (segment.x === food.x && segment.y === food.y) {
|
|
generateFood();
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
// 绘制蛇
|
|
function drawSnake() {
|
|
ctx.fillStyle = '#4CAF50';
|
|
for (let i = 0; i < snake.length; i++) {
|
|
const segment = snake[i];
|
|
|
|
// 蛇头用更亮的颜色
|
|
if (i === 0) {
|
|
ctx.fillStyle = '#66BB6A';
|
|
} else {
|
|
ctx.fillStyle = '#4CAF50';
|
|
}
|
|
|
|
ctx.fillRect(segment.x * gridSize, segment.y * gridSize, gridSize - 2, gridSize - 2);
|
|
|
|
// 添加蛇头的眼睛
|
|
if (i === 0) {
|
|
ctx.fillStyle = '#fff';
|
|
const eyeSize = 3;
|
|
const eyeOffset = 5;
|
|
|
|
// 根据移动方向调整眼睛位置
|
|
if (dx === 1) { // 向右
|
|
ctx.fillRect(segment.x * gridSize + gridSize - eyeOffset, segment.y * gridSize + eyeOffset, eyeSize, eyeSize);
|
|
ctx.fillRect(segment.x * gridSize + gridSize - eyeOffset, segment.y * gridSize + gridSize - eyeOffset - eyeSize, eyeSize, eyeSize);
|
|
} else if (dx === -1) { // 向左
|
|
ctx.fillRect(segment.x * gridSize + eyeOffset - eyeSize, segment.y * gridSize + eyeOffset, eyeSize, eyeSize);
|
|
ctx.fillRect(segment.x * gridSize + eyeOffset - eyeSize, segment.y * gridSize + gridSize - eyeOffset - eyeSize, eyeSize, eyeSize);
|
|
} else if (dy === -1) { // 向上
|
|
ctx.fillRect(segment.x * gridSize + eyeOffset, segment.y * gridSize + eyeOffset - eyeSize, eyeSize, eyeSize);
|
|
ctx.fillRect(segment.x * gridSize + gridSize - eyeOffset - eyeSize, segment.y * gridSize + eyeOffset - eyeSize, eyeSize, eyeSize);
|
|
} else if (dy === 1) { // 向下
|
|
ctx.fillRect(segment.x * gridSize + eyeOffset, segment.y * gridSize + gridSize - eyeOffset, eyeSize, eyeSize);
|
|
ctx.fillRect(segment.x * gridSize + gridSize - eyeOffset - eyeSize, segment.y * gridSize + gridSize - eyeOffset, eyeSize, eyeSize);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// 绘制食物
|
|
function drawFood() {
|
|
ctx.fillStyle = '#FF6B6B';
|
|
ctx.beginPath();
|
|
ctx.arc(food.x * gridSize + gridSize/2, food.y * gridSize + gridSize/2, gridSize/2 - 1, 0, 2 * Math.PI);
|
|
ctx.fill();
|
|
|
|
// 添加食物的高光效果
|
|
ctx.fillStyle = '#FFA07A';
|
|
ctx.beginPath();
|
|
ctx.arc(food.x * gridSize + gridSize/2 - 3, food.y * gridSize + gridSize/2 - 3, 3, 0, 2 * Math.PI);
|
|
ctx.fill();
|
|
}
|
|
|
|
// 移动蛇
|
|
function moveSnake() {
|
|
const head = {x: snake[0].x + dx, y: snake[0].y + dy};
|
|
snake.unshift(head);
|
|
|
|
// 如果没有吃到食物,移除尾部
|
|
if (head.x !== food.x || head.y !== food.y) {
|
|
snake.pop();
|
|
}
|
|
}
|
|
|
|
// 检查食物碰撞
|
|
function checkFoodCollision() {
|
|
if (snake[0].x === food.x && snake[0].y === food.y) {
|
|
score += 10;
|
|
scoreElement.textContent = score;
|
|
generateFood();
|
|
|
|
// 更新最高分
|
|
if (score > highScore) {
|
|
highScore = score;
|
|
highScoreElement.textContent = highScore;
|
|
localStorage.setItem('snakeHighScore', highScore);
|
|
}
|
|
}
|
|
}
|
|
|
|
// 检查碰撞
|
|
function checkCollision() {
|
|
const head = snake[0];
|
|
|
|
// 检查墙壁碰撞
|
|
if (head.x < 0 || head.x >= tileCount || head.y < 0 || head.y >= tileCount) {
|
|
gameOver();
|
|
return;
|
|
}
|
|
|
|
// 检查自身碰撞
|
|
for (let i = 1; i < snake.length; i++) {
|
|
if (head.x === snake[i].x && head.y === snake[i].y) {
|
|
gameOver();
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
// 游戏结束
|
|
function gameOver() {
|
|
gameRunning = false;
|
|
finalScoreElement.textContent = score;
|
|
gameOverElement.style.display = 'block';
|
|
}
|
|
|
|
// 重新开始游戏
|
|
function restartGame() {
|
|
snake = [{x: 10, y: 10}];
|
|
dx = 0;
|
|
dy = 0;
|
|
score = 0;
|
|
scoreElement.textContent = score;
|
|
gameRunning = true;
|
|
gamePaused = false;
|
|
gameOverElement.style.display = 'none';
|
|
generateFood();
|
|
gameLoop();
|
|
}
|
|
|
|
// 改变方向
|
|
function changeDirection(direction) {
|
|
if (!gameRunning || gamePaused) return;
|
|
|
|
switch (direction) {
|
|
case 'up':
|
|
if (dy !== 1) {
|
|
dx = 0;
|
|
dy = -1;
|
|
}
|
|
break;
|
|
case 'down':
|
|
if (dy !== -1) {
|
|
dx = 0;
|
|
dy = 1;
|
|
}
|
|
break;
|
|
case 'left':
|
|
if (dx !== 1) {
|
|
dx = -1;
|
|
dy = 0;
|
|
}
|
|
break;
|
|
case 'right':
|
|
if (dx !== -1) {
|
|
dx = 1;
|
|
dy = 0;
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
|
|
// 键盘控制
|
|
document.addEventListener('keydown', (e) => {
|
|
switch (e.key) {
|
|
case 'ArrowUp':
|
|
e.preventDefault();
|
|
changeDirection('up');
|
|
break;
|
|
case 'ArrowDown':
|
|
e.preventDefault();
|
|
changeDirection('down');
|
|
break;
|
|
case 'ArrowLeft':
|
|
e.preventDefault();
|
|
changeDirection('left');
|
|
break;
|
|
case 'ArrowRight':
|
|
e.preventDefault();
|
|
changeDirection('right');
|
|
break;
|
|
case ' ':
|
|
e.preventDefault();
|
|
gamePaused = !gamePaused;
|
|
if (!gamePaused && gameRunning) {
|
|
gameLoop();
|
|
}
|
|
break;
|
|
}
|
|
});
|
|
|
|
// 启动游戏
|
|
init();
|
|
</script>
|
|
</body>
|
|
</html>
|