Skip to content
TheChatArchiveThe Chat Archive
o1-mini-2024-09-12· OpenAIAi2 WildChat-4.8M (AI2)

make a simple dino jump like game with html, css and java

  1. User

    make a simple dino jump like game with html, css and java

  2. Assistant

    Creating a simple Dino jumping game similar to the Chrome offline game is a fun project that can help you understand the basics of HTML, CSS, and JavaScript. Below, I'll guide you through building a simple version of this game step-by-step.

    Project Structure

    We'll create a single HTML file that includes embedded CSS and JavaScript for simplicity. Here's the structure we'll follow:

    • HTML: Defines the game area and the characters (Dino and obstacles).
    • CSS: Styles the game elements and handles animations.
    • JavaScript: Manages game logic such as jumping, obstacle movement, collision detection, and scoring.

    Step 1: HTML

    Create a file named index.html and add the following code:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Simple Dino Jump Game</title>
        <link rel="stylesheet" href="style.css">
    </head>
    <body>
        <div class="game">
            <div class="dino" id="dino"></div>
            <div class="obstacle" id="obstacle"></div>
        </div>
        <div class="score">Score: <span id="score">0</span></div>
    
        <script src="script.js"></script>
    </body>
    </html>
    

    Explanation:

    • .game: The main game area.
    • .dino: Represents the Dino character.
    • .obstacle: Represents the obstacle that the Dino needs to jump over.
    • .score: Displays the current score.
    • JavaScript and CSS: Linked externally for better organization.

    Step 2: CSS

    Create a file named style.css and add the following styles:

    /* Reset some default styles */
    * {
        margin: 0;
        padding: 0;
        box-sizing: border-box;
    }
    
    body {
        background-color: #fafafa;
        display: flex;
        flex-direction: column;
        align-items: center;
        height: 100vh;
        overflow: hidden;
        font-family: Arial, sans-serif;
    }
    
    .game {
        position: relative;
        width: 800px;
        height: 200px;
        border: 2px solid #333;
        background-color: #f0f0f0;
        overflow: hidden;
        margin-top: 50px;
    }
    
    .dino {
        position: absolute;
        bottom: 10px;
        left: 50px;
        width: 40px;
        height: 40px;
        background-color: #555;
        border-radius: 5px;
    }
    
    .obstacle {
        position: absolute;
        bottom: 10px;
        right: -60px;
        width: 30px;
        height: 50px;
        background-color: #e74c3c;
        border-radius: 5px;
        animation: moveObstacle 2s linear infinite;
    }
    
    @keyframes moveObstacle {
        from {
            right: -60px;
        }
        to {
            right: 100%;
        }
    }
    
    .score {
        margin-top: 20px;
        font-size: 24px;
    }
    
    .jump {
        animation: jump 0.5s ease;
    }
    
    @keyframes jump {
        0% {
            bottom: 10px;
        }
        50% {
            bottom: 80px;
        }
        100% {
            bottom: 10px;
        }
    }
    

    Explanation:

    • .game: Sets up the game area with a fixed size and styling.
    • .dino: Styles the Dino character as a square with rounded corners.
    • .obstacle: Styles the obstacle and defines its movement using the moveObstacle animation.
    • .jump: Defines the jump animation.
    • .score: Styles the score display.

    Step 3: JavaScript

    Create a file named script.js and add the following code:

    const dino = document.getElementById('dino');
    const obstacle = document.getElementById('obstacle');
    const scoreElement = document.getElementById('score');
    
    let isJumping = false;
    let score = 0;
    
    // Handle the jump
    function jump() {
        if (isJumping) return;
    
        isJumping = true;
        dino.classList.add('jump');
    
        setTimeout(() => {
            dino.classList.remove('jump');
            isJumping = false;
        }, 500); // Duration of the jump animation
    }
    
    // Listen for spacebar or mouse click to jump
    document.addEventListener('keydown', function(event) {
        if (event.code === 'Space') {
            jump();
        }
    });
    
    document.addEventListener('click', jump);
    
    // Collision detection and score
    const checkCollision = setInterval(function() {
        const dinoTop = parseInt(window.getComputedStyle(dino).getPropertyValue('bottom'));
        const obstacleRight = parseInt(window.getComputedStyle(obstacle).getPropertyValue('right'));
        
        // Position of obstacle relative to dino
        const obstaclePosition = 800 - obstacleRight; // Game width is 800px
    
        // Detect collision
        if (obstaclePosition > 50 && obstaclePosition < 90 && dinoTop <= 60) {
            alert('Game Over! Your score: ' + score);
            score = 0;
            scoreElement.textContent = score;
            obstacle.style.animation = 'none';
            obstacle.style.right = '-60px';
            setTimeout(() => {
                obstacle.style.animation = 'moveObstacle 2s linear infinite';
            }, 1000);
        }
    
        // Update score
        if (obstacleRight > 800 && !isJumping) {
            score++;
            scoreElement.textContent = score;
        }
    }, 10);
    

    Explanation:

    • Elements Grabbed: The Dino, obstacle, and score elements are accessed using getElementById.
    • Jump Logic:
      • Checks if the Dino is already jumping to prevent multiple jumps.
      • Adds the jump class to trigger the CSS animation.
      • Removes the jump class after the animation completes.
    • Event Listeners:
      • Listens for the Space key press and mouse clicks to trigger jumps.
    • Collision Detection & Scoring:
      • Uses setInterval to continuously check for collisions.
      • Determines the positions of the Dino and obstacle.
      • If a collision is detected, shows a Game Over alert, resets the score, and resets the obstacle animation.
      • Increments the score when the obstacle passes the Dino without collision.

    Step 4: Running the Game

    1. Create the Files:

      • Create a folder for your project.
      • Inside the folder, create index.html, style.css, and script.js with the code provided above.
    2. Open the Game:

      • Open index.html in your preferred web browser.
      • You should see the game area with the Dino and an obstacle approaching.
      • Press the Space bar or click anywhere to make the Dino jump over the obstacle.
    3. Gameplay:

      • Each time you successfully jump over an obstacle, your score increases.
      • If the Dino collides with an obstacle, a Game Over alert appears, and the score resets.

    Enhancements

    This is a very basic version of the Dino jump game. Here are some ideas to enhance it:

    1. Multiple Obstacles: Introduce different types and speeds of obstacles.
    2. High Score Tracking: Keep track of and display the highest score achieved.
    3. Sound Effects: Add jump and collision sounds for better user experience.
    4. Mobile Support: Improve touch controls for mobile devices.
    5. Graphics: Replace the Dino and obstacles with images or sprites for better visuals.
    6. Responsive Design: Make the game responsive to different screen sizes.

    Complete Code

    For convenience, here's the complete code bundled together.

    index.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=800, initial-scale=1.0">
        <title>Simple Dino Jump Game</title>
        <link rel="stylesheet" href="style.css">
    </head>
    <body>
        <div class="game">
            <div class="dino" id="dino"></div>
            <div class="obstacle" id="obstacle"></div>
        </div>
        <div class="score">Score: <span id="score">0</span></div>
    
        <script src="script.js"></script>
    </body>
    </html>
    

    style.css

    /* Reset some default styles */
    * {
        margin: 0;
        padding: 0;
        box-sizing: border-box;
    }
    
    body {
        background-color: #fafafa;
        display: flex;
        flex-direction: column;
        align-items: center;
        height: 100vh;
        overflow: hidden;
        font-family: Arial, sans-serif;
    }
    
    .game {
        position: relative;
        width: 800px;
        height: 200px;
        border: 2px solid #333;
        background-color: #f0f0f0;
        overflow: hidden;
        margin-top: 50px;
    }
    
    .dino {
        position: absolute;
        bottom: 10px;
        left: 50px;
        width: 40px;
        height: 40px;
        background-color: #555;
        border-radius: 5px;
    }
    
    .obstacle {
        position: absolute;
        bottom: 10px;
        right: -60px;
        width: 30px;
        height: 50px;
        background-color: #e74c3c;
        border-radius: 5px;
        animation: moveObstacle 2s linear infinite;
    }
    
    @keyframes moveObstacle {
        from {
            right: -60px;
        }
        to {
            right: 100%;
        }
    }
    
    .score {
        margin-top: 20px;
        font-size: 24px;
    }
    
    .jump {
        animation: jump 0.5s ease;
    }
    
    @keyframes jump {
        0% {
            bottom: 10px;
        }
        50% {
            bottom: 80px;
        }
        100% {
            bottom: 10px;
        }
    }
    

    script.js

    const dino = document.getElementById('dino');
    const obstacle = document.getElementById('obstacle');
    const scoreElement = document.getElementById('score');
    
    let isJumping = false;
    let score = 0;
    
    // Handle the jump
    function jump() {
        if (isJumping) return;
    
        isJumping = true;
        dino.classList.add('jump');
    
        setTimeout(() => {
            dino.classList.remove('jump');
            isJumping = false;
        }, 500); // Duration of the jump animation
    }
    
    // Listen for spacebar or mouse click to jump
    document.addEventListener('keydown', function(event) {
        if (event.code === 'Space') {
            jump();
        }
    });
    
    document.addEventListener('click', jump);
    
    // Collision detection and score
    const checkCollision = setInterval(function() {
        const dinoTop = parseInt(window.getComputedStyle(dino).getPropertyValue('bottom'));
        const obstacleRight = parseInt(window.getComputedStyle(obstacle).getPropertyValue('right'));
        
        // Position of obstacle relative to dino
        const obstaclePosition = 800 - obstacleRight; // Game width is 800px
    
        // Detect collision
        if (obstaclePosition > 50 && obstaclePosition < 90 && dinoTop <= 60) {
            alert('Game Over! Your score: ' + score);
            score = 0;
            scoreElement.textContent = score;
            obstacle.style.animation = 'none';
            obstacle.style.right = '-60px';
            setTimeout(() => {
                obstacle.style.animation = 'moveObstacle 2s linear infinite';
            }, 1000);
        }
    
        // Update score
        if (obstacleRight > 800 && !isJumping) {
            score++;
            scoreElement.textContent = score;
        }
    }, 10);
    

    Final Notes

    This simple game demonstrates the basics of game development using web technologies. As you become more comfortable with HTML, CSS, and JavaScript, you can enhance and expand this game with more features and better graphics. Happy coding!