<html>
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>JS贪吃蛇</title>
  <style type="text/css">
  .box{width:25px;height:25px;border:solid 1px green;position:absolute; background:white;}
  /*红色的是食物 蓝色的是蛇*/
  .snake{width:25px;height:25px;border:solid 1px wihite;position:absolute; blue;}
  
  .food{width:25px;height:25px;border:solid 1px white;position:absolute; red;}
  </style>
 </head>
 
 

 <body>
 <input type="button" value="开始" />&nbsp;&nbsp;
 <input type="button" value="重新开始" onclick="location.reload()" />
<input type="button" value="暂停" id="zanting"/>
得分:<span id ="score"></span>
 
  <div id="wrapper">
  </div>
  <script type="text/javascript">
  var state = 0;
  var rows=20;
  var cols= 20;
  var map = new Array();
  var sx=0;var sy=0;
  var fx=0;var fy=0;
  var direc = 0;//蛇要前进的方向
  var predirec = 0;//蛇上一步的前进方向
  var snake = new Array();//装蛇的数组
  var timer;//定时器
  var speed = 500;
  var score = document.getElementById("score");
  function init(){//形成图
   for(var i=0; i<rows; i++){//生成地图数组
     map[i]=new Array();
     for(var j=0; j<cols; j++){
      map[i][j]=0;
     }
    }
    while(sx==fx && sy==fy){
     sx = Math.floor(Math.random()*rows);
     sy = Math.floor(Math.random()*cols);
     fx = Math.floor(Math.random()*rows);
     fy = Math.floor(Math.random()*cols);
    }
  }
  function draw(){//画图
   map[sx][sy] = 1;//snake
   map[fx][fy] = 2;//food
   for(var i=0;i<rows;i++){
    for(var j=0; j<cols; j++){
      var div=document.createElement('div');
      if(map[i][j]==1){
       div.className="snake";
       snake[0] = i*cols + j;
      }
      else if(map[i][j]==2){
       div.className="food";
      }
      else{
       div.className="box";
      }
      
      div.id=i*cols+j;
      div.style.top=50+25*i;
      div.style.left=50+25*j;
      document.getElementById('wrapper').appendChild(div);
     }
   }
   
  }
  window.document.onkeydown=function(ev){
   var ev= event || window.event;
   predirec = direc;
   direc = ev.keyCode;
   if(direc>40 || direc<37){
    direc = predirec;
   }
   if(predirec==37 && direc ==39){//向相反方向无效
    direc = 37;
   }
   if(predirec==38 && direc ==40){//向相反方向无效
    direc = 38;
   }
   if(predirec==39 && direc ==37){//向相反方向无效
    direc = 39;
   }
   if(predirec==40 && direc ==38){//向相反方向无效
    direc = 40;
   }
  }
  function move(){
  // var head = document.getElementById(snake[0]);//蛇头
   var prehead = snake[0];//保存上一点蛇头的位置
   var nexthead = snake[0];//下一个蛇头的位置 
   switch(direc){ 
    case 37:nexthead=snake[0]-1;break;
    case 38:nexthead=snake[0]-cols;break;
    case 39:nexthead=snake[0]+1;break;
    case 40:nexthead=snake[0]+cols;
   }
   //判断游戏是否结束
   if((nexthead < 0 || nexthead > rows*cols) || (prehead - nexthead == 1 && prehead % cols == 0) || (nexthead - prehead == 1 && nexthead % cols == 0) || (prehead != nexthead && inarr(nexthead, snake))){//游戏结束的条件
     clearInterval(timer);
     state = 3;
     alert("游戏借宿");
     return false;
    }
   if(nexthead==fx*cols+fy){//迟到食物了
    //snake[snake.length] = snake[snake.length-1];//蛇长增加了
    speed = snake.length < 4 ? 500 : 500-snake.length*5 ;//速度
    score.innerHTML =snake.length < 10 ? snake.length*20 : 200+(snake.length-10)*30;
    clearInterval(timer);
    timer=setInterval(move,speed);
    var prefx = fx;var prefy = fy;
    while(inarr(fx*cols+fy, snake) || (fx == prefx && fy == prefy)){//保证下一个生成的食物不在蛇身上,和不出现在和上一个相同的位置
      fx = Math.floor(Math.random()*rows);
      fy = Math.floor(Math.random()*cols);
     }
    document.getElementById(fx*cols+fy).className="food";
    snake.unshift(nexthead);
    for(var i=0,j=snake.length;i<j;i++){
     var tmpdiv = document.getElementById(snake[i]);
     tmpdiv.className="snake";
    }
   }
   else{
    var end = snake.pop();
    snake.unshift(nexthead);
    for(var i=0,j=snake.length;i<j;i++){
     var tmpdiv = document.getElementById(snake[i]);
     tmpdiv.className="snake";
    }
    if(end!=snake[0]){
     document.getElementById(end).className="box";
    }
   }
   
   
  }
  
   function start(){
    if(state==0){
    state = 1;
    init();
    draw();
    timer = setInterval(move,speed);
    }
    else if(state==1 || state==2 ){
    alert("处于开始或者暂停状态");
    }
    else if(state==3){
    alert("都已经输了还点呢?");
    }
   }
  var zanting = document.getElementById("zanting");
  zanting.onclick = function(){
   pause();
  };
   function pause(){
    if(state == 0){
     alert('还没开始暂停什么!');
     return false;
    }
    if(state == 2){//2是暂停状态 转入执行状态
     state = 1;
     timer=setInterval(move,speed);
     zanting.value="暂停";

    }else if(state==1){//2是暂停状态
     state = 2;
     clearInterval(timer);
     zanting.value="继续";
    }
    else if(state==3){//2是暂停状态
     state = 3;
     clearInterval(timer);
     zanting.value="你输了";
    }
   }
   function inarr(num, arr){//判断一个元素是否在数组中
    for(var i=0, j=arr.length; i<j; i++){
     if(num == arr[i]){
      return true;
     }
    }
    return false;
   }
 </script>
 </body>
</html>