Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.01 KB | None | 0 0
  1. const cvs = document.getElementById("myCanvas");
  2. const ctx = cvs.getContext('2d');
  3.  
  4. //create user paddle
  5. const user = {
  6. x: 0,
  7. y: cvs.height / 2 - 100 / 2,
  8. width: 10,
  9. height: 100,
  10. color: "WHITE",
  11. score: 0
  12. }
  13.  
  14. //create com paddle
  15. const com = {
  16. x: cvs.width - 10,
  17. y: cvs.height / 2 - 100 / 2,
  18. width: 10,
  19. height: 100,
  20. color: "WHITE",
  21. score: 0
  22. }
  23.  
  24. //create ball
  25. const ball = {
  26. x: cvs.width / 2,
  27. y: cvs.height / 2,
  28. radius: 10,
  29. speed: 5,
  30. velocityX: 5,
  31. velocityY: 5,
  32. color: "WHITE"
  33. }
  34.  
  35. //draw rect
  36. function drawRect(x, y, w, h, color) {
  37. ctx.fillStyle = color;
  38. ctx.fillRect(x, y, w, h);
  39. }
  40.  
  41. //create net
  42. const net ={
  43. x: cvs.width/2 - 1,
  44. y: 0,
  45. width: 2,
  46. height: 10,
  47. color: "WHITE"
  48. }
  49.  
  50. //draw net
  51. function drawNet(){
  52. for(let i = 0; i<= cvs.height; i+=15){
  53. drawRect(net.x, net.y + i, net.width, net.height, net.color);
  54. }
  55. }
  56.  
  57. //draw circle
  58. function drawCircle(x, y, r, color) {
  59. ctx.fillStyle = color;
  60. ctx.beginPath();
  61. ctx.arc(x, y, r, 0, Math.PI * 2, false);
  62. ctx.closePath();
  63. ctx.fill();
  64. }
  65.  
  66.  
  67. function drawText(text, x, y, color) {
  68. ctx.fillStyle = color;
  69. ctx.font = "45px Impact";
  70. ctx.fillText(text, x, y);
  71. }
  72.  
  73.  
  74. function render() {
  75. drawRect(0, 0, cvs.width, cvs.height, "BLACK");
  76.  
  77. drawNet();
  78.  
  79. drawText(user.score, cvs.width / 4, cvs.height / 5, "WHITE");
  80. drawText(com.score, 3 * cvs.width / 4, cvs.height / 5, "WHITE");
  81.  
  82. //draw user and com
  83. drawRect(user.x, user.y, user.width, user.height, user.color);
  84. drawRect(com.x, com.y, com.width, com.height, com.color);
  85.  
  86. //draw circle
  87. drawCircle(ball.x, ball.y, ball.radius, ball.color);
  88. }
  89.  
  90. //control the paddle
  91.  
  92. cvs.addEventListener("mousemove", movePaddle);
  93.  
  94. function movePaddle(evt){
  95. let rect = cvs.getBoundingClientRect();
  96.  
  97. user.y = evt.clientY - rect.top - user.height/2;
  98. }
  99.  
  100. //collision
  101. function collision(b,p){
  102. b.top = b.y - b.radius;
  103. b.bottom = b.y + b.radius;
  104. b.left = b.x - b.radius;
  105. b.right = b.x + b.radius;
  106.  
  107. p.top = p.y;
  108. p.bottom = p.y+ p.height;
  109. p.left = p.x;
  110. p.right = p.x + p.width;
  111.  
  112. return b.right > p.left && b.bottom > p.top && b.left < p.right && b.top < p.bottom;
  113. }
  114.  
  115. //reset ball
  116. function resetBall(){
  117. ball.x = cvs.width/2;
  118. ball.y = cvs.height/2;
  119.  
  120. ball.speed = 5;
  121. ball.velocityX = -ball.velocityX;
  122. }
  123.  
  124. //update
  125. function update(){
  126. ball.x += ball.velocityX;
  127. ball.y += ball.velocityY;
  128.  
  129. //simple AI for com
  130. let computerLevel = 0.1;
  131. com.y += (ball.y - (com.y + com.height/2)) * computerLevel;
  132.  
  133.  
  134. if(ball.y + ball.radius > cvs.height || ball.y - ball.radius < 0){
  135. ball.velocityY = - ball.velocityY;
  136. }
  137.  
  138. let player = (ball.x < cvs.width/2) ? user : com;
  139.  
  140. if(collision(ball, player)){
  141. //where the ball hits the player
  142. let collidePoint = ball.y - (player.y + player.height/2);
  143.  
  144. //normalization
  145. collidePoint = collidePoint/(player.height/2);
  146.  
  147. //calculate angle
  148. let angleRad = collidePoint * Math.PI/4;
  149.  
  150. //direction
  151. let direction = (ball.x < cvs.width/2) ? 1 : -1;
  152.  
  153. //change vel X and Y
  154. ball.velocityX = direction * ball.speed + Math.cos(angleRad);
  155. ball.velocityX = direction * ball.speed + Math.sin (angleRad);
  156.  
  157. //everytime ball is it
  158. ball.speed += 0.5;
  159. }
  160.  
  161. //update score
  162. if(ball.x - ball.radius < 0){
  163. com.score++;
  164. ball.x = cvs.width/2;
  165. setTimeout(resetBall(), 1000);
  166. //resetBall();
  167. }else if (ball.x + ball.radius > cvs.width){
  168. user.score++;
  169. setTimeout(resetBall(), 1500);
  170. //resetBall();
  171. }
  172. }
  173.  
  174. //game init
  175. function game(){
  176. update();
  177. render();
  178. }
  179. const framePerSecond = 50;
  180. setInterval(game, 1000/framePerSecond);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement