beannshie

Tennis Game 2 Player Cleaned

Jun 19th, 2017
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.26 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. //// document.onkeydown = checkKey;
  25.  
  26. var holdUpP1 = false;
  27. var holdDownP1 = false;
  28. var holdUpP2 = false;
  29. var holdDownP2 = false;
  30.  
  31. function pressKey(e) {
  32. e = e || window.event;
  33. keySet(e.keyCode,true);
  34. }
  35. function releaseKey(e) {
  36. e = e || window.event;
  37. keySet(e.keyCode,false);
  38. }
  39.  
  40. function keySet(whichKey,stateHeld) {
  41. if (whichKey == '38') {
  42. holdUpP2 = stateHeld;
  43. } else if (whichKey == '40') {
  44. holdDownP2 = stateHeld;
  45. } else if (whichKey == '87') {
  46. holdUpP1 = stateHeld;
  47. } else if (whichKey == '83') {
  48. holdDownP1 = stateHeld;
  49. }
  50. }
  51.  
  52. function applyKeyAction() {
  53. if(holdUpP1) {
  54. paddle1Y -= 10;
  55. }
  56. if(holdDownP1) {
  57. paddle1Y += 10;
  58. }
  59. if(holdUpP2) {
  60. paddle2Y -= 10;
  61. }
  62. if(holdDownP2) {
  63. paddle2Y += 10;
  64. }
  65. }
  66.  
  67. function handleMouseClick(evt) {
  68. if (showingWinScreen) {
  69. player1Score = 0;
  70. player2Score = 0;
  71. showingWinScreen = false;
  72. }
  73. }
  74.  
  75. window.onload = function() {
  76. console.log("Hello World!");
  77. canvas = document.getElementById('gameCanvas');
  78. canvasContext = canvas.getContext('2d');
  79.  
  80. var framesPerSecond = 30
  81. setInterval(function() {
  82. moveEverything();
  83. drawEverything();
  84. }, 1000 / framesPerSecond);
  85.  
  86. canvas.addEventListener('mousedown', handleMouseClick);
  87. document.addEventListener('keydown', pressKey);
  88. document.addEventListener('keyup', releaseKey);
  89.  
  90. (e);
  91. }
  92.  
  93. function ballReset() {
  94. if (player1Score >= WINNING_SCORE ||
  95. player2Score >= WINNING_SCORE) {
  96. showingWinScreen = true
  97. }
  98. ballSpeedX = -ballSpeedX;
  99. ballX = canvas.width / 2;
  100. ballY = canvas.height / 2;
  101. }
  102.  
  103. function moveEverything() {
  104. if (showingWinScreen) {
  105. return;
  106. }
  107.  
  108. applyKeyAction();
  109.  
  110. ballX += ballSpeedX;
  111. ballY += ballSpeedY;
  112.  
  113. if (ballX < 0) {
  114. if (ballY > paddle1Y &&
  115. ballY < paddle1Y + PADDLE_HEIGHT) {
  116. ballSpeedX = -ballSpeedX;
  117.  
  118. var deltaY = ballY - (paddle1Y + PADDLE_HEIGHT / 2);
  119. ballSpeedY = deltaY * 0.35;
  120. } else {
  121. player2Score += 1;
  122. ballReset();
  123. }
  124. }
  125. if (ballX > canvas.width) {
  126. if (ballY > paddle2Y &&
  127. ballY < paddle2Y + PADDLE_HEIGHT) {
  128. ballSpeedX = -ballSpeedX;
  129.  
  130. var deltaY = ballY - (paddle2Y + PADDLE_HEIGHT / 2);
  131. ballSpeedY = deltaY * 0.35;
  132. } else {
  133. player1Score++;
  134. ballReset();
  135. }
  136. }
  137. if (ballY < 0) {
  138. ballSpeedY = -ballSpeedY;
  139. }
  140. if (ballY > canvas.height) {
  141. ballSpeedY = -ballSpeedY;
  142. }
  143. }
  144.  
  145. function drawNet() {
  146. for (var i = 0; i < canvas.height; i += 40) {
  147. colorRect(canvas.width / 2 - 1, i, 2, 20, 'white');
  148. }
  149. }
  150.  
  151. function drawEverything() {
  152. // background
  153. colorRect(0, 0, canvas.width, canvas.height, 'black');
  154.  
  155. if (showingWinScreen) {
  156. canvasContext.fillStyle = 'white';
  157.  
  158. if (player1Score >= WINNING_SCORE) {
  159. canvasContext.fillText("Player 1 Won", 645, 200);
  160. } else if (player2Score >= WINNING_SCORE) {
  161. canvasContext.fillText("Player 2 Won", 645, 200);
  162. }
  163. canvasContext.fillText("Click to continue", 637, 480);
  164. return;
  165. }
  166.  
  167. // net
  168. drawNet();
  169.  
  170. // left player paddle
  171. colorRect(0, paddle1Y, PADDLE_THICKNESS, PADDLE_HEIGHT, 'white');
  172.  
  173. // right computer paddle
  174. colorRect(canvas.width - PADDLE_THICKNESS, paddle2Y, PADDLE_THICKNESS, PADDLE_HEIGHT, 'white');
  175.  
  176. // ball
  177. colorCircle(ballX, ballY, 10, 'white');
  178.  
  179. canvasContext.fillText(player1Score, 100, 100);
  180. canvasContext.fillText(player2Score, canvas.width - 100, 100);
  181. }
  182.  
  183. function colorCircle(centerX, centerY, radius, drawColor) {
  184. canvasContext.fillStyle = drawColor;
  185. canvasContext.beginPath();
  186. canvasContext.arc(centerX, centerY, radius, 0, Math.PI * 2, true);
  187. canvasContext.fill();
  188. }
  189.  
  190. function colorRect(leftX, topY, width, height, drawColor) {
  191. canvasContext.fillStyle = drawColor;
  192. canvasContext.fillRect(leftX, topY, width, height);
  193. }
  194. </script>
  195.  
  196. </html>
Advertisement
Add Comment
Please, Sign In to add comment