记得是去年做的了,一不小心被我翻了出来,和大家分享分享。没有实现浏览器兼容,只能用IE测试哦<html>
    <head>
        <title>贪吃蛇游戏</title>
<script type="text/javascript">
            var width = 15;  //蛇运动区域宽
            var height = 15;  //蛇运动区域高
            var snakeLength = 3; //蛇的初始长度
            var timerId ;  
            var direction = "X++";  //蛇朝哪方向前进,初始化向右
            var lastMov_id = null;  //蛇前进一步时,记录蛇的尾巴上次的位置
            var score = 0;  //得分
            var randomEgg ;  //随机产生的蛋
            var bgColor = "#00FFE0";  //背景色
            alltds = document.getElementsByTagName("td");
            
            var snake = new Array();
            
            function drawBgColor(){
                for(i=0;i<alltds.length;i++){
                    alltds[i].style.backgroundColor = bgColor;
                }
            }
            function drawSnake(){
                if(snake.length == 0){  //初始化蛇的长度
                    for(i=0;i<snakeLength;i++){
                     snakeId = "0|"+i;
                     snake[i] = snakeId;
                    }
                }else{
                    getSnakeArray();
                }
                for(i=0;i<snake.length;i++){
                 changeBgColor(snake[i],"red");
                }
                if(lastMov_id != null){
                 changeBgColor(lastMov_id,bgColor);  
                }
                if(snake[snake.length-1] == randomEgg){
                 drawEgg();
                }
                msg.innerHTML = "得分:"+score;
                timerId = window.setTimeout("drawSnake()",300);
                
            }
            function getSnakeArray(){
             if(direction == ""){
                 return;
                }
                lastMov_id = snake[0];
                movIds = snake[snake.length-1].split("|");
                movIds_Y = parseInt(movIds[0]);
                movId_X = parseInt(movIds[1]);
                
                eval(direction.replace("X","movId_X").replace("Y","movIds_Y"));
                newLeaderId = movIds_Y+"|"+movId_X;
                if(isLoss(newLeaderId)){
                 alert("哈哈,输啦~~");
                 window.location.reload();
                }
                if(newLeaderId == randomEgg){     //吃到蛋,得分+1,蛇的长度+1
                 score ++;
                 snake.length ++;
                 snake[snake.length-1] = newLeaderId;
                 lastMov_id = null;
                 
                }else{  //否则蛇向前移动一步
                 for(i=0;i<snake.length;i++){
                  if(i == snake.length-1){
                   snake[i] = newLeaderId;
                   break;
                  }
                  snake[i] = snake[i+1];
                 }
                }
            }
           function isLoss(newId){  //判断是否输了 
             var curid = newId.split("|");
             cur_Y = curid[0];
             cur_X = curid[1];
             if(cur_Y < 0 || cur_X < 0){
              return true;
             }
             if(cur_Y > height-1 || cur_X > width-1){
              return true;
             }
             for(i=0;i<snake.length-1;i++){
              if(newId == snake[i])
               return true;
             }
             return false;
           }
           function drawEgg(){
           while(true){ //如果产生的蛋与蛇重合,则从新产生
            randomEgg = getRandomNum(0,height)+"|"+getRandomNum(0,width);
            if(contains(randomEgg) == false){
             break;
            }
           }
                
                changeBgColor(randomEgg,"blue");
           }
           function contains(con){
    for(i=0;i<snake.length;i++){
     if(snake[i] == randomEgg)
       return true;
    }
    return false;
   }
           function getRandomNum(min,max){
                var randomNum = parseInt(Math.random()*(max-min)+min);
                return randomNum;
           }
           function changeBgColor(obj,color){
                if(typeof(obj) == "string"){
                    obj = document.getElementById(obj);
                }
                
                try{
                    obj.style.backgroundColor = color;
                }catch(e){
                }
           }
            function keyEvent(){  
             oldDirection = direction;
             var keyNum = event.keyCode;
             if(keyNum == 87){        //w
              direction = "Y--";
             }else if(keyNum == 83){  //s
              direction = "Y++";
             }else if(keyNum == 65){  //a
              direction = "X--";
             }else if(keyNum == 68){  //d
              direction = "X++";
             }
             if(oldDirection.substr(0,1) == direction.substr(0,1) && oldDirection.substr(1,2) != direction.substr(1,2))
             {
              direction = oldDirection;      //不能向后退
             }
            }
            function startGame(){
             drawEgg();
                drawSnake();
                startBtn.disabled = true;
            }
</script>
    </head>
    <body>
<script type="text/javascript">
            document.write("<table border=solid>");
            for(i=0;i<height;i++){
                document.write("<tr>");
                for(j=0;j<width;j++){
                    document.write("<td id="+(i+"|"+j)+" height=20px width=20px>");
                    document.write("&nbsp;");
                    document.write("</td>");
                }
                document.write("</tr>");
            }
            document.write("</table>");
            drawBgColor();
            document.write("<font id=msg></font>");
            document.write("<br/><button id=\"startBtn\">开始</button>");
            startBtn.onclick = function(){
                startGame();
            };
            document.onkeydown = function(){    //监听键盘事件
             keyEvent();
            }
        
</script>
    </body>
</html>