Repository navigation
Expand file tree
/
Copy pathsnake.js
More file actions
212 lines (204 loc) · 5.64 KB
/
Copy pathsnake.js
File metadata and controls
212 lines (204 loc) · 5.64 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
let direction = 4;
let row = 40;
// let isThroughTheWall = true;
let HeadColor = 'red';
let SnakeBodyColor = 'orange';
let foodColor = 'green';
let borderColor = 'grey';
eaten = false;
head = {
x: 3,
y: 0
};
SnakeBody = [
{ x: 2, y: 0 }, { x: 1, y: 0 }, { x: 0, y: 0 }
];
food = { x: 0, y: 0 }
document.onkeyup = function (e) {
switch (e.code) {
case "ArrowUp": {
if (direction != 2) {
direction = 1;
}
break;
}
case "ArrowDown": {
if (direction != 1) {
direction = 2;
}
break;
}
case "ArrowLeft": {
if (direction != 4) {
direction = 3;
}
break;
}
case "ArrowRight": {
if (direction != 3) {
direction = 4;
}
break;
}
case "Space": {
direction = 4;
break;
}
}
}
document.onkeyup = function (e) {
switch (e.code) {
case "ArrowUp": {
if (direction != 2) {
direction = 1;
}
break;
}
case "ArrowDown": {
if (direction != 1) {
direction = 2;
}
break;
}
case "ArrowLeft": {
if (direction != 4) {
direction = 3;
}
break;
}
case "ArrowRight": {
if (direction != 3) {
direction = 4;
}
break;
}
// case "Space": {
// direction = 5;
// break;
// }
}
}
// code捕获函数
// document.onkeydown = function (e) {
// console.log('code: ', e.code);
// }
function Start() {
Init();
//蛇头蛇身食物上色
document.getElementById(head.x + "-" + head.y).style.backgroundColor = HeadColor;
for (i of SnakeBody) {
document.getElementById(i.x + "-" + i.y).style.backgroundColor = SnakeBodyColor;
}
//随机食物位置
randomFood()
//循环开始
var loop = setInterval(function Game() {
if (isFail()) {
clearInterval(loop);
alert("Game Over")
} else {
if ((food.x === head.x && food.y === head.y)) {
eaten = true;
randomFood();
}
//蛇头加入蛇身
SnakeBody.unshift({ x: head.x, y: head.y });
//蛇身上色
document.getElementById(head.x + "-" + head.y).style.backgroundColor = SnakeBodyColor;
//移动蛇头
switch (direction) {
case 1:
head.y--;
break;
case 2:
head.y++;
break;
case 3:
head.x--;
break;
case 4:
head.x++;
break;
}
//蛇头上色
document.getElementById(head.x + "-" + head.y).style.backgroundColor = HeadColor;
//如果没吃东西去掉末尾的颜色并裁剪末尾
if (!eaten) {
document.getElementById((SnakeBody[SnakeBody.length - 1]).x + "-" + (SnakeBody[SnakeBody.length - 1]).y).style.backgroundColor = ""
SnakeBody.pop()
} else {
eaten = false;
}
}
}, 300);
}
function isFail() {
if (isSnakeBody(head.x, head.y)) {
return true;
} else if ((head.x >= row || head.x < 0) || (head.y >= row || head.y < 0)) {
return true;
} else {
return false;
}
}
function isSnakeBody(x, y) {
ret = false;
for (i of SnakeBody) {
if ((i.x === x) && (i.y === y)) {
ret = true;
}
}
return ret;
}
function moveUp() {
if (direction == 1) // Right
{
if (array[i][j] == '*') {
n++; // length++
p[n] = p[n - 1];
randomApple();
}
}
}
function Init() {
//清空网页
document.body.innerHTML = "";
//边框大div
let Container = document.createElement("div");
Container.style.border = "1px solid " + borderColor;
//每个小格子的长宽均为20px
Container.style.height = 20 * row + 1 + "px";
Container.style.width = 20 * row + 1 + "px";
Container.style.position = "relative";
//写入网页
document.body.appendChild(Container);
//循环创建小div格子并给予一个唯一的id以便后面判断以及修改颜色
for (let i = 0; i < row; i++) {
let marginTopI = 20 * i;
for (let j = 0; j < row; j++) {
let marginLeftj = 20 * j;
let Column = document.createElement("div");
//绝对定位
Column.style.position = "absolute";
//记录id(x-y)
Column.id = j + "-" + i;
//背景色白色
Column.style.backgroundColor = "white";
//长宽均为19px 边框0.5px
Column.style.width = "19px";
Column.style.height = "19px";
Column.style.marginTop = marginTopI + "px";
Column.style.marginLeft = marginLeftj + "px";
Column.style.border = "0.5px solid " + borderColor;
//append到Container div
Container.appendChild(Column);
}
}
}
function randomFood() {
do {
food.x = Math.floor(Math.random() * row)
food.y = Math.floor(Math.random() * row)
} while (isSnakeBody(food.x, food.y) || (food.x === head.x && food.y === head.y))
document.getElementById(food.x + "-" + food.y).style.backgroundColor = foodColor;
}