Phaser 3 games: a scene with physics and particles, from the official tutorial.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Zaman 3D Racing</title>
<style>
* { box-sizing: border-box; }
body {
margin: 0;
overflow: hidden;
background: #101827;
font-family: Arial, sans-serif;
color: white;
}
#game { width: 100vw; height: 100vh; display: block; }
#hud {
position: absolute;
top: 15px;
left: 15px;
right: 15px;
display: flex;
justify-content: space-between;
pointer-events: none;
font-size: 18px;
font-weight: bold;
text-shadow: 0 2px 5px black;
}
#menu {
position: absolute;
inset: 0;
background: #07101dcc;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
text-align: center;
padding: 20px;
}
h1 { color: #ffd43b; font-size: clamp(32px, 7vw, 55px); }
button {
border: 0;
border-radius: 12px;
padding: 14px 24px;
margin: 6px;
font-size: 18px;
font-weight: bold;
background: #ffd43b;
color: #151515;
cursor: pointer;
touch-action: none;
}
#controls {
position: absolute;
bottom: 25px;
left: 0;
right: 0;
display: flex;
justify-content: space-between;
padding: 0 25px;
pointer-events: none;
}
#controls button {
pointer-events: auto;
width: 75px;
height: 65px;
font-size: 27px;
background: #ffffffbb;
}
#message { max-width: 500px; line-height: 1.6; }
</style>
</head>
<body>
<div id="hud">
<div>🏁 ZAMAN RACING</div>
<div>Score: <span id="score">0</span></div>
<div>Speed: <span id="speed">0</span></div>
</div>
<div id="menu">
<h1>🏎️ ZAMAN 3D RACING</h1>
<p id="message">
Avoid traffic cars and drive as far as possible!
<br>Use arrow keys or mobile controls.
</p>
<button id="start">START RACE</button>
</div>
<div id="controls">
<button id="left">⬅</button>
<button id="right">➡</button>
</div>
<script type="module">
import * as THREE from
'https://cdn.jsdelivr.net/npm/three@0.170.0/build/three.module.js';
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x87ceeb);
scene.fog = new THREE.Fog(0x87ceeb, 35, 180);
const camera = new THREE.PerspectiveCamera(
65, innerWidth / innerHeight, 0.1, 300
);
camera.position.set(0, 7, 12);
camera.lookAt(0, 0, -10);
const renderer = new THREE.WebGLRenderer({
antialias: true
});
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
document.body.appendChild(renderer.domElement);
renderer.domElement.id = "game";
// LIGHTS
scene.add(new THREE.HemisphereLight(0xffffff, 0x48613b, 2));
const sun = new THREE.DirectionalLight(0xffffff, 2.5);
sun.position.set(-5, 12, 8);
sun.castShadow = true;
scene.add(sun);
// MATERIALS
const roadMat = new THREE.MeshStandardMaterial({
color: 0x292d35, roughness: 0.9
});
const grassMat = new THREE.MeshStandardMaterial({
color: 0x23833c
});
const whiteMat = new THREE.MeshStandardMaterial({
color: 0xffffff
});
// GROUND AND ROAD
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(250, 300), grassMat
);
ground.rotation.x = -Math.PI / 2;
ground.position.set(0, -0.12, -70);
scene.add(ground);
const road = new THREE.Mesh(
new THREE.PlaneGeometry(11, 250), roadMat
);
road.rotation.x = -Math.PI / 2;
road.position.set(0, -0.05, -70);
scene.add(road);
// ROAD EDGES
for (const x of [-5.4, 5.4]) {
const edge = new THREE.Mesh(
new THREE.BoxGeometry(0.18, 0.08, 250),
whiteMat
);
edge.position.set(x, 0, -70);
scene.add(edge);
}
// LANE MARKINGS
const laneMarks = [];
for (let z = -180; z < 15; z += 12) {
for (const x of [-1.83, 1.83]) {
const mark = new THREE.Mesh(
new THREE.BoxGeometry(0.12, 0.035, 5),
whiteMat
);
mark.position.set(x, 0.025, z);
scene.add(mark);
laneMarks.push(mark);
}
}
// CAR BUILDER
function makeCar(color) {
const car = new THREE.Group();
const bodyMat = new THREE.MeshStandardMaterial({
color, metalness: 0.3, roughness: 0.35
});
const glassMat = new THREE.MeshStandardMaterial({
color: 0x162b42, metalness: 0.5, roughness: 0.2
});
const black = new THREE.MeshStandardMaterial({
color: 0x101010
});
const body = new THREE.Mesh(
new THREE.BoxGeometry(1.7, 0.65, 3),
bodyMat
);
body.position.y = 0.65;
body.castShadow = true;
car.add(body);
const cabin = new THREE.Mesh(
new THREE.BoxGeometry(1.3, 0.7, 1.5),
glassMat
);
cabin.position.set(0, 1.25, -0.1);
cabin.castShadow = true;
car.add(cabin);
// Wheels
for (const x of [-0.88, 0.88]) {
for (const z of [-0.95, 0.95]) {
const wheel = new THREE.Mesh(
new THREE.CylinderGeometry(0.35, 0.35, 0.22, 16),
black
);
wheel.rotation.z = Math.PI / 2;
wheel.position.set(x, 0.38, z);
car.add(wheel);
}
}
// Front lights
const lightMat = new THREE.MeshBasicMaterial({
color: 0xffffcc
});
for (const x of [-0.55, 0.55]) {
const light = new THREE.Mesh(
new THREE.BoxGeometry(0.3, 0.16, 0.08),
lightMat
);
light.position.set(x, 0.72, -1.52);
car.add(light);
}
return car;
}
// PLAYER CAR
const player = makeCar(0xff2020);
player.position.set(0, 0, 4);
scene.add(player);
// TRAFFIC CARS
const traffic = [];
const colors = [0x1769ff, 0xffd000, 0xffffff, 0x16bb75];
function spawnTraffic(car, initial = false) {
const lanes = [-3.5, 0, 3.5];
car.position.x = lanes[Math.floor(Math.random() * 3)];
car.position.z = initial
? -30 - Math.random() * 110
: -130 - Math.random() * 45;
}
for (let i = 0; i < 6; i++) {
const car = makeCar(colors[i % colors.length]);
spawnTraffic(car, true);
scene.add(car);
traffic.push(car);
}
// TREES
const trees = [];
const trunkMat = new THREE.MeshStandardMaterial({
color: 0x754321
});
const leavesMat = new THREE.MeshStandardMaterial({
color: 0x126b32
});
for (let i = 0; i < 45; i++) {
for (const side of [-1, 1]) {
const tree = new THREE.Group();
const trunk = new THREE.Mesh(
new THREE.CylinderGeometry(0.18, 0.25, 1.3, 8),
trunkMat
);
trunk.position.y = 0.65;
tree.add(trunk);
const leaves = new THREE.Mesh(
new THREE.ConeGeometry(1, 2.5, 8),
leavesMat
);
leaves.position.y = 2.2;
tree.add(leaves);
tree.position.set(
side * (8 + Math.random() * 5),
0,
-180 + i * 5
);
scene.add(tree);
trees.push(tree);
}
}
// GAME STATE
let running = false;
let score = 0;
let speed = 28;
let steering = 0;
let lastTime = 0;
let spawnTimer = 0;
const scoreText = document.getElementById("score");
const speedText = document.getElementById("speed");
const menu = document.getElementById("menu");
const message = document.getElementById("message");
function startGame() {
score = 0;
speed = 28;
steering = 0;
spawnTimer = 0;
player.position.x = 0;
traffic.forEach((car, i) => {
car.position.x = [-3.5, 0, 3.5][i % 3];
car.position.z = -35 - i * 24;
});
scoreText.textContent = "0";
speedText.textContent = "0";
running = true;
menu.style.display = "none";
lastTime = performance.now();
}
function gameOver() {
running = false;
message.innerHTML =
"💥 GAME OVER!<br>Your Score: " + score +
"<br>Try again and beat your record!";
document.getElementById("start").textContent = "PLAY AGAIN";
menu.style.display = "flex";
}
// CONTROLS
let left = false;
let right = false;
function hold(id, setter) {
const btn = document.getElementById(id);
btn.addEventListener("pointerdown", e => {
e.preventDefault();
setter(true);
});
["pointerup", "pointerleave", "pointercancel"].forEach(event => {
btn.addEventListener(event, () => setter(false));
});
}
hold("left", v => left = v);
hold("right", v => right = v);
addEventListener("keydown", e => {
if (e.key === "ArrowLeft" || e.key === "a") left = true;
if (e.key === "ArrowRight" || e.key === "d") right = true;
});
addEventListener("keyup", e => {
if (e.key === "ArrowLeft" || e.key === "a") left = false;
if (e.key === "ArrowRight" || e.key === "d") right = false;
});
document.getElementById("start").onclick = startGame;
// GAME LOOP
function animate(time) {
requestAnimationFrame(animate);
const dt = Math.min((time - lastTime) / 1000, 0.04);
if (lastTime === 0) lastTime = time;
if (running) {
lastTime = time;
// Steering
steering = (right ? 1 : 0) - (left ? 1 : 0);
player.position.x += steering * 8 * dt;
player.position.x = THREE.MathUtils.clamp(
player.position.x, -4.25, 4.25
);
player.rotation.z = -steering * 0.12;
// Speed and score
speed = Math.min(60, speed + dt * 0.7);
score += Math.floor(dt * 10);
scoreText.textContent = score;
speedText.textContent = Math.floor(speed);
// Moving lane markings
laneMarks.forEach(mark => {
mark.position.z += speed * dt;
if (mark.position.z > 15) {
mark.position.z -= 192;
}
});
// Moving trees
trees.forEach(tree => {
tree.position.z += speed * dt;
if (tree.position.z > 15) {
tree.position.z -= 225;
}
});
// Traffic movement
traffic.forEach(car => {
car.position.z += speed * dt;
// Collision detection
const dx = Math.abs(player.position.x - car.position.x);
const dz = Math.abs(player.position.z - car.position.z);
if (dx < 1.45 && dz < 2.4) {
gameOver();
}
if (car.position.z > 15) {
spawnTraffic(car);
}
});
// Gentle camera follow
camera.position.x +=
(player.position.x * 0.28 - camera.position.x) * 2 * dt;
}
renderer.render(scene, camera);
}
requestAnimationFrame(animate);
// RESIZE
addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
});
</script>
</body>
</html>