-
Notifications
You must be signed in to change notification settings - Fork 0
/
贪吃蛇.html
296 lines (279 loc) · 6.52 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>js小游戏之贪吃蛇</title>
<style>
div
{
margin:0 auto;
text-align:center;
}
#mcanvas
{
border:2px solid #0F6;
display:block;
margin:0 auto;
}
</style>
</head>
<body>
<div id="instruction">
<p style="font-size:18px;color:#03C;">分别由键盘的上下左右来控制蛇的移动方向</p>
</div>
<div id="main">
</div>
<script type="text/javascript">
var canvas = document.createElement('canvas');
canvas.id = "mcanvas";
canvas.width='800';
canvas.height='600';
document.body.appendChild(canvas);
var ctx=canvas.getContext('2d');
var foods
var snakes//放食物和蛇的数组
//初始化
function foodinit(x,y) //x,y代表坐标
{
var object=new Object();
object.x=x;
object.y=y;
ctx.fillStyle='red';
ctx.fillRect(x,y,10,10);
ctx.strokeStyle='#000000';
ctx.strokeRect(x,y,10,10);
return object;
}
//蛇身
function snakeinit(x,y)
{
var object=new Object();
object.x=x;
object.y=y;
ctx.fillStyle='blue';
ctx.fillRect(x,y,10,10);
ctx.strokeStyle='#000000';
ctx.strokeRect(x,y,10,10);
return object;
}
//蛇头
function snakeinith(x,y)
{
var object=new Object();
object.x=x;
object.y=y;
ctx.fillStyle='red';
ctx.fillRect(x,y,10,10);
ctx.strokeStyle='#000000';
ctx.strokeRect(x,y,10,10);
return object;
}
//画蛇
function sinit(x,y,len)
{
x=parseInt(x);
y=parseInt(y);
var snake=snakeinith(x,y);
x=x-10;
for(var i=1;i<len;i++)
{
var snake=snakeinit(x,y);
snakes.push(snake);
x=x-10;
}
}
function makesnake(x,y,length,speed)
{
this.x=x;
this.y=y;
this.len=length;
this.dir='R';
this.snakex=x;
this.snakey=y;
var head; //蛇头
var last;//蛇尾
var odir;
sinit(snakex,snakey,len);
document.onkeydown=function movedir(e)
{
odir=dir;
var code=0;
var e=e||event;
code=e.keyCode||e.which||e.charCode;
switch(code)
{
case 37:
dir="L";
break;
case 38:
dir="U";
break;
case 39:
dir="R";
break;
case 40:
dir="D";
break;
}
}
//移动蛇
function m()
{
if(snakes[0].x+10>canvas.width+5||snakes[0].x<0||snakes[0].y<0||snakes[0].y+10>canvas.height+5)
{
over();
return;
//游戏结束
}
else
{
if(dir=='R')
{
head=snakeinith(snakes[0].x+10,snakes[0].y);
snakeinit(snakes[0].x,snakes[0].y);
}
else if(dir=='L')
{
head=snakeinith(snakes[0].x-10,snakes[0].y);
snakeinit(snakes[0].x,snakes[0].y);
}
else if(dir=='D')
{
head=snakeinith(snakes[0].x,snakes[0].y+10);
snakeinit(snakes[0].x,snakes[0].y);
}
else if(dir=='U')
{
head=snakeinith(snakes[0].x,snakes[0].y-10);
snakeinit(snakes[0].x,snakes[0].y);
}
if(snakes[1].x==head.x&&snakes[1].y==head.y)
{
dir=odir;
if(dir=='R')
{
head=snakeinith(snakes[0].x+10,snakes[0].y);
}
else if(dir=='L')
{
head=snakeinith(snakes[0].x-10,snakes[0].y);
}
else if(dir=='D')
{
head=snakeinith(snakes[0].x,snakes[0].y+10);
}
else if(dir=='U')
{
head=snakeinith(snakes[0].x,snakes[0].y-10);
}
}
snakes.unshift(head);
last=snakes[len];
clear(last.x,last.y);
snakes.pop();
}
//判断食物是否和蛇头相撞
for (var i = 0;i<foods.length; i++)
{
if(foods[i].x == snakes[0].x&&foods[i].y == snakes[0].y)
{
foods.pop();
makefood();
var tail=snakeinit(last.x,last.y);
snakes.push(tail);
len++;
clearInterval(snake_interval);
if(speed>20)
{
speed=speed-8;
snake_interval=setInterval(m,speed);
}
//
}
}
for(var i=2;i<snakes.length;i++)
{
if(snakes[0].x==snakes[i].x&&snakes[0].y==snakes[i].y)
{
alert("撞到自己了!");
}
}
}
snake_interval=setInterval(m,speed);
}
//生成地图
function map()
{
ctx.fillStyle='#000000';
ctx.fillRect(0,0,canvas.width,canvas.height);
}
//生成食物
function makefood()
{
var x=parseInt(canvas.width/10*Math.random())*10;
var y=parseInt(canvas.height/10*Math.random())*10;
var food=new foodinit(x,y);
foods.push(food);
}
//清除
function clear(x,y)
{
ctx.fillStyle='#000000';
ctx.strokeStyle='#000000';
ctx.strokeRect(x,y,10,10);
ctx.fillRect(x,y,10,10);
}
function star()
{
foods=new Array();
snakes=new Array();
map();
document.getElementById("star").setAttribute('style',"display:none;");
document.getElementById("restar").setAttribute('style',"display:block;");
document.onkeydown=function movedir(e)
{
var code=0;
var e=e||event;
code=e.keyCode||e.which||e.charCode;
if(code!=0)
{
makefood ();
var x=(40/canvas.width)*canvas.width;
makesnake(x,x,3,100);
}
}
}
function over()
{
clearInterval(snake_interval);
alert("游戏结束!");
}
function restar()
{
clearInterval(snake_interval);
foods=new Array();
snakes=new Array();
map();
//document.getElementById("star").setAttribute('style',"display:none;");
document.onkeydown=function movedir(e)
{
var code=0;
var e=e||event;
code=e.keyCode||e.which||e.charCode;
if(code!=0)
{
makefood ();
var x=(40/canvas.width)*canvas.width;
makesnake(x,x,3,100);
}
}
}
</script>
<div id="star" style="display:block;">
<button type="button" onClick="star();">开始游戏</button>
</div>
<div id="restar" style="display:none;">
<button type="button" onClick="restar();">重新开始</button>
</div>
</body>
</html>