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>
<htmll ang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Call of the Ghostly Forest</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <div id="game-container">
        <div id="cat"></div>
        <div id="score">Score: 0</div>
        <div id="message"></div>
    </div>
    <script src="script.js"></script>
</body>
<button id="toggleMusic">Вкл./Выкл. музыку</button>
<audio id="backgroundMusic" loop>
  <source src="https://www.dropbox.com/scl/fi/pzd3i3fnt9r8aq6tc4zch/Run-to-the-Beat.mp3?rlkey=pd2brhljhjwqp1duqevc44lbb&st=j9zbr76n&dl=1" type="audio/mpeg">
   Your browser does not support the audio element.
</audio>
</htmll>

              
            
!

CSS

              
                body {
    margin: 0;
    overflow: hidden;
    background-color: #111;
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    color: white;
    font-family: Arial, sans-serif;
}

#game-container {
    position: relative;
    width: 800px;
    height: 400px;
    background: url('https://i.postimg.cc/jqhc2nXz/1.jpg') no-repeat center center;
    background-size: cover;
    overflow: hidden;
    border: 2px solid #fff;
}

#cat {
    position: absolute;
    bottom: 0;
    left: 50px;
    width: 100px; /* Increased width */
    height: 80px; /* Increased height */
    background: url('https://i.postimg.cc/Jzz2M5CD/gray-cat.png') no-repeat center center;
    background-size: cover;
}

.obstacle {
    position: absolute;
    bottom: 0;
    width: 80px; /* Increased width */
    height: 80px; /* Increased height */
}

.stump {
    background: url('https://i.postimg.cc/Vs49mQfg/image.png') no-repeat center center;
    background-size: cover;
}

#score {
    position: absolute;
    top: 10px;
    left: 10px;
    font-size: 24px;
}

#message {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    font-size: 36px;
    display: none;
}
#toggleMusic {
    position: absolute;
    top: 10px;
    right: 10px;
    padding: 10px 20px;
    background-color: #333;
    color: #fff;
    border: none;
    cursor: pointer;
}

#toggleMusic:hover {
    background-color: #555;
}
              
            
!

JS

              
                const cat = document.getElementById('cat');
const scoreElement = document.getElementById('score');
const messageElement = document.getElementById('message');
const gameContainer = document.getElementById('game-container');

let catY = 0;
let jumping = false;
let obstacles = [];
let score = 0;
let gameRunning = false;
let interval;

const backgrounds = [
    'https://i.postimg.cc/jqhc2nXz/1.jpg',
    'https://i.postimg.cc/jqhc2nXz/1.jpg', // Add more background images as needed
    'https://i.postimg.cc/jqhc2nXz/1.jpg',
    'https://i.postimg.cc/jqhc2nXz/1.jpg'
];
let currentBackgroundIndex = 0;

document.addEventListener('keydown', function(event) {
    if (event.code === 'Space') {
        if (!gameRunning) {
            startGame();
        } else {
            jump();
        }
    }
});

function startGame() {
    score = 0;
    scoreElement.innerText = 'Score: 0';
    messageElement.style.display = 'none';
    catY = 0;
    cat.style.bottom = catY + 'px';
    obstacles.forEach(obstacle => gameContainer.removeChild(obstacle)); // Remove existing obstacles
    obstacles = [];
    gameRunning = true;

    gameContainer.style.backgroundImage = `url(${backgrounds[0]})`;
    currentBackgroundIndex = 0;

    interval = setInterval(updateGame, 20);
    setTimeout(addObstacle, 2000); // Increased interval for adding obstacles
}

function jump() {
    if (!jumping) {
        jumping = true;
        let jumpHeight = 150; // Adjust this value for desired jump height
        let jumpSpeed = 6; // Increased speed for jump up
        let fallSpeed = 6; // Increased speed for falling down
        let jumpUpInterval = setInterval(() => {
            if (jumpHeight > 0) {
                catY += jumpSpeed; // Faster jump speed
                jumpHeight -= jumpSpeed; // Adjusted decrement
            } else {
                clearInterval(jumpUpInterval);
                setTimeout(() => {
                    let fallInterval = setInterval(() => {
                        if (catY > 0) {
                            catY -= fallSpeed; // Faster fall speed
                        } else {
                            catY = 0;
                            clearInterval(fallInterval);
                            jumping = false;
                        }
                        cat.style.bottom = catY + 'px';
                    }, 15); // Shorter interval for falling
                }, 100);  // Shortened delay before falling
            }
            cat.style.bottom = catY + 'px';
        }, 15); // Shorter interval for jumping up
    }
}

function addObstacle() {
    if (!gameRunning) return;

    let obstacle = document.createElement('div');
    obstacle.classList.add('obstacle');
    obstacle.style.left = '900px'; // Increased starting position for obstacles
    obstacle.style.bottom = '0';
    
    obstacle.classList.add('stump'); // Only add stumps

    gameContainer.appendChild(obstacle);
    obstacles.push(obstacle);

    setTimeout(addObstacle, Math.random() * 3000 + 2000); // Increased interval for adding obstacles
}

function updateGame() {
    obstacles.forEach((obstacle, index) => {
        let obstacleX = parseInt(obstacle.style.left);

        if (obstacleX <= 100 && obstacleX >= 50 && catY <= 50) {
            endGame();
        }

        if (obstacleX < 0) {
            gameContainer.removeChild(obstacle);
            obstacles.splice(index, 1);
            score++;
            scoreElement.innerText = 'Score: ' + score;

            // Change background every 5 points
            if (score % 5 === 0) {
                changeBackground();
            }
        }

        obstacle.style.left = (obstacleX - 4) + 'px';  // Move obstacles toward the cat
    });
}

function changeBackground() {
    currentBackgroundIndex = (currentBackgroundIndex + 1) % backgrounds.length;
    gameContainer.style.backgroundImage = `url(${backgrounds[currentBackgroundIndex]})`;
}

function endGame() {
    clearInterval(interval);
    gameRunning = false;
    messageElement.innerText = 'Game Over! Score: ' + score;
    messageElement.style.display = 'block';
}
document.addEventListener('DOMContentLoaded', function () {
    const musicButton = document.getElementById('toggleMusic');
    const backgroundMusic = document.getElementById('backgroundMusic');
    let isMusicPlaying = false;

    musicButton.addEventListener('click', function () {
        if (isMusicPlaying) {
            backgroundMusic.pause();
            isMusicPlaying = false;
            musicButton.textContent = 'Вкл. музыку';
        } else {
            backgroundMusic.play();
            isMusicPlaying = true;
            musicButton.textContent = 'Выкл. музыку';
        }
    });
});

              
            
!
999px

Console