Meta Description" name="description" />
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<title>Defender 3D: Real Traffic Physics</title>
<style>
body { margin: 0; background: #444; overflow: hidden; font-family: 'Arial', sans-serif; touch-action: none; }
#ui { position: absolute; top: 15px; left: 15px; color: white; background: rgba(0,0,0,0.6); padding: 10px; border-radius: 8px; z-index: 10; border-left: 5px solid yellow; }
#controls { position: absolute; bottom: 30px; width: 100%; display: flex; justify-content: space-around; z-index: 20; }
.btn { width: 75px; height: 75px; background: rgba(255,255,255,0.2); border: 2px solid #fff; border-radius: 50%; display: flex; align-items: center; justify-content: center; font-weight: bold; color: white; box-shadow: 0 4px 10px rgba(0,0,0,0.5); }
.fire { background: radial-gradient(circle, #ff0000, #800000); border-radius: 15px; width: 90px; }
canvas { display: block; }
</style>
</head>
<body>
<div id="ui">
<b>DEFENDER PRO</b><br>
SPEED: <span id="sp">0</span> KM/H<br>
HEALTH: <span id="hp">100</span>%
</div>
<div id="controls">
<div style="display:flex; gap:10px">
<div class="btn" id="l">L</div>
<div class="btn" id="r">R</div>
</div>
<div class="btn fire" id="f">FIRE π«</div>
<div style="display:flex; gap:10px">
<div class="btn" style="background:rgba(255,0,0,0.4)" id="b">BRK</div>
<div class="btn" style="background:rgba(0,255,0,0.4)" id="g">GAS</div>
</div>
</div>
<canvas id="game"></canvas>
<script>
const canvas = document.getElementById('game');
const ctx = canvas.getContext('2d');
let w, h, speed = 0, carX = 0, roadY = 0, health = 100;
let traffic = [], bullets = [];
function init() {
w = canvas.width = window.innerWidth;
h = canvas.height = window.innerHeight;
carX = w / 2;
for(let i=0; i<4; i++) spawnTraffic();
}
function spawnTraffic() {
traffic.push({
x: w*0.3 + Math.random()*(w*0.4),
y: -Math.random()*1000,
s: 2 + Math.random()*3,
w: 60, h: 100,
vx: 0 // velocity for collision
});
}
let input = { l:0, r:0, g:0, b:0 };
const bind = (id, k) => {
const el = document.getElementById(id);
el.ontouchstart = (e) => { e.preventDefault(); input[k] = 1; if(id==='f') shoot(); };
el.ontouchend = (e) => { e.preventDefault(); input[k] = 0; };
};
bind('l','l'); bind('r','r'); bind('g','g'); bind('b','b'); bind('f','f');
function shoot() {
bullets.push({ x: carX, y: h - 160 });
}
function drawRoad() {
// Asli Road Texture
ctx.fillStyle = "#333"; // Asphalt
ctx.fillRect(w*0.2, 0, w*0.6, h);
// Road Lines
ctx.strokeStyle = "#fff";
ctx.setLineDash([30, 50]);
ctx.lineDashOffset = -roadY;
ctx.lineWidth = 4;
ctx.beginPath();
ctx.moveTo(w*0.5, 0); ctx.lineTo(w*0.5, h);
ctx.stroke();
// Footpath / Tiles
ctx.fillStyle = "#666";
ctx.setLineDash([]);
for(let i=0; i<h; i+=40) {
ctx.fillRect(w*0.15, (i + roadY)%h, w*0.05, 38);
ctx.fillRect(w*0.8, (i + roadY)%h, w*0.05, 38);
}
}
function update() {
ctx.fillStyle = "#2e7d32"; // Grass background
ctx.fillRect(0,0,w,h);
drawRoad();
// Defender Physics
if(input.g) speed += 0.3;
if(input.b) speed -= 0.7;
speed *= 0.98;
if(speed < 0) speed = 0;
if(input.l) carX -= 5;
if(input.r) carX += 5;
roadY += speed;
// Bullets
bullets.forEach((b, i) => {
b.y -= 15;
ctx.fillStyle = "orange";
ctx.fillRect(b.x-2, b.y, 4, 15);
if(b.y < 0) bullets.splice(i, 1);
});
// Traffic Interaction
traffic.forEach((t, i) => {
t.y += speed * 0.4 + t.s;
t.x += t.vx; // apply collision force
t.vx *= 0.9; // friction for collision
// Real Car Look (Traffic)
ctx.fillStyle = "white";
ctx.fillRect(t.x, t.y, t.w, t.h);
ctx.fillStyle = "black"; // Windshield
ctx.fillRect(t.x+5, t.y+10, t.w-10, 20);
// COLLISION LOGIC (Takrana)
let dx = carX - (t.x + t.w/2);
let dy = (h-150) - (t.y + t.h/2);
if(Math.abs(dx) < 45 && Math.abs(dy) < 80) {
// Jab takraye:
speed *= 0.5; // Gadi slow ho jaye
t.vx = dx < 0 ? 10 : -10; // Traffic car side mein hat jaye
health -= 0.5; // Health kam ho
document.getElementById('hp').innerText = Math.floor(health);
}
// Bullet Hit
bullets.forEach((b, bi) => {
if(b.x > t.x && b.x < t.x+t.w && b.y > t.y && b.y < t.y+t.h) {
traffic.splice(i, 1);
bullets.splice(bi, 1);
spawnTraffic();
}
});
if(t.y > h) { traffic.splice(i, 1); spawnTraffic(); }
});
// Draw Player Defender (Top View)
ctx.fillStyle = "#4b5320"; // Army Green
ctx.fillRect(carX-30, h-150, 60, 110);
ctx.fillStyle = "black"; // Roof
ctx.fillRect(carX-20, h-120, 40, 50);
ctx.fillStyle = "silver"; // Gun
ctx.fillRect(carX-4, h-170, 8, 40);
document.getElementById('sp').innerText = Math.floor(speed * 5);
if(health <= 0) { alert("CAR DESTROYED!"); location.reload(); }
requestAnimationFrame(update);
}
init(); update();
</script>
</body>
</html>1
1
6KB
6KB
127.0ms
196.0ms
127.0ms