Advertisement
Guest User

Untitled

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