01
<script type=
"text/javascript"
>
02
var
snake={
03
st:500,
04
num:0,
05
start:
function
(){
06
div=document.createElement(
'div'
);
07
div.style.cssText=
"position:absolute;margin:0;padding:0;left:300px;top:20px;width:400px;height:400px;border:1px solid #000;"
;
08
div.id=
'kj'
;
09
div1=document.createElement(
'div'
);
10
div1.style.cssText=
"position:absolute;margin:0;padding:0;left:300px;top:430px;width:400px;height:100px;"
;
11
document.body.appendChild(div);
12
document.body.appendChild(div1);
13
div1.innerHTML=
'键盘上↑↓←→代表方向控制,小键盘上0加速,1减速,空格暂停!'
;
14
document.onkeydown=
function
(e){snake.dir(e||window.event);}
15
this
.createshe(0,200);
16
this
.createshe(20,200);
17
this
.createfood();
18
},
19
pause:
function
(){
20
clearTimeout(
this
.tt);
21
},
22
createfood:
function
(){
23
this
.x=Math.round(Math.random()*19)*20;
24
this
.y=Math.round(Math.random()*19)*20;
25
this
.p=document.getElementsByTagName(
"p"
);
26
27
while
(
this
.checkbody(
this
.x,
this
.y))
28
{
29
this
.x=Math.round(Math.random()*19)*20;
30
this
.y=Math.round(Math.random()*19)*20;
31
}
32
this
.food=document.createElement(
"span"
);
33
this
.food.style.cssText=
"position:absolute;width:20px;height:20px;background:green;border:1px solid #ccc;"
;
34
this
.food.style.left=
this
.x+
"px"
;
35
this
.food.style.top=
this
.y+
"px"
;
36
document.getElementById(
'kj'
).appendChild(
this
.food);
37
},
38
createshe:
function
(a,b){
39
var
sna=document.createElement(
"p"
);
40
sna.style.cssText=
"position:absolute;margin:0;padding:0;width:20px;height:20px;background:red;border:1px solid #ccc;"
;
41
sna.style.left=parseInt(a)+
"px"
;
42
sna.style.top=parseInt(b)+
"px"
;
43
this
.x1=parseInt(a);
44
this
.y1=parseInt(b);
45
document.getElementById(
'kj'
).appendChild(sna);
46
},
47
dir:
function
(e){
48
var
edir;
49
if
(
typeof
(
this
.tt)!=
'undefined'
){clearTimeout(
this
.tt);}
50
if
(
typeof
(direction)!=
'undefined'
)
51
{edir=Math.abs(direction-e.keyCode)==2?direction:e.keyCode;}
52
else
53
{edir=e.keyCode;}
54
switch
(edir)
55
{
56
case
37:
this
.gox = -20;
this
.goy=0; direction=37;
this
.move();
break
;
57
case
39:
this
.gox = 20;
this
.goy=0; direction=39;
this
.move();
break
;
58
case
38:
this
.gox = 0;
this
.goy=-20;direction=38;
this
.move();
break
;
59
case
40:
this
.gox = 0;
this
.goy=20; direction=40;
this
.move();
break
;
60
case
32:
this
.pause();
break
;
61
case
96:
this
.st=
this
.st-100;
this
.move();
break
;
62
case
97:
this
.st=
this
.st+100;
this
.move();
break
;
63
}
64
},
65
move:
function
(){
66
w=
this
.x1+
this
.gox;
67
h=
this
.y1+
this
.goy;
68
if
(w<0||w>=400||h<0||h>=400||
this
.checkbody(w,h))
69
{
70
clearTimeout(
this
.tt);
71
alert(
"Game over!吃了"
+
this
.num+
"个食物!"
);
72
window.location.reload();
73
}
74
if
(
this
.x1==
this
.x&&
this
.y1==
this
.y)
75
{
76
document.getElementById(
'kj'
).removeChild(
this
.food);
77
this
.num++;
78
this
.createfood();
79
}
80
else
81
{
82
document.getElementById(
'kj'
).removeChild(
this
.p[0]);
83
}
84
this
.createshe(w,h);
85
this
.tt=setTimeout(
"snake.move()"
,
this
.st);
86
},
87
checkbody:
function
(a,b){
88
this
.p=document.getElementsByTagName(
"p"
);
89
for
(
var
i=0,j=
this
.p.length;i<j;i++)
90
{
91
if
(
this
.p[i].style.left==a+
"px"
&&
this
.p[i].style.top==b+
"px"
)
return
true
;
92
}
93
}
94
}
95
window.onload=
function
(){
96
snake.start();
97
}
98
</script>