create a clone of chrome's dinosaur game and give me the code that include html, css and javascript...
Assistant
Sure! I can help you create a simple clone of Chrome's Dinosaur Game using HTML, CSS, and JavaScript. Below is a complete, self-contained example that you can run directly in your browser.
How to Use
Create a Single HTML File:
Open your favorite text editor (like VS Code, Sublime Text, or Notepad).
Create a new file and name it index.html.
Copy and paste the code below into this file.
Save the file and open it in your web browser to play the game.
Complete Code
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=device-width, initial-scale=1.0">
<title>Dinosaur Game Clone</title>
<style>
/* Reset default styles */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
background-color: #f7f7f7;
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
overflow: hidden;
font-family: sans-serif;
}
#game {
position: relative;
width: 800px;
height: 200px;
border: 2px solid #000;
background-color: #fff;
overflow: hidden;
}
/* Ground */
#ground {
position: absolute;
bottom: 0;
width: 100%;
height: 20px;
background-color: #555;
overflow: hidden;
}
#ground::before {
content: '';
position: absolute;
width: 200%;
height: 100%;
background: repeating-linear-gradient(
to right,
#555,
#555 20px,
#666 20px,
#666 40px
);
animation: moveGround 1s linear infinite;
}
@keyframes moveGround {
from { transform: translateX(0); }
to { transform: translateX(-50%); }
}
/* Dinosaur */
#dino {
position: absolute;
bottom: 20px;
left: 50px;
width: 40px;
height: 40px;
background-color: #333;
border-radius: 5px;
animation: run 0.5s steps(2) infinite;
}
@keyframes run {
0% { background-color: #333; }
50% { background-color: #555; }
100% { background-color: #333; }
}
/* Obstacle */
.obstacle {
position: absolute;
bottom: 20px;
width: 20px;
height: 40px;
background-color: #e74c3c;
animation: moveObstacle 1.5s linear infinite;
}
@keyframes moveObstacle {
from { right: -20px; }
to { right: 820px; }
}
/* Score */
#score {
position: absolute;
top: 10px;
right: 20px;
font-size: 20px;
font-weight: bold;
}
/* Game Over */
#gameOver {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
font-size: 36px;
color: #e74c3c;
display: none;
}
</style>
</head>
<body>
<div id="game">
<div id="ground"></div>
<div id="dino"></div>
<div id="score">Score: 0</div>
<div id="gameOver">Game Over</div>
</div>
<script>
const dino = document.getElementById('dino');
const game = document.getElementById('game');
const scoreDisplay = document.getElementById('score');
const gameOverDisplay = document.getElementById('gameOver');
let isJumping = false;
let gravity = 0.9;
let position = 0;
let score = 0;
let obstacleInterval;
let gameOver = false;
function jump() {
if (isJumping) return;
isJumping = true;
let upInterval = setInterval(() => {
if (position >= 150) {
clearInterval(upInterval);
// Fall down
let downInterval = setInterval(() => {
if (position <= 0) {
clearInterval(downInterval);
isJumping = false;
}
position -= 5;
position *= gravity;
dino.style.bottom = position + 'px';
}, 20);
}
// Jump up
position += 30;
position *= gravity;
dino.style.bottom = position + 'px';
}, 20);
}
function createObstacle() {
const obstacle = document.createElement('div');
obstacle.classList.add('obstacle');
game.appendChild(obstacle);
let obstaclePosition = 800;
obstacle.style.right = obstaclePosition + 'px';
let timer = setInterval(() => {
if (gameOver) {
clearInterval(timer);
game.removeChild(obstacle);
return;
}
obstaclePosition -= 5;
obstacle.style.right = obstaclePosition + 'px';
// Collision Detection
if (obstaclePosition > 40 && obstaclePosition < 90 && position < 40) {
// Game Over
gameOver = true;
clearInterval(timer);
gameOverDisplay.style.display = 'block';
document.removeEventListener('keydown', handleKeydown);
}
// Increase Score
if (obstaclePosition === 100) {
score += 1;
scoreDisplay.innerText = 'Score: ' + score;
}
// Remove obstacle after it goes out of screen
if (obstaclePosition < -20) {
clearInterval(timer);
game.removeChild(obstacle);
}
}, 20);
}
function handleKeydown(e) {
if (e.code === 'Space') {
jump();
}
}
document.addEventListener('keydown', handleKeydown);
// Generate obstacles at random intervals
function startGame() {
obstacleInterval = setInterval(() => {
if (gameOver) {
clearInterval(obstacleInterval);
} else {
createObstacle();
}
}, 2000);
}
startGame();
</script>
</body>
</html>
Explanation of the Code
HTML Structure:
#game: The main container for the game.
#ground: Represents the ground that moves to simulate the dinosaur running.
#dino: The dinosaur character.
.obstacle: Obstacles (cacti) that the dinosaur must avoid.
#score: Displays the current score.
#gameOver: Shows the "Game Over" message when the player loses.
CSS Styling:
Reset Styles: Removes default margins and paddings.
Layout: Centers the game area on the screen.
Ground Animation: Creates a moving ground effect using CSS keyframes.
Dinosaur Animation: A simple animation to simulate running.
Obstacle Animation: Obstacles move from right to left across the screen.
Score and Game Over Styling: Positions the score and game over message appropriately.
JavaScript Functionality:
Jump Mechanism: Handles the dinosaur's jump when the spacebar is pressed.
Obstacle Generation: Creates obstacles at regular intervals that move towards the dinosaur.
Collision Detection: Checks if the dinosaur collides with an obstacle, triggering a game over.
Score Tracking: Increments the score as the player successfully avoids obstacles.
Event Listeners: Listens for the spacebar keypress to make the dinosaur jump.
How to Play
Start the Game: Open the index.html file in your browser. The game starts automatically.
Make the Dinosaur Jump: Press the Spacebar to make the dinosaur jump over incoming obstacles.
Objective: Avoid as many obstacles as possible to increase your score.
Game Over: If the dinosaur collides with an obstacle, the game ends, and a "Game Over" message appears.
Customization
Feel free to customize the game further by:
Adding More Obstacles: Modify the createObstacle function to include different types of obstacles.
Increasing Difficulty: Gradually increase the speed of obstacles or decrease the interval between them as the score increases.
Enhancing Graphics: Replace the simple colored blocks with images or more detailed graphics for a polished look.
Adding Sound Effects: Incorporate sound effects for jumping and collision events to make the game more engaging.
Conclusion
This is a basic implementation of Chrome's Dinosaur Game clone. It serves as a foundation that you can build upon to add more features and improve the gameplay experience. Happy coding and have fun playing your new game!