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 includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

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

Visit your global Editor Settings.

HTML

              
                <div id="canvas-wrapper" class="wrapper"></div>
              
            
!

CSS

              
                body {
  overflow: hidden;
  background: #111;
  color: #fff;
  font-family: monospace;
  font-size:16px;
}

.wrapper {
  position: relative;
}

.dimensions {
  position: absolute;
  top: 20px;
  left: 20px;
  background: #fff;
  color: #222;
  padding: 10px;
  border-radius: 5px;
  opacity: 0.5;
}

.collisions {
  position: absolute;
  bottom: 20px;
  left: 20px;
  color: #fff;
  opacity: 0.2;
  font-family: monospace;
  font-size: 10px;
}
              
            
!

JS

              
                function main()
{
  
  // Grab the window width and height
  w = window.innerWidth;
  h = window.innerHeight;
  
  // Block variables
  size = 40;

  // Collision count
  collisions = 0;
  
  // Add the canvas element to the dom and initiate
  var canvas_wrapper = document.getElementById('canvas-wrapper');
  canvas_wrapper.innerHTML = '';
  canvas_wrapper.innerHTML += '<canvas id="board" width="' + w + '" height="'+h+'"></canvas>';
  // For debugging, show window width and height
  //canvas_wrapper.innerHTML += '<div class="dimensions">' + w + 'px x ' + h + 'px</div>';
  canvas_wrapper.innerHTML += '<div id="collisions_label" class="collisions">COLLISIONS: ' + collisions + '</div>';
  collisions_label = document.getElementById('collisions_label');
  var canvas = document.getElementById('board');
  c = canvas.getContext('2d');
  
  // Create the blocks!
  createBlocks();

  // Create an empty array for the debris
  debris = [];

  // Create the render loop
  renderer = setInterval(function()
  {
    render();
  }, 40);
}

function createBlocks()
{
  blocks = [];
  // Position the first block on the left of the board
  // at a random height position
  x = 0;
  y = Math.floor((Math.random() * h) - size);
  if ( y < 0 )
  {
      y = 0;
  }
  blocks[blocks.length] = {'player':1,'size':size,'x':x,'y':y};
  
  // Position the second block on the right of the board
  // at a random height position
  x = w - size;
  y = Math.floor((Math.random() * h) - size);
  if ( y < 0 )
  {
      y = 0;
  }
  blocks[blocks.length] = {'player':2,'size':size,'x':x,'y':y};
}

// Draw the blocks on the canvas
function drawBlocks()
{
  for (i = 0; i < blocks.length; i++)
  {
    c.fillStyle = 'rgba(255,255,255, 1)';
    c.fillRect(blocks[i].x,blocks[i].y,blocks[i].size,blocks[i].size);
  }
}

function updateBlocks()
{
  // Test: do we have enough blocks/players on the board? If not,
  // recreate the blocks
  if( blocks.length < 1 )
  {
    createBlocks();
    return;
  }

  // Test: have the blocks moved out of play
  // If so remove them
  // Else update the block positions
  for (i = 0; i < blocks.length; i++)
  {
    if(blocks[i].player == 1)
    {
      if(blocks[i].x > w)
      {
          createBlocks();
          return;
      }
      else
      {
        blocks[i].x += size;
      }
    }

    if(blocks[i].player == 2)
    {
      if(blocks[i].x < 0)
      {
          createBlocks();
          return;
      }
      else
      {
        blocks[i].x -= size;
      }
    }

  }

}

function collisionTest()
{
  // Test for collision between blocks
  if(blocks.length > 1)
  {
    if (blocks[0].x <= (blocks[1].x + size)
        && blocks[1].x <= (blocks[0].x + size)
        && blocks[0].y <= (blocks[1].y + size)
        && blocks[1].y <= (blocks[0].y + size)
       )
    {
      collisions++;
      collisions_label.innerHTML = 'COLLISIONS: ' + collisions;
      explodeBlocks( blocks[1].x + (size / 2), blocks[1].y + (size / 2) );
      blocks = [];
      return;
    }
  }

  // Test for collisions bewteen blocks and debris
  if(blocks.length > 0 && debris.length > 0)
  {
    for (b = 0; b < blocks.length; b++)
    {
      for (i = 0; i < debris.length; i++)
      {
        if(typeof blocks[b] != "undefined")
        {  
          if (blocks[b].x <= (debris[i].x + (size / 4) )
            && debris[i].x <= (blocks[b].x + size )
            && blocks[b].y <= (debris[i].y + (size / 4) )
            && debris[i].y <= (blocks[b].y + size )
            )
          {
            explodeBlocks( blocks[b].x + (size / 2), blocks[b].y + (size / 2) );
            debris.splice(i, 1);
            blocks.splice(b, 1);
            collisions++;
            collisions_label.innerHTML = 'COLLISIONS: ' + collisions;
          }
        }
      }
    }
  }

}

function getDebrisDuration()
{
  // Return a random number for the debris duration
  i = Math.floor((Math.random() * 400) + 150);
  return i;
}

function explodeBlocks(x,y)
{
  // Create debris at given location
  debris[debris.length] = {'d':'n','l':getDebrisDuration(),'t':0,'x':x,'y':y}
  debris[debris.length] = {'d':'ne','l':getDebrisDuration(),'t':0,'x':x,'y':y}
  debris[debris.length] = {'d':'e','l':getDebrisDuration(),'t':0,'x':x,'y':y}
  debris[debris.length] = {'d':'se','l':getDebrisDuration(),'t':0,'x':x,'y':y}
  debris[debris.length] = {'d':'s','l':getDebrisDuration(),'t':0,'x':x,'y':y}
  debris[debris.length] = {'d':'sw','l':getDebrisDuration(),'t':0,'x':x,'y':y}
  debris[debris.length] = {'d':'w','l':getDebrisDuration(),'t':0,'x':x,'y':y}
  debris[debris.length] = {'d':'nw','l':getDebrisDuration(),'t':0,'x':x,'y':y}
}

function drawDebris()
{
  for (i = 0; i < debris.length; i++)
  {
    // Set the drebis size
    debrisSize = size / 3;
    // Paint the debris to the canvas
    c.fillStyle = 'rgba(255,255,255, 1)';
    c.fillRect(debris[i].x,debris[i].y,debrisSize,debrisSize);
  }
}

function updateDebris()
{
  // Calculate how far to move debris based on block size
  var distance = size / 4;
  for (i = 0; i < debris.length; i++)
  {
    // Update how far the debris has moved from its starting point
    // If it has moved past its designated length, remove it
    debris[i].t += distance;
    if(debris[i].t > debris[i].l)
    {
      debris.splice(i, 1);
    }
    else
    {
      // Update debris position based on it direction of travel
      switch(debris[i].d)
      { 
        case 'n':
          debris[i].y -= distance;
          break;

        case 'ne':
          debris[i].y -= distance;
          debris[i].x += distance;
          break;

        case 'e':
          debris[i].x += distance;
          break;

        case 'se':
          debris[i].y += distance;
          debris[i].x += distance;
          break;

        case 's':
          debris[i].y += distance;
          break;

        case 'sw':
          debris[i].y += distance;
          debris[i].x -= distance;
          break;

        case 'w':
          debris[i].x -= distance;
          break;

        case 'nw':
          debris[i].y -= distance;
          debris[i].x -= distance;
          break;
        
        default:
          break;
      }
    }
  }
}

var render = function()
{
  // Clear the canvas / paint with solid color
  c.fillStyle = 'rgba(17, 17, 17, 0.6)';
	c.fillRect(0,0,w,h);
  
  // Run the block movement function
  updateBlocks();
  
  // Paint the blocks to the canvas
  drawBlocks();
  
  // Check for collision
  collisionTest();
  
  // Paint the debris to the canvas
  drawDebris();

  // Run the debris movement function
  updateDebris();

  // Reset if collisions count is greater than 1000
  if( collisions > 1000 )
  {
    clearInterval(renderer);
    main();
  }
}

// Did the browser resize? If so, reset.
function resized()
{
  clearInterval(renderer);
  main();
}

// Event listening for window resizing
//window.addEventListener( 'resize', resized );

// Let's do this!
main();

              
            
!
999px

Console