日期:2014-05-16  浏览次数:20370 次

简易版js贪吃蛇代码(但技术不简单哦)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Snake!</title>
<style>
  body { background-color:dimgray;  margin:0; padding:0; font-family:Verdana; font-size:12px; font-weight:bold; color:white;}
  table#map { width:auto; height:auto; margin:0 auto; border-collapse:collapse; border-spacing:0; background-color:#EAEAEA; clear:both;}
  td { width:10px; height:10px; border:1px solid black;}
  .shead { background-color: orangered;}
  .sbody { background-color: black;}
  .sfood { background-color: orangered;}
  .info  { width:400px; margin:0 auto; padding:3em 0;}
  .info li{ float:left; height:30px; margin-right:2em; line-height:30px;}
  .query{background-color:white;
color:black;
font-size:15;
width:700;
height:28;
margin:0 0 0 20;
   }
  .query:focus{background-color:white;
color:black;
font-size:15;
width:700;
height:28;
margin:0 0 0 20;
display: inline-block;
border: 2 solid #B6B6B6;
border-color: gainsboro gainsboro gainsboro gainsboro;
vertical-align: top;
transition:border linear .2s,box-shadow linear .5s;
-moz-transition:border linear .2s,-moz-box-shadow linear .5s;
-webkit-transition:border linear .2s,-webkit-box-shadow linear .5s;
outline:none;
border-color: deepskyblue;
box-shadow:0 0 1em deepskyblue;
-moz-box-shadow:0 0 1em deepskyblue;
-webkit-box-shadow:0 0 1em deepskyblue;
   }
   a{color:white;}
</style>
<script type="text/javascript" language="javascript">
function Snake(){
        this.rows = 20;
        this.cols = 40;
        this.speed = 200;
        this.currKey = 0;//当前方向
        this.timer = 0;
        this.sid = "snakeObj"+parseInt(Math.random()*100000);
        eval(this.sid+"=this");
        this.pos = [];//蛇身
        this.foodPos = {"x":-1,"y":-1};
        this.foodNum = 0;
        this.domTab = document.getElementById("map");//地图
        this.pause = 1;//1,-1
}
Snake.prototype.init = function(){
        this.map();
        arguments[0] ? this.speed=arguments[0] : false;//选择速度
        this.pos = [{"x":2,"y":5},{"x":1,"y":5},{"x":0,"y":5}];
        for( j in this.pos ){//画全身,move只画头尾