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
Dispatching Keyboard Events
ThedispatchKey
function simulates keyboard events, such as pressing or releasing the spacebar (to jump) and the down arrow (to duck).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.
Obstacle Detection
- The script calculates an obstacle's position (
xPos
andyPos
) and determines if it’s near. - Based on the obstacle's height and position, it decides whether to jump, duck, or do nothing.
- The script calculates an obstacle's position (
Timed Execution
ThesetInterval
function executes this logic once per frame (msPerFrame
), ensuring the automation stays in sync with the game.
Running the Script
To use this script:
- Open the Chrome Dino game by visiting
chrome://dino
or by disabling your internet connection. - Press
Ctrl + Shift + I
(orCmd + Option + I
on Mac) to open Chrome Developer Tools. - Navigate to the Console tab.
- Paste the script and press Enter.
- 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
Post a Comment