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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ 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

              
                <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="./style.css">
    <title>Skull Game</title>
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Silkscreen:wght@400;700&display=swap" rel="stylesheet"> 
</head>
<body>
    <main>
        <section class="game-container">
<!--             <h1> 
                <span>Skull</span> Game
            </h1> -->
            <canvas class="game" id="game"></canvas>
            <div class="buton-container">
                <button id="up" class="">Arriba</button>
                <button id="left">Izquierda</button>
                <button id="right">Derecha</button>
                <button id="down">Abajo</button>
            </div>
            <div class="messages-container">
                <p><span>Vidas</span> <span id="lives"></span></p>
                <p><span>Tiempo</span> <span id="time">0.00</span></p>
                <p><span>Record</span> <span id="timeRecord" class=""></span></p>
            </div>
        </section>
    </main>
    <script src="./maps.js"></script>
    <script src="./game.js"></script>
</body>
</html>
              
            
!

CSS

              
                *{
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    font-family: 'Silkscreen', cursive;
}
:root{
    font-size: 62.5%;
    --azul: #10162f;
    --blanco: #ffffff;
    --negro: #10162f;
}
main{
    overflow-x: hidden;
}
section.game-container{
    width: 100vw;
    min-height: 100vh;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 10px 0;
}
h1{
    font-size: 3.4rem;
    font-weight: 900;
    color: var(--blanco);
    background-color: #6400e4;
    padding: 12px;
    border-radius: 6px;
    margin-top: 10px;
    margin-bottom: 20px;
}
h1 span{
    display: inline-block;
    padding: 3px;
    background-color: var(--negro);
}
canvas.game{
    box-sizing: initial;
    border: 2px solid var(--azul);
    box-shadow: -6px 6px 0 var(--negro);
    background-color: var(--blanco);
    margin: 0 auto;
    margin-top: 10px;
    padding: 0px 4px 4px 4px;
}
.buton-container{
    width: 100vw;
    max-width: 900px;
    height: 55px;
    display: flex;
    align-items: center;
    justify-content: center;
    column-gap: 20px;
    margin-top: 20px;
}
.buton-container button{
    width: 150px;
    height: 55px;
    border-radius: 4px;
    border: 2px solid #3a10e5;
    background-color: #3a10e5;
    color: var(--blanco);
    font-size: 1.6rem;
    font-weight: bold;
    line-height: 1.1;
    box-shadow: -6px 6px 0 var(--negro);
}
.buton-container button:hover{
    box-shadow: -8px 8px 0 var(--negro);
}
.messages-container{
    width: 700px;
    display: flex;
    align-items: center;
    justify-content: center;
    margin: 20px auto 0 auto;
    gap: 4px;
}
.messages-container p{
    padding: 8px;
    color: var(--blanco);
    border-radius: 6px;
    font-size: 1.6rem;
    min-width: 240px;
    display: flex;
    justify-content: center;
    border: 1px solid #6400e4;
}
.messages-container p span:nth-child(1){
    min-width: 86px;
    text-align: center;
    margin-right: 6px;
}
p span:not(#timeRecord){
    display: inline-block;
    padding: 8px;
    background-color: var(--negro);
    border-radius: 4px;
}
p span.timeRecord{
    display: inline-block;
    padding: 8px;
    background-color: var(--negro);
    border-radius: 4px;
}
@media (max-width: 760px){
    .messages-container{
        width: 100%;
        flex-direction: column;
    }
}
@media (max-width: 800px) {
    .buton-container{
        width: 100%;
        max-width: 350px;
        height: auto;
        display: grid;
        grid-template-columns: repeat(2,1fr);
        grid-template-rows: repeat(3,1fr);
        padding: 0 6px;
        gap: 15px;
    }
    .buton-container button{
        width: 90%;
        place-self: center;
    }
    .buton-container button:nth-child(1),.buton-container button:nth-child(4){
        width: 50%;
    }
    button#up{
        grid-column-start: 1;
        grid-column-end: 3;
    }
    button#down{
        grid-column-start: 1;
        grid-column-end: 3;
    }
}
              
            
!

JS

              
                /*
 * Reglas:
 * El final de cada nivel debe ser el inicio del siguiente
*/

const emojis = {
    '-': ' ',
    'O': '🚪',
    'X': '💣',
    'I': '🏆',
    'PLAYER': '💀',
    'COLLISION': '🔥',
    'GAME_OVER': '👎🏻',
    'WIN': '🥇',
    'HEART': '💙',
  };
  
  const maps = [];
  maps.push(`IXXXXXXXXX
    -XXXXXXXXX
    -XXXXXXXXX
    -XXXXXXXXX
    -XXXXXXXXX
    -XXXXXXXXX
    -XXXXXXXXX
    -XXXXXXXXX
    -XXXXXXXXX
    OXXXXXXXXX`);
  maps.push(`O--XXXXXXX
    X--XXXXXXX
    XX----XXXX
    X--XX-XXXX
    X-XXX--XXX
    X-XXXX-XXX
    XX--XX--XX
    XX--XXX-XX
    XXXX---IXX
    XXXXXXXXXX`);
  maps.push(`XI----XXXX
    XXXXX-XXXX
    XX----XXXX
    XX-XXXXXXX
    XX-----XXX
    XXXXXX-XXX
    XX-----XXX
    XX-XXXXXXX
    XX-----OXX
    XXXXXXXXXX`);
    maps.push(`IXX-----------X
    -X-XXXXXXXXXX-X
    -X--XXXXXXXXX-X
    -XX-XXXXXX----X
    -X---------XXXX
    -XX-XXXXXX-XXXX
    -XX-XXXXXX-XXXX
    -XX-XXXXXX-XXXX
    -XX-XXXXXX-XXXX
    -XX-XXXXXX-XXXX
    ----XXX-----XXX
    -XXXXXX-XXX--XX
    -X------XXXX--X
    -XXXXXXXXXXXXO-
    XXXXXXXXXXXXXX-
    `
    );
    maps.push(`-XX-----------X
    -X-XXXXXXXXXX-X
    -X-OXXXXXXXXX-X
    -XX-XXXXXX----X
    -X---------XXXX
    -XX-XX-XXX-XXXX
    -XX-XX-XXX-XXXX
    -XX-XX-XXX-XXXX
    -XX-XX-XXX-XXXX
    -XX-XXXXXX-XXXX
    ----XX------XXX
    -XXXXX--XXX--XX
    -X------XXXX--X
    -XXXXX-XXXXXX--
    XI-----XXXXXXXX`
    );
    maps.push(`XXX---XXXXX---XXX
    XXX-X--XXX--X-XXX
    XXX-XX--X--XX-XXX
    XX--XXX---XXX--XX
    X--XXXXX-XXXXX--X
    --XX---------XX--
    -XXX-XXXXXXX-XXX-
    --XX-X--O--X-XX--
    X--X-X-XXX-X-X--X
    XX-X---XIX---X-XX
    X--XXXX---XXXX--X
    --XXX---X---XXX--
    -XXXX--XXX--XXXX-
    --XXX--XXX--XXX--
    X--XXX--X--XXX--X
    XX--XXXX---XX--XX
    XXX-----------XXX
    `
    );
const canvas = document.querySelector('#game');
const game = canvas.getContext('2d');
const mapasOrdenados = maps.map(element => element.split(`\n    `)).map(element => element.map(item => Object.values(item)));

let canvasSize;
let elementSize;
const playerPostion = {
    x: undefined,
    y: undefined,
}
const playerPositionFinish = {
    x: undefined,
    y: undefined,
}
const trophyPostion = {
    x: undefined,
    y: undefined,
}
const collisionPosition= {
    x: undefined,
    y : undefined,
}
let enemyPositions = [];
let nivel = 0;
let lives = 5;
let valor;
const vidas = document.querySelector('#lives');
const timeGame = document.querySelector('#time');
const spanTimeRecord = document.querySelector('#timeRecord');
let timeStart;
let timeInterval;
let timePlayer;

spanTimeRecord.innerHTML = localStorage.getItem('timeRecord');

window.addEventListener('load', setCanvasSize);
window.addEventListener('resize', setCanvasSize);

function setCanvasSize(){
    window.innerHeight > window.innerWidth ? canvasSize = window.innerWidth * 0.75 : canvasSize = window.innerHeight * 0.75;
    canvas.setAttribute('width', canvasSize);
    canvas.setAttribute('height', canvasSize);
    valor = mapasOrdenados[nivel][0].length;
    elementSize = Math.floor(canvasSize/valor);
    startGame();
}

function startGame(){
    if(localStorage.getItem('timeRecord',timePlayer)){
        spanTimeRecord.classList.add('timeRecord');
    }
    game.font = `${elementSize}px Arial`;
    game.textAlign = 'start';
    let mapaActual = mapasOrdenados[nivel];

    if(!mapaActual){
        winGame();
        return;
    }
    enemyPositions = [];
    game.clearRect(0,0,canvasSize, canvasSize);
    mapaActual.forEach((row, indexRow) => {
        let posicionY = (indexRow + 1)  * elementSize;
        row.forEach((column, indexColumn) =>{
            let posiconX =  indexColumn * elementSize;

            if(playerPostion.x === undefined && column == 'O'){
                playerPostion.x  = indexColumn;
                playerPostion.y = indexRow + 1;
                
            }else if(column == 'I'){
                trophyPostion.x  = posiconX;
                trophyPostion.y = posicionY;
            }else if(column == 'X'){
                enemyPositions.push({
                    x: posiconX,
                    y: posicionY,
                })
                if(collisionPosition.x){
                    game.fillText(emojis['COLLISION'], collisionPosition.x * elementSize, collisionPosition.y * elementSize );
                }
            }
            game.fillText(emojis[column], posiconX, posicionY );
        });
    });
    showLives();
    movePlayer();
}

function movePlayer(){
    playerPositionFinish.x = playerPostion.x * elementSize;
    playerPositionFinish.y = playerPostion.y * elementSize;
    victory();
    defeat();
    game.fillText(emojis['PLAYER'], playerPositionFinish.x, playerPositionFinish.y);
}

function victory(){
    if(playerPositionFinish.x ===  trophyPostion.x && playerPositionFinish.y ===  trophyPostion.y){
        nivel += 1;
        collisionPosition.x = undefined;
        if(!mapasOrdenados[nivel]){
            winGame();
            return
        }
        else{
            playerPostion.x  = undefined;
            setCanvasSize();
        }
    }
}
function defeat(){
    const enemyCollision = enemyPositions.find(enemy => {
        const enemyCollisionX = enemy['x'] == playerPositionFinish.x;
        const enemyCollisionY = enemy['y'] == playerPositionFinish.y;
        return enemyCollisionX && enemyCollisionY;
    }
    );
    if(enemyCollision){
        lives = lives -1;
        collisionPosition.x = playerPostion.x;
        collisionPosition.y = playerPostion.y;
        playerPostion.x = undefined;
        playerPostion.y = undefined;
        if(lives <= 0){
            nivel = 0;
            lives = 5;
            clearInterval(timeInterval);
            timeGame.innerHTML = '0.00';
            timeStart = undefined;
            collisionPosition.x = undefined;
            setCanvasSize();
        }
        startGame();
      }
}

function winGame(){
    clearInterval(timeInterval);
    let recordGlobal = localStorage.getItem('timeRecord');
    if(recordGlobal){
        if(parseFloat(localStorage.getItem('timeRecord')) >= timePlayer){
            localStorage.setItem('timeRecord',timePlayer);
        }
    }else{
        localStorage.setItem('timeRecord',timePlayer);
    }
    spanTimeRecord.innerHTML = localStorage.getItem('timeRecord');
}
function defeatWame(){
    
}
function getStartTime(){
    if(!timeStart){
        timeStart = Date.now();
        timeInterval = setInterval(showTimeGame, 100);
    }else{
        timeStart = timeStart;
    }
}

function showLives(){
    vidas.innerHTML = '';
    const heartsArray = Array(lives).fill(emojis['HEART']);
    heartsArray.forEach(item =>  {
        vidas.innerHTML += `${item} `;
    });
}

function showTimeGame(){
    timePlayer = Math.round(((Date.now() - timeStart) / 1000)*100)/ 100;
    timeGame.innerHTML = timePlayer;
}

window.addEventListener('keyup', mostrarTecla);

function mostrarTecla(event){
    switch (event.code){
        case 'ArrowUp':
            moveUp();
        break;
        case 'ArrowDown':
            moveDown()
        break;
        case 'ArrowLeft':
            moveLeft()
        break;
        case 'ArrowRight':
            moveRight()
        break;
    }
    
}

const buttonUp = document.querySelector('#up');
const buttonDown = document.querySelector('#down');
const buttonRight = document.querySelector('#right');
const buttonLeft = document.querySelector('#left');

buttonUp.addEventListener('click', moveUp);
buttonDown.addEventListener('click', moveDown);
buttonRight.addEventListener('click', moveRight);
buttonLeft.addEventListener('click', moveLeft);

function moveUp(){
    if(playerPositionFinish.y == elementSize * 1){
        playerPostion.y = playerPostion.y;
    }else{
        playerPostion.y -= 1;
        playerPositionFinish.y = playerPostion.y * elementSize;
    }
    getStartTime();
    startGame();
}
function moveDown(){
    if(playerPositionFinish.y == elementSize * valor){
        playerPostion.y = playerPostion.y;
    }else{
        playerPostion.y += 1;
        playerPositionFinish.y = playerPostion.y * elementSize;
    }
    getStartTime();
    startGame();
}
function moveRight(){
    if(playerPositionFinish.x == elementSize * (valor - 1)){
        playerPostion.x = playerPostion.x;
    }else{
        playerPostion.x += 1;
        playerPositionFinish.x = playerPostion.x * elementSize;
    }
    getStartTime();
    startGame();
}
function moveLeft(){
    if(playerPositionFinish.x == 0){
        playerPostion.x = playerPostion.x;
    }else{
        playerPostion.x -= 1;
        playerPositionFinish.x = playerPostion.x * elementSize;
    }
    getStartTime();
    startGame();
}

              
            
!
999px

Console