gpt-4.1-mini-2025-04-14· OpenAI
WildChat-4.8M (AI2)
Code something in html a game for fun in One file
- User
Code something in html a game for fun in One file
- Assistant
Code something in html a game for fun in One file
Sure! Here's a simple and fun game coded entirely in one HTML file. It's a classic "Catch the Ball" game where a ball moves around the screen and you have to click on it to score points. The ball speeds up as you score more.
Just save this as catch-the-ball.html and open it in your browser to play.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Catch the Ball Game</title>
<style>
body {
margin: 0;
overflow: hidden;
background: linear-gradient(135deg, #89f7fe 0%, #66a6ff 100%);
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
user-select: none;
}
#game {
position: relative;
width: 100vw;
height: 100vh;
}
#ball {
position: absolute;
width: 60px;
height: 60px;
background: radial-gradient(circle at 30% 30%, #ff4b1f, #ff9068);
border-radius: 50%;
cursor: pointer;
box-shadow: 0 0 15px #ff4b1f;
transition: box-shadow 0.2s;
}
#ball:active {
box-shadow: 0 0 30px #ff9068;
}
#scoreboard {
position: fixed;
top: 15px;
left: 50%;
transform: translateX(-50%);
font-size: 24px;
color: #fff;
text-shadow: 1px 1px 3px #000;
user-select: none;
}
#message {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
font-size: 36px;
color: #fff;
text-shadow: 2px 2px 5px #000;
display: none;
}
#restart {
margin-top: 20px;
padding: 10px 20px;
font-size: 20px;
background: #ff4b1f;
border: none;
border-radius: 10px;
color: white;
cursor: pointer;
box-shadow: 0 0 10px #ff4b1f;
transition: background 0.3s;
}
#restart:hover {
background: #ff9068;
}
</style>
</head>
<body>
<div id="game">
<div id="ball"></div>
<div id="scoreboard">Score: 0</div>
<div id="message">
Game Over!<br />
<button id="restart">Restart</button>
</div>
</div>
<script>
(() => {
const ball = document.getElementById('ball');
const scoreboard = document.getElementById('scoreboard');
const message = document.getElementById('message');
const restartBtn = document.getElementById('restart');
const game = document.getElementById('game');
let score = 0;
let speed = 3;
let directionX = 1;
let directionY = 1;
let posX = 100;
let posY = 100;
const ballSize = 60;
let animationFrameId;
let gameRunning = true;
// Get viewport size
function getBounds() {
return {
width: window.innerWidth,
height: window.innerHeight,
};
}
function updateScore() {
scoreboard.textContent = `Score: ${score}`;
}
function resetGame() {
score = 0;
speed = 3;
directionX = 1;
directionY = 1;
posX = 100;
posY = 100;
updateScore();
message.style.display = 'none';
ball.style.display = 'block';
gameRunning = true;
animate();
}
function gameOver() {
gameRunning = false;
message.style.display = 'block';
ball.style.display = 'none';
cancelAnimationFrame(animationFrameId);
}
ball.addEventListener('click', () => {
if (!gameRunning) return;
score++;
speed += 0.3; // Increase speed
updateScore();
// Jump ball to a new random position instantly on click
const bounds = getBounds();
posX = Math.random() * (bounds.width - ballSize);
posY = Math.random() * (bounds.height - ballSize);
ball.style.left = `${posX}px`;
ball.style.top = `${posY}px`;
});
// If player misses clicking the ball for 10 seconds, game over
let lastClickTime = Date.now();
function checkTimeout() {
if (!gameRunning) return;
if (Date.now() - lastClickTime > 10000) {
gameOver();
} else {
setTimeout(checkTimeout, 1000);
}
}
checkTimeout();
// Update last click time on ball click
ball.addEventListener('click', () => {
lastClickTime = Date.now();
});
function animate() {
if (!gameRunning) return;
const bounds = getBounds();
posX += directionX * speed;
posY += directionY * speed;
// Bounce off edges
if (posX <= 0) {
directionX = 1;
posX = 0;
} else if (posX + ballSize >= bounds.width) {
directionX = -1;
posX = bounds.width - ballSize;
}
if (posY <= 0) {
directionY = 1;
posY = 0;
} else if (posY + ballSize >= bounds.height) {
directionY = -1;
posY = bounds.height - ballSize;
}
ball.style.left = `${posX}px`;
ball.style.top = `${posY}px`;
animationFrameId = requestAnimationFrame(animate);
}
restartBtn.addEventListener('click', resetGame);
// Start the game
resetGame();
// Resize handler to keep ball in bounds
window.addEventListener('resize', () => {
const bounds = getBounds();
if (posX + ballSize > bounds.width) posX = bounds.width - ballSize;
if (posY + ballSize > bounds.height) posY = bounds.height - ballSize;
ball.style.left = `${posX}px`;
ball.style.top = `${posY}px`;
});
})();
</script>
</body>
</html>
Enjoy! Let me know if you'd like a different kind of game or more features.
Not yet included in a published release.