Simple "Jumping Mouse" Game (Chrome Dino Clone) in JavaScript
Below is a basic implementation of a Chrome Dino-style game, but with a mouse character instead of a dinosaur. This code uses HTML, CSS, and JavaScript, and is designed to be easy to understand and extend. The mouse jumps over obstacles (e.g., cheese blocks), and the game ends if the mouse collides with an obstacle.
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<title>Jumping Mouse Game</title>
<style>
body { background: #e0f7fa; margin: 0; overflow: hidden; }
#gameArea {
position: relative;
width: 800px;
height: 300px;
margin: 40px auto;
background: #fffde7;
border: 2px solid #333;
overflow: hidden;
}
.mouse {
position: absolute;
left: 50px;
bottom: 0;
width: 60px;
height: 60px;
background: url('https://img.icons8.com/emoji/96/mouse-emoji.png') no-repeat center/contain;
}
.obstacle {
position: absolute;
right: 0;
bottom: 0;
width: 40px;
height: 40px;
background: url('https://img.icons8.com/emoji/48/cheese-wedge.png') no-repeat center/contain;
animation: moveObstacle 2s linear infinite;
}
@keyframes moveObstacle {
0% { right: -40px; }
100% { right: 800px; }
}
#gameOver {
display: none;
position: absolute;
top: 40%;
left: 50%;
transform: translate(-50%, -50%);
font-size: 2em;
color: #d32f2f;
background: #fff;
padding: 20px 40px;
border-radius: 10px;
border: 2px solid #d32f2f;
}
</style>
</head>
<body>
<div id="gameArea">
<div class="mouse" id="mouse"></div>
<div id="gameOver">Game Over! Click to Restart</div>
</div>
<script src="game.js"></script>
</body>
</html>
game.js
const gameArea = document.getElementById('gameArea');
const mouse = document.getElementById('mouse');
const gameOverText = document.getElementById('gameOver');
let isJumping = false;
let isGameOver = false;
let obstacle = null;
let jumpHeight = 120;
let gravity = 6;
let mouseBottom = 0;
let obstacleInterval = null;
function jump() {
if (isJumping || isGameOver) return;
isJumping = true;
let upInterval = setInterval(() => {
if (mouseBottom >= jumpHeight) {
clearInterval(upInterval);
// Falling down
let downInterval = setInterval(() => {
if (mouseBottom <= 0) {
clearInterval(downInterval);
isJumping = false;
} else {
mouseBottom -= gravity;
mouse.style.bottom = mouseBottom + 'px';
}
}, 20);
} else {
mouseBottom += gravity;
mouse.style.bottom = mouseBottom + 'px';
}
}, 20);
}
function createObstacle() {
obstacle = document.createElement('div');
obstacle.classList.add('obstacle');
obstacle.style.right = '-40px';
gameArea.appendChild(obstacle);
let obstaclePosition = 800;
let moveObstacle = setInterval(() => {
if (isGameOver) {
clearInterval(moveObstacle);
if (obstacle) obstacle.remove();
return;
}
obstaclePosition -= 6;
obstacle.style.right = obstaclePosition + 'px';
// Collision detection
let mouseRect = mouse.getBoundingClientRect();
let obsRect = obstacle.getBoundingClientRect();
if (
obsRect.left < mouseRect.right - 10 &&
obsRect.right > mouseRect.left + 10 &&
obsRect.bottom > mouseRect.top + 10 &&
obsRect.top < mouseRect.bottom - 10
) {
gameOver();
clearInterval(moveObstacle);
}
// Remove obstacle if off screen
if (obstaclePosition < -40) {
obstacle.remove();
clearInterval(moveObstacle);
}
}, 20);
}
function startGame() {
isGameOver = false;
mouseBottom = 0;
mouse.style.bottom = '0px';
gameOverText.style.display = 'none';
if (obstacle) obstacle.remove();
// Spawn obstacles every 1.5 - 2.5 seconds
obstacleInterval = setInterval(() => {
if (!isGameOver) createObstacle();
}, Math.random() * 1000 + 1500);
}
function gameOver() {
isGameOver = true;
gameOverText.style.display = 'block';
clearInterval(obstacleInterval);
}
document.addEventListener('keydown', (e) => {
if (e.code === 'Space' || e.key === ' ') jump();
});
gameArea.addEventListener('click', () => {
if (isGameOver) startGame();
else jump();
});
// Start the game
startGame();
How It Works
- The mouse character is displayed at the bottom left.
- Press the spacebar or click the game area to make the mouse jump.
- Cheese obstacles move from right to left.
- If the mouse collides with an obstacle, the game ends and displays "Game Over! Click to Restart".
- Click the game area to restart after a game over.
Customization
- Replace the mouse or cheese images with your own sprites by changing the
background URLs in the CSS.
- Adjust
jumpHeight, gravity, and obstacle speed for different game feel.
- Add a score counter, sound effects, or more advanced features as desired.
This code provides a solid foundation for a "jumping mouse" endless runner game, similar to Chrome's Dino game, and is easy to expand with more features or polish[13][12][14].