贪吃蛇.代码

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
<title>秀客|ShowCoo - showcoo.net - 贪吃蛇</title> 
<style type="text/css"> 
<!-- 
body { 
    margin-left: 0px; 
    margin-top: 0px; 
} 
#global { 
    margin:auto; 
    position: relative; 
    width: 800px; 
    height:500px; 
    border: thick double #ff0000; 
    background-color:#666666 
} 
#global #p { 
    background-color: #999999; 
    position: absolute; 
    height: 500px; 
    width: 500px; 
    left:0px; 
    top:0px; 
    border-right-width: thick; 
    border-right-style: double; 
    border-right-color: #FF0000;     
} 
#global #p #k { 
    background-color: #0000FF; 
    position: absolute; 
    height: 25px; 
    width: 25px; 
    left:0px; 
    top:0px; 
} 
#global #p .xin { 
    position: absolute; 
    height: 25px; 
    width: 25px; 
} 
--> 
</style> 
<script language="javascript" type="text/javascript"> 
//获取新生元素位置 
var bucunzai=true;//记录新元素是不是移动 
var xinkx; 
var xinky; 
//页面载入后k元素定位 
function lo(){ 
var objectp=document.getElementById("k") 
objectp.style.left="250px" 
objectp.style.top="250px" 
//K元素最终样式代码 
k_zz=objectp.currentStyle?document.getElementById("k").currentStyle:document.defaultView.getComputedStyle(objectp,null); 
} 
//记录位置的数组 
var k_x=new Array() 
var k_y=new Array() 
//记录蛇的长度 
var k_number=0 
//方向键函数 
document.onkeydown=pmove; 
var fx=0//获取上一次的按键直 
function pmove(){ 
if(bucunzai){ 
xin(); 
bucunzai=false; 
} 
objectp=document.getElementById("k") 
kcode=window.event?window.event:arguments[0]; 
if(kcode.keyCode==39&&fx!=39&&fx!=37){ 
fx=kcode.keyCode; 
try{ 
clearInterval(kbottom); 
} 
catch(e){ 
} 
try{ 
clearInterval(kright); 
} 
catch(e){ 
} 
try{ 
clearInterval(ktop); 
} 
catch(e){ 
} 
try{ 
clearInterval(kleft); 
} 
catch(e){ 
} 
kleft=setInterval(function(){if(parseInt(objectp.style.left)>=475){alert("超出了");clearInterval(kleft);}else{if(k_x.length>=400){k_x.shift();k_y.shift();};k_x[k_x.length]=k_zz.left;k_y[k_y.length]

=k_zz.top;objectp.style.left=(parseInt(objectp.style.left)+25)+"px";}if(k_zz.left==xinkx&&k_zz.top==xinky){k_number++;xin();}k_kmove();},100) 
} 
if(kcode.keyCode==37&&fx!=39&&fx!=37){ 
fx=kcode.keyCode; 
try{ 
clearInterval(kbottom); 
} 
catch(e){ 
} 
try{ 
clearInterval(kright); 
} 
catch(e){ 
} 
try{ 
clearInterval(ktop); 
} 
catch(e){ 
} 
try{ 
clearInterval(kleft); 
} 
catch(e){ 
} 
kright=setInterval(function(){if(parseInt(objectp.style.left)<=0){alert("超出了");clearInterval(kright);}else{if(k_x.length>=400){k_x.shift();k_y.shift();};k_x[k_x.length]=k_zz.left;k_y[k_y.length]

=k_zz.top;objectp.style.left=(parseInt(objectp.style.left)-25)+"px";}if(k_zz.left==xinkx&&k_zz.top==xinky){k_number++;xin();}k_kmove();},100) 
} 
if(kcode.keyCode==40&&fx!=40&&fx!=38){ 
fx=kcode.keyCode; 
try{ 
clearInterval(kbottom); 
} 
catch(e){ 
} 
try{ 
clearInterval(kright); 
} 
catch(e){ 
} 
try{ 
clearInterval(ktop); 
} 
catch(e){ 
} 
try{ 
clearInterval(kleft); 
} 
catch(e){ 
} 
kbottom=setInterval(function(){if(parseInt(objectp.style.top)>=475){alert("超出了");clearInterval(kbottom);}else{if(k_x.length>=400){k_x.shift();k_y.shift();};k_x[k_x.length]=k_zz.left;k_y[k_y.length]

=k_zz.top;objectp.style.top=(parseInt(objectp.style.top)+25)+"px";}if(k_zz.left==xinkx&&k_zz.top==xinky){k_number++;xin();}k_kmove();},100) 
} 
if(kcode.keyCode==38&&fx!=40&&fx!=38){ 
fx=kcode.keyCode; 
try{ 
clearInterval(kbottom); 
} 
catch(e){ 
} 
try{ 
clearInterval(kright); 
} 
catch(e){ 
} 
try{ 
clearInterval(ktop); 
} 
catch(e){ 
} 
try{ 
clearInterval(kleft); 
} 
catch(e){ 
} 
ktop=setInterval(function(){if(parseInt(objectp.style.top)<=0){alert("超出了");clearInterval(ktop);}else{if(k_x.length>=400){k_x.shift();k_y.shift();};k_x[k_x.length]=k_zz.left;k_y[k_y.length]

=k_zz.top;objectp.style.top=(parseInt(objectp.style.top)-25)+"px";}if(k_zz.left==xinkx&&k_zz.top==xinky){k_number++;xin();}k_kmove();},100) 
} 
} 
//生成新的元素代码 
function xin(){ 
var xinkleft=parseInt(Math.random()*20)*25+"px"; 
var xinktop=parseInt(Math.random()*20)*25+"px"; 
var xinkcolor1=parseInt(Math.random()*14).toString(16) 
var xinkcolor2=parseInt(Math.random()*14).toString(16) 
var xinkcolor3=parseInt(Math.random()*14).toString(16) 
var xinkcolor4=parseInt(Math.random()*14).toString(16) 
var xinkcolor5=parseInt(Math.random()*14).toString(16) 
var xinkcolor6=parseInt(Math.random()*14).toString(16) 
var xink=document.createElement("div") 
xink.id="k"+k_number 
xink.className="xin" 
xink.style.left=xinkleft 
xink.style.top=xinktop 
xink.style.backgroundColor="#"+xinkcolor1+xinkcolor2+xinkcolor3+xinkcolor4+xinkcolor5+xinkcolor6 
var pp=document.getElementById("p") 
pp.appendChild(xink) 
xinkx=xinkleft 
xinky=xinktop 
} 
//新的元素跟随移动函数 
function k_kmove(){ 
if(k_number==0){} 
else{ 
for(var i=0;i<k_number;i++){ 
var move=document.getElementById("k"+i) 
move.style.top=k_y[k_y.length-(i+1)] 
move.style.left=k_x[k_x.length-(i+1)] 
} 
} 
} 
</script> 
</head> 
<body onload="lo();"> 
<div id="global"> 
  <div id="p" style="top:0px; left:0px;"> 
    <div id="k"></div> 
  </div> 
</div> 
</body> 
</html>