Guest User

Untitled

a guest
Nov 17th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.49 KB | None | 0 0
  1. //code inside $(function(){} will run after the DOM is loaded and ready
  2. var KEY = {
  3. UP: 38,
  4. DOWN: 40,
  5. W: 87,
  6. S: 83
  7. }
  8.  
  9. var pingpong = {
  10. scoreA : 0, //score for player A
  11. scoreB : 0 //score for player B
  12. }
  13. pingpong.pressedKeys = [];
  14. pingpong.ball = {
  15. speed: 5,
  16. x: 150,
  17. y: 100,
  18. directionX: 1,
  19. directionY: 1
  20. }
  21.  
  22. var WINNER = 3;
  23.  
  24. $(function(){
  25. //set interval to call gameloop every 30 milliseconds
  26. pingpong.timer = setInterval(gameloop,30);
  27.  
  28. //mark down what key is down and up into an array called "pressedKeys"
  29. $(document).keydown(function(e){
  30. pingpong.pressedKeys[e.which] = true;
  31. });
  32. $(document).keyup(function(e){
  33. pingpong.pressedKeys[e.which] = false;
  34. });
  35. });
  36.  
  37. function gameloop() {
  38. if (pingpong.scoreA < WINNER && pingpong.scoreB < WINNER) {
  39. moveBall();
  40. movePaddles();
  41. }
  42. else {
  43. endGame();
  44. }
  45. }
  46.  
  47. function moveBall() {
  48. //reference useful variables
  49. var playgroundHeight = parseInt($("#playground").height());
  50. var playgroundWidth = parseInt($("#playground").width());
  51. var ball = pingpong.ball;
  52.  
  53. //check playground boundary
  54. //check bottom edge
  55. if (ball.y + ball.speed*ball.directionY > playgroundHeight) {
  56. ball.directionY = -1;
  57. }
  58.  
  59. //check top edge
  60. if (ball.y + ball.speed*ball.directionY < 0) {
  61. ball.directionY = 1;
  62. }
  63.  
  64. //check right edge
  65. if (ball.x + ball.speed*ball.directionX > playgroundWidth) {
  66. ball.directionX = -1;
  67. }
  68.  
  69. //check left edge
  70. if (ball.x + ball.speed*ball.directionX < 0) {
  71. ball.directionX = 1;
  72. }
  73.  
  74. ball.x += ball.speed*ball.directionX;
  75. ball.y += ball.speed*ball.directionY;
  76.  
  77. //check moving paddle here
  78. //check left paddle
  79. var paddleAX = parseInt($("#paddleA").css("left"))+parseInt($("#paddleA").css("width"));
  80. var paddleAYBottom = parseInt($("#paddleA").css("top"))+parseInt($("#paddleA").css("height"));
  81. var paddleAYTop = parseInt($("#paddleA").css("top"));
  82.  
  83. if (ball.x + ball.speed*ball.directionX < paddleAX) {
  84. if ((ball.y + ball.speed*ball.directionY <= paddleAYBottom) && (ball.y + ball.speed*ball.directionY >= paddleAYTop))
  85. {
  86. ball.directionX = 1;
  87. }
  88. }
  89.  
  90. //check right paddle
  91. var paddleBX = parseInt($("#paddleB").css("left"))-parseInt($("#ball").css("width"));
  92. var paddleBYBottom = parseInt($("#paddleB").css("top"))+parseInt($("#paddleB").css("height"));
  93. var paddleBYTop = parseInt($("#paddleB").css("top"));
  94.  
  95. if (ball.x + ball.speed*ball.directionX >= paddleBX) {
  96. if ((ball.y + ball.speed*ball.directionY <= paddleBYBottom) && (ball.y + ball.speed*ball.directionY >= paddleBYTop))
  97. {
  98. ball.directionX = -1;
  99. }
  100. }
  101.  
  102. //check right edge
  103. if (ball.x + ball.speed*ball.directionX > playgroundWidth) {
  104. //player B lost; reset ball.
  105. pingpong.scoreA++;
  106. $("#scoreA").html(pingpong.scoreA);
  107. ball.x = 250;
  108. ball.y = 100;
  109. $("#ball").css({
  110. "left" : ball.x,
  111. "top" : ball.y
  112. });
  113. ball.directionX = -1;
  114. }
  115.  
  116. //check left edge
  117. if (ball.x + ball.speed*ball.directionX < 0) {
  118. //player A lost; reset ball.
  119. pingpong.scoreB++;
  120. $("#scoreB").html(pingpong.scoreB);
  121. ball.x = 150;
  122. ball.y = 100;
  123. $("#ball").css({
  124. "left" : ball.x,
  125. "top" : ball.y
  126. });
  127. ball.directionX = 1;
  128. }
  129.  
  130. //actually move ball with speed and direction
  131. $("#ball").css({
  132. "left" : ball.x,
  133. "top" : ball.y
  134. });
  135. }
  136. function movePaddles(){
  137. //use custom timer to continuously check if a key is pressed
  138. var playgroundHeight = parseInt($("#playground").height()); //added for edge check
  139.  
  140. if (pingpong.pressedKeys[KEY.UP]) { //arrow-up
  141. //move paddle B up 5 pixels
  142. var top = parseInt($("#paddleB").css("top"));
  143. if (top > 0) { //edge check
  144. $("#paddleB").css("top",top-5);
  145. }
  146. }
  147.  
  148. if (pingpong.pressedKeys[KEY.DOWN]) { //arrow-down
  149. //move paddle B down 5 pixels
  150. var top = parseInt($("#paddleB").css("top"));
  151. if (top < (playgroundHeight-70)) { //edge check
  152. $("#paddleB").css("top",top+5);
  153. }
  154. }
  155.  
  156. if (pingpong.pressedKeys[KEY.W]) { //w
  157. //move paddle A up 5 pixels
  158. var top = parseInt($("#paddleA").css("top"));
  159. if (top > 0) { //edge check
  160. $("#paddleA").css("top",top-5);
  161. }
  162. }
  163.  
  164. if (pingpong.pressedKeys[KEY.S]) { //s
  165. //move paddle A down 5 pixels
  166. var top = parseInt($("#paddleA").css("top"));
  167. if (top < (playgroundHeight-70)) { //edge check
  168. $("#paddleA").css("top",top+5);
  169. }
  170. }
  171. }
  172.  
  173. function endGame() {
  174. if (pingpong.scoreA >= WINNER) {
  175. $("#winner").html("Player A has won the game!")
  176. }
  177.  
  178. else {
  179. $("#winner").html("Player B has won the game!")
  180. }
  181. clearInterval(pingpong.timer);
  182. }
Add Comment
Please, Sign In to add comment