纯DHTML版的贪食蛇

<HTML>
<HEAD>
<!------------------------------------------------------
What the hell is this:
=========
A very simple "Greedy Snake" game.

Copyright:
=========
Copyright by Lucifer Xie 2001-2002.All rights reserved!
Author Email: [email protected]

Note:
=========
1.This version maybe not support IE4.0,because I don't know whether the "setInterval()" function can be used in IE4.X rightly.
So,you can replace it by "setTimeout()".
-------------------------------------------------------->
<TITLE>Pure DHTML Greedy Snake ver0.86</TITLE>
<style>
body {margin:0;}
td {height:20;width:20;font:16 Webdings;border:1 solid #99CCCC;}
#Arena {background:#99CCCC;border:1 groove black;}
</style>
</HEAD>
<BODY>
<table id=Arena cellspacing=0 cellpadding=0>
<script>
for (var i=0;i<21;i++){
document.write("<tr>");
for (var j=0;j<21;j++){
document.write("<td></td>");
}
document.write("</tr>");
}
</script>
</table>

<script>
var level=5;
var maxlevel=6
//方向
var dir="";
//用来判断蛇身是否已经完全展开
var startflag=0;
//蛇的数据数组
var bodyX=new Array(10,10,10,10,10)
var bodyY=new Array(10,10,10,10,10)
var headX,headY,lastX,lastY;
//初始长度为5
var length=5;

//显示,只修改蛇头和临近蛇头的蛇身以及蛇尾
function show(){
Arena.rows(bodyY[0]).cells(bodyX[0]).style.background="black";
Arena.rows(headY).cells(headX).style.background="green";
}

//产生一个蛇果
function spawn(){
while(1){
with(Math){
x=round(random()*20);
y=round(random()*20);
}
if (Arena.rows(y).cells(x).style.background=="")
break;
}
Arena.rows(y).cells(x).style.background="red";
}

//判断是否GameOver
function Lucifer(){
if (bodyX[0]<0||bodyX[0]>20||bodyY[0]<0||bodyY[0]>20)
return true;
if (Arena.rows(bodyY[0]).cells(bodyX[0]).style.background=="green")
return true;
return false;
}

function GameOver(){
clearInterval(gameInterval);
alert("Game Over!" + String.fromCharCode(13,13) + "Author Email:[email protected]");
}

//根据方向移动一格
function move(){
if (dir=="") return;
if (startflag<5) startflag++;
headX=bodyX[0];headY=bodyY[0];
lastX=bodyX[length-1];lastY=bodyY[length-1];
//先改变蛇头坐标
switch (dir){
case "u":
bodyY[0]--;
break;
case "d":
bodyY[0]++;
break;
case "l":
bodyX[0]--;
break;
case "r":
bodyX[0]++;
break;
}
//判断蛇头是否撞到障碍
if (Lucifer()){
GameOver();
return;
}
//如果吃到蛇果,蛇身增长,不擦除蛇尾,否则擦除蛇尾
if (Arena.rows(bodyY[0]).cells(bodyX[0]).style.background=="red"){
length++;
bodyX[length-1]=bodyX[length-2];
bodyY[length-1]=bodyY[length-2];
spawn();
}
else{
//执行了5次才开始擦除蛇尾,即蛇身已经完全伸展
if (startflag==5)
Arena.rows(lastY).cells(lastX).style.background="";
}
//改变蛇身坐标
for (i=length-1;i>1;i--){
bodyX[i]=bodyX[i-1];
bodyY[i]=bodyY[i-1];
}
bodyX[1]=headX;bodyY[1]=headY;
//显示
show();
}

function document_onkeydown(){
switch (event.keyCode){
case 37:
if (bodyX[0]==bodyX[1]) dir="l";
break;
case 38:
if (bodyY[0]==bodyY[1]) dir="u";
break;
case 39:
if (bodyX[0]==bodyX[1]) dir="r";
break;
case 40:
if (bodyY[0]==bodyY[1]) dir="d";
break;
case 32:
clearInterval(gameInterval);
}
}
document.onkeydown=document_onkeydown;

/*主程序开始*/
//在初始位置显示蛇头
Arena.rows(bodyY[0]).cells(bodyX[0]).style.background="black";
//产生第一个蛇果
spawn();
//开始移动
gameInterval=setInterval("move()",(maxlevel-level+1)*50);
</script>
</BODY>
</HTML>