Skip to main content

Create Your Own Chrome Dino Game with HTML, CSS, and JavaScript


Have you ever wanted to create your own version of the Chrome Dino game? With just a few lines of HTML, CSS, and JavaScript, you can bring this iconic game to life. Here's a step-by-step guide to building a simple yet fun version of the Dino game.

HTML

Start by creating an HTML file to define the game structure. You'll need a container for the game, a <div> for the dinosaur, another <div> for the cactus, and a score display.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Chrome Dino Game</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <div class="game-container">
        <div id="dino" class="dino"></div>
        <div id="cactus" class="cactus"></div>
        <div id="score">Score: 0</div>
    </div>
    <script src="script.js"></script>
</body>
</html>

CSS

Next, style the game elements using CSS. Define the appearance and animation of the dinosaur and cactus.

body {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    margin: 0;
    background-color: #f7f7f7;
    font-family: Arial, sans-serif;
}

.game-container {
    position: relative;
    width: 600px;
    height: 200px;
    border: 2px solid #000;
    overflow: hidden;
    background-color: #fff;
}

.dino {
    position: absolute;
    bottom: 0;
    left: 50px;
    width: 40px;
    height: 40px;
    background-color: #333;
}

.cactus {
    position: absolute;
    bottom: 0;
    right: -40px;
    width: 20px;
    height: 40px;
    background-color: #0f0;
    animation: moveCactus 2s linear infinite;
}

#score {
    position: absolute;
    top: 10px;
    right: 10px;
    font-size: 20px;
}

@keyframes moveCactus {
    from {
        right: -40px;
    }
    to {
        right: 640px;
    }
}

JavaScript

Finally, add JavaScript to handle the game logic, including jumping, collision detection, and score updates.

let dino = document.getElementById('dino');
let cactus = document.getElementById('cactus');
let scoreDisplay = document.getElementById('score');
let isJumping = false;
let score = 0;

function jump() {
    if (isJumping) return;
    isJumping = true;
    let upInterval = setInterval(() => {
        if (parseInt(window.getComputedStyle(dino).bottom) >= 150) {
            clearInterval(upInterval);
            let downInterval = setInterval(() => {
                if (parseInt(window.getComputedStyle(dino).bottom) <= 0) {
                    clearInterval(downInterval);
                    isJumping = false;
                }
                dino.style.bottom = `${parseInt(window.getComputedStyle(dino).bottom) - 5}px`;
            }, 20);
        }
        dino.style.bottom = `${parseInt(window.getComputedStyle(dino).bottom) + 5}px`;
    }, 20);
}

function checkCollision() {
    let dinoBottom = parseInt(window.getComputedStyle(dino).bottom);
    let cactusLeft = parseInt(window.getComputedStyle(cactus).left);

    if (cactusLeft > 0 && cactusLeft < 40 && dinoBottom < 40) {
        alert('Game Over');
        cactus.style.animation = 'none';
        cactus.style.display = 'none';
    }
}

function updateScore() {
    score++;
    scoreDisplay.innerText = `Score: ${score}`;
}

document.addEventListener('keydown', function(event) {
    if (event.key === ' ') {
        jump();
    }
});

setInterval(checkCollision, 10);
setInterval(updateScore, 100);

This code creates a simple 2D version of the Chrome Dino game. The dinosaur jumps when you press the space key, and the game ends if the dinosaur collides with the cactus. Enjoy creating and playing your very own Chrome Dino game!

Comments

Popular posts from this blog

Creating an Interactive Tic Tac Toe Game

In this tutorial, we'll walk through the steps to create a dynamic Tic Tac Toe game using HTML, CSS, and JavaScript. This classic game is a great way to practice your web development skills and learn more about interactive web applications. HTML Structure We'll start with the HTML structure, which includes the game board, a reset button, and a status display: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Tic Tac Toe</title> <link rel="stylesheet" href="styles.css"> </head> <body> <div class="container"> <h1>Tic Tac Toe</h1> <div id="board" class="board"></div> <button id="reset">Reset Game</button> <div id=...

How To Make Casio Calculator using HTML, CSS Javascript with all Functional features

Creating a Casio FX-991CW calculator using HTML, CSS, and JavaScript is an engaging project for learning web development. This guide will help you create a functional calculator that can perform basic arithmetic, trigonometric functions, and square root calculations. Step 1: HTML Structure First, set up the HTML structure for the calculator. Here's an example: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Casio FX-991CW Calculator</title> <link rel="stylesheet" href="styles.css"> </head> <body> <div class="calculator"> <input type="text" class="calculator-screen" id="main-screen" value="" disabled /> <input type="text" class="calculator-screen" ...

Creating a Simple Car Racing Game with HTML, CSS, Javascript

In this post, we will create a simple car racing game using HTML, CSS, and JavaScript. This game will feature a player car, an opponent car, and a score counter. The player can move their car left and right to avoid collisions with the opponent car. Let's dive into the code! HTML Structure The HTML structure consists of a game area that contains the road, player car, opponent car, and score display: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Car Racing Game</title> <link rel="stylesheet" href="styles.css"> </head> <body> <div class="gameArea"> <div class="road"></div> <div class="playerCar"></div> <div class="opponentCar"></div...