Create Google Dino Game Logic With JavaScript

If you want to create a game like Google Dino game so you can create it with a copy of given source code.

STEP 01

Create 3 files.
index.html
style.css
index.js

STEP 02

Copy this code and paste it into the index.html file or if you know about it you can paste it into any HTML file but don't forget to change or remove the link.

<!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>Game Like Google Dino | HunerOn</title>

    <link rel="stylesheet" href="style.css" type="text/css">

</head>
<body style="background-color: rgb(17, 75, 151);">
    <div class="body">
        <div class="heading">
            <h1>Game Like Google Dino</h1>
        </div>
        <div class="maindiv">
            <div id="counterbox">
                <input type="number" value="0">
            </div>
            <div id="game">
                <div id="chracter"></div>
                <div id="block"></div>
            </div>
            <button onclick="jump()" id="addnum">Click to Jump</button>
            <button onclick="reload()">Try Again</button>
        </div>
    </div>

    <script src="index.js"></script>

</body>
</html>

STEP :- 03

Copy this code and paste it into the style.css file or if you know about it so you can paste it into any CSS file but don't forget to change or remove the link.

* {
  margin0;
  padding0;
  -webkit-box-sizingborder-box;
  box-sizingborder-box;
}

#game {
  width500px;
  height200px;
  border-bottom2px dashed #959567;
  background-colorwhite;
  overflowhidden;
}

#chracter {
  width20px;
  height50px;
  background-colorred;
  positionrelative;
  top150px;
}

.animate {
  -webkit-animation: jump 500ms infinite;
          animation: jump 500ms infinite;
}

@-webkit-keyframes jump {
  0% {
    top150px;
  }
  30% {
    top100px;
  }
  70% {
    top100px;
  }
  100% {
    top150px;
  }
}

@keyframes jump {
  0% {
    top150px;
  }
  30% {
    top100px;
  }
  70% {
    top100px;
  }
  100% {
    top150px;
  }
}

#block {
  width20px;
  height20px;
  background-colorblue;
  positionrelative;
  top130px;
  left480px;
  -webkit-animationblock 1.5s infinite linear;
          animationblock 1.5s infinite linear;
}

@-webkit-keyframes block {
  0% {
    left480px;
  }
  100% {
    left-40px;
  }
}

@keyframes block {
  0% {
    left480px;
  }
  100% {
    left-40px;
  }
}

button {
  margin-top10px;
  width120px;
  height35px;
  text-transformuppercase;
  background-colorblue;
  colorwhite;
  font-weight600;
}

button:hover {
  background-colorred;
  colorwhite;
}

.maindiv {
  widthauto;
  heightauto;
  border10px solid black;
  positionabsolute;
  margin-top100px;
  margin-left425px;
  text-aligncenter;
  background-colorwhite;
}

.heading {
  width100%;
  heightauto;
  text-aligncenter;
  padding-top20px;
}

.heading h1 {
  colorwhite;
  font-size50px;
  text-transformuppercase;
  font-family'Gill Sans''Gill Sans MT''Calibri''Trebuchet MS'sans-serif;
}

#counterbox {
  width500px;
  height50px;
  border1px solid red;
  text-aligncenter;
  text-transformuppercase;
  vertical-aligncenter;
  font-size30px;
}

STEP :- 04

Copy this code and paste it into the index.js file or if you know about it so you can paste it into any JAVASCRIPT file but don't forget to change or remove the link.

var chracter = document.getElementById("chracter");
var block = document.getElementById("block");

function jump() {
    if (chracter.classList != "animate") {
        chracter.classList.add("animate");
    }

    setTimeout(function () {
        chracter.classList.remove("animate")
    }, 500);
}

var checkDead = setInterval(function () {
    var chracterTop =
        parseInt(window.getComputedStyle(chracter).getPropertyValue("top"));

    var blockLeft =
        parseInt(window.getComputedStyle(block).getPropertyValue("left"));

    if (blockLeft < 20 && blockLeft > 0 &&
        chracterTop >= 130) {
        block.style.animation = "none";
        block.style.display = "none";
        alert("Game Over");
    }
}, 10);

function reload() {
    location.reload();
}

// ******************Score Counter Start****************

let btnAdd = document.querySelector('#addnum');
let input = document.querySelector('input');


btnAdd.addEventListener('click', () => {
    input.value = parseInt(input.value) + 1;
})

// ******************Score Counter End****************

Post a Comment

0 Comments

© Copyright 2024 & 2025 - Team Krope - All right reserved