Creating a game similar to the Chrome Dino, but featuring a mouse character instead of a dinosaur, is an engaging project that can be accomplished using HTML, CSS, and JavaScript. Below is a step-by-step guide to help you build this game:
1. Set Up the HTML Structure
Create an index.html file with the following content:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta content="width=device-width, initial-scale=1.0" name="viewport"/>
<title>Mouse Runner Game</title>
<link href="styles.css" rel="stylesheet"/>
</head>
<body>
<div class="game-container">
<div class="mouse" id="mouse"></div>
<div class="obstacle" id="obstacle"></div>
<div class="score" id="score">Score: 0</div>
</div>
<script src="script.js"></script>
</body>
</html>
2. Style the Game with CSS
Create a styles.css file to style the game elements:
body {
margin: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #f0f0f0;
font-family: Arial, sans-serif;
}
.game-container {
position: relative;
width: 600px;
height: 200px;
border: 2px solid #000;
overflow: hidden;
background-color: #fff;
}
.mouse {
position: absolute;
bottom: 0;
left: 50px;
width: 40px;
height: 40px;
background-color: gray;
border-radius: 50%;
}
.obstacle {
position: absolute;
bottom: 0;
right: -50px;
width: 20px;
height: 40px;
background-color: red;
animation: moveObstacle 2s infinite linear;
}
@keyframes moveObstacle {
from {
right: -50px;
}
to {
right: 100%;
}
}
.score {
position: absolute;
top: 10px;
left: 10px;
font-size: 18px;
}
3. Implement the Game Logic with JavaScript
Create a script.js file to handle the game mechanics:
const mouse = document.getElementById('mouse');
const obstacle = document.getElementById('obstacle');
const scoreDisplay = document.getElementById('score');
let isJumping = false;
let score = 0;
document.addEventListener('keydown', function(event) {
if (event.code === 'Space' && !isJumping) {
jump();
}
});
function jump() {
isJumping = true;
let jumpHeight = 0;
const jumpInterval = setInterval(() => {
if (jumpHeight >= 100) {
clearInterval(jumpInterval);
const fallInterval = setInterval(() => {
if (jumpHeight <= 0) {
clearInterval(fallInterval);
isJumping = false;
}
jumpHeight -= 5;
mouse.style.bottom = `${jumpHeight}px`;
}, 20);
}
jumpHeight += 5;
mouse.style.bottom = `${jumpHeight}px`;
}, 20);
}
function checkCollision() {
const mouseRect = mouse.getBoundingClientRect();
const obstacleRect = obstacle.getBoundingClientRect();
if (
mouseRect.right > obstacleRect.left &&
mouseRect.left < obstacleRect.right &&
mouseRect.bottom > obstacleRect.top &&
mouseRect.top < obstacleRect.bottom
) {
alert(`Game Over! Your score was: ${score}`);
score = 0;
scoreDisplay.textContent = `Score: ${score}`;
} else if (obstacleRect.right < mouseRect.left) {
score++;
scoreDisplay.textContent = `Score: ${score}`;
}
}
setInterval(checkCollision, 50);
4. Customize the Game
-
Mouse Character: Replace the gray circle representing the mouse with an image of a mouse. Update the .mouse class in your CSS to use a background image:
.mouse {
position: absolute;
bottom: 0;
left: 50px;
width: 40px;
height: 40px;
background-image: url('mouse.png');
background-size: cover;
}
Ensure you have a mouse.png image in your project directory.
-
Obstacles: Modify the .obstacle class to use images of obstacles, such as cheese or traps, to fit the theme.
-
Background: Add a background image or color to the .game-container to enhance the visual appeal.
5. Enhance the Game
-
Sound Effects: Add sound effects for jumping and collisions to make the game more engaging.
-
Mobile Compatibility: Implement touch controls to make the game playable on mobile devices.
-
Difficulty Levels: Increase the speed of obstacles over time to challenge the player.
Additional Resources
For more detailed tutorials and examples, consider exploring the following resources:
These guides provide in-depth explanations and code examples that can help you understand and implement various features in your game.
By following these steps and utilizing the provided resources, you can create a fun and interactive mouse-themed endless runner game.