Advertisement
Guest User

pong code

a guest
Jun 24th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.67 KB | None | 0 0
  1. <html>
  2. <head>
  3. <script src="pong.js"></script>
  4. </head>
  5. <body></body>
  6. </html>
  7. <script>
  8.  
  9. var animate = window.requestAnimationFrame ||
  10. window.webkitRequestAnimationFrame ||
  11. window.mozRequestAnimationFrame ||
  12. function(callback) { window.setTimeout(callback, 1000/60) };
  13.  
  14. var canvas = document.createElement('canvas');
  15. var width = 1350;
  16. var height = 600;
  17. canvas.width = width;
  18. canvas.height = height;
  19. var context = canvas.getContext('2d');
  20.  
  21. window.onload = function() {
  22. document.body.appendChild(canvas);
  23. animate(step);
  24. };
  25.  
  26. var step = function() {
  27. update();
  28. render();
  29. animate(step);
  30. };
  31.  
  32. var update = function() {
  33. };
  34.  
  35. var render = function() {
  36. context.fillStyle = "white";
  37. context.fillRect(0, 0, width, height);
  38. };
  39.  
  40. function Paddle(x, y, width, height) {
  41. this.x = x;
  42. this.y = y;
  43. this.width = width;
  44. this.height = height;
  45. this.x_speed = 0;
  46. this.y_speed = 0;
  47. }
  48.  
  49. Paddle.prototype.render = function() {
  50. context.fillStyle = "white";
  51. context.fillRect(this.x, this.y, this.width, this.height);
  52. };
  53.  
  54. function Player() {
  55. this.paddle = new Paddle(175, 580, 50, 10);
  56. }
  57.  
  58. function Computer() {
  59. this.paddle = new Paddle(175, 10, 50, 10);
  60. }
  61.  
  62. Player.prototype.render = function() {
  63. this.paddle.render();
  64. };
  65.  
  66. Computer.prototype.render = function() {
  67. this.paddle.render();
  68. };
  69.  
  70. function Ball(x, y) {
  71. this.x = x;
  72. this.y = y;
  73. this.x_speed = 0;
  74. this.y_speed = 3;
  75. this.radius = 5;
  76. }
  77.  
  78. Ball.prototype.render = function() {
  79. context.beginPath();
  80. context.arc(this.x, this.y, this.radius, 2 * Math.PI, false);
  81. context.fillStyle = "white";
  82. context.fill();
  83. };
  84.  
  85. var player = new Player();
  86. var computer = new Computer();
  87. var ball = new Ball(200, 300);
  88.  
  89. var render = function() {
  90. context.fillStyle = "black";
  91. context.fillRect(0, 0, width, height);
  92. player.render();
  93. computer.render();
  94. ball.render();
  95. };
  96.  
  97. var update = function() {
  98. ball.update();
  99. };
  100.  
  101. Ball.prototype.update = function() {
  102. this.x += this.x_speed;
  103. this.y += this.y_speed;
  104. };
  105.  
  106. var update = function() {
  107. ball.update(player.paddle, computer.paddle);
  108. };
  109.  
  110. Ball.prototype.update = function(paddle1, paddle2) {
  111. this.x += this.x_speed;
  112. this.y += this.y_speed;
  113. var top_x = this.x - 5;
  114. var top_y = this.y - 5;
  115. var bottom_x = this.x + 5;
  116. var bottom_y = this.y + 5;
  117.  
  118. if(this.x - 5 < 0) {
  119. this.x = 5;
  120. this.x_speed = -this.x_speed;
  121. } else if(this.x + 2 > 1300) {
  122. this.x = 1295;
  123. this.x_speed = -this.x_speed;
  124. }
  125.  
  126. if(this.y < 0 || this.y > 600) {
  127. this.x_speed = 0;
  128. this.y_speed = 3;
  129. this.x = 200;
  130. this.y = 300;
  131. }
  132.  
  133. if(top_y > 300) {
  134. if(top_y < (paddle1.y + paddle1.height) && bottom_y > paddle1.y && top_x < (paddle1.x + paddle1.width) && bottom_x > paddle1.x) {
  135. // hit the player's paddle
  136. this.y_speed = -1.5;
  137. this.x_speed += (paddle1.x_speed / 2);
  138. this.y += this.y_speed;
  139. }
  140. } else {
  141. if(top_y < (paddle2.y + paddle2.height) && bottom_y > paddle2.y && top_x < (paddle2.x + paddle2.width) && bottom_x > paddle2.x) {
  142. // hit the computer's paddle
  143. this.y_speed = 1.5;
  144. this.x_speed += (paddle2.x_speed / 2);
  145. this.y += this.y_speed;
  146. }
  147. }
  148. };
  149.  
  150. var keysDown = {};
  151.  
  152. window.addEventListener("keydown", function(event) {
  153. keysDown[event.keyCode] = true;
  154. });
  155.  
  156. window.addEventListener("keyup", function(event) {
  157. delete keysDown[event.keyCode];
  158. });
  159.  
  160. var update = function() {
  161. player.update();
  162. ball.update(player.paddle, computer.paddle);
  163. };
  164.  
  165. Player.prototype.update = function() {
  166. for(var key in keysDown) {
  167. var value = Number(key);
  168. if(value == 187) {
  169. this.paddle.move(-10, 0);
  170. } else if (value == 189) {
  171. this.paddle.move(10, 0);
  172. } else {
  173. this.paddle.move(0, 0);
  174. }
  175. }
  176. };
  177.  
  178. Paddle.prototype.move = function(x, y) {
  179. this.x += x;
  180. this.y += y;
  181. this.x_speed = x;
  182. this.y_speed = y;
  183. if(this.x < 0) {
  184. this.x = 0;
  185. this.x_speed = 0;
  186. } else if (this.x + this.width > 1300) {
  187. this.x = 1300 - this.width;
  188. this.x_speed = 0;
  189. }
  190. }
  191.  
  192. var update = function() {
  193. player.update();
  194. computer.update(ball);
  195. ball.update(player.paddle, computer.paddle);
  196. };
  197.  
  198. Computer.prototype.update = function(ball) {
  199. var x_pos = ball.x;
  200. var diff = -((this.paddle.x + (this.paddle.width / 2)) - x_pos);
  201. if(diff < 0 && diff < -4) {
  202. diff = -20;
  203. } else if(diff > 0 && diff > 4) {
  204. diff = 20;
  205. }
  206. this.paddle.move(diff, 0);
  207. if(this.paddle.x < 0) {
  208. this.paddle.x = 0;
  209. } else if (this.paddle.x + this.paddle.width > 1300) {
  210. this.paddle.x = 1300 - this.paddle.width;
  211. }
  212. };
  213.  
  214. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement