Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ 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

Auto Save

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

              
                <!-- This pen is a bit upgraded version of Ania Kubow's 2048 game -->

<!-- font -->
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@700&display=swap" rel="stylesheet">

<span id="score__label">Score:</span>
<div id="score">0</div>
<div class="grid"></div>

              
            
!

CSS

              
                body{
    height: 100vh;
    display: flex;
    flex-flow: column;
    justify-content: center;
    align-items: center;
    background-color: #FAF8EF;
    overflow: hidden;
}

.grid{
    width: 400px;
    height: 400px;
    display: flex;
    flex-wrap: wrap;
    background-color: #BBADA0;
    border: 5px solid #BBADA0;
}

.grid div{
    font-family: Roboto;
    box-sizing: border-box;
    min-width: 100px;
    height: 100px;
    background-color: #CCC0B3;
    border: 5px solid #BBADA0;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 40px;
}

#score {
    display: flex;
    border: 5px solid #BBADA0;
    justify-content: center;
    align-items: center;
    background-color: #BBADA0;
    color: white;
    width: 400px;
    height: 50px;
    margin-bottom: 10px;
    font-family: Roboto;
    font-size: 30px;
}

#score__label{
    font-family: Roboto;
    font-size: 20px;
}

.zero{
    background-color: #CCC0B3 !important;
    color: black !important;
}

.two{
    background-color: #EEE4DA !important;
    color: #776E65 !important;
}

.four{
    background-color: #EDE0C8 !important;
    color: #776E65 !important;
}

.eight{
    background-color: #F2B179 !important;
    color: #F9F6F2 !important;
}

.sixteen{
    background-color: #F59563 !important;
    color: #F9F6F2 !important;
}

.thirtytwo{
    background-color: #F67C5F !important;
    color: #F9F6F2 !important;
}

.sixtyfour{
    background-color: #F65E3B !important;
    color: #F9F6F2 !important;
}

.onetwoeight{
    background-color: #EDCF73 !important;
    color: #F9F6F2 !important;
}

.twofivesix{
    background-color: #EDCC62 !important;
    color: #F9F6F2 !important;
}

.fiveonetwo{
    background-color: #EDC850 !important; 
    color: #F9F6F2 !important;
}

.thousandtwofour{
    background-color: #EDC53F !important;
    color: #F9F6F2 !important;
}

.twothousandfoureight{
    background-color: #EDC22D !important;
    color: #F9F6F2 !important;
}
              
            
!

JS

              
                window.addEventListener('DOMContentLoaded', (event) => {
    const mainGrid = document.querySelector('.grid');
    const scoreField = document.getElementById('score');
    const width = 4;
    let gridSlots = [];
    let score = 0;

    (function createGrid() {
        for (let i = 0; i < 16; i++) {
            const gridSlot = document.createElement("div");
            gridSlot.innerHTML = 0;
            mainGrid.appendChild(gridSlot);
            gridSlots.push(gridSlot);
        }
        generate();
        generate();
        colorNumbers();

    })();

    function generate() {

        let randomNumber = Math.floor(Math.random() * 16);

        let randomSpot = gridSlots[randomNumber];

        if (parseInt(randomSpot.innerHTML) == 0) {
            randomSpot.innerHTML = 2;
        } else 
            generate();
        }
    
    function swipeRight() {
        for (let i = 0; i < 16; i++) {
            if (i % 4 == 0) {
                let first = parseInt(gridSlots[i].innerHTML);
                let second = parseInt(gridSlots[i + 1].innerHTML);
                let third = parseInt(gridSlots[i + 2].innerHTML);
                let fourth = parseInt(gridSlots[i + 3].innerHTML);

                let row = [first, second, third, fourth];

                let filteredRow = row.filter(num => num);
                let zeroes = Array(4 - filteredRow.length).fill(0);
                let newRow = [
                    ...zeroes,
                    ...filteredRow
                ];

                console.log(newRow);

                gridSlots[i].innerHTML = newRow[0];
                gridSlots[i + 1].innerHTML = newRow[1];
                gridSlots[i + 2].innerHTML = newRow[2];
                gridSlots[i + 3].innerHTML = newRow[3];
            }

        }
    }

    function swipeLeft() {
        for (let i = 0; i < 16; i++) {
            if (i % 4 == 0) {
                let first = parseInt(gridSlots[i].innerHTML);
                let second = parseInt(gridSlots[i + 1].innerHTML);
                let third = parseInt(gridSlots[i + 2].innerHTML);
                let fourth = parseInt(gridSlots[i + 3].innerHTML);

                let row = [first, second, third, fourth];

                let filteredRow = row.filter(num => num);
                let zeroes = Array(4 - filteredRow.length).fill(0);
                let newRow = [
                    ...filteredRow,
                    ...zeroes
                ];

                console.log(newRow);

                gridSlots[i].innerHTML = newRow[0];
                gridSlots[i + 1].innerHTML = newRow[1];
                gridSlots[i + 2].innerHTML = newRow[2];
                gridSlots[i + 3].innerHTML = newRow[3];
            }

        }
    }

    function swipeDown() {
        for (let i = 0; i < 4; i++) {
            let totalOne = gridSlots[i].innerHTML
            let totalTwo = gridSlots[i + width].innerHTML
            let totalThree = gridSlots[i + (width * 2)].innerHTML
            let totalFour = gridSlots[i + (width * 3)].innerHTML
            let column = [parseInt(totalOne), parseInt(totalTwo), parseInt(totalThree), parseInt(totalFour)]

            let filteredColumn = column.filter(num => num)
            let missing = 4 - filteredColumn.length
            let zeros = Array(missing).fill(0)
            let newColumn = zeros.concat(filteredColumn)

            gridSlots[i].innerHTML = newColumn[0]
            gridSlots[i + width].innerHTML = newColumn[1]
            gridSlots[i + (width * 2)].innerHTML = newColumn[2]
            gridSlots[i + (width * 3)].innerHTML = newColumn[3]
        }
    }

    function swipeUp() {
        for (let i = 0; i < 4; i++) {
            let totalOne = gridSlots[i].innerHTML;
            let totalTwo = gridSlots[i + width].innerHTML;
            let totalThree = gridSlots[i + (width * 2)].innerHTML;
            let totalFour = gridSlots[i + (width * 3)].innerHTML;
            let column = [parseInt(totalOne), parseInt(totalTwo), parseInt(totalThree), parseInt(totalFour)];

            let filteredColumn = column.filter(num => num);
            let missing = 4 - filteredColumn.length;
            let zeros = Array(missing).fill(0);
            let newColumn = filteredColumn.concat(zeros);

            gridSlots[i].innerHTML = newColumn[0];
            gridSlots[i + width].innerHTML = newColumn[1];
            gridSlots[i + (width * 2)].innerHTML = newColumn[2];
            gridSlots[i + (width * 3)].innerHTML = newColumn[3];
        }
    }

    function addRow() {
        for (let i = 0; i < 15; i++) {
            if (gridSlots[i].innerHTML === gridSlots[i + 1].innerHTML) {
                let sum = parseInt(gridSlots[i].innerHTML) + parseInt(gridSlots[i + 1].innerHTML);

                gridSlots[i].innerHTML = sum;
                gridSlots[i + 1].innerHTML = 0;
                score += sum;
                scoreField.innerHTML = score;
            }
        }
    }

    function addColumn() {
        for (let i = 0; i < 12; i++) {
            if (gridSlots[i].innerHTML === gridSlots[i + width].innerHTML) {
                let sum = parseInt(gridSlots[i].innerHTML) + parseInt(gridSlots[i + width].innerHTML);
                gridSlots[i].innerHTML = sum;
                gridSlots[i + width].innerHTML = 0;
                score += sum;
                scoreField.innerHTML = score;
            }
        }
    }


    function colorNumbers(){
        for (let i = 0; i < 16; i++){
            switch(parseInt(gridSlots[i].innerHTML)){
                case 0:
                    gridSlots[i].classList = 'zero';
                    break;

                case 2:
                    gridSlots[i].classList = 'two';
                    break;
                
                case 4:
                    gridSlots[i].classList = 'four';
                    break;
                
                case 8:
                    gridSlots[i].classList = 'eight';
                    break;

                case 16:
                    gridSlots[i].classList = 'sixteen';
                    break;
                
                case 32:
                    gridSlots[i].classList = 'thirtytwo';
                    break;
                
                case 64:
                    gridSlots[i].classList = 'sixtyfour';
                    break;

                case 128:
                    gridSlots[i].classList = 'onetwoeight';
                    break;

                case 256:
                    gridSlots[i].classList = 'twofivesix';
                    break;

                case 512:
                    gridSlots[i].classList = 'fiveonetwo';
                    break;
                
                case 1024:
                    gridSlots[i].classList = 'thousandtwofour';
                    break;

                case 2048:
                    gridSlots[i].classList = 'twothousandfoureight';
                    break;
            }
        }
    }

    function updateScore(){
        scoreField.innerHTML = score;
    }

    document.addEventListener('keyup', e => {
        /* go through all numbers and give them color classes */
        

        if (e.key === 'ArrowRight') {
            keyRight();
        }

        if (e.key === 'ArrowLeft') {
            keyLeft();
        }

        if (e.key === 'ArrowUp') {
            keyUp();
        }

        if (e.key === 'ArrowDown') {
            keyDown();
        }
        colorNumbers();
        updateScore();
    })

    function keyRight() {
        swipeRight();
        addRow();
        swipeRight();
        generate();
    }

    function keyLeft() {
        swipeLeft();
        addRow();
        swipeLeft();
        generate();
    }

    function keyDown() {
        swipeDown();
        addColumn();
        swipeDown();
        generate();
        
    }

    function keyUp() {
        swipeUp();
        addColumn();
        swipeUp();
        generate();
    }

});
              
            
!
999px

Console