首頁 >web前端 >js教程 >打豆豆小遊戲 用javascript寫的[打豆豆]小遊戲_javascript技巧

打豆豆小遊戲 用javascript寫的[打豆豆]小遊戲_javascript技巧

WBOY
WBOY原創
2016-05-16 17:44:131477瀏覽

閒來無事,花了兩天時間,用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; <🎜at.body; <🎜.body; >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 ; <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>
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn