<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Catch the Stars!</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Arial', sans-serif;
background: linear-gradient(to bottom, #0f0c29, #302b63, #24243e);
height: 100vh;
overflow: hidden;
display: flex;
justify-content: center;
align-items: center;
}
#gameContainer {
position: relative;
width: 400px;
height: 600px;
background: rgba(0, 0, 0, 0.3);
border: 3px solid #fff;
border-radius: 10px;
overflow: hidden;
}
#player {
position: absolute;
bottom: 20px;
width: 60px;
height: 60px;
background: #ffd700;
border-radius: 50%;
transition: left 0.1s;
box-shadow: 0 0 20px #ffd700;
}
.star {
position: absolute;
width: 30px;
height: 30px;
background: #fff;
clip-path: polygon(50% 0%, 61% 35%, 98% 35%, 68% 57%, 79% 91%, 50% 70%, 21% 91%, 32% 57%, 2% 35%, 39% 35%);
box-shadow: 0 0 10px #fff;
}
#score {
position: absolute;
top: 20px;
left: 20px;
color: #fff;
font-size: 24px;
font-weight: bold;
z-index: 10;
}
#gameOver {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background: rgba(0, 0, 0, 0.9);
color: #fff;
padding: 30px;
border-radius: 10px;
text-align: center;
display: none;
z-index: 20;
}
#gameOver h2 {
margin-bottom: 20px;
color: #ffd700;
}
#restartBtn {
background: #ffd700;
border: none;
padding: 10px 30px;
font-size: 18px;
border-radius: 5px;
cursor: pointer;
margin-top: 15px;
}
#restartBtn:hover {
background: #ffed4e;
}
#instructions {
position: absolute;
bottom: 20px;
left: 50%;
transform: translateX(-50%);
color: #fff;
font-size: 14px;
text-align: center;
}
</style>
</head>
<body>
<div id="gameContainer">
<div id="score">Score: 0</div>
<div id="player"></div>
<div id="gameOver">
<h2>Game Over!</h2>
<p>Your Score: <span id="finalScore">0</span></p>
<button id="restartBtn">Play Again</button>
</div>
<div id="instructions">
Use ← → Arrow Keys or Mouse
</div>
</div>
<script>
const gameContainer = document.getElementById('gameContainer');
const player = document.getElementById('player');
const scoreDisplay = document.getElementById('score');
const gameOverScreen = document.getElementById('gameOver');
const finalScoreDisplay = document.getElementById('finalScore');
const restartBtn = document.getElementById('restartBtn');
let score = 0;
let gameActive = true;
let playerX = 170;
let stars = [];
let gameSpeed = 2;
// Player movement with keyboard
document.addEventListener('keydown', (e) => {
if (!gameActive) return;
if (e.key === 'ArrowLeft' && playerX > 0) {
playerX -= 20;
} else if (e.key === 'ArrowRight' && playerX < 340) {
playerX += 20;
}
player.style.left = playerX + 'px';
});
// Player movement with mouse
gameContainer.addEventListener('mousemove', (e) => {
if (!gameActive) return;
const rect = gameContainer.getBoundingClientRect();
playerX = e.clientX - rect.left - 30;
playerX = Math.max(0, Math.min(340, playerX));
player.style.left = playerX + 'px';
});
// Create falling stars
function createStar() {
if (!gameActive) return;
const star = document.createElement('div');
star.className = 'star';
star.style.left = Math.random() * 370 + 'px';
star.style.top = '-30px';
gameContainer.appendChild(star);
stars.push({
element: star,
y: -30,
x: parseFloat(star.style.left)
});
}
// Game loop
function gameLoop() {
if (!gameActive) return;
// Move stars down
stars.forEach((star, index) => {
star.y += gameSpeed;
star.element.style.top = star.y + 'px';
// Check collision with player
if (star.y > 520 && star.y < 580) {
if (star.x > playerX - 20 && star.x < playerX + 60) {
// Caught the star!
score += 10;
scoreDisplay.textContent = 'Score: ' + score;
star.element.remove();
stars.splice(index, 1);
// Increase difficulty
if (score % 50 === 0) {
gameSpeed += 0.5;
}
}
}
// Remove stars that fell off screen
if (star.y > 600) {
star.element.remove();
stars.splice(index, 1);
// Game over if missed a star
endGame();
}
});
requestAnimationFrame(gameLoop);
}
// End game
function endGame() {
gameActive = false;
finalScoreDisplay.textContent = score;
gameOverScreen.style.display = 'block';
document.getElementById('instructions').style.display = 'none';
}
// Restart game
restartBtn.addEventListener('click', () => {
score = 0;
gameSpeed = 2;
scoreDisplay.textContent = 'Score: 0';
gameOverScreen.style.display = 'none';
document.getElementById('instructions').style.display = 'block';
// Remove all stars
stars.forEach(star => star.element.remove());
stars = [];
gameActive = true;
gameLoop();
});
// Start creating stars
setInterval(() => {
if (gameActive) createStar();
}, 1000);
// Start game
player.style.left = playerX + 'px';
gameLoop();
</script>
</body>
</html>