beannshie

Tennis Game Polished

Jul 13th, 2017
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.86 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. }
  63.  
  64. function ballReset() {
  65. if(player1Score >= WINNING_SCORE ||
  66. player2Score >= WINNING_SCORE) {
  67. showingWinScreen = true
  68. }
  69. ballSpeedX = -ballSpeedX;
  70. ballX = canvas.width/2;
  71. ballY = canvas.height/2;
  72. }
  73.  
  74. function computerMovement() {
  75. var paddle2YCenter=paddle2Y+(PADDLE_HEIGHT/2);
  76. var distToBall = (ballY-paddle2YCenter);
  77. var percentToJump = 0.25;
  78. paddle2Y+=percentToJump*distToBall;
  79. }
  80.  
  81. function moveEverything() {
  82. if(showingWinScreen) {
  83. return;
  84. }
  85. computerMovement();
  86. ballX += ballSpeedX;
  87. ballY += ballSpeedY;
  88.  
  89. if(ballX < 0) {
  90. if(ballY > paddle1Y &&
  91. ballY < paddle1Y+PADDLE_HEIGHT) {
  92. ballSpeedX = -ballSpeedX;
  93.  
  94. var deltaY = ballY - (paddle1Y+PADDLE_HEIGHT/2);
  95. ballSpeedY = deltaY * 0.35;
  96. } else {
  97. player2Score += 1;
  98. ballReset();
  99. }
  100. }
  101. if(ballX > canvas.width) {
  102. if(ballY > paddle2Y &&
  103. ballY < paddle2Y+PADDLE_HEIGHT) {
  104. ballSpeedX = -ballSpeedX;
  105.  
  106. var deltaY = ballY - (paddle2Y+PADDLE_HEIGHT/2);
  107. ballSpeedY = deltaY * 0.35;
  108. } else {
  109. player1Score++;
  110. ballReset();
  111. }
  112. }
  113. if(ballY < 0) {
  114. ballSpeedY = -ballSpeedY;
  115. }
  116. if(ballY > canvas.height) {
  117. ballSpeedY = -ballSpeedY;
  118. }
  119. }
  120.  
  121. function drawNet() {
  122. for(var i=0;i<canvas.height; i+=40) {
  123. colorRect(canvas.width/2-1,i,2,20, 'white');
  124. }
  125. }
  126.  
  127. function drawEverything() {
  128. // background
  129. colorRect(0,0,canvas.width,canvas.height,'black');
  130.  
  131. if(showingWinScreen) {
  132. canvasContext.fillStyle = 'white';
  133.  
  134. if(player1Score >= WINNING_SCORE) {
  135. canvasContext.fillText("Player Won", 650,200);
  136. } else if(player2Score >= WINNING_SCORE) {
  137. canvasContext.fillText("Computer Won", 640,200);
  138. }
  139. canvasContext.fillText("Click to continue", 637,480);
  140. return;
  141. }
  142.  
  143. // net
  144. drawNet();
  145.  
  146. // left player paddle
  147. colorRect(0,paddle1Y,PADDLE_THICKNESS,PADDLE_HEIGHT,'white');
  148.  
  149. // right computer paddle
  150. colorRect(canvas.width-PADDLE_THICKNESS,paddle2Y,PADDLE_THICKNESS,PADDLE_HEIGHT,'white');
  151.  
  152. // ball
  153. colorCircle(ballX, ballY, 10, 'white');
  154.  
  155. canvasContext.fillText(player1Score, 100,100);
  156. canvasContext.fillText(player2Score, canvas.width - 100,100);
  157. }
  158.  
  159. function colorCircle(centerX, centerY, radius, drawColor) {
  160. canvasContext.fillStyle = drawColor;
  161. canvasContext.beginPath();
  162. canvasContext.arc(centerX, centerY, radius, 0,Math.PI*2, true);
  163. canvasContext.fill();
  164. }
  165.  
  166. function colorRect(leftX,topY, width,height, drawColor) {
  167. canvasContext.fillStyle = drawColor;
  168. canvasContext.fillRect(leftX,topY, width,height);
  169. }
  170.  
  171. </script>
  172.  
  173. </html>
Advertisement
Add Comment
Please, Sign In to add comment