-
Notifications
You must be signed in to change notification settings - Fork 0
/
indexManual.html
309 lines (304 loc) · 8.8 KB
/
indexManual.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>贪吃蛇</title>
<style type="text/css">
* {
margin: 0;
padding: 0;
font-family: "Microsoft YaHei";
}
#page {
margin-right: auto;
margin-left: auto;
margin-top: 20px;
height: 680px;
width: 980px;
}
#yard {
height: 600px;
width: 900px;
border: 1px solid gray;
box-shadow: 0 0 10px black;
float: right;
}
#mark {
font-weight: 800;
}
#mark_con {
color: red;
}
#speed {
font-weight: 800;
}
#speed_con {
color: red;
}
button {
width: 50px;
}
a {
text-decoration: none;
}
</style>
<script type="text/javascript">
//常量
const NEIBOR_NUM_MAX = 4;
const UP = 0;
const DOWN = 1;
const LEFT = 2;
const RIGHT = 3;
//伪常量
var BLOCK_SIZE = 15; //格子大小
var COLS = 60; //列数
var ROWS = 40; //行数
//变量
var snakes = []; //保存蛇坐标
var tail; //蛇尾
var c = null; //绘图对象
var toGo = 3; //行进方向
var snakecount = 1; //蛇身数量
var interval = null; //计时器
var foodX = 0; //食物X轴坐标
var foodY = 0; //食物Y轴坐标
var oMark = null; //分数显示框
var oSpeed = null; //速度显示框
var isPause = false; //是否暂停
// 绘图函数
function draw() {
c.clearRect(0, 0, BLOCK_SIZE * COLS, BLOCK_SIZE * ROWS);
//画出横线
for (var i = 1; i <= ROWS; i++) {
c.beginPath();
c.moveTo(0, i * BLOCK_SIZE);
c.lineTo(BLOCK_SIZE * COLS, i * BLOCK_SIZE);
c.strokeStyle = "gray";
c.stroke();
}
//画出竖线
for (var i = 1; i <= COLS; i++) {
c.beginPath();
c.moveTo(i * BLOCK_SIZE, 0);
c.lineTo(i * BLOCK_SIZE, BLOCK_SIZE * ROWS);
c.stroke();
}
//画出蛇
for (var i = 0; i < snakes.length; i++) {
c.beginPath();
c.fillStyle = "green";
c.fillRect(
snakes[i].x * BLOCK_SIZE,
snakes[i].y * BLOCK_SIZE,
BLOCK_SIZE,
BLOCK_SIZE
);
//绘制边框
// c.moveTo(snakes[i].x, snakes[i].y);
// c.lineTo(snakes[i].x + BLOCK_SIZE, snakes[i].y);
// c.lineTo(snakes[i].x + BLOCK_SIZE, snakes[i].y + BLOCK_SIZE);
// c.lineTo(snakes[i].x, snakes[i].y + BLOCK_SIZE);
c.closePath();
// c.strokeStyle = "white";
// c.stroke();
}
//画出食物
c.beginPath();
c.fillStyle = "yellow";
c.fillRect(
foodX * BLOCK_SIZE,
foodY * BLOCK_SIZE,
BLOCK_SIZE,
BLOCK_SIZE
);
// c.moveTo(foodX, foodY);
// c.lineTo(foodX + BLOCK_SIZE, foodY);
// c.lineTo(foodX + BLOCK_SIZE, foodY + BLOCK_SIZE);
// c.lineTo(foodX, foodY + BLOCK_SIZE);
c.closePath();
// c.strokeStyle = "red";
// c.stroke();
}
//游戏初始化
function start() {
for (var i = 0; i < snakecount; i++) {
snakes[i] = { x: i, y: 0 };
}
addFood();
draw();
oMark.innerHTML = 0; //分数初始为 0
speed = 10; //速度初始为 10
oSpeed.innerHTML = speed.toString();
}
//移动函数
function move() {
head = snakes[snakecount - 1];
switch (toGo) {
case 1: //左边
snakes.push({
x: head.x - 1,
y: head.y,
});
break;
case 2: //上边
snakes.push({
x: head.x,
y: head.y - 1,
});
break;
case 3: //右边
snakes.push({
x: head.x + 1,
y: head.y,
});
break;
case 4: //下边
snakes.push({
x: head.x,
y: head.y + 1,
});
break;
default:
}
tail = snakes.shift(); //shift() 方法用于把数组的第一个元素从其中删除,并返回第一个元素的值
isEat();
isDie();
draw();
}
//吃到食物判断
function isEat() {
if (
snakes[snakecount - 1].x == foodX &&
snakes[snakecount - 1].y == foodY
) {
oMark.innerHTML = (parseInt(oMark.innerHTML) + 1).toString(); //分数+1
addFood(); //新增食物
addSnake(); //蛇身长度+1
}
}
//添加蛇身
function addSnake() {
snakecount++;
snakes.unshift(tail); //unshift() 方法可向数组的开头添加一个或更多元素,并返回新的长度
}
//交互响应函数
function keydown(keyCode) {
switch (keyCode) {
case 37: //左边
if (toGo != 1 && toGo != 3) toGo = 1;
break;
case 38: //上边
if (toGo != 2 && toGo != 4) toGo = 2;
break;
case 39: //右边
if (toGo != 3 && toGo != 1) toGo = 3;
break;
case 40: //下的
if (toGo != 4 && toGo != 2) toGo = 4;
break;
case 80: //开始/暂停
if (isPause) {
interval = setInterval(move, 1000 / speed);
isPause = false;
document.getElementById("pause").innerHTML = "P暂停";
} else {
clearInterval(interval);
isPause = true;
document.getElementById("pause").innerHTML = "P开始";
}
break;
case 81: //速度增加
speed += 1;
oSpeed.innerHTML = speed.toString();
clearInterval(interval);
interval = setInterval(move, 1000 / speed);
break;
case 82: //速度降低
speed -= 1;
oSpeed.innerHTML = speed.toString();
clearInterval(interval);
interval = setInterval(move, 1000 / speed);
break;
}
}
//制造食物
function addFood() {
foodX = Math.floor(Math.random() * (COLS - 1));
foodY = Math.floor(Math.random() * (ROWS - 1));
// console.log(foodX + " -- " + foodY);
}
//死亡判断
function isDie() {
head = snakes[snakecount - 1];
if (head.x < 0 || head.x == COLS || head.y < 0 || head.y == ROWS) {
alert("撞墙死亡");
clearInterval(interval);
}
for (var i = 0; i < snakecount - 1; i++) {
if (head.x == snakes[i].x && head.y == snakes[i].y) {
clearInterval(interval);
alert("撞自己死亡");
}
}
}
// 启动函数
window.onload = function () {
c = document.getElementById("canvas").getContext("2d");
oMark = document.getElementById("mark_con");
oSpeed = document.getElementById("speed_con");
oSpeed.innerHTML = speed.toString();
start();
interval = setInterval(move, 1000 / speed); //每1000/speed 调用一次 move
document.onkeydown = function (event) {
var event = event || window.event;
keydown(event.keyCode);
};
};
</script>
</head>
<body>
<div id="page">
<div id="yard">
<canvas id="canvas" height="600px" width="900px"></canvas>
</div>
<div id="help">
<div id="mark">得分:<span id="mark_con"></span></div>
<div id="speed">
速度: <span id="speed_con"></span>
<table>
<tr>
<td><button onclick="keydown(81);">+</button></td>
</tr>
<tr>
<td><button onclick="keydown(82);">-</button></td>
</tr>
</table>
</div>
<div id="helper">
<table>
<tr>
<td></td>
<td><button onclick="keydown(38);">上</button></td>
<td></td>
</tr>
<tr>
<td><button onclick="keydown(37);">左</button></td>
<td><button onclick="keydown(80);" id="pause">暂停</button></td>
<td><button onclick="keydown(39);">右</button></td>
</tr>
<tr>
<td></td>
<td><button onclick="keydown(40);">下</button></td>
<td></td>
</tr>
</table>
<a href="indexManual.html">再玩一次</a>
<a href="index.html">再玩一次(AI版)</a>
</div>
</div>
</div>
<div style="text-align: center">
<p>作者: 小白菜</p>
</div>
</body>
</html>