LOADING...

Preview

Pen ID
Unlock Campus Themeforest adv

 

Code
CSS
* {
  padding: 0;
  margin: 0;
}

canvas {  
  background: #eee;
  display: block;
  margin: 0 auto;  
}
JS
// Declare variables to reference the canvas and context:
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");

// Declare a variable to record the player's score:
var score = 0;

// Declare a variable to record the number of player lives:
var lives = 3;

// Declare a variable to define the radius of the ball:
var ballRadius = 10;
// Declare variables to define the starting drawing point of the ball:
var x = canvas.width/2;
var y = canvas.height-30;
// Declare variables to help redraw the ball after every frame:
var dx = 2;
var dy = -2;

// Declare variables to define the paddle:
var paddleHeight = 10;
var paddleWidth = 75;
var paddleX = (canvas.width-paddleWidth)/2;
// Declare variables to help initialize key presses:
var rightPressed = false;
var leftPressed = false;

// Declare variables to define the bricks:
var brickRowCount = 3;
var brickColumnCount = 5;
var brickWidth = 75;
var brickHeight = 20;
var brickPadding = 10;
var brickOffsetTop = 30;
var brickOffsetLeft = 30;

// Declare a two dimensional array to contain all of our bricks:
var bricks = [];
for (c=0; c 0 && relativeX < canvas.width) {
        paddleX = relativeX - paddleWidth/2;
    }
}

// Declare a function to move the paddle when a key is pressed:
function keyDownHandler(e) {
  if(e.keyCode == 39) {
      rightPressed = true;
  }
  else if(e.keyCode == 37) {
      leftPressed = true;
  }
}

// Declare a function to stop the paddle when a key is released:
function keyUpHandler(e) {
  if(e.keyCode == 39) {
      rightPressed = false;
  }
  else if(e.keyCode == 37) {
      leftPressed = false;
  }
}

// Declare a function to draw the ball:
function drawBall() {
  ctx.beginPath();
  // Specify the shape and dimensions of the object:
  ctx.arc(x, y, ballRadius, 0, Math.PI*2);
  // Specify and apply the color of the object:
  ctx.fillStyle = "#0095dd";
  ctx.fill();
  ctx.closePath();
}

// Declare a function to draw the paddle:
function drawPaddle() {
  ctx.beginPath();
  // Specify the shape and dimensions of the object:  
  ctx.rect(paddleX, canvas.height-paddleHeight, paddleWidth, paddleHeight);
  // Specify and apply the color of the object:
  ctx.fillStyle = "#0095dd";
  ctx.fill();
  ctx.closePath();
}

// Declare a function to draw the bricks:
function drawBricks() {
  for(c=0; c b.x && x < b.x+brickWidth && y > b.y && y < b.y+brickHeight) {
          dy = -dy;
          b.status = 0;
          score++;
          if(score == brickRowCount*brickColumnCount) {
             alert("YOU WIN, CONGRATULATIONS!");
             document.location.removedByCodePen();
          }
        }
      }
    }
  }
}

// Declare a function for drawing the player's score on the screen:
function drawScore() {
  ctx.font = "16px Arial";
  ctx.fillStyle = "#0095dd";
  ctx.fillText("Score: "+score, 8, 20);
}

// Declare a function for drawing the number of player lives on the screen:
function drawLives() {
    ctx.font = "16px Arial";
    ctx.fillStyle = "#0095DD";
    ctx.fillText("Lives: "+lives, canvas.width-65, 20);
}

// Declare a function for the drawing code to be used for all objects:
function draw() {
  // Clear the canvas before each frame: 
  ctx.clearRect(0, 0, canvas.width, canvas.height);
  // Now, execute the drawing functions of the individual objects:
  drawBricks();
  drawBall();
  drawPaddle();
  drawScore();
  drawLives();
  // Detect ball collision with bricks and adjust movement direction, if necessary:
  collisionDetection();
  // Detect ball collision with walls and adjust movement direction, if necessary:
  if(x + dx > canvas.width-ballRadius || x + dx < ballRadius) {
    dx = -dx;
  }
  if(y + dy < ballRadius) {
    dy = -dy;
  }
  // Check to see if the ball gets past the paddle:
  else if(y + dy > canvas.height-ballRadius) {
    if(x > paddleX && x < paddleX + paddleWidth) {
      dy = -dy;
    }
    else {
      lives--;
      if(!lives) {
        // The losing condition:
        alert("GAME OVER");
        document.location.removedByCodePen();
      }
      else {
        x = canvas.width/2;
        y = canvas.height-30;
        dx = 3;
        dy = -3;
        paddleX = (canvas.width-paddleWidth)/2;
      }
    }
  }
  // Check if either movement key is currently pressed and adjust movement direction, if necessary, while detecting collison:
  if(rightPressed && paddleX < canvas.width-paddleWidth) {
    paddleX += 7;
  }
  else if(leftPressed && paddleX > 0) {
    paddleX -= 7;
  }
  // Incrementally adjust the draw position of the ball:
  x += dx;
  y += dy;
  requestAnimationFrame(draw);
}

// Declare the speed at which our primary drawing function will be executed:
draw();
Term
Sat, 04/14/2018 - 20:42

Related Codes

Pen ID
Pen ID
Pen ID
Square Adv