DEV Community

CoderZ90
CoderZ90

Posted on

Making the chrome dino game play itself using Javascript

Hello, guys in today's blog we are gonna hack chrome dino ( automating it ) using javascript

Chrome dino game is made by Google. this game is available in chromium based browsers like brave, chrome, edge and other browsers out there

you can play this game by turning off your internet/wifi or going to chrome://dino 😊

Anyways lets automate it

Watch this video to get better understanding
https://www.youtube.com/watch?v=r0b4j8JUVUc&t=236s

first we make a function with the name autoPlay()

Note (these are the objects in the chrome dino game) :-

------ DINO ------
tRex - Is the DINO

------ CACTUS ------
CACTUS_SMALL - is the smallest cactus
CACTUS_LARGE - is the largest cactus

------ BIRD ------
The name of the bird is pretty hard 🀣
PTERODACTYL - the bird

In the code i have given comments and tried to explain you each line of code :)

function autoplay() {
  setTimeout(function () {
    myinstance = this.Runner.instance_;
    myobstacles = myinstance.horizon.obstacles;

    // if my tRex is ducking then
    if (myinstance.tRex.ducking) {
      // make my tRex to duck
      myinstance.tRex.setDuck(true);
    }
    if (myinstance.crashed) {
      //   When the game is over then
      console.log("Game Over... Paste the code again to automate the game");
      return;
    }
    if (myobstacles.length > 0) {
      action = "JUMP";
      obstacle_type = myobstacles[0]["typeConfig"]["type"];

      // Defining which action to perform if it match the following cases
      if (obstacle_type == "CACTUS_SMALL" || obstacle_type == "CACTUS_LARGE") {
        action = "JUMP";
        // i know its a hard name ( actually PTERODACTYL its the bird )
      } else if (obstacle_type == "PTERODACTYL") {
        if (myobstacles[0]["yPost"] == 75 || myobstacles[0]["yPost"] == 50)
          action = "DUCK";
      }

      // Making the action work
      if (myobstacles[0].xPos <= 100) {
        console.log(myobstacles[0]);

        // Perform the action
        if (action == "JUMP") {
          console.log("Jumping.. Yahoo");
          // we get the current speed of our dino
          curr_speed = myinstance.currentSpeed;
          // then making it jump
          myinstance.tRex.startJump(curr_speed);
        } else if (action == "DUCK") {
          console.log("Ducking.. Oo");
          myinstance.tRex.setDuck(true);
        }
      }
    }
    autoplay();
    // setting the timer for 20 mili seconds
  }, 20);
}
console.log('Done.. Automated the game, Now Start')
autoplay();
Enter fullscreen mode Exit fullscreen mode

And Voila! we are done πŸ˜€πŸ‘ so i hope you guys enjoyed this blog and had fun! Have a nice day and be safe guys πŸ’–

Dont forget to subscribe to my youtube channel for more amazing videos - https://youtube.com/codingfire?sub_confirmation=1

Top comments (3)

Collapse
 
jonrandy profile image
Jon Randy πŸŽ–οΈ • Edited

A pterodactyl was a flying dinosaur. There are no birds in the Dino game
Pterodactyl

Collapse
 
official_fire profile image
CoderZ90

Ikr that but for teaching purpose and for making it simple to pronounce and teach i used the term bird for the pterodactyl, Thankyou

Collapse
 
official_fire profile image
CoderZ90

Voila! we are done πŸ˜€πŸ‘ so i hope you guys enjoyed this blog and had fun! Have a nice day and be safe guys πŸ’–

Dont forget to subscribe to my youtube channel for more amazing videos - youtube.com/codingfire?sub_confirm...