beannshie

Tennis Game Keyboard Polished

Jul 17th, 2017
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.23 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. if(ballX > canvas.width/2) {
  90. ballSpeedX = -10
  91. }
  92. if(ballX < canvas.width/2) {
  93. ballSpeedX = 10
  94. }
  95. ballSpeedY = 8
  96. ballX = canvas.width/2;
  97. ballY = canvas.height/2;
  98. }
  99.  
  100. function computerMovement() {
  101. var paddle2YCenter=paddle2Y+(PADDLE_HEIGHT/2);
  102. var distToBall = (ballY-paddle2YCenter);
  103. var percentToJump = 0.2;
  104. paddle2Y+=percentToJump*distToBall;
  105. }
  106.  
  107. function moveEverything() {
  108. if(showingWinScreen) {
  109. return;
  110. }
  111.  
  112. applyKeyAction();
  113.  
  114. computerMovement();
  115. ballX += ballSpeedX;
  116. ballY += ballSpeedY;
  117.  
  118. if(ballX < 0) {
  119. if(ballY > paddle1Y &&
  120. ballY < paddle1Y+PADDLE_HEIGHT) {
  121. ballSpeedX = -ballSpeedX;
  122.  
  123. var deltaY = ballY - (paddle1Y+PADDLE_HEIGHT/2);
  124. ballSpeedY = deltaY * 0.35;
  125. } else {
  126. player2Score += 1;
  127. ballReset();
  128. }
  129. }
  130. if(ballX > canvas.width) {
  131. if(ballY > paddle2Y &&
  132. ballY < paddle2Y+PADDLE_HEIGHT) {
  133. ballSpeedX = -ballSpeedX;
  134.  
  135. var deltaY = ballY - (paddle2Y+PADDLE_HEIGHT/2);
  136. ballSpeedY = deltaY * 0.35;
  137. } else {
  138. player1Score++;
  139. ballReset();
  140. }
  141. }
  142. if(ballY < 0) {
  143. ballSpeedY = -ballSpeedY;
  144. }
  145. if(ballY > canvas.height) {
  146. ballSpeedY = -ballSpeedY;
  147. }
  148. }
  149.  
  150. function drawNet() {
  151. for(var i=0;i<canvas.height; i+=40) {
  152. colorRect(canvas.width/2-1,i,2,20, 'white');
  153. }
  154. }
  155.  
  156. function drawEverything() {
  157. // background
  158. colorRect(0,0,canvas.width,canvas.height,'black');
  159.  
  160. if(showingWinScreen) {
  161. canvasContext.fillStyle = 'white';
  162.  
  163. if(player1Score >= WINNING_SCORE) {
  164. canvasContext.fillText("Player Won", 650,200);
  165. } else if(player2Score >= WINNING_SCORE) {
  166. canvasContext.fillText("Computer Won", 640,200);
  167. }
  168. canvasContext.fillText("Click to continue", 637,480);
  169. return;
  170. }
  171.  
  172. // net
  173. drawNet();
  174.  
  175. // left player paddle
  176. colorRect(0,paddle1Y,PADDLE_THICKNESS,PADDLE_HEIGHT,'white');
  177.  
  178. // right computer paddle
  179. colorRect(canvas.width-PADDLE_THICKNESS,paddle2Y,PADDLE_THICKNESS,PADDLE_HEIGHT,'white');
  180.  
  181. // ball
  182. colorCircle(ballX, ballY, 10, 'white');
  183.  
  184. canvasContext.fillText(player1Score, 100,100);
  185. canvasContext.fillText(player2Score, canvas.width - 100,100);
  186. }
  187.  
  188. function colorCircle(centerX, centerY, radius, drawColor) {
  189. canvasContext.fillStyle = drawColor;
  190. canvasContext.beginPath();
  191. canvasContext.arc(centerX, centerY, radius, 0,Math.PI*2, true);
  192. canvasContext.fill();
  193. }
  194.  
  195. function colorRect(leftX,topY, width,height, drawColor) {
  196. canvasContext.fillStyle = drawColor;
  197. canvasContext.fillRect(leftX,topY, width,height);
  198. }
  199.  
  200. </script>
  201.  
  202. </html>
Advertisement
Add Comment
Please, Sign In to add comment