gpt-4.1-mini-2025-04-14· OpenAI
WildChat-4.8M (AI2)
write a game in HTML where you jump over blocks
- User
write a game in HTML where you jump over blocks
- Assistant
write a game in HTML where you jump over blocks
Sure! Below is a simple HTML game where you control a square that jumps over incoming blocks. The game is implemented using HTML, CSS, and JavaScript. You press the spacebar or click to make the player jump. If you collide with a block, the game is over and you can restart.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Jump Over Blocks Game</title>
<style>
body {
margin: 0;
background: #222;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
user-select: none;
overflow: hidden;
font-family: Arial, sans-serif;
color: white;
}
#game {
position: relative;
width: 600px;
height: 200px;
background: #444;
overflow: hidden;
border: 3px solid #fff;
border-radius: 8px;
}
#player {
position: absolute;
bottom: 0;
left: 50px;
width: 40px;
height: 40px;
background: #1abc9c;
border-radius: 6px;
}
#block {
position: absolute;
bottom: 0;
width: 30px;
height: 50px;
background: #e74c3c;
right: 0;
border-radius: 4px;
}
#score {
position: absolute;
top: 10px;
left: 10px;
font-size: 18px;
}
#gameOverScreen {
position: absolute;
top: 0; left: 0;
width: 100%;
height: 100%;
background: rgba(0,0,0,0.85);
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
font-size: 24px;
display: none;
z-index: 10;
}
#gameOverScreen button {
margin-top: 20px;
padding: 10px 20px;
font-size: 18px;
background: #1abc9c;
border: none;
border-radius: 5px;
cursor: pointer;
color: #222;
font-weight: bold;
}
#gameOverScreen button:hover {
background: #16a085;
}
</style>
</head>
<body>
<div id="game">
<div id="player"></div>
<div id="block"></div>
<div id="score">Score: 0</div>
<div id="gameOverScreen">
<div>Game Over!</div>
<div>Your score: <span id="finalScore">0</span></div>
<button id="restartBtn">Restart</button>
</div>
</div>
<script>
const player = document.getElementById('player');
const block = document.getElementById('block');
const scoreEl = document.getElementById('score');
const gameOverScreen = document.getElementById('gameOverScreen');
const finalScoreEl = document.getElementById('finalScore');
const restartBtn = document.getElementById('restartBtn');
const game = document.getElementById('game');
const gameWidth = 600;
const groundLevel = 0; // px from bottom for player's position
let playerY = 0; // player's vertical position (0 = bottom)
let playerVelocity = 0;
const gravity = 0.6;
const jumpStrength = 12;
let isJumping = false;
let blockX = gameWidth; // start block at right edge
const blockSpeed = 6;
let score = 0;
let isGameOver = false;
// Update player's vertical position
function updatePlayer() {
if (isJumping || playerY > 0) {
playerVelocity -= gravity;
playerY += playerVelocity;
if (playerY <= 0) {
playerY = 0;
playerVelocity = 0;
isJumping = false;
}
}
player.style.bottom = `${playerY}px`;
}
// Update the block position, and reset when off screen
function updateBlock() {
blockX -= blockSpeed;
if (blockX < -block.offsetWidth) {
blockX = gameWidth;
score++;
scoreEl.textContent = `Score: ${score}`;
}
block.style.right = `${gameWidth - blockX}px`;
}
// Check collision between player and block
function checkCollision() {
const playerRect = player.getBoundingClientRect();
const blockRect = block.getBoundingClientRect();
// In the game container coordinate system:
// Adjust for container position
const gameRect = game.getBoundingClientRect();
// Convert player's rect relative to game
const playerLeft = playerRect.left - gameRect.left;
const playerRight = playerRect.right - gameRect.left;
const playerBottom = playerY; // relative to bottom of container (same as player.style.bottom)
const playerTop = playerBottom + player.offsetHeight;
// Block position relative to right of container:
// We used block.style.right, so blockX is relative to left.
const blockLeft = blockX;
const blockRight = blockX + block.offsetWidth;
const blockBottom = 0;
const blockTop = block.offsetHeight;
// Check horizontal overlap
const horizontalOverlap = (playerRight > blockLeft) && (playerLeft < blockRight);
// Check vertical overlap
const verticalOverlap = (playerBottom < blockTop);
if (horizontalOverlap && verticalOverlap) {
return true;
}
return false;
}
// Handle jump
function jump() {
if (!isJumping && !isGameOver) {
isJumping = true;
playerVelocity = jumpStrength;
}
}
function gameLoop() {
if (isGameOver) return;
updatePlayer();
updateBlock();
if (checkCollision()) {
endGame();
return;
}
requestAnimationFrame(gameLoop);
}
function startGame() {
// Reset state
playerY = 0;
playerVelocity = 0;
isJumping = false;
blockX = gameWidth;
score = 0;
scoreEl.textContent = 'Score: 0';
isGameOver = false;
gameOverScreen.style.display = 'none';
// Start animation
requestAnimationFrame(gameLoop);
}
function endGame() {
isGameOver = true;
finalScoreEl.textContent = score;
gameOverScreen.style.display = 'flex';
}
// Event listeners
window.addEventListener('keydown', e => {
if (e.code === 'Space') {
e.preventDefault();
jump();
}
});
game.addEventListener('click', () => {
jump();
});
restartBtn.addEventListener('click', () => {
startGame();
});
// Start the game automatically
startGame();
</script>
</body>
</html>
Feel free to copy-paste this into an .html file and open it in a modern browser. Let me know if you want me to add features or polish it further!
Not yet included in a published release.