Javascript Jumping game part-1

 

             



<!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-1)</title>
    <style>
        body {
            width100%;
            height100vh;
            displayflex;
            justify-contentcenter;
            align-itemscenter;
            padding0;
            margin0;
            background-colorrgba(952551160.76);

        }

        #container {
            width1100px;
            height600px;
            positionrelative;
            box-shadow0px 0px 20px rgba(0000.445);
            background-imagelinear-gradient(rgb(255 246 27), rgb(212212212));
            overflowhidden;
        }

        #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 1s linear infinite;
        }

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

        .jump {
            animation: character-box-jump .5s 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% {
                left1200px;

            }

            100% {
                left-100px;

            }

        }
    </style>
</head>

<body>
    <div id="container">
        <div id="character-box">
            <div id="character"></div>
        </div>
        <div id="block"></div>
        <div id="road"></div>
    </div>

    <script>
        const character = document.getElementById('character-box');
        const block = document.getElementById('block');
        document.addEventListener('click'jump);
        function jump() {
            if (character.classList == 'jump') {
                return;
            }
            character.classList.add('jump');
            setTimeout(() => {
                character.classList.remove('jump');
            }, 500);

        }
        function check() {

            let blockleft = parseInt(window.getComputedStyle(block).getPropertyValue('left'));
            let characterbottom = parseInt(window.getComputedStyle(character).getPropertyValue('bottom'));
            if (blockleft > 50 && blockleft < 300 && characterbottom <= 150) {
                alert('Game Over');

            }

        }

        setInterval(() => {
            check();

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

</html>



for downloading character image click here




1 Comments

Post a Comment

Previous Post Next Post