"Mini flash game": When I was a kid I used to play this cool dirt bike game on hills and…
- User
"Mini flash game": When I was a kid I used to play this cool dirt bike game on hills and it was about speed and controlling the balance of the bike (tilting back and forth). It was simple the left arrow would speed forward and rotate the bike to the left and the other way around for the right arrow. When moving on a certain direction the opposite direction key would function as a break. Try to create this game using the canvas or 3djs, feel free to use any library you like, however everything must be inside a single html file (no npm nor tooling will be needed to run this program)
- Assistant
Sure! Below is a simple implementation of a 2D dirt bike hill game using HTML5 Canvas and JavaScript. It includes basic physics for gravity, rotation, and terrain collision. The controls are:
- ⬅️ Left Arrow: Tilt backward / Brake when moving right
- ➡️ Right Arrow: Tilt forward / Accelerate
Everything is contained in a single HTML file. No external libraries or tools are required.
🔧 Features:
- Procedural terrain generation
- Basic bike physics (gravity, rotation, acceleration)
- Simple controls
- Canvas rendering
📄 Copy and paste the following code into a
.htmlfile and open it in your browser:<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Mini Dirt Bike Game</title> <style> body { margin: 0; overflow: hidden; background: #87CEEB; } canvas { display: block; } </style> </head> <body> <canvas id="game"></canvas> <script> const canvas = document.getElementById('game'); const ctx = canvas.getContext('2d'); canvas.width = window.innerWidth; canvas.height = window.innerHeight; let keys = {}; document.addEventListener('keydown', e => keys[e.key] = true); document.addEventListener('keyup', e => keys[e.key] = false); // Terrain generation const terrain = []; const terrainLength = 2000; const terrainStep = 20; let lastY = canvas.height * 0.6; for (let x = 0; x < terrainLength; x += terrainStep) { let y = lastY + (Math.random() - 0.5) * 40; y = Math.min(canvas.height * 0.9, Math.max(canvas.height * 0.3, y)); terrain.push({ x, y }); lastY = y; } // Interpolate terrain height at a given x function getTerrainY(x) { for (let i = 0; i < terrain.length - 1; i++) { const p1 = terrain[i]; const p2 = terrain[i + 1]; if (x >= p1.x && x <= p2.x) { const t = (x - p1.x) / (p2.x - p1.x); return p1.y * (1 - t) + p2.y * t; } } return canvas.height; } // Bike object const bike = { x: 100, y: 100, vx: 0, vy: 0, angle: 0, angularVelocity: 0, width: 40, height: 20, onGround: false }; function update() { // Controls const accel = 0.05; const rotateSpeed = 0.002; const maxSpeed = 5; if (keys['ArrowRight']) { bike.vx += accel; bike.angularVelocity += rotateSpeed; } if (keys['ArrowLeft']) { bike.vx -= accel; bike.angularVelocity -= rotateSpeed; } // Clamp speed bike.vx = Math.max(-maxSpeed, Math.min(maxSpeed, bike.vx)); // Gravity bike.vy += 0.3; // Update position bike.x += bike.vx; bike.y += bike.vy; bike.angle += bike.angularVelocity; // Terrain collision const groundY = getTerrainY(bike.x); if (bike.y + bike.height / 2 > groundY) { bike.y = groundY - bike.height / 2; bike.vy = 0; bike.onGround = true; // Dampen rotation bike.angularVelocity *= 0.9; // Align with terrain slope const delta = 0.1; const y1 = getTerrainY(bike.x - delta); const y2 = getTerrainY(bike.x + delta); const slope = Math.atan2(y2 - y1, delta * 2); bike.angle = slope; } else { bike.onGround = false; } } function drawTerrain() { ctx.beginPath(); ctx.moveTo(terrain[0].x - bike.x + 100, terrain[0].y); for (let i = 1; i < terrain.length; i++) { ctx.lineTo(terrain[i].x - bike.x + 100, terrain[i].y); } ctx.lineTo(terrain[terrain.length - 1].x - bike.x + 100, canvas.height); ctx.lineTo(terrain[0].x - bike.x + 100, canvas.height); ctx.closePath(); ctx.fillStyle = '#654321'; ctx.fill(); } function drawBike() { ctx.save(); ctx.translate(100, bike.y); ctx.rotate(bike.angle); ctx.fillStyle = 'red'; ctx.fillRect(-bike.width / 2, -bike.height / 2, bike.width, bike.height); // Wheels ctx.beginPath(); ctx.arc(-bike.width / 2 + 5, bike.height / 2, 6, 0, Math.PI * 2); ctx.arc(bike.width / 2 - 5, bike.height / 2, 6, 0, Math.PI * 2); ctx.fillStyle = 'black'; ctx.fill(); ctx.restore(); } function loop() { ctx.clearRect(0, 0, canvas.width, canvas.height); update(); drawTerrain(); drawBike(); requestAnimationFrame(loop); } loop(); </script> </body> </html>🕹️ How to Play:
- Press the Right Arrow (→) to accelerate and tilt forward.
- Press the Left Arrow (←) to tilt backward or brake.
- Try to maintain balance while riding over hills!
Let me know if you'd like to add features like scoring, tricks, or sound effects!
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.