jquery.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. <html>
  2. <head>
  3. <script type="text/javascript" src="./js/jquery-1.6.4.min.js"></script>
  4. <style>
  5. body{
  6. margin:0;
  7. padding: 0;
  8. }
  9. .tank{
  10. width:50px;
  11. height:50px;
  12. position: absolute;
  13. border-radius: 10%;
  14. }
  15. #tank_1{
  16. background-color: deepskyblue;
  17. left:5px;
  18. top:5px;
  19. }
  20. #tank_2{
  21. background-color: navy;
  22. left:5px;
  23. top:450px;
  24. }
  25. .Circle{
  26. width:25px;
  27. height:25px;
  28. border-radius: 50%;
  29. background-color: #ddd;
  30. position: absolute;
  31. left:12px;
  32. top:12px;
  33. }
  34. .gun{
  35. width:35px;
  36. height:6px;
  37. background-color: #ddd;
  38. position: absolute;
  39. left:25px;
  40. top:22px;
  41. z-index: 10;
  42. }
  43. .bullet{
  44. width:6px;
  45. height:6px;
  46. border-radius: 50%;
  47. background-color: #000;
  48. z-index: 0;
  49. position: absolute;
  50. border-top-right-radius: ;
  51. left:25px;
  52. top:22px;
  53. transform: rotate(0deg);
  54. }
  55. .wall{
  56. background-color: blue;
  57. position: absolute;
  58. border-radius: 10px;
  59. color:#fff;
  60. font-size:40px;
  61. text-align: center;
  62. }
  63. </style>
  64. <script type="text/javascript">
  65. var arrWall=[
  66. [{x:0,y:0},{x:5,y:505},0,""],
  67. [{x:0,y:0},{x:505,y:5},0,""],
  68. [{x:500,y:0},{x:505,y:505},0,""],
  69. [{x:0,y:500},{x:500,y:505},0,""],
  70. [{x:400,y:100},{x:455,y:155},1,"10"],
  71. [{x:400,y:200},{x:455,y:255},1,"80"],
  72. [{x:400,y:300},{x:455,y:355},1,"50"],
  73. ];
  74. var isMove=true;
  75. $(document).ready(function(){
  76. buildWall();
  77. });
  78. $(document).keydown(function(event){
  79. console.log(event.keyCode);
  80. switch (event.keyCode) {
  81. case 37:
  82. case 38:
  83. case 39:
  84. case 40:
  85. move($("#tank_1"), event.keyCode, 50);
  86. break;
  87. case 32:
  88. gun($("#tank_1"),null,800);
  89. break;
  90. }
  91. switch (event.keyCode) {
  92. case 65:
  93. case 87:
  94. case 68:
  95. case 83:
  96. move($("#tank_2"), event.keyCode, 50);
  97. break;
  98. case 81:
  99. gun($("#tank_2"),null,800);
  100. break;
  101. }
  102. });
  103. function move(obj,direction,step){
  104. var directionStr="";
  105. var pointSource={
  106. x:Number($(obj).css("left").replace("px","")),
  107. y:Number($(obj).css("top").replace("px","")),
  108. };
  109. var pointTarget={
  110. x:pointSource.x,
  111. y:pointSource.y,
  112. direction:null,
  113. }
  114. switch (direction){
  115. case 37:
  116. case 65:
  117. $(obj).css("transform",'rotate(180deg)');
  118. pointTarget.x=pointSource.x-step;
  119. pointTarget.direction="left";
  120. break;
  121. case 38:
  122. case 87:
  123. $(obj).css("transform",'rotate(-90deg)');
  124. pointTarget.y=pointSource.y-step;
  125. pointTarget.direction="up";
  126. break;
  127. case 39:
  128. case 68:
  129. $(obj).css("transform",'rotate(0deg)');
  130. pointSource.x+=$(obj).width();
  131. //pointSource.y+=$(obj).height();
  132. pointTarget.x=pointSource.x+step;
  133. pointTarget.direction="right";
  134. break;
  135. case 40:
  136. case 83:
  137. $(obj).css("transform",'rotate(90deg)');
  138. //pointSource.x+=$(obj).width();
  139. pointSource.y+=$(obj).height();
  140. pointTarget.y=pointSource.y+step;
  141. pointTarget.direction="down";
  142. break;
  143. }
  144. console.log("pointSource.x:"+pointSource.x);
  145. console.log("pointSource.y:"+pointSource.y);
  146. console.log("pointTarget.x:"+pointTarget.x);
  147. console.log("pointTarget.y:"+pointTarget.y);
  148. pointTarget=checkWall(pointSource,pointTarget);
  149. if (pointTarget.direction=="right") {
  150. pointTarget.x -= $(obj).width();
  151. //pointTarget.y -= $(obj).height();
  152. }
  153. else if (pointTarget.direction=="down") {
  154. //pointTarget.x -= $(obj).width();
  155. pointTarget.y -= $(obj).height();
  156. }
  157. console.log("pointTarget2.x:"+pointTarget.x);
  158. console.log("pointTarget2.y:"+pointTarget.y);
  159. //console.log("isMove:"+isMove);
  160. if (isMove) {
  161. isMove=false;
  162. //console.log("isMove2:"+isMove);
  163. $(obj).stop();
  164. $(obj).animate({"left": pointTarget.x + "px", "top": pointTarget.y + "px"}, 400, null, function () {
  165. //console.log("isMove3:"+isMove);
  166. isMove = true;
  167. });
  168. }
  169. }
  170. function checkWall(pointSource,pointTarget){
  171. var result=pointTarget;
  172. if (pointSource.x>pointTarget.x && pointSource.y==pointTarget.y){
  173. for(var x=pointSource.x;x>=pointTarget.x;x--){
  174. var r=getPoint(x,pointSource.y,pointTarget.direction);
  175. if (r.update)
  176. return r;
  177. }
  178. }
  179. if (pointSource.x==pointTarget.x && pointSource.y>pointTarget.y){
  180. for(var y=pointSource.y;y>=pointTarget.y;y--){
  181. var r=getPoint(pointSource.x,y,pointTarget.direction);
  182. if (r.update)
  183. return r;
  184. }
  185. }
  186. if (pointSource.x<=pointTarget.x && pointSource.y==pointTarget.y){
  187. for(var x=pointSource.x;x<=pointTarget.x;x++){
  188. var r=getPoint(x,pointSource.y,pointTarget.direction);
  189. if (r.update)
  190. return r;
  191. }
  192. }
  193. if (pointSource.x==pointTarget.x && pointSource.y<=pointTarget.y){
  194. for(var y=pointSource.y;y<=pointTarget.y;y++){
  195. var r=getPoint(pointSource.x,y,pointTarget.direction);
  196. if (r.update)
  197. return r;
  198. }
  199. }
  200. return result;
  201. function getPoint(x,y,direction){
  202. var result={
  203. update:false,
  204. direction:direction,
  205. };
  206. for(var i=0;i<arrWall.length;i++){
  207. if (x>arrWall[i][0].x && x<arrWall[i][1].x && y>arrWall[i][0].y && y<arrWall[i][1].y){
  208. if (direction=="left") {
  209. result.update=true;
  210. result.x=arrWall[i][1].x;
  211. }
  212. else if (direction=="right") {
  213. result.update=true;
  214. result.x=arrWall[i][0].x;
  215. }
  216. if (direction=="up") {
  217. result.update=true;
  218. result.y=arrWall[i][1].y;
  219. }
  220. else if (direction=="down") {
  221. result.update=true;
  222. result.y=arrWall[i][0].y;
  223. }
  224. if (!result.x)
  225. result.x=x;
  226. if (!result.y)
  227. result.y=y;
  228. result.isDisappear=arrWall[i][2];
  229. result.WallObjectIndex=i;
  230. break;
  231. }
  232. }
  233. return result;
  234. }
  235. }
  236. function gun(obj,direction,step) {
  237. var pointSource = {
  238. x: (Number($(obj).css("left").replace("px", "")) + 24),
  239. y: (Number($(obj).css("top").replace("px", "")) + 22),
  240. };
  241. var pointTarget = {
  242. x: pointSource.x,
  243. y: pointSource.y,
  244. direction: null,
  245. }
  246. var bulletID = new Date().getTime();
  247. $(document.body).prepend("<div id=" + bulletID + " class='bullet'></div>");
  248. $("#" + bulletID).css("left", pointSource.x + "px");
  249. $("#" + bulletID).css("top", pointSource.y + "px");
  250. if ($(obj).css("transform") == "matrix(1, 0, 0, 1, 0, 0)") {
  251. pointTarget.x += step;
  252. pointTarget.direction = "right";
  253. }
  254. else if ($(obj).css("transform") == "matrix(6.12323e-17, 1, -1, 6.12323e-17, 0, 0)") {
  255. pointTarget.y += step;
  256. pointTarget.direction = "down";
  257. }
  258. else if ($(obj).css("transform") == "matrix(-1, 1.22465e-16, -1.22465e-16, -1, 0, 0)") {
  259. pointTarget.x -= step;
  260. pointTarget.direction = "left";
  261. }
  262. else if ($(obj).css("transform") == "matrix(6.12323e-17, -1, 1, 6.12323e-17, 0, 0)") {
  263. pointTarget.y -= step;
  264. pointTarget.direction = "up";
  265. }
  266. pointTarget = checkWall(pointSource, pointTarget);
  267. if (pointTarget.direction == "right")
  268. pointTarget.x -= $("#" + bulletID).width();
  269. else if (pointTarget.direction == "down")
  270. pointTarget.y -= $("#" + bulletID).height();
  271. $("#" + bulletID).animate({"left": pointTarget.x + "px", "top": pointTarget.y + 'px'}, 'fast',null,function(){
  272. $("#" + bulletID).animate({
  273. "opacity": '0',
  274. "width": '30px',
  275. "height": '30px',
  276. "left": '-=10px',
  277. "top": '-=10px'
  278. },
  279. '100', '', function () {
  280. $(this).remove();
  281. });
  282. if (pointTarget.isDisappear == 1) {
  283. if (Number(arrWall[pointTarget.WallObjectIndex][3])>1){
  284. arrWall[pointTarget.WallObjectIndex][3]=Number(arrWall[pointTarget.WallObjectIndex][3])-1;
  285. $("#wall_" + pointTarget.WallObjectIndex).text(arrWall[pointTarget.WallObjectIndex][3]);
  286. }
  287. else {
  288. $("#wall_" + pointTarget.WallObjectIndex).animate({
  289. "opacity": '0',
  290. "width": '100px',
  291. "height": '100px',
  292. "left": '-=20px',
  293. "top": '-=20px'
  294. }, 100, null, function () {
  295. $(this).remove();
  296. arrWall[pointTarget.WallObjectIndex][0].x = -1;
  297. arrWall[pointTarget.WallObjectIndex][0].y = -1;
  298. arrWall[pointTarget.WallObjectIndex][1].x = -1;
  299. arrWall[pointTarget.WallObjectIndex][1].y = -1;
  300. });
  301. }
  302. }
  303. });
  304. }
  305. function buildWall(){
  306. for(var i=0;i<arrWall.length;i++){
  307. var width=arrWall[i][1].x-arrWall[i][0].x;
  308. var height=arrWall[i][1].y-arrWall[i][0].y;
  309. $(document.body).append("<div id='wall_"+i+"' class='wall' style='left:"+arrWall[i][0].x+"px;top:"+arrWall[i][0].y+"px;width:"+width+"px;height:"+height+"px;'>"+arrWall[i][3]+"</div>")
  310. }
  311. }
  312. </script>
  313. </head>
  314. <body>
  315. <div id="tank_1" class="tank">
  316. <div class="Circle"></div>
  317. <div class="gun"></div>
  318. </div>
  319. <div id="tank_2" class="tank">
  320. <div class="Circle"></div>
  321. <div class="gun"></div>
  322. </div>
  323. </body>
  324. </html>