How to Create the Dino Chrome Game
In this article
You can build the Chrome dinosaur game as a single HTML file using the HTML5 canvas element and a bit of JavaScript, no libraries or downloads required. The game needs four things: a canvas to draw on, a dinosaur that jumps, obstacles that scroll toward it, and a game loop that updates the screen about 60 times per second. Open a plain text editor, save the file as dino.html, and double-click it to play in any modern browser. The steps below build a working version from scratch, then explain how to tune it and what usually breaks.
What You'll Need
- A plain text editor (Notepad, TextEdit in plain-text mode, VS Code, or any code editor).
- A modern browser such as Chrome, Edge, Firefox, or Safari to run and test the file.
- Basic familiarity with HTML tags and JavaScript variables and functions. You do not need prior game experience.
- Optionally, a local image file for the dinosaur sprite. You can also draw a simple rectangle placeholder and swap in art later.
Step-by-Step
- Create the HTML skeleton. In your text editor, add the standard document structure: a
<!DOCTYPE html>line, an<html>element, a<head>with a<title>, and a<body>. Inside the body, add a single<canvas>element with anidof your choice (for examplegame), a width of 800 and a height of 200. Give it a thin border so you can see its edges. Save the file asdino.html. - Get the drawing context. Add a
<script>tag at the end of the body. Select the canvas withdocument.getElementById, then callgetContext('2d')on it and store the result in a variable. Every drawing command goes through that context object. - Define the game state. Create variables for the dinosaur's horizontal and vertical position, its width and height, its vertical velocity, and a boolean called something like
isJumping. Add variables for gravity (a small positive number such as 0.6) and jump strength (a negative number such as -12, because the y-axis points down). Also create an empty array to hold obstacles and a numeric score. - Draw the dinosaur. Write a function that clears the canvas with
clearRect, then fills a rectangle at the dinosaur's coordinates usingfillRect. A rectangle is fine to start; you can replace it withdrawImageonce the mechanics work. Set the fill colour before drawing. - Add the jump. Listen for the
keydownevent on the document. When the key is the spacebar or the up arrow, and the dinosaur is not already jumping, set the vertical velocity to your jump strength and setisJumpingto true. CallpreventDefaulton the event so the page does not scroll. For touch devices, add atouchstartlistener that does the same thing. - Apply gravity each frame. In your update step, add gravity to the vertical velocity, then add the velocity to the dinosaur's y position. When the y position reaches the ground line, snap it back to the ground, set the velocity to zero, and set
isJumpingto false. This gives a smooth arc instead of a teleport. - Spawn and move obstacles. Use a timer or a frame counter to add a new obstacle object to the array every so often. Each obstacle stores an x position starting at the right edge of the canvas, a width, and a height. On every frame, subtract a fixed speed from each obstacle's x value so it moves left. Remove obstacles whose x position plus width is less than zero so the array does not grow forever.
- Detect collisions. For each obstacle, check whether the dinosaur's rectangle overlaps it. A simple axis-aligned check works: the dinosaur's right edge is past the obstacle's left edge, the dinosaur's left edge is before the obstacle's right edge, and the same comparison holds vertically. If all four conditions are true, the game is over.
- Run the game loop. Use
requestAnimationFrameto call a single function repeatedly. That function should update positions, draw the background, draw the dinosaur, draw every obstacle, and draw the score. Calling it again at the end keeps the loop alive. This is smoother thansetIntervalbecause the browser syncs it to the display refresh. - Handle game over and restart. When a collision happens, stop updating positions and show a message on the canvas. Listen for a key press or tap to reset the dinosaur position, empty the obstacle array, reset the score and speed, and start the loop again. Keep the reset logic in one function so restart behaves identically every time.
- Increase difficulty over time. Tie the obstacle speed and spawn rate to the score. For example, every time the score passes a threshold, raise the speed slightly and shorten the gap between spawns. Cap the maximum speed so the game stays playable rather than impossible.
- Test and polish. Open the file in your browser, jump repeatedly, and check that the dinosaur lands cleanly, obstacles never overlap unfairly, and the score resets properly. Adjust gravity, jump strength, and speed until it feels right.
Common Mistakes
The most frequent problem is forgetting to clear the canvas each frame, which leaves a trail of previous drawings behind the dinosaur. Another is using the wrong sign for gravity or jump velocity, so the dinosaur sinks instead of rising. Many beginners also forget to remove off-screen obstacles, and the array grows until the game slows down. Collision boxes that are much larger than the visible sprite make the game feel unfair, so keep the hitbox slightly smaller than the drawing. Finally, calling requestAnimationFrame more than once per frame creates duplicate loops that run at double speed.
Tips
Keep all tuning numbers (gravity, jump strength, speed, spawn interval) in one place at the top of the script so you can adjust the feel without hunting through the code. Use a fixed ground line variable rather than hard-coding a y value in several places. Draw the score with fillText in a monospace font so digits do not jitter as the number grows. Test with the browser's developer tools open so you can see console errors immediately. Once the rectangle version works, swap in a sprite sheet and animate two frames for a running effect.
Troubleshooting
If nothing appears on the canvas, check that the script runs after the canvas element exists in the page, and look at the browser console for errors. If the dinosaur jumps but never comes down, your gravity value is probably zero or your ground check uses the wrong comparison. If obstacles never appear, confirm the spawn timer actually fires and that new objects are pushed into the array. If the game runs too fast or too slow on different machines, base movement on elapsed time rather than raw frame counts. If the spacebar scrolls the page, you are missing preventDefault. If the game freezes after a collision, make sure the restart handler resets every state variable, not just the score.
Frequently Asked Questions
Do I need any software or game engine?
No. A text editor and a browser are enough. The canvas API handles drawing and animation natively.
Can I use the original Chrome dinosaur artwork?
The artwork belongs to Google. Draw your own sprite or use an image you have the rights to instead.
Why does my game stutter?
Usually because the loop is doing too much work or obstacles are never removed. Clear the canvas once per frame and prune the obstacle array.
Can I make it work on phones?
Yes. Add a touch listener that triggers the jump, and set the canvas width with CSS so it scales to the screen.
How do I add sound?
Create an Audio object pointing at an audio file and call its play method on jump and collision. Browsers may block audio until the user interacts with the page, so trigger it from a key press or tap.
Is it legal to publish my own version?
Building your own game with original code and art is fine. Copying Google's assets or branding is not.