HTML编写地贪吃蛇代码.docx
《HTML编写地贪吃蛇代码.docx》由会员分享,可在线阅读,更多相关《HTML编写地贪吃蛇代码.docx(20页珍藏版)》请在冰豆网上搜索。
HTML编写地贪吃蛇代码
使用表格
varfangxiang;
fangxiang=4;
varsk=newArray(18);
sk[0]="5,5";
sk[1]="5,4";
sk[2]="5,3";
varlong=3;
varzhanshichunweiba;
varshiwu="15,15";
varshudu=250;
varmovedong=true;
varshifouchongzhi=true;
functionkeydown(){
if(event.keyCode==38){
if(fangxiang!
=2)fangxiang=1;
if(fangxiang==2)fangxiang=2;
}
if(event.keyCode==40){
if(fangxiang!
=1)fangxiang=2;
if(fangxiang==1)fangxiang=1;
}
if(event.keyCode==37){
if(fangxiang!
=4)fangxiang=3;
if(fangxiang==4)fangxiang=4;
}
if(event.keyCode==39){
if(fangxiang!
=3)fangxiang=4;
if(fangxiang==3)fangxiang=3;
}
}
functionkaishi(){
if(shifouchongzhi){
shifouchongzhi=false;
document.getElementById(sk[0]).style.backgroundColor="red";
document.getElementById(sk[1]).style.backgroundColor="red";
document.getElementById(sk[2]).style.backgroundColor="red";
document.getElementById(shiwu).style.backgroundColor="blue";
move();
}
else
alert("你要先重置");
}
functionchongzhi(){
shifouchongzhi=true;
shiwu="15,15";
shudu=250;
fangxiang=4;
sk[0]="5,5";
sk[1]="5,4";
sk[2]="5,3";
long=3;
varm=document.getElementsByTagName("td");
for(varn=0;n<400;n++){
m[n].style.backgroundColor="yellow";
}
movedong=true;
}
functiondong(){
zhanshichunweiba=sk[long-1];
for(varm=long-1;m>0;m--){
sk[m]=sk[m-1]
}
vari=sk[0].split(',');
if(fangxiang==1){i[0]--;sk[0]=i[0]+','+i[1]}
if(fangxiang==2){i[0]++;sk[0]=i[0]+','+i[1]}
if(fangxiang==3){i[1]--;sk[0]=i[0]+','+i[1]}
if(fangxiang==4){i[1]++;sk[0]=i[0]+','+i[1]}
}
functionzhenchanshengshiwu(){
varx;
vary;
x=Math.round(Math.random()*19);
y=Math.round(Math.random()*19);
shiwu=x+','+y;
chanshengshiwu();
}
functionchanshengshiwu(){
for(varm=0;mif(shiwu==sk[m]){
zhenchanshengshiwu();
}
document.getElementById(shiwu).style.backgroundColor="blue";
}
functionchishiwu(){
if(shiwu==sk[0]){
long++;
sk[long-1]=zhanshichunweiba;
document.getElementById(sk[long-1]).style.backgroundColor="red";
chanshengshiwu();
}
}
functionjieshujiashu(){
vari=sk[0].split(',');
if(i[0]<0||i[0]>19||i[1]<0||i[1]>19){
alert("你死求");
movedong=false;
}
if(long>=6){
shudu=200;
}
if(long>=9){
shudu=150;
}
if(long>=12){
shudu=100;
}
if(long>=15){
shudu=80;
}
if(long>=18){
shudu=50;
}
if(long>=25){
alert("你真NB你赢了");
chongzhi();
movedong=false;
}
}
functionmove(){
dong();
jieshujiashu();
for(varm=0;mdocument.getElementById(sk[m]).style.backgroundColor="red";
}
document.getElementById(zhanshichunweiba).style.backgroundColor="yellow";
chishiwu();
if(movedong)
setTimeout("move()",shudu);
}
|
|
|
|
|
展开阅读全文
相关搜索
|