-
Notifications
You must be signed in to change notification settings - Fork 0
/
贪吃蛇.html
366 lines (334 loc) · 9.86 KB
/
贪吃蛇.html
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>贪吃蛇</title>
<meta name="description" content="简易版Web贪吃蛇小游戏">
<meta name="keywords" content="snake,game,online,javascript,PC game,mobile game">
<style>
body {
background-color: #ffffff;
}
.container {
text-align: center;
}
.top {
margin: 20px auto;
width: 640px;
}
#score {
float: left;
}
.main {
position: absolute;
left: 50%;
transform: translateX(-50%);
width: 642px;
height: 402px;
}
#snake {
border: 1px solid #000;
width: 640px;
height: 400px;
display: inline-block;
z-index: 99;
background-color: rgba(0, 0, 0, 0.1);
}
#mask {
background-color: rgba(0, 0, 0, 0.5);
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
z-index: 100;
display: block;
color: #fff;
line-height: 400px;
text-align: center;
font-size: 30px;
cursor: pointer;
}
</style>
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-3609041871317154"
crossorigin="anonymous"></script>
</head>
<body>
<div class="container">
<div class="top">
<span id="score">Score: 0</span>
<button id="restart">Restart</button>
<button id="stop">Pause</button>
<button id="continue">Continue</button>
</div>
<div class="main">
<canvas id="snake" width="640" height="400"></canvas>
<div id="mask">Start</div>
</div>
</div>
<script>
let greedySnake = null;
let score = document.querySelector("#score");
let restart = document.querySelector("#restart");
let stop = document.querySelector("#stop");
let conti = document.querySelector("#continue");
let mask = document.querySelector("#mask");
restart.onclick = () => {
if (!greedySnake.isStart) return;
greedySnake.start();
};
stop.onclick = () => {
if (greedySnake.isStop || !greedySnake.isStart) return;
greedySnake.stop();
};
conti.onclick = () => {
if (!greedySnake.isStop || !greedySnake.isStart) return;
greedySnake.continue();
};
mask.onclick = () => {
if (!greedySnake.isStart) {
greedySnake.start();
} else {
greedySnake.continue();
}
};
// 大小为64 * 40
class GreedySnake {
constructor() {
this.canvas = document.querySelector("#snake");
this.ctx = this.canvas.getContext("2d");
this.maxX = 64; // 最大行
this.maxY = 40; // 最大列
this.itemWidth = 10; // 每个点的大小
this.direction = "right"; // up down right left 方向
this.speed = 150; // ms 速度
this.isStop = false; // 是否暂停
this.isOver = false; // 是否结束
this.isStart = false; // 是否开始
this.score = 0; // 分数
this.timer = null; // 移动定时器
this.j = 1;
this.canChange = true;
this.grid = new Array();
for (let i = 0; i < this.maxX; i++) {
for (let j = 0; j < this.maxY; j++) {
this.grid.push([i, j]);
}
}
this.drawGridLine();
this.getDirection();
}
// 开始
start() {
if (this.timer) {
clearTimeout(this.timer);
}
if (!this.isStart) {
this.isStart = true;
}
this.score = 0;
this.speed = 150;
this.isStop = false;
this.isOver = false;
this.direction = "right";
this.createSnake();
this.createFood();
this.draw();
this.move();
mask.style.display = "none";
}
// 创建蛇主体
createSnake() {
this.snake = [
[4, 25],
[3, 25],
[2, 25],
[1, 25],
[0, 25],
];
}
// 移动
move() {
if (this.isStop) return;
let [x, y] = this.snake[0];
switch (this.direction) {
case "left":
x--;
break;
case "right":
x++;
break;
case "up":
y--;
break;
case "down":
y++;
break;
}
// 如果下一步不是食物的位置
if (x !== this.food[0] || y !== this.food[1]) {
this.snake.pop();
} else {
this.createFood();
}
if (this.over([x, y])) {
this.isOver = true;
mask.style.display = "block";
mask.innerHTML = "Game Over!";
return;
}
if (this.completed()) {
mask.style.display = "block";
mask.innerHTML = "Congratulations, you win!";
return;
}
this.snake.unshift([x, y]);
this.draw();
this.canChange = true;
this.timer = setTimeout(() => this.move(), this.speed);
}
// 暂停游戏
stop() {
if (this.isOver) return;
this.isStop = true;
mask.style.display = "block";
mask.innerHTML = "Pause";
}
// 继续游戏
continue() {
if (this.isOver) return;
this.isStop = false;
this.move();
mask.style.display = "none";
}
getDirection() {
// 上38 下40 左37 右39 不能往相反的方向走
document.onkeydown = (e) => {
// 在贪吃蛇移动的间隔内不能连续改变两次方向
if (!this.canChange) return;
switch (e.keyCode) {
case 37:
if (this.direction !== "right") {
this.direction = "left";
this.canChange = false;
}
break;
case 38:
if (this.direction !== "down") {
this.direction = "up";
this.canChange = false;
}
break;
case 39:
if (this.direction !== "left") {
this.direction = "right";
this.canChange = false;
}
break;
case 40:
if (this.direction !== "up") {
this.direction = "down";
this.canChange = false;
}
break;
case 32:
// 空格暂停与继续
if (!this.isStop) {
this.stop();
} else {
this.continue();
}
break;
}
};
}
createPos() {
let [x, y] = this.grid[(Math.random() * this.grid.length) | 0];
for (let i = 0; i < this.snake.length; i++) {
if (this.snake[i][0] == x && this.snake[i][1] == y) {
return this.createPos();
}
}
return [x, y];
}
// 生成食物
createFood() {
this.food = this.createPos();
// 更新分数
score.innerHTML = "Score: " + this.score++;
if (this.speed > 50) {
this.speed--;
}
}
// 结束
over([x, y]) {
if (x < 0 || x >= this.maxX || y < 0 || y >= this.maxY) {
return true;
}
if (this.snake.some((v) => v[0] === x && v[1] === y)) {
return true;
}
}
// 完成
completed() {
if (this.snake.length == this.maxX * this.maxY) {
return true;
}
}
// 网格线
drawGridLine() {
for (let i = 1; i < this.maxY; i++) {
this.ctx.moveTo(0, i * this.itemWidth);
this.ctx.lineTo(this.canvas.width, i * this.itemWidth);
}
for (let i = 1; i < this.maxX; i++) {
this.ctx.moveTo(i * this.itemWidth, 0);
this.ctx.lineTo(i * this.itemWidth, this.canvas.height);
}
this.ctx.lineWidth = 1;
this.ctx.strokeStyle = "#ddd";
this.ctx.stroke();
}
// 绘制
draw() {
// 清空画布
this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
this.drawGridLine();
this.ctx.fillStyle = "#000";
this.ctx.fillRect(
this.food[0] * this.itemWidth + this.j,
this.food[1] * this.itemWidth + this.j,
this.itemWidth - this.j * 2,
this.itemWidth - +this.j * 2
);
this.j ^= 1;
this.ctx.fillStyle = "green";
this.ctx.fillRect(
this.snake[0][0] * this.itemWidth + 0.5,
this.snake[0][1] * this.itemWidth + 0.5,
this.itemWidth - 1,
this.itemWidth - 1
);
this.ctx.fillStyle = "red";
for (let i = 1; i < this.snake.length; i++) {
this.ctx.fillRect(
this.snake[i][0] * this.itemWidth + 0.5,
this.snake[i][1] * this.itemWidth + 0.5,
this.itemWidth - 1,
this.itemWidth - 1
);
}
}
}
greedySnake = new GreedySnake();
</script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() { dataLayer.push(arguments); }
gtag('js', new Date());
gtag('config', 'UA-96356658-4');
</script>
</body>
</html>