1 //定义敌人和我们自己的坦克的颜色

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

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 }35

36 //英雄坦克类

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

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

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 //将坦克类的构造方法赋给敌人坦克

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

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 }100

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 }

韩顺平 java 坦克大战_HTML5坦克大战(韩顺平版本)相关推荐

  1. 韩顺平 java坦克大战_坦克大战完整版(韩顺平java)

    [实例简介] 坦克大战完整源代码(韩顺平java视频配套) [实例截图] [核心代码] 5i86q5 └── 源码 └── Class14 ├── 111.wav ├── bin │   ├── bo ...

  2. JavaStudy7(18章-坦克大战2)—B站韩顺平

    JavaStudy7(18章-坦克大战2)-B站韩顺平 1.坦克大战 1.1线程-应用到坦克大战 1.1.1 坦克大战 0.3 代码演示: //为了监听 键盘事件, 实现 KeyListener pu ...

  3. Java之详解坦克大战游戏(六)

    现在代码已经越写越多了,这里我们又新建一个包com.TankGame4,复制原来的两个java文件,首先我们来实现一个功能:防止敌人的坦克重叠运动.我们把判断是否碰撞的函数写到EnemyTank类中 ...

  4. java 让坦克移动_坦克大战_坦克移动

    MyTankGame2 package com.wxh.tank2; import javax.swing.*; import java.awt.*; import java.awt.event.*; ...

  5. Java实现经典版坦克大战(还原度很高)

    2022/4/8 13:30更(20年完结,游戏源码在文章底部,截至今天还可完美运行) 闲来无事写写帖子,想想上次更新已经是在一年前.源码里面附带了注解和思路.我把游戏效果展示给企业的老师看,给我的反 ...

  6. 【java毕业设计】基于java+Socket+Eclipse的坦克大战游戏设计与实现(毕业论文+程序源码)——坦克大战游戏

    基于java+Socket+Eclipse的坦克大战游戏设计与实现(毕业论文+程序源码) 大家好,今天给大家介绍基于java+Socket+Eclipse的坦克大战游戏设计与实现,文章末尾附有本毕业设 ...

  7. java之详解坦克大战_Java之详解坦克大战游戏(一)

    相信大家小时候一定玩过坦克大战游戏,躲避敌方坦克,炸毁敌方坦克,不断向前进攻直逼敌方基地-这次,我们来实现一个简单版的坦克大战,我想学Java的人都有想到以前的按键手机里那菜单点开"Java ...

  8. 面向对象程序设计(Java)课程设计--坦克大战

    目录 一.项目简介 1.功能描述 二.团队成员负责模块 三.功能架构图 四.项目亮点 五.功能需求分析 六.系统演示操作图片 七.项目git地址及团队成员git提交记录截图 一.项目简介 1.功能描述 ...

  9. Java实现的经典坦克大战小游戏

    Java实现的经典坦克大战小游戏 先看一下游戏结构: 有点多,没有耐心的可以不用看,这里先给出链接吧! 云链接:经典坦克大战 提取码:s9ai 这里就不介绍功能了,贴了一张游戏运行的截图,具体的功能自 ...

最新文章

  1. 【Java代码】京东商品全部分类数据获取(建表语句+Jar包依赖+树结构封装+爬虫源代码)包含csv和sql格式数据下载可用
  2. iOS:quartz2D绘图 (动画)
  3. java參数传递机制浅析
  4. nyoj 71 独木舟上的旅行 贪心
  5. Httpclient处理摘要认证
  6. 2019最受欢迎数据库:MySQL居首PostgreSQL第二Oracle位列第八
  7. 微信公众开放平台开发01---微信公众平台介绍,以及开发准备
  8. Disruptor内存消息队列简单使用
  9. 无感支付及相应技术规范
  10. 各地级市-国内及外汇旅游收入(1995-2020)
  11. centos安装stress安装失败_Linux压力测试软件Stress使用指南
  12. 基于AI的恶意软件分类技术(4)
  13. 如何使用计算机远程关闭手机软件,手机怎么控制电脑 手机远程控制电脑关机方法 (全文)...
  14. 蓝色理想的flex教材不能在flex Development做不能使用,我整理一下供初学者参考
  15. 深挖ThreadLocal
  16. 智能汽车发展战略-思维导图版本
  17. 各种下载文件方式总结
  18. 苹果取消iPhone 13显示屏的维修限制,Face ID不再受影响
  19. 用C语言检测文本编码的方法
  20. 服务器怎么跑python_如何在服务器上跑python程序

热门文章

  1. 一个荒唐类比的蝴蝶效应:千军万马做公链
  2. PC端和手机端字体显示不一致的解决办法
  3. 2021副高考试成绩查询 www.ppkao.com,2021年4月自学考试成绩查询时间及入口
  4. Islands HDU - 2808
  5. Java版坦克大战游戏的分析与实现
  6. 蓝桥杯软件类比赛java,第十届蓝桥杯大赛软件类省赛
  7. coderforce Educational Codeforces Round 10 C. Foe Pairs(贪心)
  8. python画名字七十周年快乐用英语怎么说_四周年快乐用英语怎么说?
  9. Android中常用限定符的使用
  10. Tampermonkey安装使用