-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnake.html
More file actions
229 lines (200 loc) · 7.01 KB
/
snake.html
File metadata and controls
229 lines (200 loc) · 7.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>经典贪吃蛇游戏</title>
<style>
body {
display: flex;
flex-direction: column;
align-items: center;
background-color: #f0f0f0;
font-family: Arial, sans-serif;
}
#game-container {
margin-top: 20px;
}
#game-board {
border: 2px solid #333;
background-color: #fff;
}
#score-panel {
margin-top: 10px;
font-size: 18px;
display: flex;
gap: 20px;
}
#controls {
margin-top: 20px;
display: flex;
gap: 10px;
}
button {
padding: 8px 16px;
font-size: 16px;
cursor: pointer;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 4px;
}
button:hover {
background-color: #45a049;
}
.game-over {
position: absolute;
background-color: rgba(0, 0, 0, 0.8);
color: white;
padding: 20px;
text-align: center;
border-radius: 10px;
display: none;
}
</style>
</head>
<body>
<h1>经典贪吃蛇游戏</h1>
<div id="game-container">
<canvas id="game-board" width="400" height="400"></canvas>
<div id="score-panel">
<div>当前得分: <span id="score">0</span></div>
<div>最高得分: <span id="high-score">0</span></div>
</div>
<div id="controls">
<button onclick="startGame()">开始游戏</button>
<button onclick="goToIndex()" style="background-color: #2196F3;">返回主页</button>
</div>
<div id="game-over" class="game-over">
<h2>游戏结束!</h2>
<p>最终得分: <span id="final-score">0</span></p>
<button onclick="startGame()">再玩一次</button>
</div>
</div>
<script>
const canvas = document.getElementById('game-board');
const ctx = canvas.getContext('2d');
const GRID_SIZE = 20;
const GRID_COUNT = canvas.width / GRID_SIZE;
let snake = [];
let food = {};
let direction = 'right';
let nextDirection = 'right';
let gameLoop;
let score = 0;
let highScore = localStorage.getItem('snakeHighScore') || 0;
document.getElementById('high-score').textContent = highScore;
function initGame() {
snake = [
{ x: 5, y: 5 },
{ x: 4, y: 5 },
{ x: 3, y: 5 }
];
direction = 'right';
nextDirection = 'right';
score = 0;
document.getElementById('score').textContent = score;
generateFood();
}
function generateFood() {
while (true) {
food = {
x: Math.floor(Math.random() * GRID_COUNT),
y: Math.floor(Math.random() * GRID_COUNT)
};
// 检查食物是否生成在蛇身上
if (!snake.some(segment => segment.x === food.x && segment.y === food.y)) {
break;
}
}
}
function draw() {
// 清空画布
ctx.fillStyle = '#fff';
ctx.fillRect(0, 0, canvas.width, canvas.height);
// 绘制蛇
snake.forEach((segment, index) => {
ctx.fillStyle = index === 0 ? '#4CAF50' : '#8BC34A';
ctx.fillRect(segment.x * GRID_SIZE, segment.y * GRID_SIZE, GRID_SIZE - 1, GRID_SIZE - 1);
});
// 绘制食物
ctx.fillStyle = '#FF5722';
ctx.fillRect(food.x * GRID_SIZE, food.y * GRID_SIZE, GRID_SIZE - 1, GRID_SIZE - 1);
}
function move() {
direction = nextDirection;
const head = { ...snake[0] };
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.x >= GRID_COUNT ||
head.y < 0 || head.y >= GRID_COUNT ||
snake.some(segment => segment.x === head.x && segment.y === head.y)) {
gameOver();
return;
}
snake.unshift(head);
// 吃食物检测
if (head.x === food.x && head.y === food.y) {
score += 10;
document.getElementById('score').textContent = score;
generateFood();
} else {
snake.pop();
}
}
function gameOver() {
clearInterval(gameLoop);
document.getElementById('game-over').style.display = 'block';
document.getElementById('final-score').textContent = score;
if (score > highScore) {
highScore = score;
localStorage.setItem('snakeHighScore', highScore);
document.getElementById('high-score').textContent = highScore;
}
}
function startGame() {
document.getElementById('game-over').style.display = 'none';
clearInterval(gameLoop);
initGame();
gameLoop = setInterval(() => {
move();
draw();
}, 150);
}
// 键盘控制
document.addEventListener('keydown', (event) => {
switch (event.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;
}
});
// 初始绘制
draw();
function goToIndex() {
window.location.href = "index.html";
}
</script>
<div style="margin-top: 20px; max-width: 500px;">
<h3>游戏说明:</h3>
<p>1. 点击"开始游戏"按钮开始游戏</p>
<p>2. 使用方向键控制蛇的移动方向</p>
<p>3. 吃到红色食物可以增长身体并获得10分</p>
<p>4. 避免撞到墙壁或自己的身体</p>
<p>5. 最高分会保存在本地浏览器中</p>
</div>
</body>
</html>