韩顺平 java 坦克大战_HTML5坦克大战(韩顺平版本)

2022年05月13日 阅读数:9
这篇文章主要向大家介绍韩顺平 java 坦克大战_HTML5坦克大战(韩顺平版本),主要内容包括基础应用、实用技巧、原理机制等方面,希望对大家有所帮助。

1 //定义敌人和咱们本身的坦克的颜色dom

2 var enemyColor = new Array("#0BB","#0FF");3 var heroColor = new Array("#dc0","#ff5");4 //封装一个公用的坦克父类this

5 functionTank(x,y,direct){6 this.x =x;7 this.y =y;8 this.speed = 3;9 this.direct =direct;10 this.moveUp = function(){11 if (hero.y>0) {12 hero.y -=hero.speed;13 }14 hero.direct = 0;15 }16 this.moveRight = function(){17 if (hero.x+30<500) {18 hero.x +=hero.speed;19 }20 hero.direct = 1;21 }22 this.moveBottom = function(){23 if (hero.y+30<300) {24 hero.y +=hero.speed;25 }26 hero.direct = 2;27 }28 this.moveLeft = function(){29 if (hero.x>0) {30 hero.x -=hero.speed;31 }32 hero.direct = 3;33 }34 }35get

36 //英雄坦克类it

37 functionHero(x,y,direct,color){38 //将坦克类的构造方法赋给heroio

39 this.hero =Tank;40 //调用,拥有坦克类的全部的属性和方法function

41 this.hero(x,y,direct);42 this.color =color;43 this.direct =direct;44 this.isLive = true;45 this.shotEnemy = function(){46 switch(this.direct){47 case 0:48 heroBullet = new Bullet(this.x+9,this.y,this.direct);49 break;50 case 1:51 heroBullet = new Bullet(this.x+30,this.y+9,this.direct);52 break;53 case 2:54 heroBullet = new Bullet(this.x+9,this.y+30,this.direct);55 break;56 case 3:57 heroBullet = new Bullet(this.x,this.y+9,this.direct);58 break;59 }60 heroBullets.push(heroBullet);61 heroBullets[heroBullets.length-1].timer = window.setInterval("heroBullets["+(heroBullets.length-1)+"].run()",50);62 }63 }64 //敌人的坦克方法

65 functionEnemyTank(x,y,direct,color){66 //将坦克类的构造方法赋给敌人坦克im

67 this.enemyTank =Tank;68 //调用,拥有坦克类的全部的属性和方法d3

69 this.enemyTank(x,y,direct);70 this.color =color;71 this.isLive = true;72 this.timer = null;73 this.speed = 1;74 this.count = 0;75 this.direct =direct;76 this.bulletIsLive = true;77 this.run = function(){78 switch(this.direct){79 case 0:80 if(this.y>0){81 this.y--;82 }83 break;84 case 1:85 if(this.x+30<500){86 this.x += this.speed;87 }88 break;89 case 2:90 if(this.y+30<300){91 this.y += this.speed;92 }93 break;94 case 3:95 if(this.x>0){96 this.x -= this.speed;97 }98 break;99 }100bom

101 if(this.count>=30){102 this.direct = Math.round(Math.random()*3);103 this.count=0;104 }105 this.count++;106 //在坦克走的过程当中,判断一下,这个坦克的子弹是否活着

107 if(this.bulletIsLive == false && this.isLive){108 //子弹over,加子弹

109 switch(this.direct){110 case 0:111 enemyBullets.push(new Bullet(this.x+9,this.y,this.direct,this,'enemy'));112 break;113 case 1:114 enemyBullets.push(new Bullet(this.x+30,this.y+9,this.direct,this,'enemy'));115 break;116 case 2:117 enemyBullets.push(new Bullet(this.x+9,this.y+30,this.direct,this,'enemy'));118 break;119 case 3:120 enemyBullets.push(new Bullet(this.x,this.y+9,this.direct,this,'enemy'));121 break;122 }123 enemyBullets[enemyBullets.length-1].timer = window.setInterval("enemyBullets["+(enemyBullets.length-1)+"].run()",50);124 this.bulletIsLive = true;125 }126 }127 }128 //绘制坦克

129 functiondrawTank(hero){130 switch(hero.direct){131 case 0:132 case 2:133 ctx.fillStyle = hero.color[0];134 ctx.fillRect(hero.x,hero.y,5,30);135 ctx.fillRect(hero.x+15,hero.y,5,30);136 ctx.fillRect(hero.x+6,hero.y+5,8,20);137 ctx.fillStyle = hero.color[1];138 ctx.beginPath();139 ctx.arc(hero.x+10,hero.y+15,3,0,Math.PI*2,true);140 ctx.closePath();141 ctx.fill();142 //画出炮筒(直线)

143 ctx.strokeStyle = hero.color[1];144 ctx.lineWidth = 2;145 ctx.moveTo(hero.x+10,hero.y+15);146 if(hero.direct==0){147 ctx.lineTo(hero.x+10,hero.y);148 }else if(hero.direct==2){149 ctx.lineTo(hero.x+10,hero.y+30);150 }151 ctx.stroke();152 break;153 case 1:154 case 3:155 ctx.fillStyle = hero.color[0];156 ctx.fillRect(hero.x,hero.y,30,5);157 ctx.fillRect(hero.x,hero.y+15,30,5);158 ctx.fillRect(hero.x+5,hero.y+6,20,8);159 //须要注意,画圆的时候须要从新开启路径

160 ctx.fillStyle = hero.color[1];161 ctx.beginPath();162 ctx.arc(hero.x+15,hero.y+10,3,0,Math.PI*2,true);163 ctx.closePath();164 ctx.fill();165 //画出炮筒(直线)

166 ctx.strokeStyle = hero.color[1];167 ctx.lineWidth = 2;168 ctx.moveTo(hero.x+15,hero.y+10);169 if(hero.direct ==1){170 ctx.lineTo(hero.x+30,hero.y+10);171 }else if(hero.direct ==3){172 ctx.lineTo(hero.x,hero.y+10);173 }174 ctx.stroke();175 break;176 }177 }178

179 //定义一个子弹类

180 functionBullet(x,y,direct,tank,type){181 this.x =x;182 this.y =y;183 this.speed = 3;184 this.direct =direct;185 this.timer = null;186 this.isLive = true;187 this.tank =tank;188 this.type =type;189 this.run = function(){190 switch(this.direct){191 case 0:192 this.y -= this.speed;193 break;194 case 1:195 this.x += this.speed;196 break;197 case 2:198 this.y += this.speed;199 break;200 case 3:201 this.x -= this.speed;202 break;203 }204 document.getElementById('add1').innerText = " 子弹x轴:"+this.x+" 子弹y轴:"+this.y;205 document.getElementById('add2').innerText = " 坦克x轴:"+hero.x+" 坦克y轴:"+hero.y;206 document.getElementById('add3').innerText = " hero子弹数量:"+heroBullets.length;207 if(this.x <0 || this.x>=500 ||this.y<0 || this.y>300 || this.isLive==false){208 this.isLive = false;209 if(this.type=='enemy'){210 this.tank.bulletIsLive = false;211 }212 window.clearInterval(this.timer);213 }214 }215 }216 functiondrawHeroBullet(bullets){217 for(var i=0;i

226 functiondrawEnemyBullet(enemyBullets){227 for(var i=0;i

238 if(enemyTanks[j].isLive){239 switch(enemyTanks[j].direct){240 case 0:241 case 2:242 if(heroBullets[i].x>=enemyTanks[j].x&&heroBullets[i].x<=enemyTanks[j].x+20&&heroBullets[i].y>=enemyTanks[j].y&&heroBullets[i].y<=enemyTanks[j].y+30){243 //标记敌人的坦克和咱们的子弹已经死掉了

244 heroBullets[i].isLive = false;245 enemyTanks[j].isLive = false;246 var bomb = newBomb(enemyTanks[j].x,enemyTanks[j].y);247 bombs.push(bomb);248

249 }250 break;251 case 1:252 case 3:253 if(heroBullets[i].x>=enemyTanks[j].x&&heroBullets[i].x<=enemyTanks[j].x+30&&heroBullets[i].y>=enemyTanks[j].y&&heroBullets[i].y<=enemyTanks[j].y+20){254 //标记敌人的坦克和咱们的子弹已经死掉了

255 heroBullets[i].isLive = false;256 enemyTanks[j].isLive = false;257 var bomb = newBomb(enemyTanks[j].x,enemyTanks[j].y);258 bombs.push(bomb);259 }260 break;261 }262 }263

264 }265 }266 }267

268 //定义炸弹类

269 functionBomb(x,y){270 this.x =x;271 this.y =y;272 }273

274 //判断敌人的子弹是否击中本身的坦克

275 functionisHitHeroTank(enemyBullets,heroTank){276 for(var i=0;i= heroTank.x && enemyBullets[i].x <= heroTank.x+20 && enemyBullets[i].y >= heroTank.y && enemyBullets[i].y <= heroTank.y +30){282 heroTank.isLive = false;283 enemyBullets[i].isLive = false;284 }285 break;286 case 1:287 case 3:288 if(enemyBullets[i].x >= heroTank.x && enemyBullets[i].x <= heroTank.x+30 && enemyBullets[i].y >= heroTank.y && enemyBullets[i].y <= heroTank.y +20){289 heroTank.isLive = false;290 enemyBullets[i].isLive = false;291 }292 break;293 }294 }295 }296 }