-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnake.js
More file actions
258 lines (245 loc) · 7.68 KB
/
snake.js
File metadata and controls
258 lines (245 loc) · 7.68 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
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
//=======author:"fsh" qq:"741691336" weixin:"忘我之鱼"=======//
//=======忙里偷闲的产物,界面略丑;)==========================//
//=======原生js面向对象,欢迎试玩^_^=========================//
//游戏主体
new function() {
let cvs = $("cvs"),
ctx = cvs.getContext("2d"),
width = cvs.width,
height = cvs.height,
m_great = $("m_great"),
m_over = $("m_over"),
level = 1,
score = 0,
scorePerLevel = 50,
speed = 1000 / 2,
timer = null,
scoreDiv = $("score"),
controlDiv = $("control"),
over = false,
snake = new Snake(width, height),
snakeNodes = snake.nodes,
rat = new Rat(),
paused = false;
//开启主循环
loop(true);
function loop(firstTime) { //参数,首次进主循环
timer = setTimeout(loop, speed);
//按键监听
window.onkeydown = keydown;
if (paused) {
return;
}
//画背景
drawBg();
//移蛇
snake.move();
//游戏结束判定
if (snake.eatMyself() || checkBoundaryCollision()) {
//播音乐
m_over.play();
//设标置
over = true;
scoreDiv.innerHTML = "第<strong>" +
level +
"</strong>关 分数: <strong>" +
score +
"</strong> <b>游戏结束</b>";
clearTimeout(timer);
}
//吃鼠检测
if (checkSnakeCollision()) {
//播音乐
m_great.play();
//设标置
rat.eaten = true;
//蛇增长
let newTailVx = snake.tail.vx;
let newTailVy = snake.tail.vy;
let newTailX = snake.tail.x - newTailVx * snake.size;
let newTailY = snake.tail.y - newTailVy * snake.size;
snake.tail = new SnakeNode(newTailX, newTailY, snake.size);
snake.tail.vx = newTailVx;
snake.tail.vy = newTailVy;
snakeNodes.push(snake.tail);
//计分
score += 10;
level = (score / scorePerLevel >> 0) + 1;
//加速
speed -= level;
//更新dom
scoreDiv.innerHTML = "第<strong>" +
level +
"</strong>关 分数: <strong>" +
score +
"</strong>";
}
//画蛇
for (let i = snakeNodes.length; i--;) {
draw(snake.nodes[i]);
}
//首次循环或鼠被吃掉就移鼠
if (firstTime || rat.eaten) {
rndMove(rat);
rat.eaten = false;
}
//画鼠
draw(rat);
};
//绘制游戏区
function drawBg() {
ctx.fillStyle = "#fbda30";
ctx.fillRect(0, 0, width, height);
ctx.fill();
}
function draw(obj) {
ctx.fillStyle = obj.color;
ctx.fillRect(obj.x, obj.y, obj.size, obj.size);
ctx.fill();
ctx.strokeStyle = "#fff";
ctx.strokeRect(obj.x, obj.y, obj.size, obj.size);
}
function checkSnakeCollision(obj = rat) { //物体与蛇相撞检测(默认检测鼠)
for (let i = snakeNodes.length; i--;) {
const { x: sx, y: sy } = snakeNodes[i];
const { x, y } = obj;
if (sx == x &&
sy == y) {
return true;
}
}
return false;
}
function checkBoundaryCollision() { //撞墙检测
return snake.head.x < 0 ||
snake.head.x >= width ||
snake.head.y < 0 ||
snake.head.y >= height;
}
function rndMove(obj) { //随机移块
let x, y;
do {
x = Math.floor(width * Math.random() / obj.size) * obj.size;
y = Math.floor(height * Math.random() / obj.size) * obj.size;
}
while (x == obj.x && y == obj.y ||
checkSnakeCollision({ x, y }));
obj.x = x, obj.y = y;
}
function keydown(event) {
window.onkeydown = null;
switch (event.keyCode) {
case 37: //←
case 65: //a
if (snake.head.vx != 1) {
snake.head.vx = -1;
snake.head.vy = 0;
}
break;
case 38: //↑
case 87: //w
if (snake.head.vy != 1) {
snake.head.vx = 0;
snake.head.vy = -1;
}
break;
case 39: //→
case 68: //d
if (snake.head.vx != -1) {
snake.head.vx = 1;
snake.head.vy = 0;
}
break;
case 40: //↓
case 83: //s
if (snake.head.vy != -1) {
snake.head.vx = 0;
snake.head.vy = 1;
}
break;
case 13: //enter
case 32: //space
if (over) { //游戏结束就重启
level = 1;
score = 0;
speed = 1000 / 2;
scoreDiv.innerHTML = "第<strong>" +
level +
"</strong>关 分数: <strong>" +
score +
"</strong>";
snake.init();
over = false;
loop(true);
} else { //游戏暂停就继续,游戏进行中就暂停
paused = !paused;
}
break;
}
};
};
//块类
function Box(x, y, size) {
this.x = x || 0;
this.y = y || 0;
this.size = size || 10;
this.color = "green";
};
//蛇的节点类
function SnakeNode(x, y, size) {
Box.call(this, x, y, size);
//构造方法
this.vx = 1; //横向速度
this.vy = 0; //纵向速度
};
//蛇类
function Snake(width, height) {
let snakeNodes = this.nodes = []; //节点数组,从蛇头到蛇尾
let initLength = 3;
let size = this.size = 10; //节点大小
this.head = null;
this.tail = null;
this.init = function() {
snakeNodes.length = 0;
for (let i = initLength; i--;) {
let node = new SnakeNode(width / 2 + (i - 1) * size, height / 2, size);
snakeNodes.push(node);
}
this.head = snakeNodes[0];
this.tail = snakeNodes[initLength - 1];
this.head.color = "#eb281d";
}
this.move = function() {
for (let i = snakeNodes.length; i--;) {
let node = snakeNodes[i];
// 移蛇
node.x += (size * node.vx);
node.y += (size * node.vy);
// 速度传递
if (i) {
node.vx = snakeNodes[i - 1].vx;
node.vy = snakeNodes[i - 1].vy;
}
}
};
this.eatMyself = function() {
if (snakeNodes.length < 5) return false;
for (let i = snakeNodes.length; i-- - 1;) {
if (this.head.x == snakeNodes[i].x && this.head.y == snakeNodes[i].y) {
return true;
}
}
return false;
};
this.init();
};
//鼠类
function Rat(x, y, size) {
Box.call(this, x, y, size);
this.color = "#476b8b";
this.eaten = false; //判断是否被吃了
};
//工具方法
function $(id) {
return document.getElementById(id);
}