Advertisement
Guest User

game.js

a guest
May 20th, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const canvas = document.getElementById('game');
  2. const ctx = canvas.getContext('2d');
  3.  
  4. const sizes = {
  5.   ball: 10,
  6.   paddle: { width: 75, height: 10 },
  7. };
  8. const colors = {
  9.   ball: '#ff0000',
  10.   paddle: '#0000ff',
  11.   text: '#020202',
  12. };
  13. let positions = {
  14.   ball: { x: canvas.width / 2, y: canvas.height - (sizes.ball * 3) },
  15.   paddle: { x: (canvas.width - sizes.paddle.width) / 2, y: canvas.height - sizes.paddle.height },
  16. };
  17. let velocity = {
  18.   ball: { x: 2, y: -2 },
  19.   paddle: { x: 5 },
  20. };
  21. let keyStates = {
  22.   pressed: { left: false, right: false },
  23. };
  24. let score = 0;
  25. let lost = false;
  26.  
  27. function drawBall() {
  28.   ctx.beginPath();
  29.   ctx.arc(positions.ball.x, positions.ball.y, sizes.ball, 0, Math.PI * 2);
  30.   ctx.fillStyle = colors.ball;
  31.   ctx.fill();
  32.   ctx.closePath();
  33. }
  34.  
  35. function drawPaddle() {
  36.   ctx.beginPath();
  37.   ctx.rect(positions.paddle.x, positions.paddle.y, sizes.paddle.width, sizes.paddle.height);
  38.   ctx.fillStyle = colors.paddle;
  39.   ctx.fill();
  40.   ctx.closePath();
  41. }
  42.  
  43. function drawScore() {
  44.   ctx.font = "30px Arial";
  45.   ctx.fillStyle = colors.text;
  46.   ctx.fillText(`Score: ${score}`, 10, 30);
  47. }
  48.  
  49. function updateBall() {
  50.   if (
  51.     positions.ball.x + velocity.ball.x > canvas.width - sizes.ball ||
  52.     positions.ball.x + velocity.ball.x < sizes.ball
  53.   ) {
  54.     velocity.ball.x = -velocity.ball.x;
  55.   }
  56.  
  57.   const collideWithPlayer = (
  58.     positions.ball.y + velocity.ball.y > canvas.height - sizes.paddle.height - sizes.ball &&
  59.     positions.ball.x + velocity.ball.x > positions.paddle.x &&
  60.     positions.ball.x + velocity.ball.x < positions.paddle.x + sizes.paddle.width
  61.   );
  62.  
  63.   if (positions.ball.y < sizes.ball || collideWithPlayer) {
  64.     if (collideWithPlayer) {
  65.       score += 1;
  66.       velocity.ball.x += Math.random();
  67.       velocity.ball.y += Math.random();
  68.       velocity.paddle.x = Math.abs(velocity.ball.x * 2.5);
  69.     }
  70.  
  71.     velocity.ball.y = -velocity.ball.y;
  72.   } else if (positions.ball.y - sizes.ball > canvas.height && !lost) {
  73.     lost = true;
  74.     alert(`You win with ${score} points!`);
  75.     window.location.reload();
  76.   }
  77.  
  78.   positions.ball.x += velocity.ball.x;
  79.   positions.ball.y += velocity.ball.y;
  80. }
  81.  
  82. function updatePaddle() {
  83.   if (keyStates.pressed.left && positions.paddle.x > 0) {
  84.     positions.paddle.x -= velocity.paddle.x;
  85.   }
  86.  
  87.   if (keyStates.pressed.right && positions.paddle.x + sizes.paddle.width < canvas.width) {
  88.     positions.paddle.x += velocity.paddle.x;
  89.   }
  90. }
  91.  
  92. function update() {
  93.   updateBall();
  94.   updatePaddle();
  95. }
  96.  
  97. function draw() {
  98.   ctx.clearRect(0, 0, canvas.width, canvas.height);
  99.   drawBall();
  100.  
  101.   positions.ball.x += velocity.ball.x;
  102.   positions.ball.y += velocity.ball.y;
  103.  
  104.   drawPaddle();
  105.   drawScore();
  106.   update();
  107.   requestAnimationFrame(draw);
  108. }
  109.  
  110. function keyDownHandler(event) {
  111.   if (event.keyCode === 37) {
  112.     keyStates.pressed.left = true;
  113.   } else if (event.keyCode === 39) {
  114.     keyStates.pressed.right = true;
  115.   }
  116. }
  117.  
  118. function keyUpHandler(event) {
  119.   if (event.keyCode === 37) {
  120.     keyStates.pressed.left = false;
  121.   } else if (event.keyCode === 39) {
  122.     keyStates.pressed.right = false;
  123.   }
  124. }
  125.  
  126. function setup() {
  127.   ctx.mozImageSmoothingEnabled = true;
  128.   ctx.webkitImageSmoothingEnabled = true;
  129.   ctx.msImageSmoothingEnabled = true;
  130.   ctx.imageSmoothingEnabled = true;
  131.  
  132.   document.addEventListener('keydown', keyDownHandler, false);
  133.   document.addEventListener('keyup', keyUpHandler, false);
  134.  
  135.   requestAnimationFrame(draw);
  136. }
  137.  
  138. setup();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement