歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> jQuery 寫的坦克大戰【附源代碼】

jQuery 寫的坦克大戰【附源代碼】

日期:2017/3/1 10:35:49   编辑:Linux編程

jQuery 寫的坦克大戰新鮮出爐,閒話少說。

免費下載地址在 http://linux.linuxidc.com/

用戶名與密碼都是www.linuxidc.com

具體下載目錄在 /2012年資料/1月/30日/jQuery 寫的坦克大戰【附源代碼】/

演示地址:http://www.muu.cc/html5/Tank/index.html

代碼貼出

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>坦克大戰 || www.88181.com</title>
  6. <mce:script language="javascript" type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js" mce_src="http://code.jquery.com/jquery-1.4.2.min.js"></mce:script>
  7. <mce:script language="javascript" type="text/javascript"><!--
  8. $(function() {
  9. $("body").append('<div class="map"></div>');
  10. InitTank('me', {x:(long-50)/2, y:high-50}, 'up');
  11. InitEnemy();
  12. //鍵盤點擊事件
  13. $(document).keydown(function(evt) {
  14. evtevt = evt || window.event;
  15. var key = evt.which || evt.keyCode;
  16. if(key==32)
  17. {
  18. SendBullet("me");
  19. }
  20. switch (key) {
  21. case 37: direction = "left"; break;
  22. case 38: direction = "up"; break;
  23. case 39: direction = "right"; break;
  24. case 40: direction = "down"; break;
  25. }
  26. if (key >= 37 && key <= 40) {
  27. ChangeDirection('me', direction);
  28. isMeMove = true;
  29. }
  30. });
  31. $(document).keyup(function(evt) {
  32. evtevt = evt || window.event;
  33. var key = evt.which || evt.keyCode;
  34. if (key >= 37 && key <= 40) {
  35. isMeMove = false;
  36. }
  37. });
  38. MyInterval=setInterval("Refresh()",timeSpan);
  39. });
  40. var isMeMove = false;
  41. var moveLong = 10;
  42. var bulletmoveLong = 20;
  43. var timeSpan = 300;
  44. var long = 600;
  45. var high = 600;
  46. function Refresh() {
  47. var me = $("#me");
  48. var mtop = $(me).position().top;
  49. var mleft = $(me).position().left;
  50. if (isMeMove) {
  51. direction = $(me).attr("direction");
  52. var offset = GetOffset(direction);
  53. mtop += offset.y*moveLong;
  54. mleft += offset.x*moveLong;
  55. if(mtop<0){
  56. mtop = 0;
  57. }else if(mtop>long-$(me).height())
  58. {
  59. mtop = long-$(me).height();
  60. }
  61. if(mleft<0){
  62. mleft = 0;
  63. }else if(mleft>long-$(me).width())
  64. {
  65. mleft = long-$(me).width();
  66. }
  67. $(me).css({'top':(mtop + 'px'),'left':(mleft + 'px')});
  68. }
  69. $(".tank:visible[enemy='enemy']").each(function(){
  70. var etop = $(this).position().top;
  71. var eleft = $(this).position().left;
  72. var bullettime = parseInt($(this).attr("bullettime"));
  73. if(bullettime<=0)
  74. {
  75. bullettime = 10;
  76. var topSpan = etop-mtop;
  77. var leftSpan = eleft-mleft;
  78. ex = Math.abs(leftSpan)>Math.abs(topSpan)?leftSpan/Math.abs(leftSpan)*-1:0;
  79. ey = Math.abs(leftSpan)>Math.abs(topSpan)?0:topSpan/Math.abs(topSpan)*-1;
  80. etopetop = etop + ey*moveLong;
  81. elefteleft = eleft + ex*moveLong;
  82. var direction = GetDirection({x:ex,y:ey});
  83. ChangeDirection($(this).attr("id"),direction);
  84. SendBullet($(this).attr("id"));
  85. }else
  86. {
  87. direction = $(this).attr("direction");
  88. var offset = GetOffset(direction);
  89. etopetop = etop + offset.y*moveLong;
  90. elefteleft = eleft + offset.x*moveLong;
  91. bullettime--;
  92. }
  93. if(etop<0){
  94. etop = 0;
  95. }else if(etop>long-$(this).height())
  96. {
  97. etop = long-$(this).height();
  98. }
  99. if(eleft<0){
  100. eleft = 0;
  101. }else if(eleft>long-$(this).width())
  102. {
  103. eleft = long-$(this).width();
  104. }
  105. $(this).css({'top':(etop + 'px'),'left':(eleft + 'px')}).attr("");
  106. $(this).attr("bullettime",bullettime);
  107. });
  108. $(".bullet:visible").each(function(){
  109. direction = $(this).attr("direction");
  110. var offset = GetOffset(direction);
  111. var top = $(this).position().top + offset.y*bulletmoveLong;
  112. var left = $(this).position().left + offset.x*bulletmoveLong;
  113. if(top<0){
  114. $(this).hide();
  115. return;
  116. }else if(top>long-$(this).height())
  117. {
  118. $(this).hide();
  119. return;
  120. }
  121. if(left<0){
  122. $(this).remove();
  123. return;
  124. }else if(left>long-$(this).width())
  125. {
  126. $(this).remove();
  127. return;
  128. }
  129. $(this).css({'top':(top + 'px'),'left':(left + 'px')},timeSpan);
  130. });
  131. CheckBullet();
  132. CheckTank();
  133. CheckBulletTank();
  134. }
  135. //判斷子彈相碰
  136. function CheckBullet()
  137. {
  138. $(".bullet[owner='me']:visible").each(function(){
  139. var me = this;
  140. $(".bullet[owner!='me']:visible").each(function(){
  141. if(IsCollision(me,this))
  142. {
  143. $(me).hide();
  144. $(this).hide();
  145. }
  146. });
  147. });
  148. }
  149. //判斷坦克相碰
  150. function CheckTank()
  151. {
  152. var me = $("#me");
  153. $(".tank").not("#me").each(function(){
  154. if(IsCollision(me,this))
  155. {
  156. alert("被坦克"+$(this).attr("id")+"打死了");
  157. Failure();
  158. }
  159. });
  160. }
  161. //判斷子彈打在坦克上
  162. function CheckBulletTank()
  163. {
  164. var me = $("#me");
  165. $(".bullet[owner!='me']:visible").each(function(){
  166. if(IsCollision(me,this))
  167. {
  168. alert("被子彈"+$(this).attr("id")+"打死了");
  169. Failure();
  170. }
  171. });
  172. $(".bullet[owner='me']:visible").each(function(){
  173. var fme = this;
  174. $(".tank").not("#me").each(function(){
  175. if(IsCollision(fme,this))
  176. {
  177. $(this).hide();
  178. $(fme).hide();
  179. InitEnemy();
  180. }
  181. });
  182. });
  183. }
  184. //根據方向返回偏移量
  185. function GetOffset(direction)
  186. {
  187. ox = 0;
  188. oy = 0;
  189. if(direction=='left')
  190. {
  191. ox = -1;
  192. oy = 0;
  193. }else if(direction=='right')
  194. {
  195. ox = 1;
  196. oy = 0;
  197. }else if(direction=='up')
  198. {
  199. ox = 0;
  200. oy = -1;
  201. }else if(direction=='down')
  202. {
  203. ox = 0;
  204. oy = 1;
  205. }
  206. return {x:ox,y:oy};
  207. }
  208. //獲取方向
  209. function GetDirection(offset)
  210. {
  211. var direction = 'down';
  212. if(offset.x==-1 && offset.y==0)
  213. {
  214. direction = 'left' ;
  215. }else if(offset.x==1 && offset.y==0)
  216. {
  217. direction = 'right' ;
  218. }else if(offset.x==0 && offset.y==-1)
  219. {
  220. direction = 'up' ;
  221. }else if(offset.x==0 && offset.y==1)
  222. {
  223. direction = 'down' ;
  224. }
  225. return direction;
  226. }
  227. //發送炮彈
  228. function SendBullet(tid)
  229. {
  230. if($(".bullet[owner='" + tid + "']:visible").size()<2)
  231. {
  232. var x = $("#"+tid).position().left;
  233. var y = $("#"+tid).position().top;
  234. var direction = $("#"+tid).attr("direction");
  235. var offset = GetOffset(direction);
  236. var ox = x+offset.x*20+(offset.x==1?30:20);
  237. var oy = y+offset.y*20+(offset.y==1?30:20);
  238. if($(".bullet:hidden").size()==0)
  239. {
  240. $(".map").append($('<div class="bullet" style="left:' + ox + 'px;top:' + oy + 'px;" direction="' + direction + '" owner="' + tid + '"></div>'));
  241. }else
  242. {
  243. $(".bullet:hidden").eq(0).css({left: ox + 'px',top: oy + 'px'}).attr("direction",direction).attr("owner",tid).show();
  244. }
  245. }
  246. }
  247. //初始化敵人
  248. function InitEnemy()
  249. {
  250. if($("#enemy1:hidden").size()==0)
  251. {
  252. InitTank('enemy1', {x:(Math.round(Math.random()*3)-1)*((long-50)/2), y:0}, 'down');
  253. }else
  254. {
  255. $("#enemy1").css({left:(Math.round(Math.random()*3)-1)*((long-50)/2)+"px",top:0}).show();
  256. }
  257. $("#enemy1").attr("enemy",'enemy').attr("bullettime","10");
  258. SendBullet("enemy1");
  259. }
  260. //初始化坦克
  261. //tid 坦克id
  262. //x橫坐標(left值)
  263. //y縱坐標(top值)
  264. //方向
  265. function InitTank(tid,position, direction) {
  266. x = position.x < 0 ? 0 : position.x;
  267. x = position.x > 600 ? 600 : position.x;
  268. y = position.y < 0 ? 0 : position.y;
  269. y = position.y > 600 ? 600 : position.y;
  270. $(".map").append('<div id="' + tid + '" style="left:' + position.x + 'px;top:' + position.y + 'px;" direction="' + direction + '" class="tank ' + direction + '"></div>');
  271. }
  272. //改變坦克的方向
  273. function ChangeDirection(tid, direction) {
  274. $("#" + tid).removeClass($("#" + tid).attr("direction")).addClass(direction).attr("direction", direction);
  275. }
  276. //判斷兩個元素是否碰撞
  277. function IsCollision(obja,objb)
  278. {
  279. var objaInfo = {width:$(obja).width(),height:$(obja).height(),left:$(obja).position().left,top:$(obja).position().top};
  280. var objbInfo = {width:$(objb).width(),height:$(objb).height(),left:$(objb).position().left,top:$(objb).position().top};
  281. if(objaInfo.left<objbInfo.left)
  282. {
  283. if(objaInfo.left+objaInfo.width<=objbInfo.left)
  284. {
  285. return false;
  286. }
  287. }else
  288. {
  289. if(objbInfo.left+objbInfo.width<=objaInfo.left)
  290. {
  291. return false;
  292. }
  293. }
  294. if(objaInfo.top<objbInfo.top)
  295. {
  296. if(objaInfo.top+objaInfo.height<=objbInfo.top)
  297. {
  298. return false;
  299. }
  300. }else
  301. {
  302. if(objbInfo.top+objbInfo.height<=objaInfo.top)
  303. {
  304. return false;
  305. }
  306. }
  307. return true;
  308. }
  309. function success()
  310. {
  311. clearInterval(MyInterval);
  312. alert("您獲勝了");
  313. }
  314. function Failure()
  315. {
  316. clearInterval(MyInterval);
  317. alert('結束了');
  318. }
  319. // --></mce:script>
  320. <mce:style type="text/css"><!--
  321. .map
  322. {
  323. width: 600px;
  324. height: 600px;
  325. border: 1px solid #000;
  326. float: left;
  327. position:absolute;
  328. top:0;
  329. left:0;
  330. }
  331. .bullet
  332. {
  333. width:10px;
  334. height:10px;
  335. background: #FF0;
  336. position:absolute;
  337. }
  338. .tank
  339. {
  340. width: 50px;
  341. height: 50px;
  342. position:absolute;
  343. }
  344. .enemy
  345. {
  346. background-color:Red;
  347. }
  348. .right
  349. {
  350. background-image: url(images/right.gif);
  351. }
  352. .left
  353. {
  354. background-image: url(images/left.gif);
  355. }
  356. .up
  357. {
  358. background-image: url(images/up.gif);
  359. }
  360. .down
  361. {
  362. background-image: url(images/down.gif);
  363. }
  364. --></mce:style><style type="text/css" mce_bogus="1"> .map
  365. {
  366. width: 600px;
  367. height: 600px;
  368. border: 1px solid #000;
  369. float: left;
  370. position:absolute;
  371. top:0;
  372. left:0;
  373. }
  374. .bullet
  375. {
  376. width:10px;
  377. height:10px;
  378. background: #FF0;
  379. position:absolute;
  380. }
  381. .tank
  382. {
  383. width: 50px;
  384. height: 50px;
  385. position:absolute;
  386. }
  387. .enemy
  388. {
  389. background-color:Red;
  390. }
  391. .right
  392. {
  393. background-image: url(images/right.gif);
  394. }
  395. .left
  396. {
  397. background-image: url(images/left.gif);
  398. }
  399. .up
  400. {
  401. background-image: url(images/up.gif);
  402. }
  403. .down
  404. {
  405. background-image: url(images/down.gif);
  406. }
  407. </style>
  408. </head>
  409. <body>
  410. </body>
  411. </html>

新版坦克大戰源碼,改進了運行不連貫性和增加了等級和血的信息

[xhtml] view plaincopyprint?

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>坦克大戰</title>
  6. <mce:script language="javascript" type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js" mce_src="http://code.jquery.com/jquery-1.4.2.min.js"></mce:script>
  7. <mce:script language="javascript" type="text/javascript"><!--
  8. $(function() {
  9. $("body").append('<div class="map"></div>');
  10. $("body").append('<div class="UserPanel">您共有<span id="Blood"></span>滴血<br /> 您當前的等級:<span id="Grade"></span><br />未消滅敵人個數:<span id="enemyNum"></span></div>');
  11. $("#Blood").html(Blood);
  12. $("#Grade").html(Grade);
  13. $("#enemyNum").html(enemyNum);
  14. InitTank('me', {x:(long-50)/2, y:high-50}, 'up');
  15. InitEnemy();
  16. //鍵盤點擊事件
  17. $(document).keydown(function(evt) {
  18. evtevt = evt || window.event;
  19. var key = evt.which || evt.keyCode;
  20. if(key==32)
  21. {
  22. SendBullet("me");
  23. }
  24. switch (key) {
  25. case 37: direction = "left"; break;
  26. case 38: direction = "up"; break;
  27. case 39: direction = "right"; break;
  28. case 40: direction = "down"; break;
  29. }
  30. if (key >= 37 && key <= 40) {
  31. ChangeDirection('me', direction);
  32. isMeMove = true;
  33. }
  34. });
  35. $(document).keyup(function(evt) {
  36. evtevt = evt || window.event;
  37. var key = evt.which || evt.keyCode;
  38. if (key >= 37 && key <= 40) {
  39. isMeMove = false;
  40. }
  41. });
  42. MyInterval=setInterval("Refresh()",timeSpan);
  43. });
  44. var isMeMove = false;//當前玩家坦克是否在走
  45. var moveLong = 10;//玩家坦克移動速度
  46. var enemymoveLong = 5;//敵人移動速度
  47. var bulletmoveLong = 20;//子彈移動速度
  48. var timeSpan = 300;//刷新頁面時間
  49. var long = 600;
  50. var high = 600;
  51. var enemymoveTime = 10;//敵人走多少次判斷距離坦克的方位和打子彈的頻率
  52. var enemyNum = 5;//剩余敵人個數
  53. var Blood = 3;//用戶的血量
  54. var MaxBlood = 5;//最大血量
  55. var Grade = 0;//等級
  56. function Refresh() {
  57. var me = $("#me");
  58. var mtop = $(me).position().top;
  59. var mleft = $(me).position().left;
  60. if (isMeMove) {
  61. direction = $(me).attr("direction");
  62. var offset = GetOffset(direction);
  63. mtop += offset.y*moveLong;
  64. mleft += offset.x*moveLong;
  65. if(mtop<0){
  66. mtop = 0;
  67. }else if(mtop>long-$(me).height())
  68. {
  69. mtop = long-$(me).height();
  70. }
  71. if(mleft<0){
  72. mleft = 0;
  73. }else if(mleft>long-$(me).width())
  74. {
  75. mleft = long-$(me).width();
  76. }
  77. $(me).stop().animate({'top':(mtop + 'px'),'left':(mleft + 'px')},timeSpan);
  78. }
  79. $(".tank:visible[enemy='enemy']").each(function(){
  80. var etop = $(this).position().top;
  81. var eleft = $(this).position().left;
  82. var bullettime = parseInt($(this).attr("bullettime"));
  83. if(bullettime<=0)
  84. {
  85. bullettime = enemymoveTime;
  86. var topSpan = etop-mtop;
  87. var leftSpan = eleft-mleft;
  88. ex = Math.abs(leftSpan)>Math.abs(topSpan)?leftSpan/Math.abs(leftSpan)*-1:0;
  89. ey = Math.abs(leftSpan)>Math.abs(topSpan)?0:topSpan/Math.abs(topSpan)*-1;
  90. etopetop = etop + ey*enemymoveLong;
  91. elefteleft = eleft + ex*enemymoveLong;
  92. var direction = GetDirection({x:ex,y:ey});
  93. ChangeDirection($(this).attr("id"),direction);
  94. SendBullet($(this).attr("id"));
  95. }else
  96. {
  97. direction = $(this).attr("direction");
  98. var offset = GetOffset(direction);
  99. etopetop = etop + offset.y*enemymoveLong;
  100. elefteleft = eleft + offset.x*enemymoveLong;
  101. bullettime--;
  102. }
  103. if(etop<0){
  104. etop = 0;
  105. }else if(etop>long-$(this).height())
  106. {
  107. etop = long-$(this).height();
  108. }
  109. if(eleft<0){
  110. eleft = 0;
  111. }else if(eleft>long-$(this).width())
  112. {
  113. eleft = long-$(this).width();
  114. }
  115. $(this).stop().animate({'top':(etop + 'px'),'left':(eleft + 'px')},timeSpan).attr("");
  116. $(this).attr("bullettime",bullettime);
  117. });
  118. $(".bullet:visible").each(function(){
  119. direction = $(this).attr("direction");
  120. var offset = GetOffset(direction);
  121. var top = $(this).position().top + offset.y*bulletmoveLong;
  122. var left = $(this).position().left + offset.x*bulletmoveLong;
  123. if(top<0){
  124. $(this).hide();
  125. return;
  126. }else if(top>long-$(this).height())
  127. {
  128. $(this).hide();
  129. return;
  130. }
  131. if(left<0){
  132. $(this).remove();
  133. return;
  134. }else if(left>long-$(this).width())
  135. {
  136. $(this).remove();
  137. return;
  138. }
  139. $(this).stop().animate({'top':(top + 'px'),'left':(left + 'px')},timeSpan);
  140. });
  141. CheckBullet();
  142. CheckTank();
  143. CheckBulletTank();
  144. }
  145. //判斷子彈相碰
  146. function CheckBullet()
  147. {
  148. $(".bullet[owner='me']:visible").each(function(){
  149. var me = this;
  150. $(".bullet[owner!='me']:visible").each(function(){
  151. if(IsCollision(me,this))
  152. {
  153. $(me).stop().hide();
  154. $(this).stop().hide();
  155. }
  156. });
  157. });
  158. }
  159. //判斷坦克相碰
  160. function CheckTank()
  161. {
  162. var me = $("#me");
  163. $(".tank").not("#me").each(function(){
  164. if(IsCollision(me,this))
  165. {
  166. Failure();
  167. }
  168. });
  169. }
  170. //判斷子彈打在坦克上
  171. function CheckBulletTank()
  172. {
  173. var me = $("#me");
  174. $(".bullet[owner!='me']:visible").each(function(){
  175. if(IsCollision(me,this))
  176. {
  177. $(this).stop().hide();
  178. FallBlood();
  179. }
  180. });
  181. $(".bullet[owner='me']:visible").each(function(){
  182. var fme = this;
  183. $(".tank").not("#me").each(function(){
  184. if(IsCollision(fme,this))
  185. {
  186. $(this).stop().hide();
  187. $(fme).stop().hide();
  188. InitEnemy();
  189. FallEnemy();
  190. Plusblood();
  191. }
  192. });
  193. });
  194. }
  195. //根據方向返回偏移量
  196. function GetOffset(direction)
  197. {
  198. ox = 0;
  199. oy = 0;
  200. if(direction=='left')
  201. {
  202. ox = -1;
  203. oy = 0;
  204. }else if(direction=='right')
  205. {
  206. ox = 1;
  207. oy = 0;
  208. }else if(direction=='up')
  209. {
  210. ox = 0;
  211. oy = -1;
  212. }else if(direction=='down')
  213. {
  214. ox = 0;
  215. oy = 1;
  216. }
  217. return {x:ox,y:oy};
  218. }
  219. //獲取方向
  220. function GetDirection(offset)
  221. {
  222. var direction = 'down';
  223. if(offset.x==-1 && offset.y==0)
  224. {
  225. direction = 'left' ;
  226. }else if(offset.x==1 && offset.y==0)
  227. {
  228. direction = 'right' ;
  229. }else if(offset.x==0 && offset.y==-1)
  230. {
  231. direction = 'up' ;
  232. }else if(offset.x==0 && offset.y==1)
  233. {
  234. direction = 'down' ;
  235. }
  236. return direction;
  237. }
  238. //發送炮彈
  239. function SendBullet(tid)
  240. {
  241. if($(".bullet[owner='" + tid + "']:visible").size()<2)
  242. {
  243. var x = $("#"+tid).position().left;
  244. var y = $("#"+tid).position().top;
  245. var direction = $("#"+tid).attr("direction");
  246. var offset = GetOffset(direction);
  247. var ox = x+offset.x*20+(offset.x==1?30:20);
  248. var oy = y+offset.y*20+(offset.y==1?30:20);
  249. if($(".bullet:hidden").size()==0)
  250. {
  251. $(".map").append($('<div class="bullet" style="left:' + ox + 'px;top:' + oy + 'px;" direction="' + direction + '" owner="' + tid + '"></div>'));
  252. }else
  253. {
  254. $(".bullet:hidden").eq(0).css({left: ox + 'px',top: oy + 'px'}).attr("direction",direction).attr("owner",tid).show();
  255. }
  256. }
  257. }
  258. //初始化敵人
  259. function InitEnemy()
  260. {
  261. if($("#enemy1:hidden").size()==0)
  262. {
  263. InitTank('enemy1', {x:(Math.round(Math.random()*3)-1)*((long-50)/2), y:0}, 'down');
  264. }else
  265. {
  266. $("#enemy1").css({left:(Math.round(Math.random()*3)-1)*((long-50)/2)+"px",top:0}).removeClass($("#enemy1").attr("direction")).addClass("down").attr("direction","down").show();
  267. }
  268. $("#enemy1").attr("enemy",'enemy').attr("bullettime","10");
  269. SendBullet("enemy1");
  270. }
  271. //初始化坦克
  272. //tid 坦克id
  273. //x橫坐標(left值)
  274. //y縱坐標(top值)
  275. //方向
  276. function InitTank(tid,position, direction) {
  277. x = position.x < 0 ? 0 : position.x;
  278. x = position.x > 600 ? 600 : position.x;
  279. y = position.y < 0 ? 0 : position.y;
  280. y = position.y > 600 ? 600 : position.y;
  281. $(".map").append('<div id="' + tid + '" style="left:' + position.x + 'px;top:' + position.y + 'px;" direction="' + direction + '" class="tank ' + direction + '"></div>');
  282. }
  283. //改變坦克的方向
  284. function ChangeDirection(tid, direction) {
  285. $("#" + tid).removeClass($("#" + tid).attr("direction")).addClass(direction).attr("direction", direction);
  286. }
  287. //判斷兩個元素是否碰撞
  288. function IsCollision(obja,objb)
  289. {
  290. var objaInfo = {width:$(obja).width(),height:$(obja).height(),left:$(obja).position().left,top:$(obja).position().top};
  291. var objbInfo = {width:$(objb).width(),height:$(objb).height(),left:$(objb).position().left,top:$(objb).position().top};
  292. if(objaInfo.left<objbInfo.left)
  293. {
  294. if(objaInfo.left+objaInfo.width<=objbInfo.left)
  295. {
  296. return false;
  297. }
  298. }else
  299. {
  300. if(objbInfo.left+objbInfo.width<=objaInfo.left)
  301. {
  302. return false;
  303. }
  304. }
  305. if(objaInfo.top<objbInfo.top)
  306. {
  307. if(objaInfo.top+objaInfo.height<=objbInfo.top)
  308. {
  309. return false;
  310. }
  311. }else
  312. {
  313. if(objbInfo.top+objbInfo.height<=objaInfo.top)
  314. {
  315. return false;
  316. }
  317. }
  318. return true;
  319. }
  320. //掉血
  321. function FallBlood()
  322. {
  323. Blood--;
  324. if(Blood<=0)
  325. {
  326. Failure();
  327. }
  328. $("#Blood").html(Blood);
  329. }
  330. //加血
  331. function Plusblood()
  332. {
  333. if(Blood<MaxBlood)
  334. {
  335. Blood++;
  336. }
  337. $("#Blood").html(Blood);
  338. }
  339. //升級
  340. function Upgrade()
  341. {
  342. Grade++;
  343. enemyNum = 5+Grade*2;//更新敵人個數
  344. enemymoveLong = ((10+Grade<20)?10+Grade:20);//更新敵人移動速度
  345. $("#Grade").html(Grade);
  346. }
  347. //減少敵人
  348. function FallEnemy()
  349. {
  350. if(enemyNum==0)
  351. {
  352. Upgrade();
  353. }else
  354. {
  355. enemyNum--;
  356. }
  357. $("#enemyNum").html(enemyNum);
  358. }
  359. //失敗
  360. function Failure()
  361. {
  362. clearInterval(MyInterval);
  363. alert('結束了');
  364. }
  365. // --></mce:script>
  366. <mce:style type="text/css"><!--
  367. .map
  368. {
  369. width: 600px;
  370. height: 600px;
  371. border: 1px solid #000;
  372. float: left;
  373. position:absolute;
  374. top:0;
  375. left:0;
  376. }
  377. .bullet
  378. {
  379. width:10px;
  380. height:10px;
  381. background: #FF0;
  382. position:absolute;
  383. }
  384. .tank
  385. {
  386. width: 50px;
  387. height: 50px;
  388. position:absolute;
  389. }
  390. .right
  391. {
  392. background-image: url(images/right.gif);
  393. }
  394. .left
  395. {
  396. background-image: url(images/left.gif);
  397. }
  398. .up
  399. {
  400. background-image: url(images/up.gif);
  401. }
  402. .down
  403. {
  404. background-image: url(images/down.gif);
  405. }
  406. .UserPanel{
  407. position:absolute;
  408. width:200px;
  409. height:300px;
  410. left:600px;
  411. top:0px;
  412. border:1px solid #fff000;
  413. }
  414. --></mce:style><style type="text/css" mce_bogus="1"> .map
  415. {
  416. width: 600px;
  417. height: 600px;
  418. border: 1px solid #000;
  419. float: left;
  420. position:absolute;
  421. top:0;
  422. left:0;
  423. }
  424. .bullet
  425. {
  426. width:10px;
  427. height:10px;
  428. background: #FF0;
  429. position:absolute;
  430. }
  431. .tank
  432. {
  433. width: 50px;
  434. height: 50px;
  435. position:absolute;
  436. }
  437. .right
  438. {
  439. background-image: url(images/right.gif);
  440. }
  441. .left
  442. {
  443. background-image: url(images/left.gif);
  444. }
  445. .up
  446. {
  447. background-image: url(images/up.gif);
  448. }
  449. .down
  450. {
  451. background-image: url(images/down.gif);
  452. }
  453. .UserPanel{
  454. position:absolute;
  455. width:200px;
  456. height:300px;
  457. left:600px;
  458. top:0px;
  459. border:1px solid #fff000;
  460. }
  461. </style>
  462. </head>
  463. <body>
  464. </body>
  465. </html>
Copyright © Linux教程網 All Rights Reserved