Here's a simple "Catch the Ball" game in HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Catch the Ball Game</title>
<style>
body {
margin: 0;
padding: 20px;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
background-color: #222;
font-family: Arial, sans-serif;
}
#gameContainer {
text-align: center;
}
#gameCanvas {
border: 2px solid #fff;
background-color: #111;
display: block;
margin: 20px auto;
}
#score {
color: #fff;
font-size: 24px;
margin-bottom: 10px;
}
#gameOver {
color: #ff6b6b;
font-size: 32px;
margin-top: 20px;
display: none;
}
button {
font-size: 18px;
padding: 10px 20px;
margin-top: 20px;
cursor: pointer;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 5px;
}
button:hover {
background-color: #45a049;
}
#instructions {
color: #ccc;
margin-top: 10px;
}
</style>
</head>
<body>
<div id="gameContainer">
<div id="score">Score: 0</div>
<canvas id="gameCanvas" width="400" height="500"></canvas>
<div id="gameOver">Game Over!</div>
<button onclick="startGame()">Start Game</button>
<div id="instructions">Use arrow keys or mouse to move the paddle</div>
</div>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const scoreElement = document.getElementById('score');
const gameOverElement = document.getElementById('gameOver');
let score = 0;
let gameRunning = false;
let balls = [];
let paddle = {
x: canvas.width / 2 - 40,
y: canvas.height - 30,
width: 80,
height: 10,
speed: 8
};
let keys = {};
// Ball class
class Ball {
constructor() {
this.x = Math.random() * (canvas.width - 20) + 10;
this.y = 0;
this.radius = 10;
this.speed = 2 + Math.min(score * 0.1, 5);
this.color = `hsl(${Math.random() * 360}, 70%, 50%)`;
}
update() {
this.y += this.speed;
}
draw() {
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2);
ctx.fillStyle = this.color;
ctx.fill();
ctx.closePath();
}
isOffScreen() {
return this.y > canvas.height + this.radius;
}
collidesWith(paddle) {
return this.x > paddle.x &&
this.x < paddle.x + paddle.width &&
this.y + this.radius > paddle.y &&
this.y - this.radius < paddle.y + paddle.height;
}
}
// Event listeners
document.addEventListener('keydown', (e) => {
keys[e.key] = true;
});
document.addEventListener('keyup', (e) => {
keys[e.key] = false;
});
canvas.addEventListener('mousemove', (e) => {
if (gameRunning) {
const rect = canvas.getBoundingClientRect();
paddle.x = e.clientX - rect.left - paddle.width / 2;
paddle.x = Math.max(0, Math.min(canvas.width - paddle.width, paddle.x));
}
});
function startGame() {
score = 0;
balls = [];
paddle.x = canvas.width / 2 - 40;
gameRunning = true;
gameOverElement.style.display = 'none';
updateScore();
gameLoop();
}
function updateScore() {
scoreElement.textContent = `Score: ${score}`;
}
function spawnBall() {
if (Math.random() < 0.02 + Math.min(score * 0.001, 0.05)) {
balls.push(new Ball());
}
}
function updatePaddle() {
if (keys['ArrowLeft'] && paddle.x > 0) {
paddle.x -= paddle.speed;
}
if (keys['ArrowRight'] && paddle.x < canvas.width - paddle.width) {
paddle.x += paddle.speed;
}
}
function drawPaddle() {
ctx.fillStyle = '#fff';
ctx.fillRect(paddle.x, paddle.y, paddle.width, paddle.height);
}
function gameLoop() {
if (!gameRunning) return;
// Clear canvas
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Update and draw paddle
updatePaddle();
drawPaddle();
// Spawn new balls
spawnBall();
// Update and draw balls
for (let i = balls.length - 1; i >= 0; i--) {
const ball = balls[i];
ball.update();
ball.draw();
// Check collision with paddle
if (ball.collidesWith(paddle)) {
score++;
updateScore();
balls.splice(i, 1);
}
// Check if ball is off screen
else if (ball.isOffScreen()) {
balls.splice(i, 1);
gameOver();
}
}
requestAnimationFrame(gameLoop);
}
function gameOver() {
gameRunning = false;
gameOverElement.style.display = 'block';
}
// Initial draw
drawPaddle();
</script>
</body>
</html>
This game features:
- Gameplay: Colored balls fall from the top of the screen and you need to catch them with a white paddle
- Controls: Use arrow keys or mouse to move the paddle left and right
- Scoring: You get 1 point for each ball caught
- Difficulty: Balls fall faster as your score increases
- Game Over: The game ends when you miss a ball
To play:
- Save the code as an HTML file (e.g.,
catch-ball-game.html)
- Open it in a web browser
- Click "Start Game" to begin
- Move the paddle to catch the falling balls
The game demonstrates basic game development concepts like game loops, collision detection, user input handling, and progressive difficulty.