首頁 > php教程 > php手册 > 主體

js坦克大戰以實現砲彈擊中目標消失並且記分

WBOY
發布: 2016-09-15 11:14:47
原創
1839 人瀏覽過

js坦克大战以实现炮弹击中目标消失并且记分
话不多说
下载下面的代码包就能看到效果,
效果图:
js坦克大戰以實現砲彈擊中目標消失並且記分
js代码如下:/**初始化*/<br> var shell=document.getElementById('tk');<br> shell.innerHTML='<div></div>';<br> var w=shell.offsetWidth;h=shell.offsetHeight;<br> /**初始化*/<br> function keyDown(e) {  <br>          var keycode = e.which; <br>          remove(keycode,2);//坦克移动<br>        if(keycode=='32'){//发射子弹<br>              zidan();<br>        }<br>       }<br> <br> function keyUp(e){//键位弹起时候记录最后按下方向键,并将其储存到class中去<br>      var keycode = e.which; shell_1=document.getElementById('shell_1');<br>      <br>      if(keycode=='37' || keycode=='38' || keycode=='39' || keycode=='40'){<br>         shell_1.className='qin'+keycode;<br>      }<br>     <br> }<br>   /**键位监听*/<br>    document.onkeydown = keyDown;<br>    document.onkeyup = keyUp;<br>    /**键位监听*/<br> function remove(code,su){//玩家坦克移动函数<br>     var shell_1=document.getElementById('shell_1');<br>     var l;<br>     if(code=='37'){//左移<br>         l=shell_1.style.marginLeft;<br>         l=parseInt(l);<br>         if(l>0){<br>            shell_1.style.marginLeft=(l-su)+'px';<br>         }<br>     }<br>     <br>     if(code=='38'){//上移<br>         l=shell_1.style.marginTop;<br>         l=parseInt(l);<br>         if(l>0){<br>            shell_1.style.marginTop=(l-su)+'px';<br>         }<br>     }<br>     <br>     if(code=='39'){//右移<br>         l=shell_1.style.marginLeft;<br>         l=parseInt(l);<br>         if(l            shell_1.style.marginLeft=(l+su)+'px';<br>         }<br>     }<br> <br>     if(code=='40'){//下移<br>         l=shell_1.style.marginTop;<br>         l=parseInt(l);<br>         if(l            shell_1.style.marginTop=(l+su)+'px';<br>         }<br>     }<br>     <br> }<br> <br> function zidan(){//玩家坦克子弹函数<br>     /**获取子弹初始位置*/<br>     var che=document.getElementById('shell_1');<br>     var l=che.style.marginLeft;<br>     var t=che.style.marginTop;<br>     var html=shell.innerHTML;//获取原有html<br>     /**获取子弹初始位置*/<br>     <br>     var myDate = new Date();<br>     var myid=myDate.getTime(); <br>     <br>     /**创建初始子弹*/<br>        shell.innerHTML=html+'<div></div>';<br>     /**建立初始子彈*/<br>     <br>     /**取得子彈的運行方向*/<br>       var fx=che.className;//子彈方向只能在子彈初始的時候取得<br>     /**取得子彈的運行方向*/<br>       <br>       <br>       <br>     <br>     var set=setInterval(function(){<br>         <br>         var zd=document.getElementById(myid);//取得子彈<br>         <br>         if(fx=='qin39'){//子彈向右<br>             var ls=zd.style.marginLeft;<br>             ls=parseInt(ls);<br>              <br>             if(ls<w></w>                zd.style.marginLeft=(ls+1)+'px';<br>             }else{<br>                 rezidan(myid);<br>                 clearInterval(set);<br>             }<br>         }<br>         if(fx=='qin37'){//子彈向左<br>             var ls=zd.style.marginLeft;<br>             ls=parseInt(ls);<br>             if(ls>0){ <br>               zd.style.marginLeft=(ls-1)+'px';<br>             }else{<br>                 rezidan(myid);<br>                 clearInterval(set);<br>             }<br>         }<br>         if(fx=='qin38'){//子彈向上<br>             var ls=zd.style.marginTop;<br>             ls=parseInt(ls);<br>             if(ls>0){ <br>               zd.style.marginTop=(ls-1)+'px';<br>             }else{<br>                 rezidan(myid);<br>                 clearInterval(set);<br>             }<br>         }<br>         if(fx=='qin40'){//子彈向下<br>             var ls=zd.style.marginTop;<br>             ls=parseInt(ls);<br>             if(ls<h></h>               zd.style.marginTop=(ls+1)+'px';<br>             }else{<br>                 rezidan(myid);<br>                 clearInterval(set);<br>             }<br>         }<br>         <br>     },10);<br>     <br> }<br> <br> <br> function rezidan(id){//删除子弹函数或者坦克<br>     var child=document.getElementById(id);<br>     if(child){<br>        shell.removeChild(child);<br>     }<br> }<br> <br> <br> //**电脑坦克*/<br> <br> function ran(){//随机1到4的方位函数<br>   var kwc=Math.ceil(Math.random()*10);<br>   var kwv;<br>   if(kwc%4==0){kwv=1;}<br>   if(kwc%4==1){kwv=2;}<br>   if(kwc%4==2){kwv=3;}<br>   if(kwc%4==3){kwv=4;}<br>   return kwv;<br> }<br> <br> <br> <br> function rantanke(){//随机创建敌方坦克<br>     <br>     <br>     <br>     /**初始坦克随机出现位置*/<br>     var html=shell.innerHTML;//获取原有html<br>     var t=Math.random()*1000;<br>     var l=Math.random()*1000;<br>     <br>     if(t>h){t=(t-h)+'px';}else{t=t+'px';}//判断坦克不会超出边界太远<br>     if(l>w){l=(l-w)+'px';}else{l=l+'px';}//判断坦克不会超出边界太远<br>     /**初始坦克随机出现位置*/<br>     <br>     <br>     <br>     var myDate = new Date();<br>     var myid=myDate.getTime();<br>     /**创建坦克*/<br>        shell.innerHTML=html+'<div></div>';<br>     /**創建坦克*/<br>     <br>        var setz=setInterval(function(){<br>            /**被擊中*/  <br>            var ztk=document.getElementById(myid);dt=ztk.style.marginTop;dl=ztk.style.marginLeft;<br>            dt=parseInt(dt);dl=parseInt(dl);           <br>            var zidanob=document.getElementsByClassName('zidan');<br>             if(zidanob.length!=0){                 <br>                  for(i=0;i<zidanob.length></zidanob.length>              var zt=zidanob[i].style.marginTop;<br>              var zl=zidanob[i].style.marginLeft;<br>              zt=parseInt(zt);zl=parseInt(zl);<br>                 if(zt>dt&&ztdl&&zl                     zidanob[i].style.display='none';//隱藏子彈<br>                     zidanob[i].className='';//當子彈碰撞後消除子彈的類別以防止下次繼續擊中敵方坦克<br>                     rezidan(myid);//刪除被擊中坦克<br>                     清除間隔(setz);  //刪除&戰車的時間函數<br>                     fensu();//記錄分數<br>                 }                      <br>             }<br>             }<br>            /**&*/          <br>               var classz=ztk.className;<br>               var oArr = classz.split("""");<br>               var fwz;//每次執行易卜程式碼FWZ就清空,fwz是方向判斷變數<br>               <br>               <br>               if(classz){//判斷是否為&後第一次移動<br>              <br>                     if(oArr[0]=='qinr'){//持續向右移動50次<br>                         if(parseInt(oArr[1])                          var lz=ztk.style.marginLeft;<br>                        lz=parseInt(lz);<br>                       if(lz<w></w>                          ztk.style.marginLeft=(lz+2)+'px';<br>                        }else{<br>                            fwz=ran();//撞牆重新選定方位<br>                        }<br>                        var clsaa=parseInt(oArr[1])+1;<br>                        ztk.className='qinr '+clsaa;<br>                       }else{<br>                         fwz=ran();//重新選取方位<br>                     }<br>                     }<br>                     <br>                     <br>                     if(oArr[0]=='qinl'){//持續以左移50次<br>                         if(parseInt(oArr[1])                          var lz=ztk.style.marginLeft;<br>                        lz=parseInt(lz);<br>                        if(lz>0){<br>                           ztk.style.marginLeft=(lz-2)+'px'  ztk.style.marginLeft=(lz-2)+'px' <br>                        }else{<br>                            fwz=ran();//撞牆重新選定方位 <br>位                        }<br>                        var clsaa=parseInt(oArr[1])+1;<br>                        ztk.className='qinl '+clsaa;<br>                       }else{<br>                         fwz=ran();//重新選取方位<br>                     }<br>                     }<br>                     <br>                     <br>                    if(oArr[0]=='qint'){//持續向上移動50次<br>                         if(parseInt(oArr[1])                          var lz=ztk.style.marginTop;<br>                        lz=parseInt(lz);<br>                        if(lz>0){ <br>                          ztk.style.marginTop=(lz-2)+'px';                        }else{<br>                           fwz=ran();//撞牆重新選定方位 <br>                        }<br>                        var clsaa=parseInt(oArr[1])+1;<br>                        ztk.className='qint '+clsaa;<br>                       }else{<br>                         fwz=ran();//重新選取方位<br>                     }<br>                     }<br>                     <br>                     if(oArr[0]=='qinf'){//持續向下移動50次<br>                         if(parseInt(oArr[1])                          var lz=ztk.style.marginTop;<br>                        lz=parseInt(lz);<br>                        if(lz<h></h>                           ztk.style.marginTop=(lz+2)+'px';<br>                        }else{<br>                            fwz=ran();//撞牆重新選定方位 <br>位                        }<br>                        var clsaa=parseInt(oArr[1])+1;<br>                        ztk.className='qinf '+clsaa;<br>                       }else{<br>                         fwz=ran();//重新選取方位<br>                    }<br>                     }<br>                     <br>               }else{<br>                   fwz=ran();//方位函數<br>               }<br>               <br>               <br>               <br>               <br>               if(fwz==2){//方位值為2就執行向右移動<br>                    var lz=ztk.style.marginLeft;<br>                    lz=parseInt(lz);<br>                    ztk.style.marginLeft=(lz+2)+'px'; <br>                    ztk.className='qinr 1';<br>               }<br>               <br>               if(fwz==3){//方位值為3位執行向左移<br>                    var lz=ztk.style.marginLeft;<br>                    lz=parseInt(lz);<br>                    ztk.style.marginLeft=(lz-2)+'px';<br>                    ztk.className='qinl 1';<br>               }<br>               <br>               if(fwz==1){//方位值為1就執行向上移動<br>                    var lz=ztk.style.marginTop;<br>                    lz=parseInt(lz);<br>                    ztk.style.marginTop=(lz-2)+'px';<br>                    ztk.className='qint 1';<br>               }<br>               <br>               if(fwz==4){//方位值為4就執行向下移動<br>                    var lz=ztk.style.marginTop;<br>                    lz=parseInt(lz);<br>                    ztk.style.marginTop=(lz+2)+'px';<br>                    ztk.className='qinf 1';<br>               }<br>        },50);<br>     <br> }<br> function fensu(){//記分函數<br>     var ob=document.getElementById('fs');<br>    var s=ob.innerText;<br>     s=parseInt(s);<br>     s=s+1;<br>     ob.innerText=s;<br> }

js坦克大戰以實現砲彈擊中目標消失並且記分 rew.zip ( 33.16 KB 下載:9 次 )

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門推薦
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!