car game racing 3d html in one file
- User
car game racing 3d html in one file
Assistant
Here is a single-file 3-D-ish racing game that runs in any modern browser.
Copy everything into a file calledracing3d.html, open it, and use the arrow keys to steer.<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"/> <title>Mini 3-D Racer</title> <style> html,body{margin:0;height:100%;background:#000;overflow:hidden;font-family:sans-serif;color:#fff} #hud{position:absolute;top:10px;left:10px;font-size:20px;z-index:10} canvas{display:block;width:100%;height:100%} </style> </head> <body> <canvas id="c"></canvas> <div id="hud">Speed: <span id="speed">0</span> km/h</div> <script> /* ---------- CONFIG ---------- */ const cfg = { fov: 110 * Math.PI / 180, cameraHeight: 1.5, playerX: 0, playerY: 0, playerZ: 0, speed: 0, maxSpeed: 12, accel: 0.02, decel: 0.04, turnSpeed: 0.03, segmentLength: 200, roadWidth: 8, lanes: 3, drawDistance: 150 }; /* ---------- CANVAS ---------- */ const cvs = document.getElementById('c'); const ctx = cvs.getContext('2d'); function resize(){ cvs.width = innerWidth; cvs.height = innerHeight; } window.addEventListener('resize', resize); resize(); /* ---------- ROAD ---------- */ const road = []; const totalSegments = 500; for (let i = 0; i < totalSegments; i++) { road.push({ curve: Math.sin(i * 0.1) * 2, y: 0 }); } /* ---------- INPUT ---------- */ const keys = {}; onkeydown = e => keys[e.code] = true; onkeyup = e => keys[e.code] = false; /* ---------- GAME LOOP ---------- */ let pos = 0; function frame() { /* --- physics --- */ if (keys['ArrowUp']) cfg.speed = Math.min(cfg.speed + cfg.accel, cfg.maxSpeed); else if (keys['ArrowDown']) cfg.speed = Math.max(cfg.speed - cfg.decel, -cfg.maxSpeed/2); else cfg.speed *= 0.96; if (keys['ArrowLeft']) cfg.playerX -= cfg.turnSpeed * cfg.speed; if (keys['ArrowRight']) cfg.playerX += cfg.turnSpeed * cfg.speed; pos += cfg.speed; const baseSegment = Math.floor(pos / cfg.segmentLength) % totalSegments; /* --- draw sky / grass --- */ ctx.fillStyle = '#72d7ef'; ctx.fillRect(0, 0, cvs.width, cvs.height/2); ctx.fillStyle = '#10aa10'; ctx.fillRect(0, cvs.height/2, cvs.width, cvs.height/2); /* --- project road --- */ const maxy = cvs.height; let maxdim = 1; for (let n = 0; n < cfg.drawDistance; n++) { const segIndex = (baseSegment + n) % totalSegments; const seg = road[segIndex]; /* world -> camera */ let z = (n * cfg.segmentLength) - (pos % cfg.segmentLength); let curve = seg.curve * 200; let camX = -cfg.playerX * cfg.roadWidth + curve; let camY = cfg.cameraHeight * 200; let camZ = z; /* perspective projection */ const scale = cfg.fov / camZ; const px = cvs.width / 2 + camX * scale; const py = maxy - camY * scale; if (n > 0) { /* draw road segment */ const prevScale = cfg.fov / (z - cfg.segmentLength); const prevX = cvs.width / 2 + (-cfg.playerX * cfg.roadWidth + road[(segIndex-1+totalSegments)%totalSegments].curve * 200) * prevScale; const prevY = maxy - cfg.cameraHeight * 200 * prevScale; const w1 = cfg.roadWidth * prevScale; const w2 = cfg.roadWidth * scale; /* road */ ctx.fillStyle = '#444'; ctx.beginPath(); ctx.moveTo(prevX - w1, prevY); ctx.lineTo(prevX + w1, prevY); ctx.lineTo(px + w2, py); ctx.lineTo(px - w2, py); ctx.closePath(); ctx.fill(); /* lanes */ ctx.strokeStyle = '#fff'; ctx.lineWidth = 2; for (let l = 1; l < cfg.lanes; l++) { const laneX1 = prevX - w1 + (2 * w1 * l / cfg.lanes); const laneX2 = px - w2 + (2 * w2 * l / cfg.lanes); ctx.beginPath(); ctx.moveTo(laneX1, prevY); ctx.lineTo(laneX2, py); ctx.stroke(); } } } /* --- HUD --- */ document.getElementById('speed').textContent = Math.round(cfg.speed * 10); requestAnimationFrame(frame); } requestAnimationFrame(frame); </script> </body> </html>How to play
- Save the code as
racing3d.html. - Double-click to open in any modern browser.
- Arrow keys: steer left/right, accelerate/brake.
- Save the code as
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.