Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const rulesBtn = document.querySelector('#rules-btn');
- const closeBtn = document.querySelector('#close-btn');
- const rules = document.querySelector('.rules');
- const canvas = document.querySelector('#canvas');
- const ctx = canvas.getContext('2d');
- let score = 0;
- const brickRowCount = 9;
- const brickColCount = 5;
- // Create ball props
- const ball = {
- x: canvas.width / 2,
- y: canvas.height / 2,
- size: 10,
- speed: 4,
- dx: 4,
- dy: -4 // to go up
- }
- // Create paddle props
- const paddle = {
- x: canvas.width / 2 - 40,
- y: canvas.height - 20,
- w: 80,
- h: 10,
- speed: 8,
- dx: 0,
- }
- // Create brick props
- const brickInfo = {
- w: 70,
- h: 20,
- padding: 10,
- offsetX: 45, // the first brick
- offsetY: 60,
- visible: true
- }
- // Create bricks
- const bricks = [];
- for (let i = 0; i < brickRowCount; i++) {
- bricks[i] = [];
- for (let j = 0; j < brickColCount; j++) {
- const x = i * (brickInfo.w + brickInfo.padding) + brickInfo.offsetX;
- const y = j * (brickInfo.h + brickInfo.padding) + brickInfo.offsetY;
- bricks[i][j] = { x, y, ...brickInfo };
- }
- }
- function drawBricks() {
- bricks.forEach(column => {
- column.forEach(brick => {
- ctx.beginPath();
- ctx.rect(brick.x, brick.y, brick.w, brick.h);
- ctx.fillStyle = brick.visible ? '#0095dd' : 'transparent';
- ctx.fill();
- ctx.closePath();
- })
- })
- }
- // Draw paddle
- function drawPaddle() {
- ctx.beginPath();
- ctx.rect(paddle.x, paddle.y, paddle.w, paddle.h);
- ctx.fillStle = "#0095dd";
- ctx.fill();
- ctx.closePath();
- }
- // Draw ball
- function drawBall() {
- ctx.beginPath();
- ctx.arc(ball.x, ball.y, ball.size, 0, Math.PI * 2); // outer circle x,y
- ctx.fillStyle = '#0095dd';
- ctx.fill();
- ctx.closePath();
- }
- // Draw score on canvas
- function drawScore() {
- ctx.font = '20px Arial';
- ctx.fillText(`Score: ${score}`, canvas.width - 100, 30);
- }
- function draw() {
- drawBall();
- drawPaddle();
- drawScore();
- drawBricks();
- }
- draw();
- rulesBtn.addEventListener('click', () => {
- rules.classList.add('show');
- });
- closeBtn.addEventListener('click', () => {
- rules.classList.remove('show');
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement