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

              
                <h1>Tetris Board</h1>

<p class='description'>
  A skeleton look at the construction of a Tetris board. Use the keyboard arrows to move the shape around the board. For more functionality, you can check out my complete article right over at <a href='https://www.thatsoftwaredude.com/content/8519/coding-tetris-in-javascript-part-1' target='_blank'>thatsoftwaredude.com</a>.
</p>

<div class='center'>
  <a href='javascript:void(0)' class='btn' onclick='start()'>New shape</a>
</div>

<div class="tetris-board"></div>
              
            
!

CSS

              
                body{
  background:black;
  color:white;
}

a{
  color:dodgerblue;
}

.center{
  margin-bottom: 20px;
  text-align:center;
}

.btn{
  background:gold;
  color:black;
  text-decoration:none;
  padding: 10px 20px;
  display:inline-block;
  border-radius: 6px;
  font-weight:bold;
}

.description{
  text-align:center;
  padding: 2%;
  font-size: 16pt;
}
h1{
  text-align:center;
  font-size: 40pt;
  letter-spacing: -3px;
}
.tetris-board
{
  margin: auto;
  text-align:center;
  width: 500px;
}

.tetris-board .block
{
    width: 40px;
    height: 40px;
    border:solid 1px #ddd;
    display:inline-block;
    font-size:10pt;
    color:white;
    text-align:center;
  padding:5px;
}

.tetris-board .row
{
    margin: 0px;
    padding: 0px;
    display: flex;
}

.tetris-board .floor .block
{
    background:#333;
}

.tetris-board .block.filled
{
    background-color: black;
    color:white;
}
              
            
!

JS

              
                var shapes = new Array();
var currentShape;
var height = 15;
var width = 10;
var state = 1;      // 1 running - 0 paused - 2 game over
var colors = ['white', 'orange', 'red', 'dodgerblue'];
var move = 0;
var occupiedblocks = new Array();
var direction = "";
var points = 0;

function createBoard()
{
    var board = document.getElementsByClassName('tetris-board')[0];
    board.innerHTML = '';
    var counter = 0;
    for (var y = 0; y < height; y++)
    {
        var row = document.createElement('div');
        row.className = 'row';
        row.dataset.row = y;
        
        for (var x = 0; x < width; x++)
        {
            var block = document.createElement('div');
            block.className = 'block';
            block.dataset.x = x;
            block.dataset.y = y;
            block.dataset.index = counter;
            block.dataset.state = 0;
            block.innerHTML = "0 : " + counter;
            row.appendChild(block);
            counter++;
        }
        
        board.appendChild(row);
    }
}

function createShapes()
{
    var other = [[1, 0], [0,1], [1,1],[2,1]]; // 't' shape
    var line = [[0, 0], [0, 1], [0, 2], [0, 3]]; // line
    var square = [[0, 0], [0, 1], [1, 0], [1, 1]]; // 4 x 4 square
    var l = [[2,0], [0, 1], [1, 1], [2,1]]; // 'L' shape
    
    shapes.push(square);
    shapes.push(line);
    shapes.push(other);
    shapes.push(l);
}

function createShape()
{
    var randomShape = Math.floor(Math.random() * shapes.length);
    var randomColor = Math.floor(Math.random() * colors.length);
    var center = Math.floor(width / 2);
    var shape = shapes[randomShape];
    var location = [center, 0];

    currentShape = {
        shape: shape,
        color: colors[randomColor],
        location: location,
        indexes: getBlockNumbers(shape, location)
    };
}
  
  function getBlockNumbers(a,b){
    // placeholder for now
  }

function drawShape()
{
    // draw the current shape onto board
    var shape = currentShape.shape;
    var location = currentShape.location;

    // update status to unoccupied of current block
    clearCurrent();

    // based on direction of block, set the offset
    if (direction=="down")
        currentShape.location[1]++;
    else if(direction=="left")
        currentShape.location[0]--;
    else if (direction=="right")
        currentShape.location[0]++;
    
    // redraw the shape onto the board
    for(var i = 0; i < shape.length; i++)
    {
        var x = shape[i][0] + location[0];    //  x + offset
        var y = shape[i][1] + location[1];    // y + offset
        var block = document.querySelector('[data-x="' + x + '"][data-y="' + y + '"]');
        block.classList.add('filled');
        block.style.backgroundColor = currentShape.color;
    }

    currentShape.indexes = getBlockNumbers(currentShape.shape, currentShape.location);
}

function clearCurrent()
{
    // reset all blocks
    var shape = currentShape.shape;
    var location = currentShape.location;
    
    for(var i = 0; i < shape.length; i++)
    {
        var x = shape[i][0] + location[0];
        var y = shape[i][1] + location[1];
        var block = document.querySelector('[data-x="' + x + '"][data-y="' + y + '"]');
        
        block.classList.remove('filled');
        block.style.backgroundColor="";
    }
}

function checkKey(e) {
    e.preventDefault();

    e = e || window.event;

    if (e.keyCode == '40') {
        // down arrow
        direction="down";
    }
    else if (e.keyCode == '37') {
        // left arrow
        direction="left";
    }
    else if (e.keyCode == '39') {
        // right arrow
        direction="right";
    }

    drawShape();
}

function start()
{
    createBoard();
    createShapes();
    createShape();
    drawShape();
    document.onkeydown = checkKey;
}

window.addEventListener('load', function(){
    start();
});
              
            
!
999px

Console