Here's an HTML file that creates a complex racing track with at least 12 curves, where two AI-controlled players race around the track. The scoreboard displays the number of laps each player has completed and the race timer.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>AI Racing Game</title>
<style>
body {
font-family: Arial, sans-serif;
background: #222;
color: #fff;
text-align: center;
}
#gameCanvas {
background: #444;
display: block;
margin: 0 auto;
border: 2px solid #fff;
}
#scoreboard {
margin-top: 10px;
}
.score {
display: inline-block;
margin: 0 20px;
}
#timer {
margin-top: 10px;
}
</style>
</head>
<body>
<h1>AI Racing Game</h1>
<canvas id="gameCanvas" width="800" height="800"></canvas>
<div id="scoreboard">
<div class="score" id="player1Score">Player 1 Laps: 0</div>
<div class="score" id="player2Score">Player 2 Laps: 0</div>
</div>
<div id="timer">Time: 0.00s</div>
<script>
// Get canvas and context
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
// Game variables
const trackWidth = 35;
const carSize = 20;
let gameTime = 0;
// Scoreboard elements
const player1ScoreEl = document.getElementById('player1Score');
const player2ScoreEl = document.getElementById('player2Score');
const timerEl = document.getElementById('timer');
// Define the track as an array of waypoints
const track = [
{ x: 400, y: 50 },
{ x: 650, y: 100 },
{ x: 700, y: 200 },
{ x: 600, y: 300 },
{ x: 550, y: 400 },
{ x: 600, y: 500 },
{ x: 700, y: 600 },
{ x: 650, y: 700 },
{ x: 400, y: 750 },
{ x: 150, y: 700 },
{ x: 100, y: 600 },
{ x: 200, y: 500 },
{ x: 250, y: 400 },
{ x: 200, y: 300 },
{ x: 100, y: 200 },
{ x: 150, y: 100 }
];
// Ensure the track has at least 12 curves
// (The above waypoints form a complex track with multiple curves)
// Players
const players = [
{
x: track[0].x,
y: track[0].y,
angle: 0,
speed: 2,
waypointIndex: 0,
laps: 0,
color: 'red'
},
{
x: track[0].x + 10,
y: track[0].y + 10,
angle: 0,
speed: 2,
waypointIndex: 0,
laps: 0,
color: 'blue'
}
];
// Draw track
function drawTrack() {
ctx.lineWidth = trackWidth;
ctx.strokeStyle = '#aaa';
ctx.beginPath();
ctx.moveTo(track[0].x, track[0].y);
for (let i = 1; i < track.length; i++) {
ctx.lineTo(track[i].x, track[i].y);
}
ctx.closePath();
ctx.stroke();
}
// Game loop
function updateGame(deltaTime) {
// Update game time
gameTime += deltaTime / 1000;
timerEl.textContent = `Time: ${gameTime.toFixed(2)}s`;
// Clear canvas
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw track
drawTrack();
// Update and draw players
players.forEach((player, index) => {
// Move towards the next waypoint
const waypoint = track[player.waypointIndex];
const dx = waypoint.x - player.x;
const dy = waypoint.y - player.y;
const distance = Math.hypot(dx, dy);
if (distance < player.speed) {
// Arrived at waypoint
player.waypointIndex = (player.waypointIndex + 1) % track.length;
// Check for lap completion
if (player.waypointIndex === 0) {
player.laps += 1;
// Update scoreboard
if (index === 0) {
player1ScoreEl.textContent = `Player 1 Laps: ${player.laps}`;
} else if (index === 1) {
player2ScoreEl.textContent = `Player 2 Laps: ${player.laps}`;
}
}
} else {
// Move towards waypoint
player.x += (dx / distance) * player.speed;
player.y += (dy / distance) * player.speed;
// Calculate angle for rotation
player.angle = Math.atan2(dy, dx);
}
// Draw player
ctx.save();
ctx.translate(player.x, player.y);
ctx.rotate(player.angle);
ctx.fillStyle = player.color;
ctx.fillRect(-carSize / 2, -carSize / 2, carSize, carSize);
ctx.restore();
});
// Request next frame
requestAnimationFrame((timestamp) => {
updateGame(timestamp - lastFrameTime);
lastFrameTime = timestamp;
});
}
// Start game
let lastFrameTime = performance.now();
requestAnimationFrame((timestamp) => {
updateGame(0);
lastFrameTime = timestamp;
});
</script>
</body>
</html>
Explanation:
-
HTML Structure:
- A
<canvas> element with id="gameCanvas" is used for drawing the game.
- A scoreboard is included below the canvas to display laps for each player.
- A timer displays the elapsed time of the race.
-
Styles:
- Basic styles are applied to center the canvas and display the scoreboard and timer.
- The background is set to dark to represent a racetrack environment.
-
JavaScript Logic:
- The game uses the Canvas API to draw the track and the cars.
- The
track array defines multiple waypoints, forming a complex track with at least 12 curves.
- Each player is represented by an object with properties like position (
x, y), speed, waypoint index, laps completed, and color.
- The
updateGame function is called on every frame to:
- Update the game timer.
- Clear and redraw the track.
- Update each player's position based on their movement toward the next waypoint.
- Check if the player has reached a waypoint and update the lap count if they've completed a lap.
- Draw each player's car on the canvas.
- The
requestAnimationFrame method is used for smooth animation.
How the AI Control Works:
- Each car moves toward the next waypoint in the
track array.
- When it reaches a waypoint (within a certain distance), it sets the next waypoint as its target.
- When the waypoint index resets to 0, it means the car has completed a lap.
Adjustments and Extensions:
- You can adjust the
speed property of each player to make the cars move faster or slower.
- The
track array can be modified to change the shape of the track. Ensure it always forms a closed loop.
Running the Game:
- Simply save the code in a file with an
.html extension (e.g., racing_game.html) and open it in a modern web browser.
- The game will start automatically, and you'll see the two AI-controlled cars racing around the track.
- The scoreboard will keep track of the number of laps each car completes, and the timer will show the elapsed race time.
Feel free to customize the track waypoints, car properties, and styles to enhance the game!