Automating the Chrome Dino Game: A Step-by-Step Guide





Have you ever tried playing the Chrome Dino game and wondered if there’s a way to automate it? Maybe you’ve tried jumping over cacti and ducking under pterodactyls, only to think, What if my computer could do this for me? Well, with a bit of JavaScript magic, you can!

In this article, we'll explore a nifty script that uses Chrome’s developer tools to automate gameplay in the famous offline Dino game.


The Dino Game and Automation

The Chrome Dino game is a hidden browser game that appears when you lose your internet connection. The goal is simple: control a pixelated T-Rex by jumping over obstacles or ducking under flying creatures to survive as long as possible.

The script we'll share automates these actions by detecting obstacles and triggering the appropriate key presses. It does this by analyzing game variables exposed in the browser's JavaScript engine.


The Automation Code

Here’s the full code:

    
function dispatchKey(type, key) {
    document.dispatchEvent(new KeyboardEvent(type, {keyCode: key}));
}

setInterval(function () {
    const KEY_CODE_SPACE_BAR = 32;
    const KEY_CODE_ARROW_DOWN = 40;
    const CANVAS_HEIGHT = Runner.instance_.dimensions.HEIGHT;
    const DINO_HEIGHT = Runner.instance_.tRex.config.HEIGHT;

    const obstacle = Runner.instance_.horizon.obstacles[0];
    const speed = Runner.instance_.currentSpeed;

    if (obstacle) {
        const w = obstacle.width;
        const x = obstacle.xPos; // Measured from the left of the canvas
        const y = obstacle.yPos; // Measured from the top of the canvas
        const yFromBottom = CANVAS_HEIGHT - y - obstacle.typeConfig.height;
        const isObstacleNearby = x < 25 * speed - w / 2;

        if (isObstacleNearby) {
            if (yFromBottom > DINO_HEIGHT) {
                // Pterodactyl flying above, do nothing
            } else if (y > CANVAS_HEIGHT / 2) {
                // Jump
                dispatchKey("keyup", KEY_CODE_ARROW_DOWN);
                dispatchKey("keydown", KEY_CODE_SPACE_BAR);
            } else {
                // Duck
                dispatchKey("keydown", KEY_CODE_ARROW_DOWN);
            }
        }
    }
}, Runner.instance_.msPerFrame);


 
```

How It Works

  1. Dispatching Keyboard Events
    The dispatchKey function simulates keyboard events, such as pressing or releasing the spacebar (to jump) and the down arrow (to duck).

  2. Checking Game Elements
    The script accesses game variables like:

    • Runner.instance_.horizon.obstacles: Array of obstacles currently on the screen.
    • Runner.instance_.dimensions.HEIGHT: The canvas height.
    • Runner.instance_.currentSpeed: The T-Rex's current speed.
  3. Obstacle Detection

    • The script calculates an obstacle's position (xPos and yPos) and determines if it’s near.
    • Based on the obstacle's height and position, it decides whether to jump, duck, or do nothing.
  4. Timed Execution
    The setInterval function executes this logic once per frame (msPerFrame), ensuring the automation stays in sync with the game.


Running the Script

To use this script:

  1. Open the Chrome Dino game by visiting chrome://dino or by disabling your internet connection.
  2. Press Ctrl + Shift + I (or Cmd + Option + I on Mac) to open Chrome Developer Tools.
  3. Navigate to the Console tab.
  4. Paste the script and press Enter.
  5. Watch the T-Rex play the game on its own!

A Word of Caution

While this is a fun experiment, remember:

  • Use this for educational purposes only.
  • Automating games might take the fun out of playing them manually.
  • Avoid using automation scripts in multiplayer or competitive scenarios.

Conclusion

With just a few lines of JavaScript, you can turn the Chrome Dino game into a self-playing bot. This script demonstrates the power of JavaScript and Chrome’s developer tools to interact with browser-based games. Now you can sit back, relax, and let your T-Rex handle the obstacles!

Do you have ideas for other fun browser automations? Share them in the comments below!

Comments

Popular posts from this blog

Building a Background Remover Application with Python Sor

How to Convert Python Files into Executable (.exe) Files

How to Build a Beautiful and Responsive Tic-Tac-Toe Game with HTML, CSS, and JavaScript