in one html file make complex racing track (800px to 800px) similar to f1 tracks with at least 12 curves where 2 ai controlled players (20px to 20px) are racing by following the track and maneveur on road 35px wide; scoreboard shows how many laps each player done and timer of the race
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Here's an HTML file that creates a racing game with two AI-controlled players racing around a complex track with at least 12 curves. The game includes a scoreboard showing the number of laps each player has completed and a timer for the race.
html
<!DOCTYPE html>
<html>
<head>
<title>AI Racing Game</title>
<style>
body {
text-align: center;
font-family: Arial, sans-serif;
}
#gameCanvas {
border: 1px solid black;
background-color: green;
}
#scoreboard {
margin-top: 10px;
}
#scoreboard p {
margin: 5px;
font-size: 18px;
}
</style>
</head>
<body>
<h1>AI Racing Game</h1>
<canvas id="gameCanvas" width="800" height="800"></canvas>
<div id="scoreboard">
<p>Time: <span id="timer">0.00</span> seconds</p>
<p>Player 1 Laps: <span id="laps1">0</span></p>
<p>Player 2 Laps: <span id="laps2">0</span></p>
</div>
<script>
// Get canvas and context
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
// Define waypoints along the track (at least 12 curves)
const waypoints = [
{x: 400, y: 100},
{x: 700, y: 100},
{x: 720, y: 200},
{x: 680, y: 300},
{x: 500, y: 300},
{x: 320, y: 300},
{x: 280, y: 400},
{x: 300, y: 500},
{x: 500, y: 500},
{x: 700, y: 500},
{x: 720, y: 600},
{x: 680, y: 700},
{x: 400, y: 700},
{x: 120, y: 700},
{x: 100, y: 600},
{x: 120, y: 500},
{x: 300, y: 500},
{x: 480, y: 500},
{x: 500, y: 400},
{x: 500, y: 300},
{x: 400, y: 100} // Loop back to start
];
// Create track path based on waypoints
const track = new Path2D();
track.moveTo(waypoints[0].x, waypoints[0].y);
for (let i = 1; i < waypoints.length; i++) {
track.lineTo(waypoints[i].x, waypoints[i].y);
}
// Optionally, close the path
// track.closePath();
// Car class
class Car {
constructor(color, x, y, startingWaypoint) {
this.x = x; // starting x position
this.y = y; // starting y position
this.angle = 0; // current angle in radians
this.speed = 2; // movement speed
this.color = color;
this.laps = 0;
this.currentWaypoint = startingWaypoint; // Start aiming for the next waypoint
}
update() {
// Get current waypoint
const target = waypoints[this.currentWaypoint];
// Calculate desired angle towards waypoint
const dx = target.x - this.x;
const dy = target.y - this.y;
const desiredAngle = Math.atan2(dy, dx);
// Adjust angle towards desired angle
let angleDifference = desiredAngle - this.angle;
// Ensure shortest rotation direction
angleDifference = Math.atan2(Math.sin(angleDifference), Math.cos(angleDifference));
// Limit the turn rate
const maxTurnRate = 0.05; // Adjust this value as needed
if (angleDifference > maxTurnRate) angleDifference = maxTurnRate;
if (angleDifference < -maxTurnRate) angleDifference = -maxTurnRate;
this.angle += angleDifference;
// Move forward
this.x += this.speed * Math.cos(this.angle);
this.y += this.speed * Math.sin(this.angle);
// Check if reached waypoint
const distanceToWaypoint = Math.hypot(target.x - this.x, target.y - this.y);
if (distanceToWaypoint < 10) {
this.currentWaypoint += 1;
if (this.currentWaypoint >= waypoints.length) {
this.currentWaypoint = 0; // Loop back to start
this.laps += 1;
}
}
}
draw() {
ctx.save();
ctx.translate(this.x, this.y);
ctx.rotate(this.angle);
ctx.fillStyle = this.color;
ctx.fillRect(-10, -10, 20, 20); // Car size 20x20
ctx.restore();
}
}
// Create two AI-controlled cars
const car1 = new Car('red', waypoints[0].x - 15, waypoints[0].y, 1);
const car2 = new Car('blue', waypoints[0].x + 15, waypoints[0].y, 1);
// Draw the track
function drawTrack() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Fill background
ctx.fillStyle = 'green';
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Draw track
ctx.lineWidth = 35; // Track width
ctx.strokeStyle = 'gray';
ctx.stroke(track);
// Draw centerline
ctx.lineWidth = 2;
ctx.strokeStyle = 'white';
ctx.stroke(track);
// Optional: Draw waypoints for debugging (comment out if not needed)
/*
for (let i = 0; i < waypoints.length; i++) {
ctx.beginPath();
ctx.arc(waypoints[i].x, waypoints[i].y, 5, 0, 2 * Math.PI);
ctx.fillStyle = 'yellow';
ctx.fill();
}
*/
}
// Animation loop
function animate() {
drawTrack();
car1.update();
car2.update();
car1.draw();
car2.draw();
// Update scoreboard
document.getElementById('laps1').innerText = car1.laps;
document.getElementById('laps2').innerText = car2.laps;
// Update timer
const elapsedTime = ((Date.now() - startTime) / 1000).toFixed(2);
document.getElementById('timer').innerText = elapsedTime;
requestAnimationFrame(animate);
}
const startTime = Date.now();
animate();
</script>
</body>
</html>
Explanation:
HTML Structure: The HTML file includes a <canvas> element where the game is rendered and a <div> for the scoreboard displaying the timer and laps for each player.
CSS Styles:
The canvas is styled with a black border and a green background to represent the grass area.
The scoreboard is styled to display the timer and laps clearly.
JavaScript Logic:
Canvas Setup: The script gets the canvas context to draw on it.
Track Creation:
A series of waypoints are defined to create a complex track with at least 12 curves. These points form the path that the cars will follow.
A Path2D object (track) is created using these waypoints to draw the track.
Car Class:
The Car class defines the properties and methods for each car.
Each car has properties like position (x, y), angle, speed, color, laps, and currentWaypoint.
The update() method adjusts the car's angle towards the next waypoint, moves the car forward, checks for waypoint completion, and increments laps when a lap is completed.
The draw() method renders the car on the canvas.
Car Instances:
Two car instances (car1 and car2) are created with different colors and starting positions.
Track Drawing Function (drawTrack):
Clears the canvas and fills the background with green to represent grass.
Draws the track by stroking the Path2D object with a specified line width to represent the track width.
Optionally, draws the waypoints (commented out in the code).
Animation Loop (animate):
Calls drawTrack() to render the track.
Updates and draws each car.
Updates the scoreboard with the current laps and elapsed time.
Uses requestAnimationFrame to continuously update the game state and render the next frame.
Game Start:
How to Use:
Copy the entire code above into a text editor.
Save the file with an .html extension (e.g., racing_game.html).
Open the file in a modern web browser (e.g., Chrome, Firefox, Edge).
The game will start automatically, displaying two AI-controlled cars racing around the track.
The scoreboard will show the number of laps completed by each car and the elapsed time.
Note:
The AI logic is simplified for demonstration purposes. Each car aims for the next waypoint and adjusts its angle to follow the track.
You can modify the speed property in the Car class or adjust the maxTurnRate to change the cars' behavior.
The waypoints and track can be customized to create different track shapes and difficulties.