javascript 实现的多浏览器支持的贪吃蛇webgame 时间:2020-05-12 人气:0 写的太累了 ,写了3个小时!大家可以玩玩看 界面不美,尽管批 www.qb5200.com 贪吃蛇 <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 [Ctrl+A 全选 注:如需引入外部Js需刷新才能执行] 加载全部内容