Zoomcar2.0
Zoomcar2.0
Zoomcar2.0
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Racing Game</title>
<style>
body {
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #f5f5f5;
}
canvas {
border: 2px solid black;
}
#game-over {
position: absolute;
font-size: 48px;
color: red;
display: none;
}
#score {
position: absolute;
font-size: 24px;
color: black;
top: 10px;
left: 10px;
}
</style>
</head>
<body>
<canvas id="gameCanvas" width="800" height="600"></canvas>
<div id="game-over">Game Over!</div>
<div id="score">Score: 0</div>
<script>
// Setup game canvas
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
// Create an obstacle
function createObstacle() {
const x = Math.random() * (canvas.width - 50);
const y = -100;
obstacles.push({ x, y });
}
// Draw obstacles
function drawObstacles() {
ctx.fillStyle = 'green';
obstacles.forEach((obstacle) => {
ctx.fillRect(obstacle.x, obstacle.y, 50, 100);
});
}
// Move obstacles
function moveObstacles() {
obstacles.forEach((obstacle, index) => {
obstacle.y += obstacleSpeed;
if (obstacle.y > canvas.height) {
obstacles.splice(index, 1);
score++;
if (score % 5 === 0) { // Increase speed every 5 obstacles
avoided
obstacleSpeed += 0.5;
}
if (score % 3 === 0) { // Add a new obstacle every 3 score
points
createObstacle();
}
}
});
}
// Move obstacles
moveObstacles();
requestAnimationFrame(update);
}