HTML
<!DOCTYPE html>
<html lang="tr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>4x4 Dağ Off-Road</title>
<style>
* { box-sizing: border-box; margin: 0; padding: 0; user-select: none; }
body {
background: #0f172a;
color: #f8fafc;
font-family: system-ui, -apple-system, sans-serif;
overflow: hidden;
}
canvas {
display: block;
width: 100vw;
height: 100vh;
}
#hud {
position: fixed;
top: 16px;
left: 16px;
display: flex;
gap: 12px;
flex-wrap: wrap;
pointer-events: none;
}
.panel {
background: rgba(15, 23, 42, 0.78);
border: 1px solid rgba(255, 255, 255, 0.14);
backdrop-filter: blur(8px);
padding: 10px 14px;
border-radius: 10px;
font-size: 13px;
line-height: 1.4;
}
.label { color: #94a3b8; font-size: 11px; text-transform: uppercase; letter-spacing: 0.05em; }
.val { font-size: 18px; font-weight: 700; color: #38bdf8; }
#controls {
position: fixed;
bottom: 16px;
left: 16px;
background: rgba(15, 23, 42, 0.78);
border: 1px solid rgba(255, 255, 255, 0.14);
backdrop-filter: blur(8px);
padding: 10px 14px;
border-radius: 10px;
font-size: 12px;
color: #cbd5e1;
pointer-events: none;
}
kbd {
background: #1e293b;
border: 1px solid #475569;
border-radius: 4px;
padding: 1px 6px;
font-family: monospace;
color: #f8fafc;
}
#banner {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background: rgba(15, 23, 42, 0.9);
border: 1px solid #38bdf8;
padding: 20px 28px;
border-radius: 14px;
text-align: center;
display: none;
}
#banner h2 { color: #38bdf8; margin-bottom: 8px; }
#banner p { color: #cbd5e1; font-size: 14px; }
</style>
</head>
<body>
<canvas id="game"></canvas>
<div id="hud">
<div class="panel">
<div class="label">Mesafe</div>
<div class="val" id="distVal">0 m</div>
</div>
<div class="panel">
<div class="label">Yükseklik</div>
<div class="val" id="altVal">0 m</div>
</div>
<div class="panel">
<div class="label">Hız</div>
<div class="val" id="spdVal">0 km/s</div>
</div>
<div class="panel">
<div class="label">4x4 Tork</div>
<div class="val" id="gearVal">LOW 4L</div>
</div>
</div>
<div id="controls">
<kbd>W</kbd>/<kbd>↑</kbd> Gaz
<kbd>S</kbd>/<kbd>↓</kbd> Fren/Geri
<kbd>A</kbd>/<kbd>D</kbd> veya <kbd>←</kbd>/<kbd>→</kbd> Eğim Dengesi
<kbd>R</kbd> Aracı Kurtar
</div>
<div id="banner">
<h2>Zirveye Ulaştın!</h2>
<p>Dağ parkurunu tamamladın. İstediğin gibi serbest tırmanmaya devam edebilir veya <kbd>R</kbd> ile başa dönebilirsin.</p>
</div>
<script>
const canvas = document.getElementById("game");
const ctx = canvas.getContext("2d");
function resize() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
}
window.addEventListener("resize", resize);
resize();
// Kontroller
const keys = {};
window.addEventListener("keydown", (e) => {
keys[e.code] = true;
if (e.code === "KeyR") resetCar();
});
window.addEventListener("keyup", (e) => {
keys[e.code] = false;
});
// Arazi fonksiyonu (dağ sırtları, dik yokuşlar, tümsekler ve zirve platosu)
const TERRAIN_STEP = 16;
const MAX_X = 9500;
function getTerrainY(x) {
if (x < 0) return 520;
// Başlangıç düzlüğü
const startFlat = Math.min(1, Math.max(0, (x - 250) / 350));
// Dalgalı dağ sırtları ve kademeli tırmanış
const climb = -Math.pow(Math.min(x, MAX_X) / MAX_X, 0.88) * 1450;
const ridge1 = Math.sin(x * 0.0022) * 115;
const ridge2 = Math.cos(x * 0.0057 + 0.8) * 58;
const bumps = Math.sin(x * 0.017) * 18 + Math.cos(x * 0.039) * 8;
// Özel dik rampalar ve çukurlar
const ramp1 = Math.exp(-Math.pow((x - 1800) / 360, 2)) * -170;
const dip1 = Math.exp(-Math.pow((x - 3200) / 260, 2)) * 120;
const ramp2 = Math.exp(-Math.pow((x - 5400) / 420, 2)) * -220;
const ridge3 = Math.exp(-Math.pow((x - 7300) / 300, 2)) * -160;
// Zirve sonrası düz plato
const summitFactor = Math.min(1, Math.max(0, (x - 8600) / 500));
const rawY = 520 + (climb + ridge1 + ridge2 + bumps + ramp1 + dip1 + ramp2 + ridge3) * startFlat;
const plateauY = 520 - 1420;
return rawY * (1 - summitFactor) + plateauY * summitFactor;
}
function getSlopeAngle(x) {
const dx = 6;
const y1 = getTerrainY(x - dx);
const y2 = getTerrainY(x + dx);
return Math.atan2(y2 - y1, dx * 2);
}
// Off-road aracı durumu
const car = {
x: 160,
y: getTerrainY(160) - 55,
vx: 0,
vy: 0,
angle: 0,
angularVel: 0,
wheelRadius: 21,
wheelBase: 44,
suspensionRest: 24,
flCompress: 0,
frCompress: 0,
wheelSpin: 0,
groundedRear: false,
groundedFront: false
};
let camX = car.x - 240;
let camY = car.y - 340;
let reachedSummit = false;
const particles = [];
function resetCar() {
car.x = 160;
car.y = getTerrainY(160) - 55;
car.vx = 0;
car.vy = 0;
car.angle = 0;
car.angularVel = 0;
car.flCompress = 0;
car.frCompress = 0;
reachedSummit = false;
document.getElementById("banner").style.display = "none";
}
function spawnDust(x, y, count, color = "rgba(180, 145, 105, 0.65)") {
for (let i = 0; i < count; i++) {
particles.push({
x,
y,
vx: (Math.random() - 0.5) * 3.5 - car.vx * 0.15,
vy: -Math.random() * 2.5 - 0.5,
size: 3 + Math.random() * 5,
alpha: 0.75,
color
});
}
}
function updatePhysics() {
const dt = 1 / 60;
const gravity = 16.5;
const throttle = (keys["KeyW"] || keys["ArrowUp"]) ? 1 : 0;
const brakeOrRev = (keys["KeyS"] || keys["ArrowDown"]) ? 1 : 0;
const tiltLeft = (keys["KeyA"] || keys["ArrowLeft"]) ? 1 : 0;
const tiltRight = (keys["KeyD"] || keys["ArrowRight"]) ? 1 : 0;
// Tekerlek dünya pozisyonları
const cosA = Math.cos(car.angle);
const sinA = Math.sin(car.angle);
const rearAttachX = car.x - cosA * car.wheelBase - sinA * car.suspensionRest;
const rearAttachY = car.y - sinA * car.wheelBase + cosA * car.suspensionRest;
const frontAttachX = car.x + cosA * car.wheelBase - sinA * car.suspensionRest;
const frontAttachY = car.y + sinA * car.wheelBase + cosA * car.suspensionRest;
const rearGroundY = getTerrainY(rearAttachX);
const frontGroundY = getTerrainY(frontAttachX);
const rearDist = rearGroundY - rearAttachY - car.wheelRadius;
const frontDist = frontGroundY - frontAttachY - car.wheelRadius;
car.groundedRear = rearDist < 4;
car.groundedFront = frontDist < 4;
// Süspansiyon yay kuvvetleri
const springK = 145;
const damperC = 16;
let totalForceY = gravity * 12;
let totalTorque = 0;
if (car.groundedRear) {
const comp = Math.max(0, -rearDist);
car.flCompress = Math.min(16, comp);
const velY = car.vy - car.angularVel * car.wheelBase * cosA;
const f = Math.max(0, comp * springK - velY * damperC);
totalForceY -= f;
totalTorque += f * car.wheelBase * cosA * 0.014;
} else {
car.flCompress *= 0.85;
}
if (car.groundedFront) {
const comp = Math.max(0, -frontDist);
car.frCompress = Math.min(16, comp);
const velY = car.vy + car.angularVel * car.wheelBase * cosA;
const f = Math.max(0, comp * springK - velY * damperC);
totalForceY -= f;
totalTorque -= f * car.wheelBase * cosA * 0.014;
} else {
car.frCompress *= 0.85;
}
// 4x4 tırmanma torku ve eğim desteği
const groundContactCount = (car.groundedRear ? 1 : 0) + (car.groundedFront ? 1 : 0);
const slope = getSlopeAngle(car.x);
if (groundContactCount > 0) {
const driveDirX = Math.cos(slope);
const driveDirY = Math.sin(slope);
// Yokuşta yüksek tork desteği (4L modu)
const torqueBoost = slope < -0.15 ? 1.35 : 1.0;
const drivePower = 26 * torqueBoost;
if (throttle) {
car.vx += driveDirX * drivePower * dt * (groundContactCount * 0.65);
car.vy += driveDirY * drivePower * dt * (groundContactCount * 0.65);
car.wheelSpin += 0.42;
if (Math.random() < 0.55) {
const wx = car.groundedRear ? rearAttachX : frontAttachX;
const wy = (car.groundedRear ? rearGroundY : frontGroundY) - 2;
spawnDust(wx, wy, 1);
}
} else if (brakeOrRev) {
if (car.vx > 1.2) {
// Fren
car.vx *= 0.94;
car.vy *= 0.94;
} else {
// Geri vites
car.vx -= driveDirX * 15 * dt;
car.vy -= driveDirY * 15 * dt;
car.wheelSpin -= 0.25;
}
} else {
// Doğal yuvarlanma direnci
car.vx *= 0.992;
}
// Araç gövdesini arazi eğimine yumuşak hizalama
const targetAngle = slope;
const angleDiff = targetAngle - car.angle;
totalTorque += angleDiff * 14 - car.angularVel * 5.2;
} else {
car.wheelSpin += (throttle - brakeOrRev) * 0.35;
}
// Havada veya yokuşta sürücü ağırlık/pitch kontrolü
const pitchControl = (tiltRight - tiltLeft) * (groundContactCount > 0 ? 5.5 : 11.5);
totalTorque += pitchControl;
// Hız sınırları
car.vx = Math.max(-11, Math.min(18, car.vx));
car.vy = Math.max(-16, Math.min(18, car.vy));
car.angularVel = Math.max(-4.5, Math.min(4.5, car.angularVel + totalTorque * dt));
car.x += car.vx;
car.y += car.vy + totalForceY * dt * 0.08;
car.angle += car.angularVel * dt;
// Sol sınır koruması
if (car.x < 80) {
car.x = 80;
car.vx = Math.max(0, car.vx);
}
// Gövdenin yere gömülmesini önle
const chassisGroundY = getTerrainY(car.x);
if (car.y + 18 > chassisGroundY) {
car.y = chassisGroundY - 18;
if (car.vy > 0) {
if (car.vy > 5) spawnDust(car.x, chassisGroundY, 6, "rgba(148, 163, 184, 0.7)");
car.vy *= -0.25;
}
car.vx *= 0.96;
}
// Zirve kontrolü
if (!reachedSummit && car.x >= 8700) {
reachedSummit = true;
document.getElementById("banner").style.display = "block";
}
// Toz parçacıkları
for (let i = particles.length - 1; i >= 0; i--) {
const p = particles[i];
p.x += p.vx;
p.y += p.vy;
p.vy += 0.06;
p.size *= 1.03;
p.alpha -= 0.022;
if (p.alpha <= 0) particles.splice(i, 1);
}
// Kamera yumuşak takip
const targetCamX = car.x - canvas.width * 0.28 + car.vx * 12;
const targetCamY = car.y - canvas.height * 0.56 + car.vy * 8;
camX += (targetCamX - camX) * 0.09;
camY += (targetCamY - camY) * 0.09;
// HUD güncelle
const distMeters = Math.max(0, Math.round((car.x - 160) / 14));
const altMeters = Math.max(0, Math.round((520 - car.y) / 12));
const speedKmh = Math.round(Math.hypot(car.vx, car.vy) * 7.5);
document.getElementById("distVal").textContent = `${distMeters} m`;
document.getElementById("altVal").textContent = `${altMeters} m`;
document.getElementById("spdVal").textContent = `${speedKmh} km/s`;
document.getElementById("gearVal").textContent = slope < -0.18 ? "LOW 4L (TORK)" : "4H AUTO";
}
function drawBackground() {
// Dağ gökyüzü gradyanı
const sky = ctx.createLinearGradient(0, 0, 0, canvas.height);
sky.addColorStop(0, "#0c4a6e");
sky.addColorStop(0.55, "#38bdf8");
sky.addColorStop(1, "#bae6fd");
ctx.fillStyle = sky;
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Uzak silüet dağlar (paralaks)
ctx.fillStyle = "rgba(30, 58, 95, 0.35)";
ctx.beginPath();
ctx.moveTo(0, canvas.height);
for (let sx = 0; sx <= canvas.width + 40; sx += 40) {
const wx = sx + camX * 0.25;
const y = 320 + Math.sin(wx * 0.0014) * 140 + Math.cos(wx * 0.0031) * 75 - camY * 0.15;
ctx.lineTo(sx, y);
}
ctx.lineTo(canvas.width, canvas.height);
ctx.closePath();
ctx.fill();
// Zirve bayrağı
const flagWorldX = 8700;
const flagWorldY = getTerrainY(flagWorldX);
const fx = flagWorldX - camX;
const fy = flagWorldY - camY;
if (fx > -80 && fx < canvas.width + 80) {
ctx.strokeStyle = "#f8fafc";
ctx.lineWidth = 4;
ctx.beginPath();
ctx.moveTo(fx, fy);
ctx.lineTo(fx, fy - 95);
ctx.stroke();
ctx.fillStyle = "#ef4444";
ctx.beginPath();
ctx.moveTo(fx, fy - 95);
ctx.lineTo(fx + 52, fy - 78);
ctx.lineTo(fx, fy - 60);
ctx.closePath();
ctx.fill();
}
}
function drawTerrain() {
const startWorldX = Math.floor((camX - 80) / TERRAIN_STEP) * TERRAIN_STEP;
const endWorldX = Math.ceil((camX + canvas.width + 80) / TERRAIN_STEP) * TERRAIN_STEP;
// Toprak ve kaya gövdesi
ctx.beginPath();
ctx.moveTo(startWorldX - camX, canvas.height + 200);
for (let x = startWorldX; x <= endWorldX; x += TERRAIN_STEP) {
ctx.lineTo(x - camX, getTerrainY(x) - camY);
}
ctx.lineTo(endWorldX - camX, canvas.height + 200);
ctx.closePath();
const earthGrad = ctx.createLinearGradient(0, 120, 0, canvas.height);
earthGrad.addColorStop(0, "#78350f");
earthGrad.addColorStop(0.35, "#451a03");
earthGrad.addColorStop(1, "#1c1917");
ctx.fillStyle = earthGrad;
ctx.fill();
// Yüzey çim / toprak şeridi
ctx.beginPath();
for (let x = startWorldX; x <= endWorldX; x += TERRAIN_STEP) {
const sx = x - camX;
const sy = getTerrainY(x) - camY;
if (x === startWorldX) ctx.moveTo(sx, sy);
else ctx.lineTo(sx, sy);
}
ctx.strokeStyle = "#65a30d";
ctx.lineWidth = 8;
ctx.stroke();
// Arazi üzerindeki taş detayları
ctx.fillStyle = "rgba(28, 25, 23, 0.55)";
for (let x = startWorldX; x <= endWorldX; x += 96) {
const hash = Math.abs(Math.sin(x * 12.9898) * 43758.5453) % 1;
if (hash > 0.45) {
const rx = x + (hash - 0.5) * 40 - camX;
const ry = getTerrainY(x + (hash - 0.5) * 40) - camY + 14 + hash * 28;
ctx.beginPath();
ctx.arc(rx, ry, 5 + hash * 8, 0, Math.PI * 2);
ctx.fill();
}
}
}
function drawWheel(wx, wy, radius, spin) {
ctx.save();
ctx.translate(wx - camX, wy - camY);
ctx.rotate(spin);
// Kalın off-road lastiği
ctx.fillStyle = "#18181b";
ctx.beginPath();
ctx.arc(0, 0, radius, 0, Math.PI * 2);
ctx.fill();
// Dişli lastik blokları
ctx.fillStyle = "#27272a";
const teeth = 10;
for (let i = 0; i < teeth; i++) {
const a = (i / teeth) * Math.PI * 2;
ctx.save();
ctx.rotate(a);
ctx.fillRect(-4, -radius - 2, 8, 5);
ctx.restore();
}
// Jant
ctx.fillStyle = "#cbd5e1";
ctx.beginPath();
ctx.arc(0, 0, radius * 0.52, 0, Math.PI * 2);
ctx.fill();
// Bijon/spokes
ctx.strokeStyle = "#334155";
ctx.lineWidth = 2.5;
for (let i = 0; i < 4; i++) {
const a = (i / 4) * Math.PI;
ctx.beginPath();
ctx.moveTo(Math.cos(a) * radius * 0.45, Math.sin(a) * radius * 0.45);
ctx.lineTo(-Math.cos(a) * radius * 0.45, -Math.sin(a) * radius * 0.45);
ctx.stroke();
}
ctx.restore();
}
function drawCar() {
const cosA = Math.cos(car.angle);
const sinA = Math.sin(car.angle);
// Süspansiyon kolları ve tekerlekler
const rearAttachX = car.x - cosA * car.wheelBase - sinA * (car.suspensionRest - car.flCompress);
const rearAttachY = car.y - sinA * car.wheelBase + cosA * (car.suspensionRest - car.flCompress);
const frontAttachX = car.x + cosA * car.wheelBase - sinA * (car.suspensionRest - car.frCompress);
const frontAttachY = car.y + sinA * car.wheelBase + cosA * (car.suspensionRest - car.frCompress);
ctx.strokeStyle = "#f59e0b";
ctx.lineWidth = 4;
ctx.beginPath();
ctx.moveTo(car.x - cosA * 32 - camX, car.y - sinA * 32 - camY);
ctx.lineTo(rearAttachX - camX, rearAttachY - camY);
ctx.moveTo(car.x + cosA * 32 - camX, car.y + sinA * 32 - camY);
ctx.lineTo(frontAttachX - camX, frontAttachY - camY);
ctx.stroke();
drawWheel(rearAttachX, rearAttachY, car.wheelRadius, car.wheelSpin);
drawWheel(frontAttachX, frontAttachY, car.wheelRadius, car.wheelSpin);
// Off-road gövdesi
ctx.save();
ctx.translate(car.x - camX, car.y - camY);
ctx.rotate(car.angle);
// Alt koruma plakası
ctx.fillStyle = "#334155";
ctx.fillRect(-54, 8, 108, 9);
// Ana gövde
ctx.fillStyle = "#ea580c";
ctx.beginPath();
ctx.moveTo(-56, 10);
ctx.lineTo(-56, -8);
ctx.lineTo(-34, -12);
ctx.lineTo(-22, -32);
ctx.lineTo(18, -32);
ctx.lineTo(32, -12);
ctx.lineTo(58, -6);
ctx.lineTo(58, 10);
ctx.closePath();
ctx.fill();
// Camlar
ctx.fillStyle = "#0f172a";
ctx.beginPath();
ctx.moveTo(-18, -28);
ctx.lineTo(-2, -28);
ctx.lineTo(-2, -13);
ctx.lineTo(-27, -13);
ctx.closePath();
ctx.fill();
ctx.beginPath();
ctx.moveTo(3, -28);
ctx.lineTo(15, -28);
ctx.lineTo(25, -13);
ctx.lineTo(3, -13);
ctx.closePath();
ctx.fill();
// Tavan bagaj barı & LED ışık
ctx.fillStyle = "#1e293b";
ctx.fillRect(-26, -36, 46, 4);
ctx.fillStyle = "#fef08a";
ctx.fillRect(16, -37, 6, 3);
// Ön far
ctx.fillStyle = "#fef9c3";
ctx.fillRect(54, -4, 5, 7);
ctx.restore();
}
function drawParticles() {
for (const p of particles) {
ctx.save();
ctx.globalAlpha = Math.max(0, p.alpha);
ctx.fillStyle = p.color;
ctx.beginPath();
ctx.arc(p.x - camX, p.y - camY, p.size, 0, Math.PI * 2);
ctx.fill();
ctx.restore();
}
}
function loop() {
updatePhysics();
drawBackground();
drawTerrain();
drawParticles();
drawCar();
requestAnimationFrame(loop);
}
loop();
</script>
</body>
</html>1
1
18KB
18KB
217.0ms
336.0ms
217.0ms