beannshie

Tennis Game Keyboard Finalised

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