beannshie

Tennis Game

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