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 {
            width: 100%;
            height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            padding: 0;
            margin: 0;
            background-color: rgba(95, 255, 116, 0.76);

        }

        #container {
            width: 1100px;
            height: 600px;
            position: relative;
            box-shadow: 0px 0px 20px rgba(0, 0, 0, 0.445);
            background-image: linear-gradient(rgb(255 246 27), rgb(212, 212, 212));
            overflow: hidden;
        }

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

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

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

            }

            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