Advertisement
Guest User

Untitled

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