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

              
                <!DOCTYPE html>
<html lang="en">
<body>
    <header>
        <h1>2048</h1>
        <select class="size">
            <option value="4">4x4</option>
            <option value="5">5x5</option>
            <option value="6">6x6</option>
        </select>
        <div class="container">
            <p class="player-score"></p>
            <p class="player-record"></p>
            <button type="button" class="btn-new-game">New game</button>
        </div>
        <p class="game-over"></p>
    </header>
    <section>
        <div class="horizontal-center">
            <svg class="dir-up" viewBox="0 0 320 512" width="50" title="angle-up">
                <path d="M177 159.7l136 136c9.4 9.4 9.4 24.6 0 33.9l-22.6 22.6c-9.4 9.4-24.6 9.4-33.9 0L160 255.9l-96.4 96.4c-9.4 9.4-24.6 9.4-33.9 0L7 329.7c-9.4-9.4-9.4-24.6 0-33.9l136-136c9.4-9.5 24.6-9.5 34-.1z" />
            </svg>
        </div>
        <canvas width="420" height="420"></canvas>
        <div class="horizontal-center">
            <svg class="dir-down" viewBox="0 0 320 512" width="50" title="angle-down">
                <path d="M143 352.3L7 216.3c-9.4-9.4-9.4-24.6 0-33.9l22.6-22.6c9.4-9.4 24.6-9.4 33.9 0l96.4 96.4 96.4-96.4c9.4-9.4 24.6-9.4 33.9 0l22.6 22.6c9.4 9.4 9.4 24.6 0 33.9l-136 136c-9.2 9.4-24.4 9.4-33.8 0z" />
            </svg>
        </div>
        <div class="vertical-center">
            <svg class="dir-left" viewBox="0 0 256 512" width="50" title="angle-left">
                <path d="M31.7 239l136-136c9.4-9.4 24.6-9.4 33.9 0l22.6 22.6c9.4 9.4 9.4 24.6 0 33.9L127.9 256l96.4 96.4c9.4 9.4 9.4 24.6 0 33.9L201.7 409c-9.4 9.4-24.6 9.4-33.9 0l-136-136c-9.5-9.4-9.5-24.6-.1-34z" />
            </svg>
            <svg class="dir-right" viewBox="0 0 256 512" width="50" title="angle-right">
                <path d="M224.3 273l-136 136c-9.4 9.4-24.6 9.4-33.9 0l-22.6-22.6c-9.4-9.4-9.4-24.6 0-33.9l96.4-96.4-96.4-96.4c-9.4-9.4-9.4-24.6 0-33.9L54.3 103c9.4-9.4 24.6-9.4 33.9 0l136 136c9.5 9.4 9.5 24.6.1 34z" />
            </svg>
        </div>
    </section>
    <script src="script.js" defer></script>
</body>
</html>
              
            
!

CSS

              
                :root {
  --black: #2d2c2c;
  --fontGloria: 'Gloria Hallelujah', cursive;
  --block: block;
  --auto: auto;
  --center: center;
  --fontSize: 20px;
}

body {
  background: #cb6bff;
  overflow-y: hidden;
}


p {
  color: var(--black);
}

header h1 {
  text-align: var(--center);
  font-family: var(--fontGloria);
}
header .size {
  background: none;
  width: 10%;
  border:1px solid #fff;
  color: var(--black);
  display: var(--block);
  margin: var(--auto);
}

header .game-over {
  text-align: var(--center);
  margin-top: -15px;
  font-size: var(--fontsize);
  font-family: var(--fontGloria);
}

header > .container {
  display: flex;
  justify-content: var(--center);
  margin-top: 5px;
  padding-left: 20%;
  padding-right: 20%;
}
header > .container > .btn-new-game {
  height:45px;
  width: 15%;
  border: 1px solid #fff;
  background: none;
  color: #fff;
  border-radius: 10px;
  font-family: var(--fontGloria);
  cursor:pointer;
  margin-left: 80px;
  margin-top: 10px;
}
header > .container > .player-score {
  text-align: var(--center);
  font-family: var(--fontGloria);
  font-size: var(--fontsize);
  padding-left: 10px;
}
header > .container > .player-record {
  text-align: var(--center);
  font-family: var(--fontGloria);
  font-size: var(--fontsize);
  background-color: #A4A4A4;
  border-radius: 5px;
  color: #fff;
  width: 30%;
  margin-left: 80px;
}
.title-arrow {
  text-align: var(--center);
  margin-top: 15px;
  font-size: var(--fontsize);
  font-family: var(--fontGloria);
}

section > canvas {
  -moz-animation: changeBackground 10s infinite;
  -webkit-animation: changeBackground 10s infinite; 
  display: var(--block);
  margin: var(--auto);
  margin-top: 5px;
}
@keyframes changeBackground
{
  0%   {background: #F5D0A9;}
  25%  {background: #F2F5A9;}
  50%  {background: #BCF5A9;}
  75%  {background: #A9F5F2;}
  100% {background: #A9A9F5;}
}

section > .horizontal-center {
  display: flex;
  justify-content: var(--center);
}

section > .dir-up,
section > .dir-left,
section > .dir-right,
section > .dir-down {
  cursor: pointer;
}

section > .float-right {
  float: right;
}

section > .vertical-center {
  display: flex;
  justify-content: center;
  margin-top: -360px;
}

section > .vertical-center > .dir-right {
  margin-left: 520px;
}

              
            
!

JS

              
                const element = document.querySelector("canvas");
const ctx = element.getContext('2d');

const playerScoreText = document.querySelector(".player-score");
const gameOver = document.querySelector(".game-over");
const sizeInput = document.querySelector(".size");
const changeSize = document.querySelector(".change-size");
const recordBis = document.querySelector(".player-record");
const newGame = document.querySelector('.btn-new-game');

let playerScore = 0;
let record = 0;
let size = 4;
let width = element.width / size - 7;

let cellArray = [];
let keyPress = true;

let sizes;
let moveRight;
let moveLeft;
let moveUp;
let moveDown;


startGame();

sizeInput.onchange = () => {
    if (sizeInput.value >= 4 && sizeInput.value <= 6) {
        size = sizeInput.value;
        width = element.width / size - 6;
        ctx.clearRect(0, 0, 430, 430);
        playerScore = 0;
        startGame();
    }
}

function cell(row, col) {
    this.value = 0;
    this.x = col * width + 5 * (col + 1);
    this.y = row * width + 5 * (row + 1);
}

function createCell() {
    for (let i = 0; i < size; i++) {
        cellArray[i] = [];
        for (let j = 0; j < size; j++) {
            cellArray[i][j] = new cell(i, j);
        }
    }
}

function createCellBis(cell) {
    ctx.beginPath();
    ctx.rect(cell.x, cell.y, width, width);

    switch (cell.value) {
        case 0:
            ctx.fillStyle = "rgb(66, 66, 64)";
            break;
        case 2:
            ctx.fillStyle = "rgb(246, 215, 100)";
            break;
        case 4:
            ctx.fillStyle = "rgb(149, 246, 100)";
            break;
        case 8:
            ctx.fillStyle = "rgb(100, 229, 246)";
            break;
        case 16:
            ctx.fillStyle = "rgb(140, 100, 246)";
            break;
        case 32:
            ctx.fillStyle = "rgb(215, 171, 11)";
            break;
        case 64:
            ctx.fillStyle = "rgb(215, 11, 11)";
            break;
        case 128:
            ctx.fillStyle = "rgb(43, 145, 251";
            break;
        case 256:
            ctx.fillStyle = "rgb(235, 150, 235)";
            break;
        case 512:
            ctx.fillStyle = "rgb(1, 251, 105";
            break;
        case 1024:
            ctx.fillStyle = "rgb(143, 202, 200)";
            break;
        case 2048:
            ctx.fillStyle = "rgb(225, 224, 0)";
            break;
        case 4096:
            ctx.fillStyle = "rgb(152, 103, 229)";
            break;
    }

    ctx.fill();

    if (cell.value) {
        sizes = width / 2;
        sizeWidth = width / 7;
        ctx.font = sizes + "px 'Roboto', sans-serif";
        ctx.fillStyle = 'rgb(66, 66, 64)';
        ctx.textAlign = "center";
        ctx.fillText(cell.value, cell.x + sizes, cell.y + sizes + sizeWidth);
    }
}

function createAllCell() {
    for (let i = 0; i < size; i++) {
        for (let j = 0; j < size; j++) {
            createCellBis(cellArray[i][j]);
        }
    }
}

function startGame() {
    createCell();
    createAllCell();
    addNewCell();
    addNewCell();
    startRecord();
    funcRecord();
}

function createCookie(cname, cvalue, exdays) {
    let d = new Date();
    d.setTime(d.getTime() + (exdays + 1000 * 60 * 60 * 24 * 365));
    let expires = "expires=" + d.toUTCString();
    document.cookie = cname + "=" + cvalue + "; " + expires + "; path=/";
}

function getCookie(cname) {
    let name = cname + "=";
    let ca = document.cookie.split(';');
    for (let i = 0; i < ca.length; i++) {
        let c = ca[i];
        while (c.charAt(0) == ' ') {
            c = c.substring(1);
        }
        if (c.indexOf(name) == 0) {
            return c.substring(name.length, c.length);
        }
    }
    return "";
}

function funcRecord() {
    if (playerScore > record) {
        record = playerScore;
        createCookie("record", record, 1);
        value = getCookie("record");
        recordBis.innerHTML = "Meilleur score : " + value;
    }
}

function startRecord() {
    let value = getCookie("record");
    record = value;
    recordBis.innerHTML = "Meilleur score : " + value;
}

function addNewCell() {
    let add = 0;
    for (let i = 0; i < size; i++) {
        for (let j = 0; j < size; j++) {
            if (!cellArray[i][j].value) {
                add++;
            } else {
                if (playerScore > record) {
                    funcRecord();
                }
            }
        }
    }
    if (add === 0) return;

    while (true) {
        let row = Math.floor(Math.random() * size);
        let col = Math.floor(Math.random() * size);
        if (!cellArray[row][col].value) {
            cellArray[row][col].value = 2 * Math.ceil(Math.random() * 2);
            createAllCell();
            return true;
        }
    }
}

document.onkeydown = (e) => {
    if (keyPress) {
        if (e.keyCode == 37) onMoveLeft();
        else if (e.keyCode == 38) onMoveUp();
        else if (e.keyCode == 39) onMoveRight();
        else if (e.keyCode == 40) onMoveDown();
        playerScoreText.innerHTML = "Score : " + playerScore;
    }
}

function onMoveRight() {
    moveRight = false;
    for (let i = 0; i < size; i++) {
        for (let j = size - 2; j >= 0; j--) {
            if (cellArray[i][j].value) {
                let col = j;
                while (col + 1 < size) {
                    if (!cellArray[i][col + 1].value) {
                        cellArray[i][col + 1].value = cellArray[i][col].value;
                        cellArray[i][col].value = 0;
                        col++;
                        moveRight = true;
                    } else if (cellArray[i][col].value == cellArray[i][col + 1].value) {
                        cellArray[i][col + 1].value *= 2;
                        playerScore += cellArray[i][col + 1].value;
                        cellArray[i][col].value = 0;
                        col++;
                        moveRight = true;
                        break;
                    } else break;
                }
            }
        }
    }
    if (moveRight === true) addNewCell();
}

function onMoveLeft() {
    moveLeft = false;
    for (let i = 0; i < size; i++) {
        for (let j = 1; j < size; j++) {
            if (cellArray[i][j].value) {
                let col = j;
                while (col - 1 >= 0) {
                    if (!cellArray[i][col - 1].value) {
                        cellArray[i][col - 1].value = cellArray[i][col].value;
                        cellArray[i][col].value = 0;
                        col--;
                        moveLeft = true;
                    } else if (cellArray[i][col].value == cellArray[i][col - 1].value) {
                        cellArray[i][col - 1].value *= 2;
                        playerScore += cellArray[i][col - 1].value;
                        cellArray[i][col].value = 0;
                        moveLeft = true;
                        break;
                    } else break;
                }
            }
        }
    }
    if (moveLeft === true)
        addNewCell();
}

function onMoveUp() {
    moveUp = false;
    for (let j = 0; j < size; j++) {
        for (let i = 1; i < size; i++) {
            if (cellArray[i][j].value) {
                let row = i;
                while (row > 0) {
                    if (!cellArray[row - 1][j].value) {
                        cellArray[row - 1][j].value = cellArray[row][j].value;
                        cellArray[row][j].value = 0;
                        row--;
                        moveUp = true;
                    } else if (cellArray[row][j].value == cellArray[row - 1][j].value) {
                        cellArray[row - 1][j].value *= 2;
                        playerScore += cellArray[row - 1][j].value;
                        cellArray[row][j].value = 0;
                        moveUp = true;
                        break;
                    } else break;
                }
            }
        }
    }
    if (moveUp === true)
        addNewCell();
}

function onMoveDown() {
    moveDown = false;
    for (let j = 0; j < size; j++) {
        for (let i = size - 2; i >= 0; i--) {
            if (cellArray[i][j].value) {
                let row = i;
                while (row + 1 < size) {
                    if (!cellArray[row + 1][j].value) {
                        cellArray[row + 1][j].value = cellArray[row][j].value;
                        cellArray[row][j].value = 0;
                        row++;
                        moveDown = true;
                    } else if (cellArray[row][j].value == cellArray[row + 1][j].value) {
                        cellArray[row + 1][j].value *= 2;
                        playerScore += cellArray[row + 1][j].value;
                        cellArray[row][j].value = 0;
                        moveDown = true;
                        break;
                    } else break;
                }
            }
        }
    }
    if (moveDown === true)
        addNewCell();
}

function arrowDir() {
    document.querySelector(".dir-left").addEventListener("click", () => {
        onMoveLeft();
        playerScoreText.innerHTML = "Score : " + playerScore;
    });
    document.querySelector(".dir-up").addEventListener("click", () => {
        onMoveUp();
        playerScoreText.innerHTML = "Score : " + playerScore;
    });
    document.querySelector(".dir-right").addEventListener("click", () => {
        onMoveRight();
        playerScoreText.innerHTML = "Score : " + playerScore;
    });
    document.querySelector(".dir-down").addEventListener("click", () => {
        onMoveDown();
        playerScoreText.innerHTML = "Score : " + playerScore;
    });
}

arrowDir();

newGame.addEventListener("click", () => {
    playerScore = 0;
    playerScoreText.innerHTML = "Score: " + playerScore;
    startGame();
    if (playerScore > record) funcRecord();
})
              
            
!
999px

Console