Advertisement
beannshie

Tennis Game 2 Player Finalised

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