racing car game in html
- User
racing car game in html
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
racing car game in html
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
I'll create a simple racing car game for you using HTML, CSS, and JavaScript. Here's a complete working game:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Racing Car Game</title>
<style>
body {
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
background-color: #333;
font-family: Arial, sans-serif;
}
#gameContainer {
position: relative;
width: 400px;
height: 600px;
background-color: #555;
overflow: hidden;
border: 3px solid #000;
}
#road {
position: absolute;
width: 100%;
height: 100%;
background-color: #333;
}
.roadLine {
position: absolute;
width: 10px;
height: 100px;
background-color: white;
left: 50%;
transform: translateX(-50%);
}
#playerCar {
position: absolute;
width: 50px;
height: 80px;
background-color: #ff0000;
bottom: 20px;
left: 175px;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0,0,0,0.5);
}
#playerCar::before {
content: '';
position: absolute;
width: 40px;
height: 20px;
background-color: #cc0000;
top: 15px;
left: 5px;
border-radius: 5px;
}
.enemyCar {
position: absolute;
width: 50px;
height: 80px;
background-color: #0066cc;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0,0,0,0.5);
}
.enemyCar::before {
content: '';
position: absolute;
width: 40px;
height: 20px;
background-color: #0052a3;
top: 15px;
left: 5px;
border-radius: 5px;
}
#score {
position: absolute;
top: 10px;
left: 10px;
color: white;
font-size: 24px;
font-weight: bold;
z-index: 100;
}
#gameOver {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
color: white;
font-size: 36px;
font-weight: bold;
display: none;
text-align: center;
z-index: 100;
}
#startScreen {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0,0,0,0.8);
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
color: white;
z-index: 200;
}
#startButton {
padding: 15px 30px;
font-size: 24px;
background-color: #ff0000;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
margin-top: 20px;
}
#startButton:hover {
background-color: #cc0000;
}
</style>
</head>
<body>
<div id="gameContainer">
<div id="road"></div>
<div id="playerCar"></div>
<div id="score">Score: 0</div>
<div id="gameOver">
Game Over!<br>
<span style="font-size: 24px;">Press Space to Restart</span>
</div>
<div id="startScreen">
<h1>Racing Car Game</h1>
<p>Use Arrow Keys to Move</p>
<p>Avoid the Blue Cars!</p>
<button id="startButton">Start Game</button>
</div>
</div>
<script>
const gameContainer = document.getElementById('gameContainer');
const playerCar = document.getElementById('playerCar');
const scoreElement = document.getElementById('score');
const gameOverElement = document.getElementById('gameOver');
const startScreen = document.getElementById('startScreen');
const startButton = document.getElementById('startButton');
const road = document.getElementById('road');
let playerX = 175;
let score = 0;
let gameRunning = false;
let enemyCars = [];
let roadLines = [];
let speed = 5;
// Create road lines
for (let i = 0; i < 6; i++) {
const line = document.createElement('div');
line.className = 'roadLine';
line.style.top = i * 120 + 'px';
road.appendChild(line);
roadLines.push(line);
}
// Player movement
document.addEventListener('keydown', (e) => {
if (!gameRunning) {
if (e.key === ' ' && gameOverElement.style.display === 'block') {
resetGame();
}
return;
}
if (e.key === 'ArrowLeft' && playerX > 0) {
playerX -= 20;
playerCar.style.left = playerX + 'px';
} else if (e.key === 'ArrowRight' && playerX < 350) {
playerX += 20;
playerCar.style.left = playerX + 'px';
}
});
function createEnemyCar() {
const enemy = document.createElement('div');
enemy.className = 'enemyCar';
enemy.style.left = Math.floor(Math.random() * 8) * 50 + 'px';
enemy.style.top = '-100px';
gameContainer.appendChild(enemy);
enemyCars.push(enemy);
}
function moveRoadLines() {
roadLines.forEach(line => {
let topPos = parseInt(line.style.top);
topPos += speed;
if (topPos > 600) {
topPos = -100;
}
line.style.top = topPos + 'px';
});
}
function gameLoop() {
if (!gameRunning) return;
// Move road lines
moveRoadLines();
// Move enemy cars
enemyCars.forEach((enemy, index) => {
let topPos = parseInt(enemy.style.top);
topPos += speed;
enemy.style.top = topPos + 'px';
// Remove cars that are off screen
if (topPos > 650) {
enemy.remove();
enemyCars.splice(index, 1);
score += 10;
scoreElement.textContent = 'Score: ' + score;
}
// Check collision
const enemyRect = enemy.getBoundingClientRect();
const playerRect = playerCar.getBoundingClientRect();
if (enemyRect.left < playerRect.right &&
enemyRect.right > playerRect.left &&
enemyRect.top < playerRect.bottom &&
enemyRect.bottom > playerRect.top) {
gameOver();
}
});
// Create new enemy cars
if (Math.random() < 0.02) {
createEnemyCar();
}
// Increase difficulty
if (score > 0 && score % 100 === 0) {
speed += 0.5;
}
requestAnimationFrame(gameLoop);
}
function gameOver() {
gameRunning = false;
gameOverElement.style.display = 'block';
}
function resetGame() {
score = 0;
speed = 5;
playerX = 175;
playerCar.style.left = playerX + 'px';
scoreElement.textContent = 'Score: 0';
gameOverElement.style.display = 'none';
// Remove all enemy cars
enemyCars.forEach(enemy => enemy.remove());
enemyCars = [];
gameRunning = true;
gameLoop();
}
startButton.addEventListener('click', () => {
startScreen.style.display = 'none';
gameRunning = true;
gameLoop();
});
</script>
</body>
</html>
This racing car game features:
Gameplay:
Features:
To play the game:
racing-game.html)The game is designed to be simple but challenging, with a retro arcade feel. You can customize it further by adding features like power-ups, different car types, sound effects, or multiple lanes.