Javascript Jumping game (part-2)


               


<!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">
    <title>Jumping Game (Part-2)</title>
    <style>
        body {
            width: 100%;
            height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            padding: 0;
            margin: 0;
        }

        #container {
            width: 100%;
            height: 100%;
            position: relative;
            box-shadow: 0px 0px 20px rgba(0, 0, 0, 0.445);
            background: linear-gradient(rgba(255, 217, 0, 0.692), rgb(255, 255, 255));
            overflow: hidden;

        }

        #container::before {
            content: '';
            position: absolute;
            Width: 100%;
            height: 100%;
            opacity: 0;
            background: linear-gradient(rgba(1, 13, 119, 0.65), rgb(231, 231, 231));
            animation: background-animation 60s linear alternate infinite;

        }

        @keyframes background-animation {
            0% {
                opacity: 0;
            }

            47% {
                opacity: 0;
            }

            53% {
                opacity: 1;
            }

            100% {
                opacity: 1;
            }

        }

        #scoreBox {
            font-size: 30px;
            font-family: cursive;

            display: flex;
            justify-content: end;
            align-items: center;
            padding: 30px 0px;
            color: black;
            font-weight: bold;




        }

        #score {
            width: 150px;
            text-align: left;
            margin-left: 5px;
            font-size: 25px;
            margin-top: 1px;


        }

        span {
            z-index: 3;
        }

        #character-box {
            height: 256px;
            width: 256px;
            position: absolute;
            left: 100px;
            bottom: 50px;

        }

        #character {
            width: 100%;
            height: 100%;
            background: url(character.png);
            animation: character-animate 1s steps(6) infinite;
        }

        #block {
            height: 100px;
            width: 100px;
            background-color: rgb(27 205 255);
            border: 2px solid black;
            position: absolute;
            bottom: 50px;
            left: 900px;
            animation: block-animate 1.2s linear forwards infinite;
        }


        #road {
            width: 100%;
            height: 50px;
            background-image: linear-gradient(#db1717, #440707);
            position: absolute;
            bottom: 0px;
        }

        .jump {
            animation: character-box-jump .7s linear;
        }

        @keyframes character-animate {
            0% {
                background-position: 1536px;

            }

            100% {
                background-position: 0px;

            }

        }

        @keyframes character-box-jump {
            0% {
                bottom: 50px;

            }

            30% {
                bottom: 300px;

            }

            70% {
                bottom: 300px;
            }

            100% {
                bottom: 50px;

            }

        }



        @keyframes block-animate {
            0% {
                left: 1300px;

            }

            100% {
                left: -200px;

            }

        }
    </style>
</head>

<body>
    <div id="container">
        <div id="scoreBox"> <span> Score : </span> <span id='score'>0</span></div>
        <div id="character-box">
            <div id="character"></div>
        </div>
        <div id="block"></div>
        <div id="road"></div>
    </div>

    <script>
        const characterBox = document.getElementById('character-box');
        const block = document.getElementById('block');
        const character = document.getElementById('character');



        let gameover = new Audio('gameover.wav');
        let music = new Audio('music.mp3');
        let jumpMusic=new Audio('jump.wav');

        let score = document.getElementById('score');
        let scoreIncrease = 0;

        music.loop = true;
        music.play();
        music.volume=0.5;

        document.addEventListener('click', jump);
        function jump() {
            if (characterBox.classList == 'jump') {
                return;
            }
            characterBox.classList.add('jump');
            jumpMusic.play();
           
            setTimeout(() => {
                characterBox.classList.remove('jump');
                jumpMusic.paused();
                jumpMusic.currentTime=0;
              
              
            }, 700);

        }

        let flag = 1;
        function check() {

            let blockleft = parseInt(window.getComputedStyle(block).getPropertyValue('left'));
            let characterbottom = parseInt(window.getComputedStyle(characterBox).getPropertyValue('bottom'));

            if (blockleft > 30 && blockleft < 336 && characterbottom <= 150) {

                music.pause();
                block.style.animationPlayState = 'paused';
                character.style.animationPlayState = 'paused';
                characterBox.style.animationPlayState = 'paused';
                jumpMusic.muted=true;


                if (flag == 1) {
                    gameover.play();
                    flag++;
                }


            }
            else {

                scoreIncrease++;
                score.innerHTML = scoreIncrease;
            }


        }

        setInterval(() => {
            check();

        }, 10);
    </script>
</body>

</html>



 Game Resources

1.  character image

2.  jump sound  effect

3. background music 

4. game over sound effect


Put all files in the same folder

Post a Comment

Previous Post Next Post