Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const rulesBtn = document.querySelector('#rules-btn');
  2. const closeBtn = document.querySelector('#close-btn');
  3. const rules = document.querySelector('.rules');
  4. const canvas = document.querySelector('#canvas');
  5. const ctx = canvas.getContext('2d');
  6.  
  7. let score = 0;
  8. const brickRowCount = 9;
  9. const brickColCount = 5;
  10.  
  11. // Create ball props
  12. const ball = {
  13.     x: canvas.width / 2,
  14.     y: canvas.height / 2,
  15.     size: 10,
  16.     speed: 4,
  17.     dx: 4,
  18.     dy: -4 // to go up
  19. }
  20.  
  21. // Create paddle props
  22. const paddle = {
  23.     x: canvas.width / 2 - 40,
  24.     y: canvas.height - 20,
  25.     w: 80,
  26.     h: 10,
  27.     speed: 8,
  28.     dx: 0,
  29. }
  30.  
  31. // Create brick props
  32. const brickInfo = {
  33.     w: 70,
  34.     h: 20,
  35.     padding: 10,
  36.     offsetX: 45, // the first brick
  37.     offsetY: 60,
  38.     visible: true
  39. }
  40.  
  41. // Create bricks
  42. const bricks = [];
  43. for (let i = 0; i < brickRowCount; i++) {
  44.     bricks[i] = [];
  45.     for (let j = 0; j < brickColCount; j++) {
  46.         const x = i * (brickInfo.w + brickInfo.padding) + brickInfo.offsetX;
  47.         const y = j * (brickInfo.h + brickInfo.padding) + brickInfo.offsetY;
  48.         bricks[i][j] = { x, y, ...brickInfo };
  49.     }
  50. }
  51.  
  52. function drawBricks() {
  53.     bricks.forEach(column => {
  54.         column.forEach(brick => {
  55.             ctx.beginPath();
  56.             ctx.rect(brick.x, brick.y, brick.w, brick.h);
  57.             ctx.fillStyle = brick.visible ? '#0095dd' : 'transparent';
  58.             ctx.fill();
  59.             ctx.closePath();
  60.         })
  61.     })
  62. }
  63.  
  64. // Draw paddle
  65. function drawPaddle() {
  66.     ctx.beginPath();
  67.     ctx.rect(paddle.x, paddle.y, paddle.w, paddle.h);
  68.     ctx.fillStle = "#0095dd";
  69.     ctx.fill();
  70.     ctx.closePath();
  71. }
  72.  
  73.  
  74. // Draw ball
  75. function drawBall() {
  76.     ctx.beginPath();
  77.     ctx.arc(ball.x, ball.y, ball.size, 0, Math.PI * 2); // outer circle  x,y
  78.     ctx.fillStyle = '#0095dd';
  79.     ctx.fill();
  80.     ctx.closePath();
  81. }
  82.  
  83. // Draw score on canvas
  84. function drawScore() {
  85.     ctx.font = '20px Arial';
  86.     ctx.fillText(`Score: ${score}`, canvas.width - 100, 30);
  87. }
  88.  
  89. function draw() {
  90.     drawBall();
  91.     drawPaddle();
  92.     drawScore();
  93.     drawBricks();
  94. }
  95.  
  96. draw();
  97.  
  98.  
  99.  
  100.  
  101.  
  102. rulesBtn.addEventListener('click', () => {
  103.     rules.classList.add('show');
  104. });
  105.  
  106.  
  107. closeBtn.addEventListener('click', () => {
  108.     rules.classList.remove('show');
  109. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement