make a simple dino jump like game with html, css and java
- User
make a simple dino jump like game with html, css and java
- 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.htmland 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.cssand 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 themoveObstacleanimation..jump: Defines the jump animation..score: Styles the score display.
Step 3: JavaScript
Create a file named
script.jsand 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
jumpclass to trigger the CSS animation. - Removes the
jumpclass after the animation completes.
- Event Listeners:
- Listens for the
Spacekey press and mouse clicks to trigger jumps.
- Listens for the
- Collision Detection & Scoring:
- Uses
setIntervalto 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.
- Uses
Step 4: Running the Game
-
Create the Files:
- Create a folder for your project.
- Inside the folder, create
index.html,style.css, andscript.jswith the code provided above.
-
Open the Game:
- Open
index.htmlin your preferred web browser. - You should see the game area with the Dino and an obstacle approaching.
- Press the
Spacebar or click anywhere to make the Dino jump over the obstacle.
- Open
-
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:
- Multiple Obstacles: Introduce different types and speeds of obstacles.
- High Score Tracking: Keep track of and display the highest score achieved.
- Sound Effects: Add jump and collision sounds for better user experience.
- Mobile Support: Improve touch controls for mobile devices.
- Graphics: Replace the Dino and obstacles with images or sprites for better visuals.
- 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.jsconst 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!