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>
/**Create initial bullet*/<br>
<br>
/**Get the direction of the bullet*/<br>
var fx=che.className;//The bullet direction can only be obtained at the initial stage of the bullet<br>
/**Get the direction of the bullet*/<br>
<br>
<br>
<br>
<br>
var set=setInterval(function(){<br>
<br>
var zd=document.getElementById(myid);//Get bullets<br>
<br>
If(fx=='qin39'){//The bullet goes to the right<br>
var ls=zd.style.marginLeft;<br>
ls=parseInt(ls);<br>
If(ls<w></w>
zd.style.marginLeft=(ls+1)+'px';<br>
}else{<br>
rezidan(myid);<br>
“
}<br>
}<br>
If(fx=='qin37'){//The bullet goes to the left<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>
“
}<br>
}<br>
If(fx=='qin38'){//Bullet upward<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>
“
}<br>
}<br>
If(fx=='qin40'){//Bullet downwards<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>
“
}<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>
/**Create a tank*/<br>
<br>
var setz=setInterval(function(){<br>
/**Being hit*/ <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='';//当子弹碰撞后删除子弹的class以防止下次继续击中敌方坦克<br>
rezidan(myid);//删除Being hit坦克<br>
clearInterval(setz); //删除&坦克的时间函数<br>
fensu();//记录分数<br>
} <br>
}<br>
}<br>
/**&*/ <br>
var clasz=ztk.className;<br>
var oArr = clasz.split(" “);<br>
var fwz;//每次执行这段代码FWZ就清空,fwz是方向判断变量<br>
<br>
<br>
if(clasz){//判断是否是&后第一次移动<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 (); // Struggle the wall and re -select the position <br>
Var clsaa = PARSEINT (our [1])+1; <br>
ztk.className='qinr '+clsaa;<br>
}else{<br>
Fwz = ran (); // Re -select the position <br>
}<br>
}<br>
<br>
<br>
If(oArr[0]=='qinl'){//Continue to move left 50 times<br>
If(parseInt(oArr[1])
lz=parseInt(lz);<br>
If(lz>0){<br>
ztk.style.marginLeft=(lz-2)+'px'; <br>
}else{<br>
Fwz = ran (); // Struggle the wall and re -select the position <br>
Var clsaa = PARSEINT (our [1])+1; <br>
ztk.className='qinl '+clsaa;<br>
}else{<br>
Fwz = ran (); // Re -select the position <br>
}<br>
}<br>
<br>
<br> if(oArr[0]=='qint'){//Continue to move up 50 times<br>
If(parseInt(oArr[1])
lz=parseInt(lz);<br>
If(lz>0){<br>
ztk.style.marginTop=(lz-2)+'px'; <br>
}else{<br>
Fwz = ran (); // Struggle the wall and re -select the position <br>
Var clsaa = PARSEINT (our [1])+1; <br>
ztk.className='qint '+clsaa;<br>
}else{<br>
Fwz = ran (); // Re -select the position <br>
}<br>
}<br>
If(oArr[0]=='qinf'){//Continue to move down 50 times<br>
If(parseInt(oArr[1])
lz=parseInt(lz);<br>
If(lz<h></h>
ztk.style.marginTop=(lz+2)+'px';<br>
}else{<br>
Fwz = ran (); // Struggle the wall and re -select the position <br>
Var clsaa = PARSEINT (our [1])+1; <br>
ztk.className='qinf '+clsaa;<br>
}else{<br>
Fwz = ran (); // Re -select the position <br> }<br>
}<br>
}else{<br>
Fwz = ran (); // Activity function <br>
}<br>
If (fwz == 2) {// Interesting value is 2, execute to the right
var lz=ztk.style.marginLeft;<br>
lz=parseInt(lz);<br>
ztk.style.marginLeft=(lz+2)+'px'; <br>
ztk.className='qinr 1';<br>
}<br>
If (fwz == 3) {// Interesting value 3 is executed to the left
var lz=ztk.style.marginLeft;<br>
lz=parseInt(lz);<br>
ztk.style.marginLeft=(lz-2)+'px';<br>
ztk.className='qinl 1';<br>
}<br>
If (fwz == 1) {// Interesting value 1 is executed upward <br>
var lz=ztk.style.marginTop;<br>
lz=parseInt(lz);<br>
ztk.style.marginTop=(lz-2)+'px';<br>
ztk.className='qint 1';<br>
}<br>
If (fwz == 4) {// Interesting value is 4, move downward <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(){//Scoring function<br>
var ob=document.getElementById('fs');<br>var s=ob.innerText;<br>
s=parseInt(s);<br>
s=s+1;<br>
Ob.innerText=s;<br>
}
rew.zip ( 33.16 KB Download: 9 times )