beannshie

Tennis Game Keyboard

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