"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
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Absolutely! Here’s a simple dirt bike hill game in a single HTML file using the <canvas> element and vanilla JavaScript (no external libraries).
It features:
Hilly terrain (randomly generated)
Bike (represented as a rectangle)
Physics: gravity, speed, rotation, and basic collision
Controls:
Left Arrow: accelerate left, tilt left
Right Arrow: accelerate right, tilt right
When moving, the opposite key acts as a brake
Copy-paste this into a .html file and open in your browser!
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Mini Dirt Bike Hill Game</title>
<style>
body { background: #222; margin: 0; overflow: hidden; }
canvas { display: block; margin: 0 auto; background: #87ceeb; }
#info {
position: absolute; top: 10px; left: 10px; color: #fff; font-family: sans-serif;
background: rgba(0,0,0,0.4); padding: 8px 16px; border-radius: 8px;
}
</style>
</head>
<body>
<canvas id="game"></canvas>
<div id="info">
<b>Dirt Bike Hill Game</b><br>
← / → : Move & Tilt<br>
<small>Opposite key = brake</small>
</div>
<script>
const W = 900, H = 500;
const canvas = document.getElementById('game');
canvas.width = W;
canvas.height = H;
const ctx = canvas.getContext('2d');
// --- Terrain Generation ---
const terrain = [];
const terrainStep = 5;
let lastY = H * 0.7;
for (let x = 0; x <= W * 3; x += terrainStep) {
// Perlin-like smooth hills
let y = H * 0.7
+ 60 * Math.sin(x * 0.004)
+ 30 * Math.sin(x * 0.013)
+ 20 * Math.sin(x * 0.021)
+ 10 * Math.sin(x * 0.037);
// Add some random bumps
if (Math.random() < 0.01) y += (Math.random() - 0.5) * 40;
terrain.push({x, y});
lastY = y;
}
// --- Bike State ---
const bike = {
x: 100,
y: 0,
vx: 0,
vy: 0,
angle: 0, // radians
vangle: 0,
width: 50,
height: 18,
wheelR: 10,
onGround: false,
crashed: false,
score: 0,
};
// --- Controls ---
const keys = { left: false, right: false };
window.addEventListener('keydown', e => {
if (e.key === 'ArrowLeft') keys.left = true;
if (e.key === 'ArrowRight') keys.right = true;
if (bike.crashed && (e.key === ' ' || e.key === 'Enter')) resetGame();
});
window.addEventListener('keyup', e => {
if (e.key === 'ArrowLeft') keys.left = false;
if (e.key === 'ArrowRight') keys.right = false;
});
// --- Physics Constants ---
const GRAVITY = 0.5;
const ACCEL = 0.15;
const BRAKE = 0.2;
const MAX_SPEED = 6;
const ROTATE_SPEED = 0.04;
const ROTATE_AIR = 0.03;
const FRICTION = 0.98;
const GROUND_FRICTION = 0.85;
const ANGLE_DAMP = 0.92;
// --- Helper: Get terrain Y at X ---
function getTerrainY(x) {
// Find two terrain points
let i = Math.floor(x / terrainStep);
if (i < 0) i = 0;
if (i >= terrain.length - 1) i = terrain.length - 2;
let t0 = terrain[i], t1 = terrain[i+1];
let f = (x - t0.x) / (t1.x - t0.x);
return t0.y * (1 - f) + t1.y * f;
}
// --- Helper: Get terrain angle at X ---
function getTerrainAngle(x) {
let i = Math.floor(x / terrainStep);
if (i < 0) i = 0;
if (i >= terrain.length - 1) i = terrain.length - 2;
let t0 = terrain[i], t1 = terrain[i+1];
return Math.atan2(t1.y - t0.y, t1.x - t0.x);
}
// --- Game Loop ---
function update() {
if (!bike.crashed) {
// Controls
let movingLeft = keys.left && !keys.right;
let movingRight = keys.right && !keys.left;
let braking = (bike.vx > 0 && keys.left) || (bike.vx < 0 && keys.right);
// Accelerate
if (movingLeft) {
bike.vx -= ACCEL;
if (bike.onGround) bike.vangle -= ROTATE_SPEED;
else bike.vangle -= ROTATE_AIR;
}
if (movingRight) {
bike.vx += ACCEL;
if (bike.onGround) bike.vangle += ROTATE_SPEED;
else bike.vangle += ROTATE_AIR;
}
// Brake
if (braking) {
bike.vx *= (1 - BRAKE);
}
// Clamp speed
if (bike.vx > MAX_SPEED) bike.vx = MAX_SPEED;
if (bike.vx < -MAX_SPEED) bike.vx = -MAX_SPEED;
// Gravity
bike.vy += GRAVITY;
// Move
bike.x += bike.vx;
bike.y += bike.vy;
bike.angle += bike.vangle;
// Friction
bike.vx *= FRICTION;
bike.vangle *= ANGLE_DAMP;
// --- Collision with terrain ---
let wheelBase = bike.width * 0.5;
let frontX = bike.x + Math.cos(bike.angle) * wheelBase;
let frontY = bike.y + Math.sin(bike.angle) * wheelBase;
let backX = bike.x - Math.cos(bike.angle) * wheelBase;
let backY = bike.y - Math.sin(bike.angle) * wheelBase;
let frontGroundY = getTerrainY(frontX);
let backGroundY = getTerrainY(backX);
let onFront = frontY + bike.wheelR > frontGroundY;
let onBack = backY + bike.wheelR > backGroundY;
bike.onGround = onFront || onBack;
if (bike.onGround) {
// Snap wheels to terrain
if (onFront) {
let diff = (frontY + bike.wheelR) - frontGroundY;
bike.y -= diff * 0.5;
bike.vy = 0;
}
if (onBack) {
let diff = (backY + bike.wheelR) - backGroundY;
bike.y -= diff * 0.5;
bike.vy = 0;
}
// Align angle to terrain
let targetAngle = getTerrainAngle(bike.x);
bike.angle += (targetAngle - bike.angle) * 0.2;
// Friction
bike.vx *= GROUND_FRICTION;
// Prevent sinking
if (bike.y > H) bike.y = H - 50;
}
// --- Crash Detection ---
// If angle too steep or bike under terrain
if (Math.abs(bike.angle) > Math.PI/2 + 0.4 ||
bike.y > getTerrainY(bike.x) + 30) {
bike.crashed = true;
}
// --- Score ---
if (bike.x > bike.score) bike.score = Math.floor(bike.x);
}
}
// --- Drawing ---
function draw() {
// Camera follows bike
let camX = bike.x - W * 0.3;
if (camX < 0) camX = 0;
// Sky
ctx.fillStyle = '#87ceeb';
ctx.fillRect(0, 0, W, H);
// Terrain
ctx.beginPath();
for (let i = 0; i < terrain.length; i++) {
let tx = terrain[i].x - camX;
let ty = terrain[i].y;
if (i === 0) ctx.moveTo(tx, ty);
else ctx.lineTo(tx, ty);
}
ctx.lineTo(terrain[terrain.length-1].x - camX, H);
ctx.lineTo(terrain[0].x - camX, H);
ctx.closePath();
ctx.fillStyle = '#654321';
ctx.fill();
// Draw bike
ctx.save();
ctx.translate(bike.x - camX, bike.y);
ctx.rotate(bike.angle);
// Body
ctx.fillStyle = bike.crashed ? '#a00' : '#222';
ctx.fillRect(-bike.width/2, -bike.height/2, bike.width, bike.height);
// Wheels
ctx.beginPath();
ctx.arc(-bike.width/2, bike.height/2, bike.wheelR, 0, 2*Math.PI);
ctx.arc(bike.width/2, bike.height/2, bike.wheelR, 0, 2*Math.PI);
ctx.fillStyle = '#111';
ctx.fill();
// Rider (head)
ctx.beginPath();
ctx.arc(0, -bike.height/2 - 8, 8, 0, 2*Math.PI);
ctx.fillStyle = '#ffe0a0';
ctx.fill();
ctx.restore();
// Score
ctx.fillStyle = '#fff';
ctx.font = '20px sans-serif';
ctx.fillText('Distance: ' + Math.floor(bike.score) + ' m', 20, 40);
// Crash message
if (bike.crashed) {
ctx.fillStyle = 'rgba(0,0,0,0.7)';
ctx.fillRect(W/2-150, H/2-60, 300, 120);
ctx.fillStyle = '#fff';
ctx.font = '32px sans-serif';
ctx.textAlign = 'center';
ctx.fillText('CRASHED!', W/2, H/2-10);
ctx.font = '18px sans-serif';
ctx.fillText('Press SPACE or ENTER to restart', W/2, H/2+30);
ctx.textAlign = 'left';
}
}
// --- Reset ---
function resetGame() {
bike.x = 100;
bike.y = getTerrainY(100) - 30;
bike.vx = 0;
bike.vy = 0;
bike.angle = 0;
bike.vangle = 0;
bike.crashed = false;
bike.score = 0;
}
// --- Main Loop ---
function loop() {
update();
draw();
requestAnimationFrame(loop);
}
resetGame();
loop();
</script>
</body>
</html>
Features:
Left/Right Arrow: Move and tilt
Opposite key: Brakes
Crash if you flip or fall under terrain
Restart with SPACE or ENTER
No dependencies, just open and play!
Let me know if you want more features or tweaks!