Update index.html
Browse files- index.html +59 -76
index.html
CHANGED
|
@@ -2,7 +2,7 @@
|
|
| 2 |
<html lang="es">
|
| 3 |
<head>
|
| 4 |
<meta charset="UTF-8">
|
| 5 |
-
<title>OutRun
|
| 6 |
<style>
|
| 7 |
body { margin:0; overflow:hidden; background:#6dd5fa; font-family:sans-serif; touch-action:none; }
|
| 8 |
canvas { display:block; width:100vw; height:80vh; background:skyblue; }
|
|
@@ -16,11 +16,22 @@
|
|
| 16 |
background:rgba(255,255,255,0.7);
|
| 17 |
font-size:24px; font-weight:bold; user-select:none;
|
| 18 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
@media(max-width:600px){ .btn{ width:60px; height:60px; font-size:20px; } }
|
| 20 |
</style>
|
| 21 |
</head>
|
| 22 |
<body>
|
| 23 |
<canvas id="game"></canvas>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
<div id="controls">
|
| 25 |
<button class="btn" id="left">⟵</button>
|
| 26 |
<button class="btn" id="right">⟶</button>
|
|
@@ -35,25 +46,28 @@ const ctx = canvas.getContext("2d");
|
|
| 35 |
function resize(){ canvas.width = window.innerWidth; canvas.height = window.innerHeight*0.8; }
|
| 36 |
resize(); window.addEventListener("resize", resize);
|
| 37 |
|
| 38 |
-
let roadWidth = 2000;
|
| 39 |
-
let
|
| 40 |
-
let
|
| 41 |
-
let playerX = 0;
|
| 42 |
-
let pos = 0;
|
| 43 |
-
let speed = 200;
|
| 44 |
let keys = {left:false,right:false,up:false,down:false};
|
| 45 |
let segments = [];
|
| 46 |
|
| 47 |
// road segments with curves
|
| 48 |
-
for(let i=0;i<500;i++){
|
| 49 |
-
segments.push({i,curve: Math.sin(i/30)*2, y:i*segL});
|
| 50 |
-
}
|
| 51 |
|
| 52 |
-
//
|
| 53 |
-
let palmImg = new Image();
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
// ===================== INPUT =====================
|
| 59 |
window.addEventListener("keydown",e=>{
|
|
@@ -68,7 +82,6 @@ window.addEventListener("keyup",e=>{
|
|
| 68 |
if(e.key==="ArrowUp") keys.up=false;
|
| 69 |
if(e.key==="ArrowDown") keys.down=false;
|
| 70 |
});
|
| 71 |
-
|
| 72 |
function bindTouch(id,key){
|
| 73 |
let el=document.getElementById(id);
|
| 74 |
el.addEventListener("touchstart",()=>keys[key]=true);
|
|
@@ -85,99 +98,69 @@ function project(p, camX, camY, camZ){
|
|
| 85 |
let dx = p.x - camX;
|
| 86 |
let dy = p.y - camY;
|
| 87 |
let scale = camD / dz;
|
| 88 |
-
return {
|
| 89 |
-
x: (1+scale*dx)*canvas.width/2,
|
| 90 |
-
y: (1-scale*dy)*canvas.height/2,
|
| 91 |
-
w: scale*roadWidth/2
|
| 92 |
-
};
|
| 93 |
}
|
| 94 |
-
|
| 95 |
function drawSegment(p1,p2,color){
|
| 96 |
-
|
| 97 |
-
ctx.fillStyle=color.grass;
|
| 98 |
-
ctx.fillRect(0,p2.y,canvas.width,p1.y-p2.y);
|
| 99 |
-
// road
|
| 100 |
ctx.fillStyle=color.road;
|
| 101 |
ctx.beginPath();
|
| 102 |
-
ctx.moveTo(p1.x-p1.w,p1.y);
|
| 103 |
-
ctx.lineTo(
|
| 104 |
-
ctx.lineTo(p2.x+p2.w,p2.y);
|
| 105 |
-
ctx.lineTo(p2.x-p2.w,p2.y);
|
| 106 |
ctx.fill();
|
| 107 |
-
// rumble strips
|
| 108 |
ctx.fillStyle=color.rumble;
|
| 109 |
-
ctx.
|
| 110 |
-
ctx.
|
| 111 |
-
ctx.lineTo(p1.x-p1.w,p1.y);
|
| 112 |
-
ctx.lineTo(p2.x-p2.w,p2.y);
|
| 113 |
-
ctx.lineTo(p2.x-p2.w*1.2,p2.y);
|
| 114 |
-
ctx.fill();
|
| 115 |
-
ctx.beginPath();
|
| 116 |
-
ctx.moveTo(p1.x+p1.w*1.2,p1.y);
|
| 117 |
-
ctx.lineTo(p1.x+p1.w,p1.y);
|
| 118 |
-
ctx.lineTo(p2.x+p2.w,p2.y);
|
| 119 |
-
ctx.lineTo(p2.x+p2.w*1.2,p2.y);
|
| 120 |
-
ctx.fill();
|
| 121 |
-
}
|
| 122 |
-
|
| 123 |
-
function drawSprite(img,scale,x,y,width,height){
|
| 124 |
-
let w=width*scale, h=height*scale;
|
| 125 |
-
ctx.drawImage(img,x-w/2,y-h,w,h);
|
| 126 |
}
|
|
|
|
| 127 |
|
| 128 |
// ===================== UPDATE =====================
|
| 129 |
function update(dt){
|
| 130 |
-
pos += speed*dt;
|
| 131 |
-
while(pos >= segments.length*segL) pos -= segments.length*segL;
|
| 132 |
if(keys.left) playerX-=0.02;
|
| 133 |
if(keys.right) playerX+=0.02;
|
| 134 |
if(keys.up) speed+=5;
|
| 135 |
if(keys.down) speed-=5;
|
| 136 |
-
if(speed<0) speed=0;
|
| 137 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
}
|
| 139 |
|
| 140 |
// ===================== RENDER =====================
|
| 141 |
function render(){
|
| 142 |
ctx.clearRect(0,0,canvas.width,canvas.height);
|
| 143 |
-
let base=Math.floor(pos/segL);
|
| 144 |
-
let camY=1000, camZ=pos+500;
|
| 145 |
let x=0,dx=0;
|
| 146 |
for(let n=0;n<300;n++){
|
| 147 |
let seg=segments[(base+n)%segments.length];
|
| 148 |
-
seg.z=seg.y-
|
| 149 |
-
x+=dx; dx+=seg.curve*0.001;
|
| 150 |
let p1=project({x:x,y:0,z:seg.z},playerX*roadWidth,camY,camZ);
|
| 151 |
let p2=project({x:x+dx,y:0,z:seg.z+segL},playerX*roadWidth,camY,camZ);
|
| 152 |
let color={ road:n%2?"#707070":"#696969", grass:n%2?"#10aa10":"#009900", rumble:"#fff" };
|
| 153 |
if(p1.y>=p2.y && p2.y<canvas.height) drawSegment(p1,p2,color);
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
if(n%20===0 && palmImg.complete){
|
| 157 |
-
let scale= p1.w/250;
|
| 158 |
-
drawSprite(palmImg,scale,p1.x-p1.w*1.5,p1.y,200,200);
|
| 159 |
-
drawSprite(palmImg,scale,p1.x+p1.w*1.5,p1.y,200,200);
|
| 160 |
-
}
|
| 161 |
-
|
| 162 |
-
// auto rival cada 60 segmentos
|
| 163 |
-
if(n%60===0 && carImg.complete){
|
| 164 |
-
let scale=p1.w/350;
|
| 165 |
-
drawSprite(carImg,scale,p1.x,p1.y,120,60);
|
| 166 |
-
}
|
| 167 |
}
|
| 168 |
-
//
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
ctx.fillStyle="red";
|
| 170 |
ctx.fillRect(canvas.width/2-20+playerX*200, canvas.height-80,40,60);
|
| 171 |
}
|
| 172 |
|
| 173 |
// ===================== GAME LOOP =====================
|
| 174 |
let last=0;
|
| 175 |
-
function loop(ts){
|
| 176 |
-
let dt=(ts-last)/1000; if(dt>0.05) dt=0.05; last=ts;
|
| 177 |
-
update(dt);
|
| 178 |
-
render();
|
| 179 |
-
requestAnimationFrame(loop);
|
| 180 |
-
}
|
| 181 |
requestAnimationFrame(loop);
|
| 182 |
</script>
|
| 183 |
</body>
|
|
|
|
| 2 |
<html lang="es">
|
| 3 |
<head>
|
| 4 |
<meta charset="UTF-8">
|
| 5 |
+
<title>OutRun Engine + HUD + Rivales</title>
|
| 6 |
<style>
|
| 7 |
body { margin:0; overflow:hidden; background:#6dd5fa; font-family:sans-serif; touch-action:none; }
|
| 8 |
canvas { display:block; width:100vw; height:80vh; background:skyblue; }
|
|
|
|
| 16 |
background:rgba(255,255,255,0.7);
|
| 17 |
font-size:24px; font-weight:bold; user-select:none;
|
| 18 |
}
|
| 19 |
+
#hud {
|
| 20 |
+
position:fixed; top:5px; left:0; right:0;
|
| 21 |
+
display:flex; justify-content:space-around;
|
| 22 |
+
font-size:20px; font-weight:bold;
|
| 23 |
+
color:white; text-shadow:2px 2px 3px black;
|
| 24 |
+
}
|
| 25 |
@media(max-width:600px){ .btn{ width:60px; height:60px; font-size:20px; } }
|
| 26 |
</style>
|
| 27 |
</head>
|
| 28 |
<body>
|
| 29 |
<canvas id="game"></canvas>
|
| 30 |
+
<div id="hud">
|
| 31 |
+
<div id="score">SCORE: 0</div>
|
| 32 |
+
<div id="time">TIME: 60</div>
|
| 33 |
+
<div id="speed">SPEED: 0 km/h</div>
|
| 34 |
+
</div>
|
| 35 |
<div id="controls">
|
| 36 |
<button class="btn" id="left">⟵</button>
|
| 37 |
<button class="btn" id="right">⟶</button>
|
|
|
|
| 46 |
function resize(){ canvas.width = window.innerWidth; canvas.height = window.innerHeight*0.8; }
|
| 47 |
resize(); window.addEventListener("resize", resize);
|
| 48 |
|
| 49 |
+
let roadWidth = 2000, segL = 200, camD = 0.84;
|
| 50 |
+
let playerX = 0, pos = 0, speed = 200;
|
| 51 |
+
let score=0, timeLeft=60;
|
|
|
|
|
|
|
|
|
|
| 52 |
let keys = {left:false,right:false,up:false,down:false};
|
| 53 |
let segments = [];
|
| 54 |
|
| 55 |
// road segments with curves
|
| 56 |
+
for(let i=0;i<500;i++){ segments.push({i,curve:Math.sin(i/30)*2,y:i*segL}); }
|
|
|
|
|
|
|
| 57 |
|
| 58 |
+
// sprites
|
| 59 |
+
let palmImg = new Image(); palmImg.src="https://i.ibb.co/6Hw6yHr/palm.png";
|
| 60 |
+
let carImg = new Image(); carImg.src="https://i.ibb.co/VVvTqYm/car.png";
|
| 61 |
+
|
| 62 |
+
// rivales
|
| 63 |
+
let rivals = [];
|
| 64 |
+
function spawnRival(){
|
| 65 |
+
let lane = (Math.random()*2-1)*0.8; // -0.8, 0, 0.8
|
| 66 |
+
let z = pos + 2000 + Math.random()*3000;
|
| 67 |
+
let spd = 400+Math.random()*400;
|
| 68 |
+
rivals.push({x:lane,z,spd});
|
| 69 |
+
}
|
| 70 |
+
setInterval(spawnRival,4000); // cada 4 segundos aparece uno nuevo
|
| 71 |
|
| 72 |
// ===================== INPUT =====================
|
| 73 |
window.addEventListener("keydown",e=>{
|
|
|
|
| 82 |
if(e.key==="ArrowUp") keys.up=false;
|
| 83 |
if(e.key==="ArrowDown") keys.down=false;
|
| 84 |
});
|
|
|
|
| 85 |
function bindTouch(id,key){
|
| 86 |
let el=document.getElementById(id);
|
| 87 |
el.addEventListener("touchstart",()=>keys[key]=true);
|
|
|
|
| 98 |
let dx = p.x - camX;
|
| 99 |
let dy = p.y - camY;
|
| 100 |
let scale = camD / dz;
|
| 101 |
+
return { x:(1+scale*dx)*canvas.width/2, y:(1-scale*dy)*canvas.height/2, w:scale*roadWidth/2 };
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
}
|
|
|
|
| 103 |
function drawSegment(p1,p2,color){
|
| 104 |
+
ctx.fillStyle=color.grass; ctx.fillRect(0,p2.y,canvas.width,p1.y-p2.y);
|
|
|
|
|
|
|
|
|
|
| 105 |
ctx.fillStyle=color.road;
|
| 106 |
ctx.beginPath();
|
| 107 |
+
ctx.moveTo(p1.x-p1.w,p1.y); ctx.lineTo(p1.x+p1.w,p1.y);
|
| 108 |
+
ctx.lineTo(p2.x+p2.w,p2.y); ctx.lineTo(p2.x-p2.w,p2.y);
|
|
|
|
|
|
|
| 109 |
ctx.fill();
|
|
|
|
| 110 |
ctx.fillStyle=color.rumble;
|
| 111 |
+
ctx.fillRect(p1.x-p1.w*1.2,p2.y,p1.w*0.2,(p1.y-p2.y));
|
| 112 |
+
ctx.fillRect(p1.x+p1.w,p2.y,p1.w*0.2,(p1.y-p2.y));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
}
|
| 114 |
+
function drawSprite(img,scale,x,y,w,h){ ctx.drawImage(img,x-w*scale/2,y-h*scale,w*scale,h*scale); }
|
| 115 |
|
| 116 |
// ===================== UPDATE =====================
|
| 117 |
function update(dt){
|
| 118 |
+
pos += speed*dt; if(pos >= segments.length*segL) pos-=segments.length*segL;
|
|
|
|
| 119 |
if(keys.left) playerX-=0.02;
|
| 120 |
if(keys.right) playerX+=0.02;
|
| 121 |
if(keys.up) speed+=5;
|
| 122 |
if(keys.down) speed-=5;
|
| 123 |
+
if(speed<0) speed=0; if(speed>2000) speed=2000;
|
| 124 |
+
// rivals
|
| 125 |
+
for(let r of rivals){ r.z -= (speed-r.spd)*dt; }
|
| 126 |
+
rivals = rivals.filter(r=>r.z>pos && r.z<pos+8000);
|
| 127 |
+
// tiempo y score
|
| 128 |
+
timeLeft-=dt; if(timeLeft<=0){ timeLeft=0; speed=0; }
|
| 129 |
+
score += Math.floor(speed*dt*0.1);
|
| 130 |
+
document.getElementById("score").textContent="SCORE: "+score;
|
| 131 |
+
document.getElementById("time").textContent="TIME: "+Math.floor(timeLeft);
|
| 132 |
+
document.getElementById("speed").textContent="SPEED: "+Math.floor(speed/10)+" km/h";
|
| 133 |
}
|
| 134 |
|
| 135 |
// ===================== RENDER =====================
|
| 136 |
function render(){
|
| 137 |
ctx.clearRect(0,0,canvas.width,canvas.height);
|
| 138 |
+
let base=Math.floor(pos/segL), camY=1000, camZ=pos+500;
|
|
|
|
| 139 |
let x=0,dx=0;
|
| 140 |
for(let n=0;n<300;n++){
|
| 141 |
let seg=segments[(base+n)%segments.length];
|
| 142 |
+
seg.z=seg.y-pos; x+=dx; dx+=seg.curve*0.001;
|
|
|
|
| 143 |
let p1=project({x:x,y:0,z:seg.z},playerX*roadWidth,camY,camZ);
|
| 144 |
let p2=project({x:x+dx,y:0,z:seg.z+segL},playerX*roadWidth,camY,camZ);
|
| 145 |
let color={ road:n%2?"#707070":"#696969", grass:n%2?"#10aa10":"#009900", rumble:"#fff" };
|
| 146 |
if(p1.y>=p2.y && p2.y<canvas.height) drawSegment(p1,p2,color);
|
| 147 |
+
// palmeras
|
| 148 |
+
if(n%20===0 && palmImg.complete){ let s=p1.w/250; drawSprite(palmImg,s,p1.x-p1.w*1.5,p1.y,200,200); drawSprite(palmImg,s,p1.x+p1.w*1.5,p1.y,200,200); }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
}
|
| 150 |
+
// rivales
|
| 151 |
+
for(let r of rivals){
|
| 152 |
+
let dz=r.z-pos; if(dz<0) continue;
|
| 153 |
+
let proj=project({x:r.x*roadWidth,y:0,z:dz},playerX*roadWidth,camY,camZ);
|
| 154 |
+
if(carImg.complete){ let s=proj.w/350; drawSprite(carImg,s,proj.x,proj.y,120,60); }
|
| 155 |
+
}
|
| 156 |
+
// auto jugador
|
| 157 |
ctx.fillStyle="red";
|
| 158 |
ctx.fillRect(canvas.width/2-20+playerX*200, canvas.height-80,40,60);
|
| 159 |
}
|
| 160 |
|
| 161 |
// ===================== GAME LOOP =====================
|
| 162 |
let last=0;
|
| 163 |
+
function loop(ts){ let dt=(ts-last)/1000; if(dt>0.05) dt=0.05; last=ts; update(dt); render(); requestAnimationFrame(loop); }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
requestAnimationFrame(loop);
|
| 165 |
</script>
|
| 166 |
</body>
|