Dinossaur game

The dinossaur game also known as the Chrome Dino is a built-in browser game in the Google Chrome web browser. The player guides a pixelated Tyrannosaurus rex across a side-scrolling landscape, avoiding obstacles. In this tutorial we will explan how to create a basic version of the game using LimonadeJS.

A working example

Source code

var Dinosaur = function() {
// Initializing self.
var self = {
    // Propertie to store the dino element.
    dino: document.getElementById("dino"),
    // Propertie to store the cactus element.
    cactus: document.getElementById("cactus"),
}
/**
 * Self.init is used to initialize some methods and functions.
 */ 
self.init = function() {
  /*
   * Call the function self.Jump() when a key is pressed.
   */ 
  document.addEventListener("keydown", function () {
  self.jump();
  });
  /**
   * This interval will call self.isAlive() at every 10 miliseconds to check 
   * if the dinosaur is already alive.
   * @param {function} self.isAlive - The function that will be called.
   * @param {number} 10 - The number represents the milliseconds of the interval.
   */
  setInterval(self.isAlive, 10);
}
/**
 * Make the dinosaur recieve the class jump, and after the jump, remove it. 
 */ 
self.jump = function() {
    if (dino.classList != "jump") {
    dino.classList.add("jump");
    /**
     * Time out execute a function after 300 milliseconds.
     * @param {function} dino.classList.remove - The function to remove jump class from the dinossaur.
     * @param {number} 300 - The number represents the milliseconds of the timeout.
     */
    setTimeout(function () {
        dino.classList.remove("jump");
    }, 300);
    }
}
/**
 * Cactus recieve a class to make his animation;
 * @param {object} o - The cactus object.
 */
self.play = function (o) {
    o.setAttribute("class", "cactus");
    o.style.display = "";
}
/**
 * Self.isAlive uses an if condition to check the position of
 * the dinosaur and the cactus to detect collision.
 */
self.isAlive = function() {
    // Variable to store the position: of the top of the dino.
    var dinoTop = parseInt(window.getComputedStyle(dino).getPropertyValue("top"));
    // Variable to store the position of the left of the cactus.
    var cactusLeft = parseInt(window.getComputedStyle(cactus).getPropertyValue("left"));
    if (cactusLeft < 50 && cactusLeft > 0 && dinoTop >= 140) {
        alert("Game Over!");
        cactus.classList.remove("cactus");
    }
}
// Game template.
let template = `
    <div class="game" @ready="self.init()">
        <div id="dino"><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGYAA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3rHRXOSpKfaRF88hieITpAjuA9asMaxQh03WccT59kZ46+jyqoBIpCNKuv2MXVDzWaAiW5XtD9Pf9+ELOx9NYv8ryf/o8+XM+PsfC6XuWtx51c4B0hnjU2P3OEGOJEXGTyXYCa6YMWR1t/O8oQAGannXPQpwgjaxGedQoHqNYN1p79dVXzTy6vVisnzJl9w9A3+HhIcpkdKpRnrhnSXke9RxXB48asXl0dHRwcGCUIXoS72TKF4/157L44n+iSC8ldVycuGd1iR8fibOP7ndUZpRDg9BGnxeO9VMGKUIJfBZoKJOSSEa9lno+B0g3SpwiXUbTYg9SsZBvY3pMnlvDliiTxWiN50os4M6r5wvHLSxVSn/ortGNv+8N6Jpmnh63hi1RpkTWqGOtPs8COhB648YNEhOexo7/emUkyhjHYP+mszVsiTI+K8WRmDpLNNXHKFkPayK0FsCmaaNpNG4TW6JMZJ3IRwvTOXLxJTbN0N3t3r17J84W91vHSCfJ1rAlykCFn72KtedoRDdEOChlPZc4d47RruxfkkIt28SWKCMWpUbaKSQXHnM6GLW3t9f1rT9ygP7wlUh9P7UE9Go72BJlIkvKV3rkPxZ0GGffafTlMO66yq4poruxBebsVWNrWDNlOEo+XKpy5coVkYU1nkvkiIMC7fSPObEMpScEGegDsz/FUL9+/bprh1fobvhCta4d61eZkOEGn3NGMgL6IhPpnFI6B71CzWTEPxFfwXBDjJXRkptJDGxsS9BXctsm1kwZB2aPwocPH965c4eTrqPEwnMUeNQ4/tRjFlO2JlTKWboRpv790OiChrXT/3+xonhL+Ouv/wP7NHMFXXEVjwAAAABJRU5ErkJggg=="></div>
        <div id="cactus" style="display: none"><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAC8AAABJCAIAAAAi6Oi8AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAASqSURBVGhD7drZUuNIEIXhfjQuCMy+7xgTwA1vz2p2hvnsY2scI7kbM5a6J8LnojprUdVPZlapRPPj80/SjGa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K89ayqKnDZR3VFqqCxFcWo+AWdatyDwJIWc77JJDbl1Ujs6dMkaSJ2/nfj+vpayUkoGUCTQwhqzOKfK+dYyp5bhi+pEMRIe2j8eJXqzVWlHx4ra/BQSR4ofnfhmLEqDwWFyjT9ySo0mK6k7/gmytoSuU/SE4jkdUEzqSajKSR7Bgg7O9lcjNDEaJSGkrzS1s7/zTT2Wk4/7wfLg6CChkY35tf1n3wDyBujoGEUO7xRGoGQxQi4JyjUNI1NqExOWDJAAkSBCI1qE3kzoxmvGc14zWjG68+iIcvkTujq7gYYIHKHV82t2QHdxOlXXPy63a5Lu4VBQMHBJa6k7FarpWv072G+rol9EwHKHTmfoe5cSkC5LyubiJSf2IdfPqf5AEFQBAhEp9PBl7dpQ3lDwgRIciRRfBH7HuUYZAIHSPU7f2Py+fk3MkUn+Iu49v4AAAAASUVORK5CYII="></div>
    </div>
    <div class="btn"><button onclick="self.play(cactus)">Play</button></div>`;
    
return lemonade.element(template, self);
}

Acess dinosaur game github here