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 {
            width100%;
            height100vh;
            displayflex;
            justify-contentcenter;
            align-itemscenter;
            padding0;
            margin0;
        }

        #container {
            width100%;
            height100%;
            positionrelative;
            box-shadow0px 0px 20px rgba(0000.445);
            backgroundlinear-gradient(rgba(25521700.692), rgb(255255255));
            overflowhidden;

        }

        #container::before {
            content'';
            positionabsolute;
            Width100%;
            height100%;
            opacity0;
            backgroundlinear-gradient(rgba(1131190.65), rgb(231231231));
            animation: background-animation 60s linear alternate infinite;

        }

        @keyframes background-animation {
            0% {
                opacity0;
            }

            47% {
                opacity0;
            }

            53% {
                opacity1;
            }

            100% {
                opacity1;
            }

        }

        #scoreBox {
            font-size30px;
            font-family: cursive;

            displayflex;
            justify-contentend;
            align-itemscenter;
            padding30px 0px;
            colorblack;
            font-weightbold;




        }

        #score {
            width150px;
            text-alignleft;
            margin-left5px;
            font-size25px;
            margin-top1px;


        }

        span {
            z-index3;
        }

        #character-box {
            height256px;
            width256px;
            positionabsolute;
            left100px;
            bottom50px;

        }

        #character {
            width100%;
            height100%;
            backgroundurl(character.png);
            animation: character-animate 1s steps(6infinite;
        }

        #block {
            height100px;
            width100px;
            background-colorrgb(27 205 255);
            border2px solid black;
            positionabsolute;
            bottom50px;
            left900px;
            animation: block-animate 1.2s linear forwards infinite;
        }


        #road {
            width100%;
            height50px;
            background-imagelinear-gradient(#db1717#440707);
            positionabsolute;
            bottom0px;
        }

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

        @keyframes character-animate {
            0% {
                background-position1536px;

            }

            100% {
                background-position0px;

            }

        }

        @keyframes character-box-jump {
            0% {
                bottom50px;

            }

            30% {
                bottom300px;

            }

            70% {
                bottom300px;
            }

            100% {
                bottom50px;

            }

        }



        @keyframes block-animate {
            0% {
                left1300px;

            }

            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