beannshie

Tennis Game Extra Polished / Finalised

Jul 13th, 2017
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.21 KB | None | 0 0
  1. <html>
  2.  
  3. <canvas id="gameCanvas" width="1350" height="640"></canvas>
  4.  
  5. <script>
  6. var canvas;
  7. var canvasContext;
  8. var ballX = 50;
  9. var ballY = 50;
  10. var ballSpeedX = 10;
  11. var ballSpeedY = 5;
  12.  
  13. var player1Score = 0
  14. var player2Score = 0
  15. const WINNING_SCORE = 3;
  16.  
  17. var showingWinScreen = false;
  18.  
  19. var paddle1Y = 250;
  20. var paddle2Y = 250;
  21. const PADDLE_THICKNESS = 10;
  22. const PADDLE_HEIGHT = 100;
  23.  
  24. function calculateMousePos(evt) {
  25. var rect = canvas.getBoundingClientRect();
  26. var root = document.documentElement;
  27. var mouseX = evt.clientX - rect.left - root.scrollLeft;
  28. var mouseY = evt.clientY - rect.top - root.scrollTop;
  29. return {
  30. x:mouseX,
  31. y:mouseY
  32. };
  33. }
  34.  
  35. function handleMouseClick(evt) {
  36. if(showingWinScreen) {
  37. player1Score = 0;
  38. player2Score = 0;
  39. showingWinScreen = false;
  40. }
  41. }
  42.  
  43. window.onload = function() {
  44. console.log("Hello World!");
  45. canvas = document.getElementById('gameCanvas');
  46. canvasContext = canvas.getContext('2d');
  47.  
  48. var framesPerSecond = 30
  49. setInterval(function() {
  50. moveEverything();
  51. drawEverything();
  52. }, 1000/framesPerSecond);
  53.  
  54.  
  55. canvas.addEventListener('mousedown',handleMouseClick);
  56.  
  57. canvas.addEventListener('mousemove',
  58. function(evt) {
  59. var mousePos = calculateMousePos(evt);
  60. paddle1Y = mousePos.y-(PADDLE_HEIGHT/2);
  61. })
  62. console.log(ballSpeedX);
  63. console.log(ballSpeedY);
  64. }
  65.  
  66. function ballReset() {
  67. if(player1Score >= WINNING_SCORE ||
  68. player2Score >= WINNING_SCORE) {
  69. showingWinScreen = true
  70. }
  71. if(ballX > canvas.width/2) {
  72. ballSpeedX = -10
  73. }
  74. if(ballX < canvas.width/2) {
  75. ballSpeedX = 10
  76. }
  77. ballSpeedY = 8
  78. ballX = canvas.width/2;
  79. ballY = canvas.height/2;
  80.  
  81. console.log(ballSpeedX);
  82. console.log(ballSpeedY);
  83. }
  84.  
  85. function computerMovement() {
  86. var paddle2YCenter=paddle2Y+(PADDLE_HEIGHT/2);
  87. var distToBall = (ballY-paddle2YCenter);
  88. var percentToJump = 0.2;
  89. paddle2Y+=percentToJump*distToBall;
  90. }
  91.  
  92. function moveEverything() {
  93. if(showingWinScreen) {
  94. return;
  95. }
  96. computerMovement();
  97. ballX += ballSpeedX;
  98. ballY += ballSpeedY;
  99.  
  100. if(ballX < 0) {
  101. if(ballY > paddle1Y &&
  102. ballY < paddle1Y+PADDLE_HEIGHT) {
  103. ballSpeedX = -ballSpeedX;
  104.  
  105. var deltaY = ballY - (paddle1Y+PADDLE_HEIGHT/2);
  106. ballSpeedY = deltaY * 0.35;
  107. } else {
  108. player2Score += 1;
  109. ballReset();
  110. }
  111. }
  112. if(ballX > canvas.width) {
  113. if(ballY > paddle2Y &&
  114. ballY < paddle2Y+PADDLE_HEIGHT) {
  115. ballSpeedX = -ballSpeedX;
  116.  
  117. var deltaY = ballY - (paddle2Y+PADDLE_HEIGHT/2);
  118. ballSpeedY = deltaY * 0.35;
  119. } else {
  120. player1Score++;
  121. ballReset();
  122. }
  123. }
  124. if(ballY < 0) {
  125. ballSpeedY = -ballSpeedY;
  126. }
  127. if(ballY > canvas.height) {
  128. ballSpeedY = -ballSpeedY;
  129. }
  130. }
  131.  
  132. function drawNet() {
  133. for(var i=0;i<canvas.height; i+=40) {
  134. colorRect(canvas.width/2-1,i,2,20, 'white');
  135. }
  136. }
  137.  
  138. function drawEverything() {
  139. // background
  140. colorRect(0,0,canvas.width,canvas.height,'black');
  141.  
  142. if(showingWinScreen) {
  143. canvasContext.fillStyle = 'white';
  144.  
  145. if(player1Score >= WINNING_SCORE) {
  146. canvasContext.fillText("Player Won", 650,200);
  147. } else if(player2Score >= WINNING_SCORE) {
  148. canvasContext.fillText("Computer Won", 640,200);
  149. }
  150. canvasContext.fillText("Click to continue", 637,480);
  151. return;
  152. }
  153.  
  154. // net
  155. drawNet();
  156.  
  157. // left player paddle
  158. colorRect(0,paddle1Y,PADDLE_THICKNESS,PADDLE_HEIGHT,'white');
  159.  
  160. // right computer paddle
  161. colorRect(canvas.width-PADDLE_THICKNESS,paddle2Y,PADDLE_THICKNESS,PADDLE_HEIGHT,'white');
  162.  
  163. // ball
  164. colorCircle(ballX, ballY, 10, 'white');
  165.  
  166. canvasContext.fillText(player1Score, 100,100);
  167. canvasContext.fillText(player2Score, canvas.width - 100,100);
  168. }
  169.  
  170. function colorCircle(centerX, centerY, radius, drawColor) {
  171. canvasContext.fillStyle = drawColor;
  172. canvasContext.beginPath();
  173. canvasContext.arc(centerX, centerY, radius, 0,Math.PI*2, true);
  174. canvasContext.fill();
  175. }
  176.  
  177. function colorRect(leftX,topY, width,height, drawColor) {
  178. canvasContext.fillStyle = drawColor;
  179. canvasContext.fillRect(leftX,topY, width,height);
  180. }
  181.  
  182. </script>
  183.  
  184. </html>
Advertisement
Add Comment
Please, Sign In to add comment