Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

HTML

            
              <H3><div id="result">You are Playing Bouncing Ball</div></H3>
<canvas id="canvas" width="1000" height="400"</canvas>

            
          
!

CSS

            
              #canvas { border: 2px solid red}
            
          
!

JS

            
              right = false;
left = false;
var paddleXValue = 0;
running = true;
bricksArray = [];

function onKeyDown(e) {
  if(e.keyCode === 39 ) right = true;
  if(e.keyCode === 37) left = true;
}

function onKeyUp(e) {
  if(e.keyCode === 39 ) right = false;
  if(e.keyCode === 37) left = false;
}

document.addEventListener("keydown", onKeyDown, false);
document.addEventListener("keyup", onKeyUp, false);

var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');

function Ball(x, y, dx, dy, radius, color) {
  if(x === undefined) { x = 50 }
  if(y === undefined) { y = 50 }
  if(dx === undefined) { dx = 2 }
  if(dy === undefined) { dy = 3 }
  if(radius === undefined) { radius = 10 }
  if(color === undefined) { color = 'green' }
  this.radius = radius; this.color = color; this.x = x; this.y = y; this.dx = dx; this.dy = dy;
  this.draw = function() {
    context.save();
    context.beginPath();
    context.arc(this.x, this.y, this.radius, 0, 2*Math.PI, true);
    context.closePath();

    this.y += this.dy;
    this.x += this.dx;
    
    var row = Math.floor(this.y/10);
    var column = Math.floor(this.x/100);
    if(this.y < 100 && row >= 0 && column >= 0 && bricksArray[row][column] === 1) {
      this.dy = -this.dy;
      bricksArray[row][column] = 0;
    }
    
    if(this.x >= canvas.width - this.radius) { this.x = canvas.width - this.radius; this.dx = -this.dx; }
    else if(this.x < 0 + this.radius) { this.x = 0 + this.radius; this.dx = -this.dx; }
    
    if(this.y + this.dy - this.radius < 0) {
      this.dy = -this.dy;
    } else if(this.y  + this.dy + this.radius > canvas.height - 10) {
      if(this.x > paddleXValue && this.x < paddleXValue + 100) {
         this.dx = 2 * ((this.x - ((paddleXValue + 100) / 2))/100)
         this.dy = -this.dy;
      } else if (this.y + this.dy + this.radius > canvas.height) {
          running = false;
      }
    }
    context.fillStyle = this.color; context.fill(); context.restore();
  }
}

function Paddle() {
  this.paddleWidth = 100;
  this.draw = function(x) {
    context.save();
    context.fillRect(paddleXValue, 385, this.paddleWidth, 15);
    context.restore();
  }
  this.paddleMove = function() {
    if(right) { 
      paddleXValue += 10 
      if(paddleXValue + this.paddleWidth > canvas.width) { paddleXValue = canvas.width - this.paddleWidth }
    }
    else if(left) { 
      paddleXValue -= 10 
      if(paddleXValue < 0) { paddleXValue = 0 }
    }
  }
}

function Bricks() {
  this.width = 99;
  this.columns = 10;
  this.rows = 10;
  this.height = 9;
  this.x = 0;
  this.y = 0;
  this.padding = 1;
  
  this.initBrickArray = function() {
    for(i = 0; i < this.rows; i++) {
      bricksArray[i] = new Array(this.columns);
      for(j = 0; j < this.columns; j++) {
        bricksArray[i][j] = 1;
      }
    }
  }
  
  this.draw = function() {
    context.save();
    var x1 = 0;
    var y1 = 0;
    for(i = 0; i < this.rows; i++) {
      y1 = 0;
      for(j = 0; j < this.columns; j++) {
        if(bricksArray[i][j] === 1) {
          context.fillRect(y1 + this.padding, x1 + this.padding, this.width, this.height);
          y1 += this.width + this.padding;
        }
        else {
          y1 += this.width + this.padding;
        }
      }
      x1 += this.height + this.padding;
    }
  }
}

ball = new Ball(100,120,3,2,10,'green');
paddle = new Paddle();
bricks = new Bricks();
bricks.initBrickArray();

function animateFunction() {
  context.clearRect(0, 0, canvas.width, canvas.height);
  bricks.draw();
  ball.draw();
  paddle.paddleMove();
  paddle.draw(paddleXValue);
  if(running) {
    requestAnimationFrame(animateFunction);
  }
  else {
    var count = 0;
    for(var i = 0; i < bricks.rows; i++) {
      for(var j = 0; j < bricks.columns; j++) {
        if(bricksArray[i][j] === 0) {
          count += 1;
        }
      }
    }
    document.getElementById("result").innerHTML = "Game Ended, you Scored " + count;
  }
}
requestAnimationFrame(animateFunction);
            
          
!
999px

Console