asimryu

javascript 축구 게임 1(랜덤,자동찾기)

Sep 29th, 2014
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>coding game 1</title>
  6. <style>
  7. #buttons {}
  8. #buttons div {width:20px; height:20px; font-family:Arial;font-size:15px;background:#000;color:#fff;line-height:20px;text-align:center;display:inline-block;}
  9. #buttons div:hover {background:#00f;color:#ff0;cursor:pointer;}
  10. #box {width:500px;height:500px;border:1px solid #000;position:relative;background:url(http://webskills.kr/images/grid.png);}
  11. #post, #ball {width:50px;height:50px;position:absolute;}
  12. #post { background:url(http://webskills.kr/images/post.jpg);}
  13. #ball { background:url(http://webskills.kr/images/ball.jpg);}
  14. </style>
  15. </head>
  16. <body>
  17. <div id="buttons">
  18. <div onclick="goLeft();">←</div>
  19. <div onclick="goRight();">→</div>
  20. <div onclick="goUp();">↑</div>
  21. <div onclick="goDown();">↓</div>
  22. <div onclick="goAuto();" title="자동으로">A</div>
  23. <div onclick="checkGoal();" title="골체크">C</div>
  24. <div onclick="goRefresh();" title="새로시작">R</div>
  25. </div>
  26. <div id="box">
  27. <div id="post"></div>
  28. <div id="ball"></div>
  29. </div>
  30. <script src="http://code.jquery.com/jquery-1.11.1.js"></script>
  31. <script>
  32. var bx = 0;
  33. var by = 0;
  34. var px = 0;
  35. var py = 0;
  36. function goLeft(){
  37. bx = bx - 50;
  38. if(bx < 0) bx = 0;
  39. $("#ball").animate({left:bx,top:by});
  40. checkGoal();
  41. }
  42. function goRight(){
  43. bx = bx + 50;
  44. if(bx > 450) bx = 450;
  45. $("#ball").animate({left:bx,top:by});
  46. checkGoal();
  47. }
  48. function goUp(){
  49. by = by - 50;
  50. if(by < 0) by = 0;
  51. $("#ball").animate({left:bx,top:by});
  52. checkGoal();
  53. }
  54. function goDown(){
  55. by = by + 50;
  56. if(by > 450) by = 450;
  57. $("#ball").animate({left:bx,top:by});
  58. checkGoal();
  59. }
  60. function goRefresh(){
  61. bx = (Math.floor((Math.random() * 9) + 1)) * 50;
  62. by = (Math.floor((Math.random() * 9) + 1)) * 50;
  63. px = (Math.floor((Math.random() * 9) + 1)) * 50;
  64. py = (Math.floor((Math.random() * 9) + 1)) * 50;
  65. $("#ball").css({left:bx,top:by});
  66. $("#post").css({left:px,top:py});
  67. }
  68. function checkGoal(){
  69. if( bx == px && by == py ) {
  70. alert("Goooooooooal!!!");
  71. }
  72. }
  73. function goAuto_old(){
  74. goRefresh();
  75. for(var i=1;i<=4;i++){
  76. goRight();
  77. goDown();
  78. }
  79. }
  80. function goAuto(){
  81. var goal = false;
  82. while(goal == false){
  83. if(bx < px) {
  84. bx = bx + 50;
  85. $("#ball").animate({left:bx,top:by});
  86. }
  87. if(bx > px) {
  88. bx = bx - 50;
  89. $("#ball").animate({left:bx,top:by});
  90. }
  91. if(by < py) {
  92. by = by + 50;
  93. $("#ball").animate({left:bx,top:by});
  94. }
  95. if(by > py) {
  96. by = by - 50;
  97. $("#ball").animate({left:bx,top:by});
  98. }
  99. if(bx == px && by == py) {
  100. goal = true;
  101. }
  102. }
  103. //if( goal == true ) alert("Goooooooooal!!!");
  104. }
  105. goRefresh();
  106. </script>
  107. </body>
  108. </html>
Advertisement
Add Comment
Please, Sign In to add comment