坦克大战

举报
兔老大 发表于 2021/04/23 23:24:50 2021/04/23
【摘要】 效果 map.js var map4 = [ [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,2,2,0,...

效果

map.js


  
  1. var map4 = [
  2. [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
  3. [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
  4. [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
  5. [0,2,2,0,0,2,2,0,0,0,2,2,0,0,2,2,0,0,2,2,0,2,2,0],
  6. [0,2,2,0,0,2,2,0,0,0,2,2,3,3,2,2,0,0,2,2,0,2,2,0],
  7. [0,2,2,0,0,2,2,0,0,0,2,2,3,3,2,2,0,0,2,2,0,2,2,0],
  8. [0,2,2,0,0,2,2,0,0,0,2,2,0,0,2,2,0,0,2,2,0,2,2,0],
  9. [0,2,2,0,0,2,2,0,0,0,2,2,0,0,2,2,0,0,2,2,0,2,2,0],
  10. [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
  11. [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
  12. [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
  13. [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
  14. [0,2,2,0,0,2,2,0,0,0,2,0,0,0,0,2,0,0,2,2,0,2,2,0],
  15. [0,2,2,0,0,2,2,0,0,0,2,0,0,0,0,2,0,0,2,2,0,2,2,0],
  16. [0,2,2,0,0,2,2,0,0,0,2,2,2,2,2,2,0,0,2,2,0,2,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,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
  100. [0,0,0,0,0,0,0,0,0,0,2,2,2,2,2,2,0,0,0,0,0,0,0,0],
  101. [0,0,0,0,0,0,0,0,0,0,2,2,2,2,2,2,0,0,0,0,0,0,0,0],
  102. [0,0,0,0,0,0,0,0,0,0,2,2,7,7,2,2,0,0,0,0,0,0,0,0],
  103. [0,0,0,0,0,0,0,0,0,0,2,2,7,7,2,2,0,0,0,0,0,0,0,0]
  104. ];

index.html


  
  1. <html xmlns="http://www.w3.org/1999/xhtml">
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  4. <title>坦克大战</title>
  5. <meta content="坦克大战" name="keywords" />
  6. <meta content="坦克大战," name="description" />
  7. <script src="js/jquery-1.3.2.min.js"></script>
  8. <script src="js/map.js"></script>
  9. <script>
  10. ~function(){
  11. var mainTank; //主坦克
  12. var subTank; //副坦克
  13. var eTanks = []; //敌坦克
  14. var plays; //单人或者双人模式
  15. var eTankLife = 20; //敌坦克总数量
  16. var level = 1; //当前关数
  17. var eTanksLength = 3; //每次显示的敌坦克数量
  18. var maps = [map1, map2, map3]; //地图
  19. var map; //当前地图
  20. var u; //长定时器
  21. var xs = [[0,12,22], [0, 8, 16, 22], [0, 6, 12, 18, 22] ];
  22. var Gb = {
  23. attr : function(){
  24. if (arguments.length == 1){
  25. return Gb[arguments[0]];
  26. }else if (arguments.length == 2){
  27. Gb[arguments[1]] = arguments[0]
  28. return Gb;
  29. }
  30. }
  31. }
  32. //--------------------------------------Mover类---------------------------------------------
  33. function Mover(){};
  34. Mover.prototype = {
  35. locked : false //是否允许移动
  36. ,stepLocked : false //移动间隔锁
  37. ,der : false //移动方向
  38. ,obj : null //占位,子类对应的dom
  39. ,speed : 20 //移动速度
  40. ,moveRepeat : false //是否重复移动,默认否
  41. ,setXy : function(y, x){this.obj.css({top:y, left:x}); return this;}
  42. ,useGird : function(){ //占据网格
  43. var x = this.x, y = this.y;
  44. map[y][x].type = map[y][x+1].type = map[y+1][x].type = map[y+1][x+1].type = this.type;
  45. map[y][x].span = map[y][x+1].span = map[y+1][x].span = map[y+1][x+1].span = this;
  46. }
  47. ,freeGird : function(){ //释放网格
  48. var x = this.x, y = this.y;
  49. map[y][x].type = map[y][x+1].type = map[y+1][x].type = map[y+1][x+1].type = 0;
  50. return this;
  51. }
  52. ,move : function(fn){ //移动, fn: 撞击之后触发函数
  53. if (this.stepLocked || this.locked || this.reliveLocked) return; //如果被锁定
  54. if (this.toBorder(this.der, fn)) { //如果撞到边界
  55. return;
  56. }
  57. this.freeGird();
  58. if (this.hit(fn) == 1) return; //如果遇到障碍
  59. this.stepLocked = true; //加锁
  60. var der = this.der, w, d;
  61. switch(der){ //计算下个网格的x,y
  62. case 0: this.x -= 1; w = 'left'; d = -2; break;
  63. case 2: this.x += 1; w = 'left'; d = 2; break;
  64. case 1: this.y -= 1; w = 'top'; d = -2; break;
  65. case 3: this.y += 1; w = 'top'; d = 2; break;
  66. }
  67. this.useGird(); //占据网格
  68. var obj = this.obj.get(0);
  69. for (var i = 0; i < 10; i++){ //移动
  70. var closure = ~function(i, t){
  71. var s = setTimeout(function(){
  72. obj.style[w] = parseInt(obj.style[w]) + d + 'px';
  73. clearTimeout(s);
  74. if (i == 9){
  75. closure = null;
  76. t.stepLocked = false; //移动完成的时候释放移动锁
  77. t.moveRepeat && t.move(fn); //是否重复移动
  78. }
  79. }, i*t.speed)
  80. }(i, this);
  81. }
  82. }
  83. ,toBorder : function(der, fn){ //撞到边界
  84. if (der == 1 && this.y == 0 || der == 3 && this.y == 22 || der == 0 && this.x == 0 || der == 2 && this.x == 22) {
  85. if (jQuery.isFunction(fn)) fn();
  86. return true;
  87. }
  88. }
  89. ,probe : function(){ //探测
  90. var x = this.x, y = this.y;
  91. var baffles = []; //前方障碍
  92. switch(this.der){
  93. case 0 : baffles[0] = [y, x-1]; baffles[1] = [y+1, x-1]; break; //左
  94. case 2 : baffles[0] = [y, x+2]; baffles[1] = [y+1, x+2]; break; //右
  95. case 1 : baffles[0] = [y-1, x]; baffles[1] = [y-1, x+1]; break; //上
  96. case 3 : baffles[0] = [y+2, x]; baffles[1] = [y+2, x+1]; break; //下
  97. }
  98. return baffles;
  99. }
  100. }
  101. //--------------------------------------------------------坦克--------------------------------------
  102. function Tank(obj, speed, x, y, der){
  103. this.obj = obj; //span
  104. this.type = 5;
  105. this.speed = speed;
  106. this.x = x;
  107. this.y = y;
  108. this.der = der; //移动方向
  109. this.fx = x; //初始x,坦克复活的时候使用
  110. this.fy = y; //初始y
  111. this.fder = der; //初始移动方向
  112. this.clipLength = 3; //弹夹大小,最多存放3个子弹
  113. this.clip = []; //弹夹
  114. this.clipLocked = false; //弹夹锁
  115. this.fireLocked = true;
  116. this.life = 3;
  117. this.exp = []; //爆炸效果
  118. this.invTime = 2000; //无敌时间
  119. this.reliveLocked = false; //复活锁,复活期间不能移动
  120. }
  121. Tank.prototype = new Mover; //继承Mover
  122. Tank.prototype.init = function(){
  123. this.setPos(this.der); //设置坦克方向
  124. this.setXy(this.y*20, this.x*20); //设置位置
  125. this.useGird(this); //占据网格
  126. this.initClip(); //初始化弹夹
  127. if (this.invTime) this.invincible(this.invTime); //无敌
  128. return this;
  129. }
  130. Tank.prototype.initClip = function(){ //初始化弹夹
  131. for (var i = 0;i < this.clipLength; i++) {
  132. this.exp.push($('.explode').eq(0).clone().appendTo(Gb.attr('gameMap'))); //爆炸效果
  133. this.clip.push(new Ball(this, 7));
  134. }
  135. }
  136. Tank.prototype.setPos = function(der){ //设置移动方向
  137. if (der == 0) this.der = 0;
  138. else this.der = der || this.fder;
  139. this.obj.css('background-position', '0px -'+ [3, 0, 1, 2][this.der]*40 +'px');
  140. this.bobDer = der; //记录子弹方向
  141. return this;
  142. }
  143. Tank.prototype.hit = function(){ //碰撞
  144. var baffles = this.probe(); //前方物体
  145. for (var i = 0; i < baffles.length; i++){
  146. var type = map[baffles[i][0]][baffles[i][1]].type;
  147. if ( (/^2|3|4|5|6|7$/).test(type)) return 1; //遇到障碍
  148. if (type == 'speed' || type == 'hide' || type == 'life' || type == 'power' || type == 'defense') {
  149. if (this == mainTank || this == subTank){
  150. Gb.attr('.prop').hide();
  151. Props[type](this); //吃道具
  152. }
  153. }
  154. }
  155. return 0;
  156. }
  157. Tank.prototype.fire = function(){ //开火
  158. if (this.reliveLocked || this.clipLocked == true || this.clip.length <= 0) {
  159. return;
  160. }
  161. this.clipLocked = true; //发射后锁定弹夹
  162. this.clip.pop().init(); //取出一个子弹并发射
  163. var self = this;
  164. setTimeout(function(){ //子弹间隔时间
  165. self.clipLocked = false;
  166. }, 600)
  167. return this;
  168. }
  169. Tank.prototype.relive = function(){ //复活
  170. this.life ? this.life-- : eTankLife--;
  171. if (this.showInfo() == 1){
  172. this.freeGird();
  173. if (this == mainTank){
  174. mainTank = false;
  175. }else if (this == subTank){
  176. subTank = false;
  177. }
  178. return;
  179. };
  180. this.locked = true;
  181. this.der = this.fder;
  182. var me = this;
  183. setTimeout(function(){
  184. me.freeGird();
  185. me.x = me.fx;
  186. me.y = me.fy;
  187. if( me != mainTank && me != subTank ) me.locked = false;
  188. me.reliveLocked = false;
  189. me.clipLocked = false;
  190. me.setXy(me.y*20, me.x*20);
  191. me.setPos(me.fder);
  192. me.useGird();
  193. me.invincible(me == mainTank ? 3000 : 1500);
  194. }, 500);
  195. return this;
  196. }
  197. Tank.prototype.showInfo = function(){
  198. if ((plays == 1 && !mainTank.life) || (plays == 2 && !mainTank.life && !subTank.life)){
  199. setTimeout(function(){ $('#gameOver').show(); return}, 1000);
  200. }
  201. var ary = this == mainTank ? [Gb.attr('P1'), '1P'] : [Gb.attr('P2'), '2P'];
  202. ary[0].html(ary[1]+' * ' +this.life);
  203. if (this.life <= 0) return 1;
  204. }
  205. Tank.prototype.invincible = function(time){ //坦克无敌
  206. this.invTime = time;
  207. var i = 0, me = this;
  208. var u = setInterval(function(){
  209. me.obj[i++ % 2 == 0 ? 'show' : 'hide']();
  210. if (i >= time/150){
  211. me.obj.show();
  212. me.invTime = 0;
  213. clearTimeout(u);
  214. }
  215. }, 150);
  216. }
  217. //---------------------------------------------敌方坦克-----------------------------------------------
  218. var ETank = function(obj, speed, x, y, der){
  219. this.obj = obj; //span
  220. this.speed = speed;
  221. this.x = x;
  222. this.y = y;
  223. this.type = 6;
  224. this.der = der;
  225. this.fx = x;
  226. this.fy = y;
  227. this.fder = der;
  228. this.life = false;
  229. this.clip = []; //弹夹
  230. this.exp = []; //爆炸效果
  231. this.invTime = 0; //无敌状态
  232. }
  233. ETank.prototype = new Tank;
  234. ETank.prototype.showInfo = function(){
  235. $('.etankSam').eq(0).remove();
  236. if (eTankLife <= 0){
  237. if (level == 3){
  238. alert ('没有地图了,算你过关 - -!');
  239. window.location.reload();
  240. return;
  241. }
  242. level++;
  243. setTimeout(function(){mapReload()}, 100);
  244. return 1;
  245. }
  246. if (eTankLife <= eTanksLength-1){
  247. this.freeGird();
  248. return 1;
  249. }
  250. }
  251. ETank.prototype.Ai = function(){
  252. this.move();
  253. var self = this;
  254. var m = Math.round(Math.random()*100);
  255. var ary = $.grep([0,1,2,3,3,2,3,1], function(n, i){
  256. return n != self.der;
  257. });
  258. var len = ary.length;
  259. for (var i = 0 ; i < len ; i++){ //洗牌,下一次的方向
  260. var num = parseInt(Math.random()*len);
  261. var tmp = ary[num];
  262. ary[num] = ary[i];
  263. ary[i] = tmp;
  264. }
  265. var nextDer = ary[0];
  266. if (this.toBorder(this.der) || this.hit() == 1){
  267. var s1 = setTimeout(function(){
  268. self.setPos(nextDer).fire();
  269. self.move();
  270. var s2 = setTimeout(function(){
  271. self.Ai();
  272. clearTimeout(s2);
  273. }, 400)
  274. clearTimeout(s1);
  275. }, 400);
  276. return;
  277. }
  278. if (m < 20){
  279. this.setPos(nextDer).fire();
  280. }
  281. else if (m < 60) this.fire();
  282. var s3 = setTimeout(function(){
  283. self.Ai();
  284. clearTimeout(s3);
  285. }, 200)
  286. }
  287. //----------------------------------------------------------------------------------------------------
  288. //---------------------------------------------------------炮弹---------------------------------------
  289. function Ball(pTank, speed){
  290. this.obj = $('.ball').eq(0).clone().appendTo(Gb.attr('gameMap'));
  291. this.speed = speed;
  292. this.pTank = pTank;
  293. this.type = 10;
  294. this.moveRepeat = true;
  295. }
  296. Ball.prototype = new Mover;
  297. Ball.prototype.init = function(){
  298. this.reviseXy(); var self = this;
  299. this.move(function(){
  300. self.freeGird();
  301. self.explode();
  302. self.recover(); //爆炸并回收子弹
  303. });
  304. self.pTank.useGird(); //还原坦克所占网格
  305. }
  306. Ball.prototype.reviseXy = function(){ //调整子弹位置
  307. var x = this.x = this.pTank.x, y = this.y = this.pTank.y;
  308. this.der = this.pTank.bobDer || 0; //确认子弹发射方向
  309. switch(this.der){
  310. case 0: x -= 1; break; //左
  311. case 1: y -= 1; break; //上
  312. case 2: x += 1; break; //右
  313. case 3: y += 1; break; //下
  314. }
  315. this.setXy(y*20, x*20);
  316. }
  317. Ball.prototype.hit = function(fn){
  318. var ret = 0;
  319. var baffles = this.probe(); //前方物体
  320. for (var i = 0; i < baffles.length; i++){
  321. var y = baffles[i][0], x = baffles[i][1], baffle = map[y][x], type = baffle.type, span = baffle.span;
  322. if (type == 'speed' || type == 'hide' || type == 'life' || type == 'power' || type == 'defense'){ //击中道具
  323. if ($.isFunction(fn)) { fn()}; //爆炸
  324. this.clearUi(y, x, span);
  325. Gb.attr('.prop').hide(); //隐藏物品
  326. return 1;
  327. }
  328. if ((/^2|3|4|5|6|7$/).test(type)) { //爆炸
  329. if (type == 4) {
  330. this.freeGird().recover(); //击中海洋,回收子弹但不爆炸.
  331. return 1;
  332. }
  333. if (type == 6 && this.pTank.type == 6) { //敌方坦克互相击中无效
  334. this.freeGird().recover();
  335. return 1;
  336. }
  337. if (type == 5 || type == 6) {
  338. if (span.invTime) { //如果处在无敌状态
  339. this.freeGird().recover(); //回收子弹但不爆炸.
  340. return 1;
  341. }
  342. if ($.isFunction(fn)) { fn()};
  343. span.obj.hide();
  344. span.reliveLocked = true;
  345. span.invTime = 1; //无敌
  346. span.relive(); //坦克复活
  347. return 1;
  348. }
  349. if ($.isFunction(fn)) { fn()};
  350. if (type == 7) { $('#gameOver').show(); return};
  351. if (type == 2 || (this.type == 11 && type == 3)) {
  352. this.clearUi(y, x, span); //清除砖块
  353. ret = 1;
  354. }
  355. else if (type == 3){
  356. return 1;
  357. }
  358. }
  359. }
  360. return ret;
  361. }
  362. Ball.prototype.explode = function(){ //子弹爆炸
  363. var explode = this.pTank.exp.pop();
  364. if (!explode) return;
  365. var x = this.x, y = this.y, der = this.der;
  366. der == 0 ? x -= 1 : (der == 1 ? y -= 1 : ''); //调整爆炸位置
  367. explode.show().css( {top: y*20, left: x*20} );
  368. var m = Math.random()>.2;
  369. for (var i = 0; i < 8; i++){
  370. var closure = ~function(i, t){
  371. var s = setTimeout(function(){
  372. explode.css('background-position', '0px -'+i*60+'px');
  373. if (i == 7) {
  374. explode.hide();
  375. t.pTank.exp.push(explode);
  376. }
  377. clearTimeout(s);
  378. closure = null;
  379. }, (m ? Math.sqrt(i): i*1.2)*100)
  380. }(i, this);
  381. }
  382. return this;
  383. };
  384. Ball.prototype.clearUi = function(y, x, span){
  385. map[y][x].type = 0;
  386. if (span.length) span.attr('className', 'aa');
  387. }
  388. Ball.prototype.recover = function(){ //回收子弹
  389. this.obj.css('left', '-1000px'); //扔到屏幕外
  390. var clip = this.pTank.clip;
  391. if(clip.length < 3) clip.push(this); //回收子弹,重新装入弹夹
  392. }
  393. //------------------------------------------道具-----------------------------------------------
  394. $(document).ready(function(){
  395. Gb.attr($('#gameMap'), 'gameMap');
  396. $('#bottomBg img').each(function(i){
  397. $(this).click(function(){
  398. plays = i == 0 ? 1 : 2;
  399. $('#gameBg').remove();
  400. createMap();
  401. });
  402. $(this).hover(function(){
  403. $(this).attr('src', i == 0 ? 'img/play12.jpg' : 'img/play22.jpg');
  404. },function(){
  405. $(this).attr('src', i == 0 ? 'img/play11.jpg' : 'img/play21.jpg');
  406. })
  407. });
  408. $(document).keydown(function(event){
  409. var k = event.keyCode;
  410. if ( (/^65|87|68|83$/).test(k) ) {
  411. mainTank.der = {'65':0,'87':1,'68':2,'83':3}[k];
  412. mainTank.locked = false;
  413. }
  414. if (k == 32) mainTank.fireLocked = false;
  415. if (plays == 1) return;
  416. if ( (/^37|38|39|40$/).test(k) ) {
  417. subTank.der = k - 37;
  418. subTank.locked = false;
  419. }
  420. if (k == 76) subTank.fireLocked = false;
  421. })
  422. $(document).keyup(function(event){
  423. var k = event.keyCode;
  424. if ( (/^65|87|68|83$/).test(k) ) mainTank.locked = true;
  425. if (k == 32) mainTank.fireLocked = true;
  426. if (plays == 1) return;
  427. if ((/^37|38|39|40$/).test(k) ) subTank.locked = true;
  428. if (k == 76) subTank.fireLocked = true;
  429. })
  430. })
  431. function createMap(){
  432. var _class = ['aa', 'wall', 'ston', 'steel', 'sea'];
  433. map = maps[level-1];
  434. $(map).each(function(i, n){
  435. $(n).each(function(j, n){
  436. map[i][j] = { type : n, span : $('<span class='+_class[n]+'></span>').appendTo(Gb.attr('gameMap'))};
  437. })
  438. })
  439. $('<span class=AC></span>').appendTo(Gb.attr('gameMap'));
  440. $('<span class=tank></span>').appendTo(Gb.attr('gameMap'));
  441. $('<span class=etank></span>').appendTo(Gb.attr('gameMap'));
  442. $('<span class=ball></span>').appendTo(Gb.attr('gameMap'));
  443. $('<span class=explode></span>').appendTo(Gb.attr('gameMap'));
  444. $('<span class=props></span>').appendTo(Gb.attr('gameMap'));
  445. Gb.attr($('#infoBottom'), '#infoBottom').attr($('.props'), '.prop').attr($('#gameInfo'), 'gameInfo').attr($('.tank'), '.tank').attr($('.etank'), '.etank').attr('$(.ball)', '.ball');
  446. $('.AC').css({top : '440px',left : '240px'});
  447. mainTank = new Tank(Gb.attr('.tank'), 15, 8, 22, 1).init();
  448. mainTank.locked = true;
  449. if (plays == 2){
  450. subTank = new Tank(Gb.attr('.tank').clone().appendTo(Gb.attr('gameMap')), 15, 16, 22, 1).init();
  451. subTank.locked = true;
  452. }
  453. for (var i = 0; i < eTanksLength; i++){
  454. var x = xs[level -1];
  455. eTanks[i] = new ETank(Gb.attr('.etank').eq(0).clone().appendTo(Gb.attr('gameMap')), 20, x[i], 0, 3).init().Ai();
  456. }
  457. for (var i = 0; i < eTankLife; i++){
  458. $('<span class=etankSam></span>').appendTo(Gb.attr('gameInfo'));
  459. }
  460. $('<span class=play1>1P * ' +mainTank.life+ '</span>').appendTo(Gb.attr('#infoBottom'));
  461. if (plays == 2) $('<span class=play2>2P * ' +subTank.life+ '</span>').appendTo(Gb.attr('#infoBottom'));
  462. $('<span class=level>第 ' +level+ ' 关</span>').appendTo(Gb.attr('#infoBottom'));
  463. Gb.attr($('.play1'), 'P1').attr($('.play2'), 'P2');
  464. Props.init(); //道具初始化
  465. u = setInterval(function(){
  466. if (mainTank) mainTank.setPos(mainTank.der).move();
  467. if (mainTank && !mainTank.fireLocked) mainTank.fire();
  468. if (plays == 1) return;
  469. if (subTank) subTank.setPos(subTank.der).move();
  470. if (subTank && !subTank.fireLocked) subTank.fire();
  471. }, 19);
  472. }
  473. var Props = { //道具类
  474. Ui : [0, 0]
  475. ,init : function(){
  476. setInterval(function(){
  477. if (Props.use) {
  478. Gb.attr('.prop').hide();
  479. Props.use = false;
  480. return;
  481. }
  482. var fn = ['speed', 'hide', 'life', 'power', 'defense'][Math.round(Math.random()*4)];
  483. var Ui = Props.getUi();
  484. Props.Ui = Ui;
  485. map[Ui[0]][Ui[1]].type = fn;
  486. Props.use = true;
  487. Gb.attr('.prop').css({'background' : 'url(img/'+ fn +'.jpg)', top : Ui[0]*20, left : Ui[1]*20} ).show();
  488. }, Math.max(10000, Math.round(Math.random()*20000)));
  489. }
  490. ,use : false
  491. ,speed : function(obj){
  492. obj.speed = obj.speed / 2;
  493. setTimeout(function(){
  494. obj.speed = obj.speed * 2;
  495. Props.use = false;
  496. map[Props.Ui[0]][Props.Ui[1]].type = 0;
  497. }, 15000);
  498. }
  499. ,hide : function(obj){
  500. obj.invincible(5000);
  501. setTimeout(function(){
  502. Props.use = false;
  503. map[Props.Ui[0]][Props.Ui[1]].type = 0;
  504. }, 10000);
  505. }
  506. ,life : function(obj){
  507. obj.life++;
  508. obj.showInfo();
  509. Props.use = false;
  510. map[Props.Ui[0]][Props.Ui[1]].type = 0;
  511. }
  512. ,power : function(obj){
  513. for (var i = 0; i < obj.clip.length; i++){
  514. obj.clip[i].type = 11;
  515. }
  516. setTimeout(function(){
  517. for (var i = 0; i < obj.clip.length; i++){
  518. obj.clip[i].type = 10;
  519. }
  520. }, 10000)
  521. }
  522. ,defense : function(obj){
  523. var ary = [[21, 11], [21, 12], [21, 13], [21, 14], [22, 11], [22, 14], [23, 11], [23, 14]];
  524. for (var i = 0; i < ary.length; i++){
  525. var y = ary[i][0], x = ary[i][1];
  526. map[y][x].type = 3;
  527. map[y][x].span.attr('className', 'steel');
  528. }
  529. setTimeout(function(){
  530. for (var i = 0; i < ary.length; i++){
  531. var y = ary[i][0], x = ary[i][1];
  532. map[y][x].type = 2;
  533. map[y][x].span.attr('className', 'ston');
  534. }
  535. }, 15000)
  536. }
  537. ,fn : false
  538. ,getUi : function(){
  539. var i = Math.round(Math.random()*23);
  540. var j = Math.round(Math.random()*23);
  541. if (map[j][i].type == 0) {
  542. return [j, i];
  543. }
  544. else {
  545. return this.getUi();
  546. }
  547. }
  548. }
  549. function mapReload(){
  550. Gb.attr('gameMap').empty();
  551. Gb.attr('#infoBottom').empty();
  552. $(document).keydown(function(event){
  553. return false;
  554. });
  555. $(document).keyup(function(event){
  556. return false;
  557. });
  558. clearInterval(u);
  559. mainTank = null;
  560. subTank = null;
  561. eTanks = [];
  562. eTankLife = 20;
  563. map = maps[level-1];
  564. eTanksLength++;
  565. if (level == 3) {
  566. eTanksLength = 12;
  567. xs[2] = [0,2,4,6,8,10,12,14,16,18,20,22];
  568. }
  569. createMap();
  570. }
  571. }();
  572. </script>
  573. <style>
  574. html, body{overflow:hidden}
  575. #game{width:650px;margin:0 auto;overflow:hidden;position:relative;height:600px;}
  576. #gameBg{height:480px;width:650px;margin:3px auto;border:10px ridge #eee;background:#000;overflow:hidden;position:absolute;left:00px;top:8%;z-index:1000}
  577. #gameBg div {width:100%;height:50%}
  578. #gameBg #topBg img {margin:30px auto;display:block}
  579. #gameBg #bottomBg img {margin:20px auto;display:block;cursor:pointer;height:40px;width:150px}
  580. #gameMap{height:480px;width:480px;margin:3px auto;border:10px ridge #eee;background:#000;overflow:hidden;position:absolute;left:00px;top:8%;float:left}
  581. #gameInfo{height:480px;width:150px;margin:3px auto;border:10px ridge #eee;background:#000;overflow:hidden;position:absolute;left:490px;top:8%}
  582. #gameMap span{width:20px;height:20px;float:left}
  583. #gameMap span.steel{background:url(img/steel.gif) no-repeat}
  584. #gameMap span.sea{background:url(img/sea.gif) no-repeat}
  585. #gameMap span.wall{background:url(img/wall.gif) no-repeat;position:relative;z-index:104}
  586. #gameMap span.ston{background:url(img/ston.gif) no-repeat}
  587. #gameMap span.tank{position:absolute;background:url(img/tank.gif) no-repeat; width:40px; height:40px;z-index:101}
  588. #gameMap span.etank{background:url(img/etank.gif) no-repeat; position:absolute; width:40px; height:40px;z-index:101}
  589. #gameMap span.ball{background:url(img/ball.gif) no-repeat; position:absolute; width:40px; height:40px;z-index:103}
  590. #gameMap span.aa{background:url(); width:20px; height:20px}
  591. #gameMap span.explode{background:url(img/explode.gif) no-repeat; position:absolute; width:60px; height:60px;z-index:103}
  592. #gameMap span.props{background:url(); width:20px; height:20px; position:absolute;z-index:101}
  593. #gameMap span.info{position:absolute; width:60px; height:60px; top:100;left:100;z-index:102}
  594. #gameMap span.AC{background:url(img/1.gif) no-repeat;position:absolute; width:40px; height:40px;z-index:102}
  595. #gameInfo span.etankSam{background:url(img/etank.gif) no-repeat 0px -80px; width:40px; height:40px;float:left;margin-right:10px}
  596. #gameInfo span.play1, .play2{background:url(img/tank.gif) no-repeat; width:100%; height:40px;float:left;margin-top:25px;margin-left:10px;color:#fff;text-indent:60px;Font-weight:bold;font-size:1.2em;line-height:40px}
  597. #gameInfo span.level{width:100%;height:30px;float:left;margin-top:20px;color:#fff;text-align:center;font-weight:bold;font-size:1.2em;line-height:30px;letter-spacing:5px}
  598. #gameInfo #infoBottom{position:absolute; width:100%; height:200px; top:295px;left:0px}
  599. #gameOver{height:480px;width:650px;margin:3px auto;border:10px ridge #eee;background:#000;overflow:hidden;position:absolute;left:0px;top:8%;display:none;z-index:1001}
  600. #gameOver img{height:100%;width:100%}
  601. #desc img{width:100%;}
  602. </style>
  603. </head>
  604. <body>
  605. <div id="game">
  606. <div id="gameBg">
  607. <div id="topBg"><img src="img/bg.jpg"></img></div>
  608. <div id="bottomBg"><img src="img/play11.jpg"></img><img src="img/play21.jpg"></img></div>
  609. </div>
  610. <div id="gameMap"> </div>
  611. <div id="gameInfo"> <div id="infoBottom"></div></div>
  612. <div id="gameOver"><img src="img/gameover.jpg"></img></div>
  613. <div id="desc">说明:js版坦克大战, 主坦克方向:WASD,空格发射炮弹; 副坦克方向:上下左右,L发射炮弹.</div>
  614. </div>
  615. </body>
  616. </html>

 

文章来源: fantianzuo.blog.csdn.net,作者:兔老大RabbitMQ,版权归原作者所有,如需转载,请联系作者。

原文链接:fantianzuo.blog.csdn.net/article/details/103935009

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。