<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Ultimate Car Racing</title>
<style>
*{box-sizing:border-box}
body{margin:0;background:#101820;color:white;font-family:Arial;text-align:center;overflow:hidden}
h2{margin:8px;font-size:22px;color:#00ffcc}
#game{display:block;width:100%;height:72vh;background:#333}
#hud{position:absolute;top:45px;left:0;right:0;display:flex;justify-content:space-around;font-size:18px;font-weight:bold;pointer-events:none}
.controls{height:20vh;display:flex;align-items:center;justify-content:space-evenly}
button{font-size:25px;padding:16px 25px;border:0;border-radius:15px;background:#00cc99;color:#001b16;font-weight:bold;touch-action:manipulation}
#start{position:absolute;top:40%;left:50%;transform:translate(-50%,-50%);z-index:3;background:#111d;color:white;padding:25px;border-radius:15px;width:85%}
#start button{font-size:20px}
</style>
</head>
<body>
<h2>🏎️ ULTIMATE CAR RACING</h2>
<div id="hud">
<span>Score: <b id="score">0</b></span>
<span>Speed: <b id="speed">0</b></span>
</div>
<canvas id="game"></canvas>
<div id="start">
<h1>🏁 CAR RACING</h1>
<p>Traffic سے بچو اور زیادہ سے زیادہ اسکور بناؤ!</p>
<button onclick="startGame()">START GAME ▶</button>
</div>
<div class="controls">
<button id="left">⬅️</button>
<button id="brake">🛑</button>
<button id="right">➡️</button>
<button id="gas">🚀</button>
</div>
<script>
const canvas=document.getElementById('game');
const ctx=canvas.getContext('2d');
let W,H,player,obstacles,score,speed,roadOffset,running;
let left=false,right=false,gas=false,brake=false;
let last=0,spawn=0;
function resize(){
const dpr=Math.min(devicePixelRatio||1,2);
W=innerWidth;H=innerHeight*.72;
canvas.width=W*dpr;canvas.height=H*dpr;
canvas.style.width=W+'px';canvas.style.height=H+'px';
ctx.setTransform(dpr,0,0,dpr,0,0);
if(!player)reset();
}
function reset(){
player={x:W/2,y:H*.78,w:38,h:68};
obstacles=[];score=0;speed=4;roadOffset=0;spawn=0;
}
function road(){
ctx.fillStyle='#237b38';ctx.fillRect(0,0,W,H);
const rw=W*.68,rx=(W-rw)/2;
ctx.fillStyle='#30343b';ctx.fillRect(rx,0,rw,H);
ctx.fillStyle='#eee';
ctx.fillRect(rx+5,0,4,H);ctx.fillRect(rx+rw-9,0,4,H);
for(let i=1;i<3;i++){
let x=rx+rw*i/3;
for(let y=-70+(roadOffset%70);y<H;y+=70){
ctx.fillRect(x-2,y,4,35);
}
}
roadOffset+=speed;
}
function car(x,y,w,h,color){
ctx.fillStyle='rgba(0,0,0,.35)';
ctx.fillRect(x+4,y+h-5,w,h*.15);
ctx.fillStyle=color;
ctx.beginPath();
ctx.roundRect(x,y,w,h,8);ctx.fill();
ctx.fillStyle='#b9ecff';
ctx.beginPath();
ctx.roundRect(x+w*.18,y+h*.18,w*.64,h*.25,5);ctx.fill();
ctx.fillStyle='#15232d';
ctx.fillRect(x+w*.15,y+h*.52,w*.7,h*.18);
ctx.fillStyle='#fff3a0';
ctx.fillRect(x+4,y+4,7,5);
ctx.fillRect(x+w-11,y+4,7,5);
ctx.fillStyle='#151515';
ctx.fillRect(x-4,y+h*.2,5,h*.2);
ctx.fillRect(x+w-1,y+h*.2,5,h*.2);
ctx.fillRect(x-4,y+h*.65,5,h*.2);
ctx.fillRect(x+w-1,y+h*.65,5,h*.2);
}
function spawnCar(){
const rw=W*.68,rx=(W-rw)/2;
const lane=Math.floor(Math.random()*3);
obstacles.push({
x:rx+rw*(lane+.5)/3-18,
y:-80,w:36,h:62,
color:['#ff4141','#ffcc33','#439cff','#ef65ff'][Math.floor(Math.random()*4)]
});
}
function hit(a,b){
return a.x<b.x+b.w&&a.x+a.w>b.x&&
a.y<b.y+b.h&&a.y+a.h>b.y;
}
function update(dt){
if(!running)return;
if(gas)speed=Math.min(speed+.08,11);
else speed=Math.max(speed-.025,4);
if(brake)speed=Math.max(2,speed-.15);
const move=(left?-1:0)+(right?1:0);
player.x+=move*speed*1.3*(dt/16);
const rw=W*.68,rx=(W-rw)/2;
player.x=Math.max(rx+8,Math.min(rx+rw-player.w-8,player.x));
spawn+=dt;
if(spawn>Math.max(420,1000-speed*45)){
spawn=0;spawnCar();
}
obstacles.forEach(o=>o.y+=speed*(dt/16));
obstacles=obstacles.filter(o=>o.y<H+100);
for(const o of obstacles){
if(hit(player,o)){endGame();return}
}
score+=Math.floor(speed*dt/100);
document.getElementById('score').textContent=score;
document.getElementById('speed').textContent=Math.round(speed*12);
}
function draw(){
ctx.clearRect(0,0,W,H);
road();
obstacles.forEach(o=>car(o.x,o.y,o.w,o.h,o.color));
car(player.x,player.y,player.w,player.h,'#00e5a0');
}
function loop(t){
let dt=Math.min(t-last||16,40);last=t;
update(dt);draw();
requestAnimationFrame(loop);
}
function startGame(){
reset();running=true;
document.getElementById('start').style.display='none';
}
function endGame(){
running=false;
document.getElementById('start').style.display='block';
document.getElementById('start').innerHTML=
`<h1>💥 GAME OVER</h1><h2>Score: ${score}</h2>
<button onclick="startGame()">PLAY AGAIN 🔄</button>`;
}
function bind(id,key){
const b=document.getElementById(id);
const down=e=>{e.preventDefault();window[key]=true};
const up=e=>{e.preventDefault();window[key]=false};
b.addEventListener('pointerdown',down);
b.addEventListener('pointerup',up);
b.addEventListener('pointercancel',up);
b.addEventListener('pointerleave',up);
}
bind('left','left');bind('right','right');
bind('gas','gas');bind('brake','brake');
addEventListener('keydown',e=>{
if(e.key==='ArrowLeft'||e.key==='a')left=true;
if(e.key==='ArrowRight'||e.key==='d')right=true;
if(e.key==='ArrowUp'||e.key==='w')gas=true;
if(e.key==='ArrowDown'||e.key==='s')brake=true;
});
addEventListener('keyup',e=>{
if(e.key==='ArrowLeft'||e.key==='a')left=false;
if(e.key==='ArrowRight'||e.key==='d')right=false;
if(e.key==='ArrowUp'||e.key==='w')gas=false;
if(e.key==='ArrowDown'||e.key==='s')brake=false;
});
addEventListener('resize',resize);
resize();reset();requestAnimationFrame(loop);
</script>
</body>
</html>1
1
6KB
6KB
177.0ms
280.0ms
177.0ms