 
                    
                
         
    
    
    
	
	
		
		
 ![]() <html xmlns="http://www.w3.org/1999/xhtml">
<html xmlns="http://www.w3.org/1999/xhtml">
![]() <head>
<head>
![]() <title>Snake</title>
<title>Snake</title>
![]() <style type="text/css">
<style type="text/css">
![]() body
body
![]() {
{
![]() background-color:White;
    background-color:White;
![]() font-size:12px;
    font-size:12px;
![]() }
}
![]() #main
#main
![]() {
{
![]() position:absolute;
    position:absolute;
![]() width:410px;
    width:410px;
![]() height:310px;
    height:310px;
![]() border-style:inset;
    border-style:inset;
![]() border-color:#0000cc;
    border-color:#0000cc;
![]() border-width:5px;
    border-width:5px;
![]() }
}
![]() #monitor
#monitor
![]() {
{
![]() position:absolute;
    position:absolute;
![]() top:330px;
    top:330px;
![]() }
}
![]() .snake
.snake
![]() {
{
![]() width:20px;
    width:20px;
![]() height:20px;
    height:20px;
![]() overflow:hidden;
    overflow:hidden;
![]() background-color:blue;
    background-color:blue;
![]() position:absolute;
    position:absolute;
![]() }
}
![]() .food
.food
![]() {
{
![]() width:20px;
    width:20px;
![]() height:20px;
    height:20px;
![]() overflow:hidden;
    overflow:hidden;
![]() background-color:red;
    background-color:red;
![]() position:absolute;
    position:absolute;
![]() }
}
![]() </style>
</style>
![]() <script type="text/jscript">
<script type="text/jscript">
![]() var AllDiv, AllSpan
var AllDiv, AllSpan
![]() var Sx, Sy; //Snake的头
var Sx, Sy; //Snake的头
![]() var Gox, Goy; //移动方向
var Gox, Goy; //移动方向
![]() var IsStart = false; //是否开始
var IsStart = false; //是否开始
![]() var Fx, Fy; //食物的地址
var Fx, Fy; //食物的地址
![]() var TimeHandle;
var TimeHandle;
![]() var IsOver = false;
var IsOver = false;
![]() var Sec = 0; //记时
var Sec = 0; //记时
![]()
![]() function page_load()
function page_load()
![]() {
{
![]() CreateSnake(0,0);
    CreateSnake(0,0);
![]() CreateSnake(0,20);
    CreateSnake(0,20);
![]() CreateSnake(20,20);
    CreateSnake(20,20);
![]() AllDiv = main.all.tags('DIV');
    AllDiv = main.all.tags('DIV');
![]() CreatFood();
    CreatFood();    
![]() AllSpan = main.all.tags('SPAN');
    AllSpan = main.all.tags('SPAN');
![]() }
}
![]()
![]() function CreateSnake(x,y)
function CreateSnake(x,y)
![]() {
{
![]() main.innerHTML += "<div class=\"snake\" style=\"top:"+y+"; left:"+x+"\"></div>";
    main.innerHTML += "<div class=\"snake\" style=\"top:"+y+"; left:"+x+"\"></div>";
![]() Sx = x;
    Sx = x;
![]() Sy = y;
    Sy = y;
![]() dSx.innerText = Sx;
    dSx.innerText = Sx;
![]() dSy.innerText = Sy;
    dSy.innerText = Sy;
![]() }
}
![]()
![]() function CreatFood()
function CreatFood()
![]() {
{
![]() if (AllSpan != null)
    if (AllSpan != null)
![]() {
    {
![]() AllSpan[0].removeNode(true);
        AllSpan[0].removeNode(true);
![]() }
    }
![]() 
    
![]() do
    do
![]() {
    {
![]() Fx=Math.round(Math.random()*19)*20;
        Fx=Math.round(Math.random()*19)*20;
![]() Fy=Math.round(Math.random()*14)*20;
        Fy=Math.round(Math.random()*14)*20;
![]() 
        
![]() dFx.innerText = Fx;
        dFx.innerText = Fx;
![]() dFy.innerText = Fy;
        dFy.innerText = Fy;
![]() }
    }
![]() while(CheckBody(Fx,Fy))
    while(CheckBody(Fx,Fy))
![]() main.innerHTML += "<span class=\"food\" style=\"top:"+Fy+"; left:"+Fx+"\"></span>";
    main.innerHTML += "<span class=\"food\" style=\"top:"+Fy+"; left:"+Fx+"\"></span>";
![]() }
}
![]()
![]() function Move()
function Move()
![]() {
{
![]() var NSx = Sx+Gox;
    var NSx = Sx+Gox;
![]() var NSy = Sy+Goy; //下个格子
    var NSy = Sy+Goy; //下个格子
![]() 
    
![]() //是否GameOver
    //是否GameOver
![]() IsOver = IsGameOver(NSx,NSy);
    IsOver = IsGameOver(NSx,NSy);
![]() 
    
![]() if (IsOver)
    if (IsOver)
![]() {
    {       
![]() return;
        return;
![]() }
    }
![]() 
    
![]() if (NSx == Fx && NSy == Fy)
    if (NSx == Fx && NSy == Fy)
![]() {
    {
![]() CreatFood();
        CreatFood();
![]() }else{
    }else{
![]() AllDiv[0].removeNode(true); //移动
        AllDiv[0].removeNode(true); //移动
![]() }
    }
![]() 
     
![]() CreateSnake(NSx,NSy);
    CreateSnake(NSx,NSy);
![]() }
}
![]()
![]() function IsGameOver(x,y)
function IsGameOver(x,y)
![]() {
{
![]() if (x < 0 || x >= 400 || y >= 300 || y < 0)
    if (x < 0 || x >= 400 || y >= 300 || y < 0)
![]() {
    {
![]() return true;
        return true;
![]() }
    }
![]() 
    
![]() return CheckBody(x,y)
    return CheckBody(x,y)
![]() 
   
![]() }
}
![]()
![]() //键盘控制
//键盘控制
![]() function KeyDown(){
function KeyDown(){
![]() if (IsOver) return;
    if (IsOver) return;
![]() Key=event.keyCode
    Key=event.keyCode
![]() switch(Key){
    switch(Key){
![]() case 37:Dir(-1,0);break//左
    case 37:Dir(-1,0);break//左
![]() case 39:Dir(1,0);break//右
    case 39:Dir(1,0);break//右
![]() case 38:Dir(0,-1);break//上
    case 38:Dir(0,-1);break//上
![]() case 40:Dir(0,1);break}//下
    case 40:Dir(0,1);break}//下
![]() return false
    return false
![]() }
}
![]()
![]() function Dir(x,y)
function Dir(x,y)
![]() {
{
![]() if (Gox == -x*20 && Goy == -y*20) return
    if (Gox == -x*20 && Goy == -y*20) return
![]() 
    
![]() Gox = x*20;
    Gox = x*20;
![]() Goy = y*20;
    Goy = y*20;    
![]() 
    
![]() if (IsStart == false)
    if (IsStart == false)
![]() {
    {
![]() IsStart = true;
        IsStart = true;
![]() PlayGame();
        PlayGame();
![]() }
    }
![]() dGox.innerText = Gox;
    dGox.innerText = Gox;
![]() dGoy.innerText = Goy;
    dGoy.innerText = Goy;
![]() }
}
![]()
![]() //开始游戏,并不停移动
//开始游戏,并不停移动
![]() function PlayGame()
function PlayGame()
![]() {
{
![]() Sec++;
    Sec++;
![]() if (IsOver)
    if (IsOver)
![]() {
    {
![]() clearTimeout(TimeHandle);
        clearTimeout(TimeHandle);
![]() }else{
    }else{    
![]() Move();
        Move();
![]() TimeHandle = setTimeout('PlayGame()',50)
        TimeHandle = setTimeout('PlayGame()',50)
![]() }
    }
![]() var d = new Date(0,0,0,0,0,0,0);
    var d = new Date(0,0,0,0,0,0,0);
![]() d.setTime(d.getTime()+Sec*50);
    d.setTime(d.getTime()+Sec*50);
![]() 
    
![]() dSec.innerText = Sec;
    dSec.innerText = Sec;
![]() dTime.innerText = d.toLocaleTimeString();
    dTime.innerText = d.toLocaleTimeString();
![]() }
}
![]()
![]() //检测食物
//检测食物
![]() function CheckBody(x,y)
function CheckBody(x,y)
![]() {
{
![]() for (var i = 0; i< AllDiv.length;i++)
    for (var i = 0; i< AllDiv.length;i++)
![]() {
    {
![]() if (AllDiv[i].style.left == x+"px" && AllDiv[i].style.top == y+"px")
        if (AllDiv[i].style.left == x+"px" && AllDiv[i].style.top == y+"px")
![]() {
        {
![]() return true;
            return true;
![]() }
        }
![]() }
    }
![]() return false;
    return false;
![]() }
}
![]()
![]() </script>
</script>
![]() </head>
</head>
![]() <body onload="page_load()" onkeydown="KeyDown()">
<body onload="page_load()" onkeydown="KeyDown()">
![]() <div id="main">
<div id="main">
![]() </div>
</div>
![]() <div id="monitor">
<div id="monitor">
![]() <input type="button" value="test" onclick="clearTimeout(TimeHandle)" />
    <input type="button" value="test" onclick="clearTimeout(TimeHandle)" />
![]() 
    
![]() <fieldset>
    <fieldset>
![]() <legend>参数</legend>
    <legend>参数</legend>
![]() <ul>
    <ul>
![]() <li>Sx:<span id="dSx"></span></li>
        <li>Sx:<span id="dSx"></span></li>
![]() <li>Sy:<span id="dSy"></span></li>
        <li>Sy:<span id="dSy"></span></li>
![]() <li>Fx:<span id="dFx"></span></li>
        <li>Fx:<span id="dFx"></span></li>
![]() <li>Fy:<span id="dFy"></span></li>
        <li>Fy:<span id="dFy"></span></li>
![]() <li>Gox:<span id="dGox"></span></li>
        <li>Gox:<span id="dGox"></span></li>
![]() <li>Goy:<span id="dGoy"></span></li>
        <li>Goy:<span id="dGoy"></span></li>
![]() <li>Sec:<span id="dSec"></span></li>
        <li>Sec:<span id="dSec"></span></li>
![]() <li>Time:<span id="dTime"></span></li>
        <li>Time:<span id="dTime"></span></li>
![]() </ul>
    </ul>    
![]() </fieldset>
    </fieldset>
![]() </div>
</div>
![]() </body>
</body>
![]() </html>
</html>