Creating a 3D racing car game in a single HTML file is a bit more complex than a 2D game, but it's definitely possible using WebGL and a library like Three.js. Below is a simple example of a 3D racing car game using Three.js. This example will include basic car movement, a simple track, and a basic game loop.
To keep it in one file, we'll include the Three.js library directly in the HTML file.
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>3D Racing Car Game</title>
<style>
body {
margin: 0;
overflow: hidden;
}
canvas {
display: block;
}
</style>
</head>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script>
// Scene setup
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
// Lighting
const ambientLight = new THREE.AmbientLight(0x404040);
scene.add(ambientLight);
const directionalLight = new THREE.DirectionalLight(0xffffff, 0.5);
directionalLight.position.set(10, 10, 10);
scene.add(directionalLight);
// Ground
const groundGeometry = new THREE.PlaneGeometry(100, 100);
const groundMaterial = new THREE.MeshStandardMaterial({ color: 0x228B22 });
const ground = new THREE.Mesh(groundGeometry, groundMaterial);
ground.rotation.x = -Math.PI / 2;
scene.add(ground);
// Car
const carGeometry = new THREE.BoxGeometry(2, 1, 4);
const carMaterial = new THREE.MeshStandardMaterial({ color: 0xff0000 });
const car = new THREE.Mesh(carGeometry, carMaterial);
car.position.set(0, 0.5, 0);
scene.add(car);
// Camera setup
camera.position.set(0, 5, 10);
camera.lookAt(car.position);
// Controls
const keys = {};
document.addEventListener('keydown', (event) => {
keys[event.code] = true;
});
document.addEventListener('keyup', (event) => {
keys[event.code] = false;
});
// Game loop
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const delta = clock.getDelta();
if (keys['ArrowUp']) {
car.position.z -= 5 * delta;
}
if (keys['ArrowDown']) {
car.position.z += 5 * delta;
}
if (keys['ArrowLeft']) {
car.position.x -= 5 * delta;
}
if (keys['ArrowRight']) {
car.position.x += 5 * delta;
}
camera.position.z = car.position.z + 10;
camera.position.y = 5;
camera.lookAt(car.position);
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
Explanation:
Scene Setup: We create a basic Three.js scene with a camera and renderer.
Lighting: We add ambient and directional lighting to illuminate the scene.
Ground: We create a simple ground plane.
Car: We create a simple car model using a box geometry.
Camera Setup: We position the camera to follow the car.
Controls: We handle keyboard inputs to move the car.
Game Loop: We use a game loop to update the car's position and render the scene.
How to Play:
Save the code as an HTML file (e.g., 3d-racing-game.html).
Open it in a web browser.
Use the arrow keys to move the car around the scene.
This example is quite basic and can be expanded with more features such as a proper track, obstacles, scoring, and more advanced car models. For a more complex game, you might want to consider using additional libraries or frameworks like Phaser with Three.js integration.