Advertisement
Guest User

Untitled

a guest
Feb 16th, 2019
644
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. <html>
  2.  
  3. <canvas id="gameCanvas" width="800" height="600"></canvas>
  4.  
  5. <style> #yourScore {
  6. background-color: lightblue; color: black; padding: 40px; text-align: center;
  7. }
  8. </style>
  9. <h1 id="yourScore">Your Score: </h1>
  10.  
  11. <style> #theirScore{
  12. background-color: lightblue; color: black; padding: 40px; text-align: center;
  13. }
  14. </style>
  15. <h1 id="theirScore">Their Score: </h1>
  16.  
  17. <script>
  18.  
  19. var canvas;
  20. var canvasContext;
  21. var ballX = 50;
  22. var ballY = 50;
  23. var ballSpeedX = 10;
  24. var ballSpeedY = 5;
  25.  
  26. var paddle1Y = 250;
  27. var paddle2Y = 250;
  28. const PADDLE_HEIGHT = 100;
  29. const PADDLE_THICKNESS = 15;
  30.  
  31. function calculateMousePos(evt){
  32. var rect = canvas.getBoundingClientRect();
  33. var root = document.documentElement;
  34. var mouseX = evt.clientX - rect.left - root.scrollLeft;
  35. var mouseY = evt.clientY - rect.top - root.scrollTop;
  36. return{
  37. x:mouseX,
  38. y:mouseY
  39. };
  40. }
  41.  
  42. window.onload = function() {
  43. canvas = document.getElementById('gameCanvas');
  44. canvasContext = canvas.getContext('2d');
  45.  
  46. var framesPerSecond = 60;
  47. setInterval(function(){
  48. moveEverything();
  49. drawEverything();
  50. }, 1000/framesPerSecond); // 1000 = ms which slows down the function drawEverything
  51.  
  52. canvas.addEventListener('mousemove',
  53. function(evt){
  54. var mousePos = calculateMousePos(evt);
  55. paddle1Y = mousePos.y-(PADDLE_HEIGHT/2);
  56. });
  57. }
  58.  
  59. function computerMovement(){
  60. var paddle2YCenter = paddle2Y + (PADDLE_HEIGHT/2)
  61. if(paddle2YCenter < ballY){
  62. paddle2Y += 5;
  63. }
  64. else{
  65. paddle2Y -= 5;
  66. }
  67. }
  68.  
  69. function ballReset(){
  70. ballSpeedX = -ballSpeedX;
  71. ballX = canvas.width/2;
  72. ballY = canvas.height/2;
  73. }
  74.  
  75. function moveEverything() {
  76. computerMovement();
  77.  
  78. ballX += ballSpeedX;
  79. ballY += ballSpeedY;
  80.  
  81. if(ballX < 0){
  82. if(ballY > paddle1Y && ballY < paddle1Y+PADDLE_HEIGHT){
  83. ballSpeedX = -ballSpeedX;
  84. }
  85. else{
  86. ballReset();
  87. }
  88. }
  89. if(ballX > canvas.width){
  90. if(ballY > paddle2Y && ballY < paddle2Y+PADDLE_HEIGHT){
  91. ballSpeedX = -ballSpeedX;
  92. }
  93. else{
  94. ballReset();
  95. }
  96. }
  97.  
  98. if(ballY < 0){
  99. ballSpeedY = -ballSpeedY; // -(ballSpeedY) Its multiplying
  100. }
  101. if(ballY > canvas.height){
  102. ballSpeedY = -ballSpeedY;
  103. }
  104. }
  105.  
  106. function drawEverything() {
  107. colorRect(0,0,canvas.width,canvas.height,'black');
  108.  
  109. colorRect(0,paddle1Y,PADDLE_THICKNESS,PADDLE_HEIGHT,'red');
  110. colorRect(canvas.width-PADDLE_THICKNESS,paddle2Y,PADDLE_THICKNESS,PADDLE_HEIGHT,'red');
  111.  
  112. colorCircle(ballX, ballY, 10, 'white');
  113. }
  114.  
  115. function colorCircle(centerX, centerY, radius, drawColor){
  116. canvasContext.fillStyle = drawColor;
  117. canvasContext.beginPath();
  118. canvasContext.arc(centerX, centerY, radius, 0,Math.PI*2, true); // arc allows for circles
  119. canvasContext.fill();
  120. }
  121.  
  122. function colorRect(leftX,topY, width,height, drawColor){
  123. canvasContext.fillStyle = drawColor;
  124. canvasContext.fillRect(leftX,topY, width,height);
  125. }
  126.  
  127. </script>
  128. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement