设为首页收藏本站订阅更新

无忧脚本

 找回密码
 加入无忧

QQ登录

只需一步,快速开始

楼主: dewin

js做游戏 --- 推箱子 [复制链接]

Rank: 1

升级  94%

注册时间
2003-4-11
在线时间
0 小时
威望
38
阅读权限
10
积分
47
帖子
18
精华
0
UID
1852
发表于 2006-8-27 19:56:40 |显示全部楼层 |串个门|加好友|打招呼|发消息 |
查看详细资料
  1. <!--
  2. Please use "EditPlus" softeare to view this code.
  3. Start   2002-10-02 17:50
  4. Finish  2002-10-15 22:41
  5. Last Edit 2003-06-11 18:55

  6. Copyright(c) 1998-2003 dewin all rights reserved


  7. Power By Dewin
  8. -->
  9. <body>
  10. <style>
  11. .Ground{}
  12. .Floor{}
  13. .Wall{border:2px outset #ADFF2F;background-color:#888888;z-index:0}
  14. .Aim{background-color:#FF69B4;filter:Alpha(opacity=100,FinishOpacity=0,Style=3);z-index:0}
  15. .Box{background-color:#87CEFA;filter:Alpha(opacity=0,FinishOpacity=100,Style=3);z-index:1}
  16. .Man{background-color:red;z-index:1;filter:filpv}
  17. </style>
  18. <textarea id=Info rows=20>rddlllllluurrrDulllddrrRRRRuulDrdLuLDDDldRdrUluuulllluurrrDulllddrrRurDDDlddrruLuuuulullDldRRRurDDDDrddlluRuuuuluulDldRRurDDDDDldRuuuuuurrurrddLLLulDDDDuuuurrurDrdLLLulDDDlDR</textarea><br>
  19. <input type=button value='copy' onclick='Info.select();document.execCommand("Copy")'>
  20. <input type=button value='paste' onclick='Info.focus(),document.execCommand("Paste")'>
  21. <input type=button value='Auto Run' onclick='Run()'><br><br>

  22. Boxes has Moved<br><input id=BoxMoveInfo readonly> Steps<br>
  23. Man has Moved<br><input id=ManMoveInfo readonly> Steps

  24. <span id=Base style='position:absolute;overflow:hidden'></span>
  25. <script>

  26. function init(){
  27. MainMap = [];

  28. RePlayTime = 10 //million second
  29. MapW = MapH = 30;
  30. PushScrollNo = 30;
  31. BoxCompletes = 0;
  32. TotalBox = 0;

  33. Timer = null;
  34. BoxMoves = 0;
  35. Count = -1;
  36. BackRecord = [];
  37. BoxMoveInfo.value = 0;
  38. ManMoveInfo.value = 0;
  39. }

  40. /*
  41. W = Wall
  42. 0 = Box
  43. @ = Box on Aim
  44. * = Man
  45. # = Man on Aim
  46. . = Aim
  47.   = Space
  48. */



  49. Map = [];
  50. Map[0] = [
  51. 'WWWWWWWWWWW',
  52. 'W    W*   W',
  53. 'W 00000 0 W',
  54. 'W         W',
  55. 'WWWWW WWWWW',
  56. '   W  .W   ',
  57. '   W  .W   ',
  58. '   W...W   ',
  59. '   W  .W   ',
  60. '   WWWWW   '
  61. ]


  62. function ReadMap(k){
  63. init();
  64. Base.innerHTML = '';
  65. var w=Map[k][0].length*MapW
  66. var h=Map[k].length*MapW
  67. Base.style.width = w;
  68. Base.style.height = h;
  69. Base.style.left = (w>document.body.clientWidth)?0:(document.body.offsetWidth-w)/2;
  70. Base.style.top = 10;
  71. for(var y=0;y<Map[k].length;y++){
  72.         MainMap[y] = [];
  73.         for(var x=0;x<Map[k][y].length;x++){
  74.                 MainMap[y][x] = Map[k][y].charAt(x);
  75.                 if(MainMap[y][x]=='W')iHtml(x,y,'Wall');
  76.                 else{
  77.                         //if(MainMap[y][x]=="|"){iHtml(x,y,'Floor');MainMap[y][x]=' '}
  78.                         //else iHtml(x,y,'Ground');
  79.                         iHtml(x,y,'Ground');
  80.                         switch(MainMap[y][x]){
  81.                                 case '0':iBox(x,y,0);break;
  82.                                 case '.':iHtml(x,y,'Aim');break;
  83.                                 case '@':iHtml(x,y,'Aim');iBox(x,y,1);BoxCompletes++;break;
  84.                                 case '*':iMan(x,y,0);break;
  85.                                 case '#':iHtml(x,y,'Aim');iMan(x,y,1);break;
  86.                                 }
  87.                         }
  88.                 }
  89.         }
  90. }

  91. function iHtml(x,y,k){
  92. Base.insertAdjacentHTML("beforeEnd","<span style='position:absolute;left:"+x*MapW+";top:"+y*MapH+";width:"+MapW+";height:"+MapH+"' class='"+k+"' x="+x+" y="+y+">");
  93. }



  94. function iBox(x,y,k){
  95. MainMap[y][x] = Base.appendChild(document.createElement("<span style='position:absolute;left:"+x*MapW+";top:"+y*MapH+";width:"+MapW+";height:"+MapH+"' class='Box' complete='"+k+"'>"));
  96. TotalBox++;
  97. }

  98. function iMan(x,y,k){
  99. Man = Base.appendChild(document.createElement("<img alt='Man' style='position:absolute;left:"+x*MapW+";top:"+y*MapH+";width:"+MapW+";height:"+MapH+";' x="+x+" y="+y+" class='Man'>"));
  100. MainMap[y][x] = (k==0)?' ':'.';
  101. }

  102. function Dir(x,y,k){
  103. var ManFront = MainMap[y+parseInt(Man.y)][x+parseInt(Man.x)];
  104. if(ManFront==' ' || ManFront=='.')if(k!=null)Move1(x,y,k.toLowerCase());else Move1(x,y);
  105. else{
  106.         if(typeof(ManFront)=='object'){
  107.                 var ManFrontFront = MainMap[2*y+parseInt(Man.y)][2*x+parseInt(Man.x)];
  108.                 if(ManFrontFront == ' '){if(k!=null)Move1(x,y,k.toUpperCase());else Move1(x,y);Move2(ManFront,x,y);}
  109.                 else if(ManFrontFront == '.'){if(k!=null)Move1(x,y,k.toUpperCase());else Move1(x,y);Move3(ManFront,x,y);}
  110.                 BoxMoves++
  111.                 }
  112.         }
  113. }







  114. function Move1(x,y,k){
  115. if(k != null){
  116.         BackRecord[++Count] = k
  117.         BackRecord.length = Count+1
  118.         }
  119. Man.x = x+parseInt(Man.x);
  120. Man.y = y+parseInt(Man.y);
  121. Man.style.left = Man.x*MapW;
  122. Man.style.top = Man.y*MapH;
  123. }






  124. function Move2(obj,x,y){
  125. obj.style.left = (x+parseInt(Man.x))*MapW;//parseInt
  126. obj.style.top = (y+eval(Man.y))*MapH;//eval
  127. MainMap[y+parseInt(Man.y)][x+parseInt(Man.x)] = obj;
  128. if(obj.complete == 0) MainMap[Man.y][Man.x]=' ';
  129. else{
  130.         MainMap[Man.y][Man.x] = '.';
  131.         BoxCompletes --;
  132.         }
  133. obj.complete = 0;
  134. }

  135. function Move3(obj,x,y){
  136. obj.style.left = (x-(-Man.x))*MapW;// (- -) = +
  137. obj.style.top = (y+parseFloat(Man.y))*MapH;//parseFloat
  138. MainMap[y+parseInt(Man.y)][x+parseInt(Man.x)] = obj;
  139. if(obj.complete == 1) MainMap[Man.y][Man.x]='.';
  140. else{
  141.         MainMap[Man.y][Man.x] = ' ';
  142.         if(++BoxCompletes == TotalBox) oWin();
  143.         }
  144. obj.complete = 1;
  145. }


  146. function UnDo(){
  147. if(Count >= 0){
  148.         switch(BackRecord[Count]){
  149.                 //not object
  150.                 case 'l':Move1(1,0);break;//left -> right
  151.                 case 'u':Move1(0,1);break;//up -> down
  152.                 case 'r':Move1(-1,0);break;//right -> left
  153.                 case 'd':Move1(0,-1);break;//down -> up
  154.                 //object
  155.                 case 'L':UnGo(1,0);break;//left -> right
  156.                 case 'U':UnGo(0,1);break;//up -> down
  157.                 case 'R':UnGo(-1,0);break;//right -> left
  158.                 case 'D':UnGo(0,-1);break;//down -> up
  159.                 }
  160.         iSelects(--Count)
  161.         }
  162. }


  163. function UnGo(x,y){
  164. BoxMoves--;
  165. var obj = MainMap[-y+parseInt(Man.y)][-x+parseInt(Man.x)];
  166. if(MainMap[Man.y][Man.x]==' '){
  167.         if(obj.complete == 0)MainMap[-y+parseInt(Man.y)][-x+parseInt(Man.x)]=' ';
  168.         else{
  169.                 MainMap[-y+parseInt(Man.y)][-x+parseInt(Man.x)]='.';
  170.                 BoxCompletes --;
  171.                 }
  172.         obj.complete=0
  173.         }
  174. else{
  175.         if(obj.complete == 0){
  176.                 MainMap[-y+parseInt(Man.y)][-x+parseInt(Man.x)]=' ';
  177.                 if(++BoxCompletes == TotalBox) oWin();
  178.                 }
  179.         else MainMap[-y+parseInt(Man.y)][-x+parseInt(Man.x)]='.';
  180.         obj.complete=1
  181.         }
  182. obj.style.left = Man.x*MapW;
  183. obj.style.top = Man.y*MapH;
  184. MainMap[Man.y][Man.x]=obj;
  185. Move1(x,y);
  186. }


  187. function ReDo(){
  188. if(Count+1<BackRecord.length){
  189.         switch(BackRecord[++Count]){
  190.                 case 'l':Dir(-1,0);break;//left
  191.                 case 'u':Dir(0,-1);break;//up
  192.                 case 'r':Dir(1,0);break;//right
  193.                 case 'd':Dir(0,1);break;//down
  194.                 case 'L':Dir(-1,0);break;//left
  195.                 case 'U':Dir(0,-1);break;//up
  196.                 case 'R':Dir(1,0);break;//right
  197.                 case 'D':Dir(0,1);break;//down
  198.                 }
  199.         iSelects(Count)
  200.         }
  201. else clearInterval(Timer);
  202. }




  203. function oWin(){
  204. alert('Congratulation! You have pass.')
  205. }






  206. function window.onload(){
  207. ReadMap(0);
  208. document.body.scroll='no'

  209. function document.onkeydown(){
  210.         if(event.ctrlKey){
  211.         switch(event.keyCode){
  212.                 case 37:document.body.scrollLeft-=PushScrollNo;break;//left
  213.                 case 38:document.body.scrollTop-=PushScrollNo;break;//up
  214.                 case 39:document.body.scrollLeft+=PushScrollNo;break;//right
  215.                 case 40:document.body.scrollTop+=PushScrollNo;break;//down
  216.                 }
  217.                 }
  218.         else{
  219.         switch(event.keyCode){
  220.                 case 37:Dir(-1,0,'l');break;//left
  221.                 case 38:Dir(0,-1,'u');break;//up
  222.                 case 39:Dir(1,0,'r');break;//right
  223.                 case 40:Dir(0,1,'d');break;//down
  224.                 }
  225.         event.returnValue = false;
  226.         Info.value = BackRecord.join("")
  227.         iSelects(Count)
  228.         }
  229. }

  230. function document.onclick(){ReDo()}
  231. function document.oncontextmenu(){UnDo();event.returnValue = false;}

  232. window.focus();
  233. Base.focus();
  234. }





  235. function Run(){
  236. var temp = Info.value;
  237. if(temp == '')return;
  238. ReadMap(0);
  239. BackRecord=temp.split("")
  240. Timer = setInterval(ReDo,RePlayTime)
  241. }

  242. function iSelects(x){
  243. var iRange = Info.createTextRange()
  244. iRange.collapse(true)
  245. iRange.moveStart("character",x)
  246. iRange.moveEnd("character",1)
  247. iRange.select()
  248. BoxMoveInfo.value = BoxMoves;
  249. ManMoveInfo.value = x+1;
  250. }
  251. </script>
复制代码运行代码另存代码

[ 本帖由 xjj2000 最后编辑于 2006-8-27 20:00 ]

使用道具 举报

Rank: 1

升级  18%

注册时间
2003-7-5
在线时间
2 小时
威望
4
阅读权限
10
积分
9
帖子
5
精华
0
UID
4153
发表于 2008-1-15 03:06:27 |显示全部楼层 |串个门|加好友|打招呼|发消息 |
查看详细资料
很不错的技术

使用道具 举报

您需要登录后才可以回帖 登录 | 加入无忧

Archiver|手机版|无忧脚本 ( 苏ICP备05080427号 )|值班电话:027-62300445  

GMT+8, 2012-5-17 23:19 , Processed in 0.072397 second(s), 7 queries , Gzip On, Memcache On.

Powered by Discuz! X2

© 1999-2011 无忧脚本

回顶部