This is the latest version of the T-Rex Runner source code, extracted from the newly migrated Chromium repository. We have enhanced it further to make it independent of the Chromium browser and to improve the overall code structure. This is the latest codebase written in ES6, free from the circular dependencies present in the original source, making it easy to integrate into modern frameworks like React, Next.js, and others. Below, you’ll find updated documentation to help you start editing the game and creating your own customized variant.
The game assets are loaded from the offline-resources div in the HTML. There are two main types of assets:
- Sprite Images
- Two sprite files are used to support different display densities:
/images/default_100_percent/100-offline-sprite.png
(1x density)/images/default_200_percent/200-offline-sprite.png
(2x density)
- These sprite sheets contain all game graphics including:
- T-rex character animations
- Background elements
- Obstacles and game objects
- UI elements
- Audio Resources The game sounds are embedded directly in the HTML as base64 encoded audio:
- Press sound - Played when jumping
- Hit sound - Played on collision
- Reached sound - Played when reaching score milestones
The game automatically selects the appropriate sprite sheet based on the device's pixel density. The 2x sprite is used for high DPI displays to maintain sharp graphics.
To modify the game assets:
- Edit the sprite sheets to change visuals
- Replace the base64 audio strings to update sounds
- Keep the same sprite layout/coordinates when modifying graphics
Its required to always import atleast 1 CSS Stylesheet. The game logic injects some css in the first stylesheet if its not found then it will crash.
The game can be initialized using the Runner class - it takes the container element for the game and optional configs. Using the Runner class you can create a new instance and this instance can be used to control the game settings and events.
Example:
const trexGameContainer = document.querySelector('.trex-game');
const runner = new Runner(trexGameContainer);
console.log(runner); // to see all the available methods
We have extracted this source code from chromium and have made some changes on the top the source code to make it runnable with modern frameworks. All the changes can be tracked in the codebase using the fingerprint comment structure "@change -> Change reason".
- Refactored Runner function to class.
- Removed singleton pattern which prevents initializing another instance from the Runner class.
- Removed slow speed mode checkbox. (It can still be enabled by clicking on the T-Rex first and then starting the game)
- Attached Runner to
window.Runner
and removed circular dependency in any files. (Modern compilers treat it as a bad practice and throws an error) - Migrated some deprecated functions.
- Copy resources folder to your project.
- Copy images folder to your public folder.
- Copy
trex.css
to your folder and import it in your HTML file or React component. - In you html file or component add the div with id
offline-resources
from theindex.html
. - Create a new script which will import the
Runner
class from theoffline.js
file and create a new instance. (Similar toscript.js
) - This should run the dino game in your project. You may need to adjust the styling of your container.
Slow Speed Mode is an accessibility setting that makes the game more accessible for people with vision impairments. When enabled, it:
- Reduces the game speed and acceleration
- Enables audio cues when obstacles are approaching
- Adjusts the gap between obstacles to be more forgiving
- Lowers the maximum speed cap
This mode can be activated by:
- Clicking on the T-Rex character before starting the game
- Then starting the game normally
The specific adjustments made in slow mode include:
- Slower acceleration (0.0005 vs 0.001)
- Lower max speed (9 vs 13)
- Smaller gaps between obstacles (0.3 vs 0.6 coefficient)
- Audio proximity threshold adjusted for better cues
This allows players with visual impairments to rely more on audio feedback and have more time to react to obstacles, making the game more inclusive and enjoyable for all players.
The Runner class provides several methods to control the game. Here are the key methods available:
setSpeed(speed: number)
- Sets the game speed to a specific valuesetArcadeMode()
- Enables arcade mode with different positioning and behaviorrestart()
- Restarts the game from the beginningplay()
- Starts/resumes the gamestop()
- Pauses the gamecrash()
- Triggers the crash animation and game over state
onKeyDown(e: KeyboardEvent)
- Handles keyboard input for controlsonKeyUp(e: KeyboardEvent)
- Handles keyboard key release eventsonGamepadConnected(e: GamepadEvent)
- Handles gamepad connectiononGamepadDisconnected()
- Handles gamepad disconnection
invert()
- Inverts the game colors (dark/light mode)update()
- Updates game objects and animations each frameclearCanvas()
- Clears the game canvasupdateCanvasScaling()
- Updates canvas size based on device pixel ratio
generatedSoundFx.init()
- Initializes sound effectsplaySound(soundType)
- Plays a specific sound effectstopAllSounds()
- Stops all currently playing sounds
isRunning()
- Returns whether game is currently runninghasObstacles()
- Checks if there are active obstaclesisArcadeMode()
- Checks if arcade mode is enabledisInvertedMode()
- Checks if colors are inverted
updateHighScore(highScore)
- Updates the high scoredistanceRan()
- Gets the current distance/scoregetSpeedY()
- Gets current vertical speed
The older source code from 2016 can be found in the wayou/t-rex-runner repository. This version uses ES5 syntax without classes. Shout-out to @wayou for maintaining it.
Key differences from the current version:
- Uses constructor functions instead of ES6 classes
- No module system, all code in one file
- Global namespace with
Runner
object - Prototype-based inheritance
- No strict mode
- Older browser compatibility
Source Code : https://source.chromium.org/chromium/chromium/src/+/main:components/neterror/resources/
Github Mirror : https://github.com/chromium/chromium/tree/main/components/neterror/resources
Feel free to open issues and pull requests!