Rumah >hujung hadapan web >tutorial js >打豆豆小游戏 用javascript编写的[打豆豆]小游戏_javascript技巧

打豆豆小游戏 用javascript编写的[打豆豆]小游戏_javascript技巧

WBOY
WBOYasal
2016-05-16 17:44:131475semak imbas

闲来无事,花了两天时间,用javascript 写了一个打豆豆的游戏,至于怎么玩的就不细说了,到网上搜一下就知道了,很简单。

下面是源码,发布出来供大家学习交流。
由于本程序没有用到一个图片,所以界面在ie下豆豆都是方形的比较扎眼,建议用chrome浏览器运行,或者firefox
演示地址:http://www.imkoko.com/app/dadoudou.php
不多说,上代码

复制代码 代码如下:












剩余时间:120

得分:0

Power by 酷酷网



<script> <BR>/* <BR>*作者:江奇 <BR>*演示网站:酷酷网 http://www.imkoko.com/app/dadoudou.php <BR>*程序完全免费公开,转载请保留此信息,谢谢 <BR>*/ <BR>function $(o){ <BR>return document.getElementById(o); <BR>} <BR>var MoveObject={ <BR>down:function (o,fun) <BR>{ <BR>document.onselectstart=new Function("event.returnValue=false"); <BR>document.body.setAttribute('style','-moz-user-select: none;'); <BR>//e=e?e:window.event; <BR>e=arguments.callee.caller.arguments[0] || window.event; <BR>x=e.clientX; <BR>y=e.clientY; <BR>fun([x,y]); <BR>return false; <BR>} <BR>}; <BR>var o=new Array(); <BR>var it=0; <BR>var fen=0; <BR>function ini(){ <BR>var _doc = (document.compatMode != "BackCompat") ? document.documentElement : document.body; <BR>fly_left=_doc.scrollLeft+_doc.clientWidth/2-$('fly').offsetWidth/2; <BR>$('fly').style.left=fly_left+'px'; <BR>for(i=0;i<15;i++){ <BR>for(j=0;j<12;j++){ <BR>tmp=document.createElement('div'); <BR>tmp.className="b"; <BR>tmp.style.left=(j*50+i%2*25)+'px'; <BR>tmp.style.top=i*25+'px'; <BR>$('main').appendChild(tmp); <BR>} <BR>} <BR>for(i=0;i<24;i++){ <BR>o[i]=new Array(); <BR>} <BR>for(i=0;i<200;i++){ <BR>var ji=cre(); <BR>tmp=document.createElement('div'); <BR>tmp.innerHTML=t; <BR>tmp.id=ji[0]+'_'+ji[1]; <BR>tmp.className="c c"+t; <BR>tmp.style.left=(x*25)+'px'; <BR>tmp.style.top=y*25+'px'; <BR>$('main').appendChild(tmp); <BR>} <BR>} <BR>function cre(){ <BR>x=parseInt(Math.random()*24); <BR>y=parseInt(Math.random()*15); <BR>t=parseInt(Math.random()*10+1); <BR>if(o[x][y]>0){ <BR>var re=cre(); <BR>return re; <BR>}else{ <BR>o[x][y]=t; <BR>return [x,y]; <BR>} <BR>} <BR>function cc(p){ <BR>x=parseInt((p[0]-$('fly').offsetLeft)/25); <BR>y=parseInt((p[1]-$('fly').offsetTop)/25); <BR>if(o[x][y]>0){ <BR>return false; <BR>}else{ <BR>var finded=[]; <BR>for(i=y;i>=0;i--){ <BR>if(o[x][i]>0){ <BR>finded.push([o[x][i],x,i]); <BR>break; <BR>} <BR>} <BR>for(i=y;i<o[x].length;i++){ <BR>if(o[x][i]>0){ <BR>finded.push([o[x][i],x,i]); <BR>break; <BR>} <BR>} <BR>for(i=x;i>=0;i--){ <BR>if(o[i][y]>0){ <BR>finded.push([o[i][y],i,y]); <BR>break; <BR>} <BR>} <BR>for(i=x;i<o.length;i++){ <BR>if(o[i][y]>0){ <BR>finded.push([o[i][y],i,y]); <BR>break; <BR>} <BR>} <BR>find=0; <BR>po(finded,0); <BR>if(find==0){ <BR>it+=10; <BR>} <BR>fen=fen+find; <BR>$('cou').innerHTML='得分:'+fen; <BR>} <BR>} <BR>function po(obj,n){ <BR>a=obj[n]; <BR>tag=0; <BR>for(j=n+1;j<obj.length;){ <BR>if(obj[j][0]==a[0]){ <BR>o[obj[j][1]][obj[j][2]]=0; <BR>paowuxian.p($(obj[j][1]+'_'+obj[j][2])); <BR>obj.splice(j,1); <BR>tag=1; <BR>find++; <BR>}else{ <BR>j++; <BR>} <BR>} <BR>if(tag==0){ <BR>n++; <BR>}else{ <BR>o[a[1]][a[2]]=0; <BR>paowuxian.p($(a[1]+'_'+a[2])); <BR>find++; <BR>obj.splice(n,1); <BR>} <BR>if(n<obj.length-1){ <BR>po(obj,n); <BR>} <BR>} <BR>function time(){ <BR>it++; <BR>if(it<121){ <BR>$('time').innerHTML='剩余时间:'+(121-it); <BR>setTimeout('time()',1000); <BR>}else{ <BR>$('pin').style.display='block'; <BR>$('menu').style.display='block'; <BR>$('menu').innerHTML='<button onclick="start()" >开始<br><br><br>'+'恭喜你 '+fen+' 分' <BR>} <BR>} <BR>function start(){ <BR>it=0; <BR>fen=0; <BR>$('main').innerHTML=''; <BR>o=new Array(); <BR>ini(); <BR>$('pin').style.display='none'; <BR>$('menu').style.display='none'; <BR>time(); <BR>} <BR>ini(); <BR>var paowuxian={ <BR>_type:[], <BR>bot:0, <BR>o:[], <BR>ini:function(o){ <BR>for(i=0;i<this.o.length;i++){ <BR>if(this.o[i]==o){ <BR>return i; <BR>} <BR>} <BR>for(i=0;i<this.o.length;i++){ <BR>if(this._type[i].sta==-1){ <BR>break; <BR>} <BR>} <BR>o.style.position='absolute'; <BR>this.o[i]=o; <BR>this._type[i]={l:o.offsetLeft,t:o.offsetTop,s:0,nt:0,sta:1}; <BR>return i; <BR>}, <BR>p:function(o){ <BR>this.ini(o); <BR>var _doc = (document.compatMode != "BackCompat") ? document.documentElement : document.body; <BR>var sctop= document.documentElement.scrollTop||document.body.scrollTop; <BR>fly_top=sctop+_doc.clientHeight <BR>this.bot=fly_top-30; <BR>setTimeout("paowuxian.dos("+i+")",20); <BR>}, <BR>dos:function(i){ <BR>if(this._type[i].nt>this.bot){ <BR>if(this._type[i].sta==1){ <BR>document.body.removeChild(this.o[i]); <BR>this._type.sta=-1; <BR>} <BR>}else{ <BR>this._type[i].l=this._type[i].l+3; <BR>this._type[i].s++; <BR>this._type[i].nt=this._type[i].t-5*this._type[i].s+0.5*1*this._type[i].s*this._type[i].s; <BR>this.o[i].style.top=this._type[i].nt+'px'; <BR>this.o[i].style.left=this._type[i].l+'px'; <BR>setTimeout("paowuxian.dos("+i+")",20); <BR>} <BR>} <BR>}; <BR></script>



我是江奇
Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn