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. ORB_SLAM2代码阅读(1)——系统入口
  2. python 倒计时功能怎么用print实现_python 实现倒计时功能(gui界面)
  3. ubuntu mysql 安装
  4. Konstrukt PHP REST框架 教程二
  5. 【OpenCV3】图像旋转与平移——cv::warpAffine()详解
  6. 我的 Vue.js 学习日记 (七) - 事件与修饰符
  7. prim算法求最小生成树_克鲁斯卡尔算法(Kruskal算法)求最小生成树
  8. Spring-Boot使用RedisCluster
  9. 160 - 54 eKH
  10. Angular深入理解基本组成
  11. 判断 Map 中是否包含指定的 key 和 value
  12. Javascript常用语法 (一)
  13. 仿药易通输入单位信息后如果没有则自动加入功能
  14. 计算机在智能制造专业中的应用,数控技术在智能制造中的应用及发展分析
  15. 闪迪u盘不能识别好办法_SanDisk U盘无法识别解决
  16. 带你学微信小程序开发
  17. Python爬虫:爬取网页图片
  18. 让你的工作事半功倍的语音转文字转换器
  19. 陪你云sdk用户指南
  20. 零售业100个创意促销方案

热门文章

  1. Android日历备忘录案例
  2. 计算机在测控技术与仪器中的应用,浅谈智能化技术在测控技术与仪器中的应用...
  3. python语言程序设计网课答案-好消息,2020大学慕课Python 语言程序设计完整答案...
  4. R实战 | 文章第一表:三线表的绘制
  5. 【Multisim仿真】病房呼叫系统
  6. 穿越寒冬 向新而行 | 智和信通2022年度年终总结大会圆满落幕
  7. linux安装nginx1.14.0,Ubuntu 14.04 安装最新稳定版Nginx 1.6.0
  8. 【服务器数据恢复】raid0数据恢复案例raid数据网络回迁过程
  9. python递归求斐波那契数列前20项_用递归算法实现斐波那契数列1,1,2,3,5,8,13……的前20项,每输出5项一换行,用C++...
  10. 精彩回顾:软件的生命力在于创新