beannshie

Tennis Game Keyboard Fixed

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