Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2017
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. <html>
  2.  
  3. <canvas id="gameCanvas" width="800" height="600"></canvas>
  4.  
  5. <script>
  6. var canvas;
  7. var canvasContex;
  8. var ballX = 50;
  9. var ballY = 50;
  10. var ballSpeedX = 10;
  11. var ballSpeedY = 5;
  12.  
  13. var paddle1Y = 250;
  14. var paddle2Y = 250;
  15. const PADDLE_THICKNESS = 10;
  16. const PADDLE_HEIGHT = 100;
  17.  
  18. function calculateMousePos(evt) {
  19. var rect = canvas.getBoundingClientRect();
  20. var root = document.documentElement;
  21. var mouseX = evt.clientX - rect.left - root.scrollLeft;
  22. var mouseY = evt.clientY - rect.top - root.scrollTop;
  23. return {
  24. x: mouseX,
  25. y: mouseY
  26. };
  27. }
  28.  
  29. window.onload = function() {
  30. canvas = document.getElementById('gameCanvas');
  31. canvasContext = canvas.getContext('2d');
  32.  
  33. var framesPerSecond = 30;
  34. setInterval(function() {
  35. drawEverything();
  36. moveEverything();
  37.  
  38. }, 1000 / framesPerSecond);
  39.  
  40. canvas.addEventListener('mousemove',
  41. function(evt) {
  42. var mousePos = calculateMousePos(evt);
  43. paddle2Y = mousePos.y - (PADDLE_HEIGHT / 2);
  44. });
  45. }
  46.  
  47. function ballReset() {
  48. ballSpeeX = -ballSpeeX;
  49. ballX = canvas.width / 2;
  50. ballY = canvas.height / 2;
  51. }
  52.  
  53. function moveEverything() {
  54. ballX = ballX + ballSpeedX;
  55. ballY = ballY + ballSpeedY;
  56.  
  57. if (ballX < 0) {
  58. if (ballY > paddle1Y &&
  59. ballY < paddle1Y + PADDLE_HEIGHT) {
  60. ballSpeedX = -ballSpeedX;
  61. } else {
  62. ballReset();
  63. }
  64. if (ballX > canvas.width) {
  65. ballSpeedX = -ballSpeedX;
  66. }
  67. if (ballY < 0) {
  68. ballSpeedY = -ballSpeedY;
  69. }
  70. if (ballY > canvas.height) {
  71. ballSpeedY = -ballSpeedY;
  72. }
  73. }
  74. } //// <-- add this
  75.  
  76. function drawEverything() {
  77. //next line blanks out the screen
  78. colorRect(0, 0, canvas.width, canvas.height, 'black');
  79. //left paddle
  80. colorRect(0, paddle1Y, PADDLE_THICKNESS, PADDLE_HEIGHT, 'white');
  81. // this is the right paddle
  82. colorRect(canvas.width - PADDLE_THICKNESS, paddle2Y, PADDLE_THICKNESS, PADDLE_HEIGHT, 'white');
  83. //the ball
  84. colorCircle(ballX, ballY, 10, 'white');
  85. }
  86.  
  87. function colorCircle(centerX, centerY, radius, drawColor) {
  88. canvasContext.fillStyle = drawColor;
  89. canvasContext.beginPath();
  90. canvasContext.arc(centerX, centerY, radius, 0, Math.PI * 2, true);
  91. canvasContext.fill();
  92. }
  93.  
  94. function colorRect(leftX, topY, width, height, drawColor) {
  95. canvasContext.fillStyle = drawColor;
  96. canvasContext.fillRect(leftX, topY, width, height);
  97. }
  98. // } //// <-- remove
  99. </script>
  100.  
  101. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement