beannshie

Tennis Game 2 Player

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