Javascript Jumping Game (Part=3)

 

               


<!DOCTYPE html>
<html lang="en">

<!-- jumping Game part-3 -->
<!-- press up key to jump -->

<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-3)</title>
    <style>
        body {
            width100%;
            height100vh;
            displayflex;
            justify-contentcenter;
            align-itemscenter;
            padding0;
            margin0;
       
        }

        #container {

            width100%;
            height100%;
            position:relative;
            box-shadow0px 0px 30px rgba(0000.445);
            overflowhidden;
            
     

        }

        #sky {
            positionabsolute;
            width100%;
            height100%;
            z-index-5;
            backgroundurl(sky.png);
        }

        #cloud {
            positionabsolute;
            width100%;
            height100%;
            background-imageurl(cloud_sprite.png);
            animation: cloud-move 60s linear infinite;
            z-index-4;
        }

        #rock2 {
            positionabsolute;
            width100%;
            height100%;
            z-index-3;
            animation: rock-move 20s linear infinite;

            backgroundurl(rock2.png)
        }

        #rock1 {
            positionabsolute;
            width100%;
            height100%;
            z-index-1;
            animation: rock-move 20s linear infinite;
            background-imageurl(rock1.png);
        }


        @keyframes cloud-move {
            0% {
                background-position7680px;
            }

            100% {
                background-position0px;
            }

        }

        @keyframes rock-move {
            0% {
                background-position3840px;
                transformtranslateY(0px);

            }

            50% {
                transformtranslateY(50px);
            }

            100% {
                background-position0px;
                transformtranslateY(0px);

            }

        }

        #container::before {
            content'';
            positionabsolute;
            Width100%;
            height100%;
            opacity0;
            backgroundlinear-gradient(rgba(0000.507), rgba(0000.486));
            animation: background-animation 30s linear alternate infinite;
            z-index-3;

        }



        @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(0 149 255);
            border2px solid black;
            positionabsolute;
            bottom50px;
            left900px;
            animation: block-animate 1.5s 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="sky"></div>
        <div id="cloud">

        </div>

        <div id="rock2">

        </div>
        <div id="rock1">

        </div>



        <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;

        let cloud = document.getElementById('cloud');
        let rock1 = document.getElementById('rock1');
        let rock2 = document.getElementById('rock2');

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

        document.addEventListener('keydown'jump);


        function jump(e) {

            if (e.keyCode == 38) {
                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;
        let scoreFlag=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';
                cloud.style.animationPlayState = 'paused';
                rock1.style.animationPlayState = 'paused';
                rock2.style.animationPlayState = 'paused';
                jumpMusic.muted = true;
                scoreFlag=0;
                if (flag == 1) {
                    gameover.play();
                    flag++;
                }


            }
            else {

                if(scoreFlag==1){

                    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

5. clouds img

6. sky img

7. rock1 img

8. rock2 img


Put all files in the same folder



Post a Comment

Previous Post Next Post