Update index.html
Browse files- index.html +83 -125
index.html
CHANGED
|
@@ -2,43 +2,21 @@
|
|
| 2 |
<html lang="es">
|
| 3 |
<head>
|
| 4 |
<meta charset="UTF-8">
|
| 5 |
-
<title>Mini OutRun
|
| 6 |
<style>
|
| 7 |
-
body {
|
| 8 |
-
|
| 9 |
-
overflow: hidden;
|
| 10 |
-
background: #6dd5fa;
|
| 11 |
-
font-family: sans-serif;
|
| 12 |
-
touch-action: none;
|
| 13 |
-
}
|
| 14 |
-
canvas {
|
| 15 |
-
display: block;
|
| 16 |
-
width: 100vw;
|
| 17 |
-
height: 80vh;
|
| 18 |
-
background: skyblue;
|
| 19 |
-
}
|
| 20 |
#controls {
|
| 21 |
-
position: fixed;
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
display: flex;
|
| 25 |
-
justify-content: space-around;
|
| 26 |
-
padding: 10px;
|
| 27 |
-
background: rgba(0,0,0,0.3);
|
| 28 |
}
|
| 29 |
.btn {
|
| 30 |
-
width: 70px;
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
border: none;
|
| 34 |
-
background: rgba(255,255,255,0.7);
|
| 35 |
-
font-size: 24px;
|
| 36 |
-
font-weight: bold;
|
| 37 |
-
user-select: none;
|
| 38 |
-
}
|
| 39 |
-
@media (max-width:600px){
|
| 40 |
-
.btn { width: 60px; height: 60px; font-size: 20px; }
|
| 41 |
}
|
|
|
|
| 42 |
</style>
|
| 43 |
</head>
|
| 44 |
<body>
|
|
@@ -51,127 +29,107 @@
|
|
| 51 |
</div>
|
| 52 |
|
| 53 |
<script>
|
| 54 |
-
const canvas
|
| 55 |
-
const ctx
|
| 56 |
-
function resize(){
|
| 57 |
-
resize();
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
let speed = 200;
|
| 66 |
-
let keys = {left:false,right:false,up:false,down:false};
|
| 67 |
-
let segments = [];
|
| 68 |
|
|
|
|
| 69 |
for(let i=0;i<500;i++){
|
| 70 |
-
segments.push({i,curve:
|
|
|
|
|
|
|
|
|
|
| 71 |
}
|
| 72 |
|
| 73 |
-
function project(p,
|
| 74 |
-
let dz
|
| 75 |
-
let
|
| 76 |
-
let dy = p.y - camY;
|
| 77 |
-
let scale = camD / dz;
|
| 78 |
return {
|
| 79 |
-
x:
|
| 80 |
-
y:
|
| 81 |
-
w:
|
| 82 |
};
|
| 83 |
}
|
| 84 |
|
| 85 |
-
function drawSegment(p1,
|
| 86 |
-
ctx.fillStyle
|
| 87 |
-
ctx.fillRect(0,
|
| 88 |
-
ctx.fillStyle
|
| 89 |
ctx.beginPath();
|
| 90 |
-
ctx.moveTo(p1.x-p1.w,
|
| 91 |
-
ctx.lineTo(p1.x+p1.w,
|
| 92 |
-
ctx.lineTo(p2.x+p2.w,
|
| 93 |
-
ctx.lineTo(p2.x-p2.w,
|
| 94 |
-
ctx.closePath();
|
| 95 |
-
ctx.fill();
|
| 96 |
-
ctx.fillStyle = color.rumble;
|
| 97 |
-
ctx.beginPath();
|
| 98 |
-
ctx.moveTo(p1.x-p1.w*1.2, p1.y);
|
| 99 |
-
ctx.lineTo(p1.x-p1.w, p1.y);
|
| 100 |
-
ctx.lineTo(p2.x-p2.w, p2.y);
|
| 101 |
-
ctx.lineTo(p2.x-p2.w*1.2, p2.y);
|
| 102 |
-
ctx.closePath();
|
| 103 |
-
ctx.fill();
|
| 104 |
-
ctx.beginPath();
|
| 105 |
-
ctx.moveTo(p1.x+p1.w*1.2, p1.y);
|
| 106 |
-
ctx.lineTo(p1.x+p1.w, p1.y);
|
| 107 |
-
ctx.lineTo(p2.x+p2.w, p2.y);
|
| 108 |
-
ctx.lineTo(p2.x+p2.w*1.2, p2.y);
|
| 109 |
-
ctx.closePath();
|
| 110 |
-
ctx.fill();
|
| 111 |
}
|
| 112 |
|
| 113 |
function frame(dt){
|
| 114 |
ctx.clearRect(0,0,canvas.width,canvas.height);
|
| 115 |
-
pos
|
| 116 |
-
|
| 117 |
-
let
|
| 118 |
-
let camY = 1000;
|
| 119 |
-
let camZ = pos+500;
|
| 120 |
-
let x=0,dx=0;
|
| 121 |
for(let n=0;n<300;n++){
|
| 122 |
-
let seg
|
| 123 |
-
seg.
|
| 124 |
-
|
| 125 |
-
x
|
| 126 |
-
|
| 127 |
-
let
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
}
|
|
|
|
| 136 |
ctx.fillStyle="red";
|
| 137 |
-
ctx.fillRect(canvas.width/2-20+playerX*200,
|
| 138 |
}
|
| 139 |
|
| 140 |
let last=0;
|
| 141 |
function loop(ts){
|
| 142 |
-
let dt=(ts-last)/1000;
|
| 143 |
-
if(keys.left)
|
| 144 |
-
if(keys.right)
|
| 145 |
-
if(keys.up)
|
| 146 |
-
if(keys.down)
|
| 147 |
-
if(speed<0)
|
| 148 |
-
|
| 149 |
-
frame(dt);
|
| 150 |
-
requestAnimationFrame(loop);
|
| 151 |
}
|
| 152 |
requestAnimationFrame(loop);
|
| 153 |
|
|
|
|
| 154 |
window.addEventListener("keydown",e=>{
|
| 155 |
-
if(e.key==="ArrowLeft")
|
| 156 |
-
if(e.key==="ArrowRight")
|
| 157 |
-
if(e.key==="ArrowUp")
|
| 158 |
-
if(e.key==="ArrowDown")
|
| 159 |
});
|
| 160 |
window.addEventListener("keyup",e=>{
|
| 161 |
-
if(e.key==="ArrowLeft")
|
| 162 |
-
if(e.key==="ArrowRight")
|
| 163 |
-
if(e.key==="ArrowUp")
|
| 164 |
-
if(e.key==="ArrowDown")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
});
|
| 166 |
-
|
| 167 |
-
document.getElementById("left").addEventListener("touchstart",()=>keys.left=true);
|
| 168 |
-
document.getElementById("left").addEventListener("touchend",()=>keys.left=false);
|
| 169 |
-
document.getElementById("right").addEventListener("touchstart",()=>keys.right=true);
|
| 170 |
-
document.getElementById("right").addEventListener("touchend",()=>keys.right=false);
|
| 171 |
-
document.getElementById("up").addEventListener("touchstart",()=>keys.up=true);
|
| 172 |
-
document.getElementById("up").addEventListener("touchend",()=>keys.up=false);
|
| 173 |
-
document.getElementById("down").addEventListener("touchstart",()=>keys.down=true);
|
| 174 |
-
document.getElementById("down").addEventListener("touchend",()=>keys.down=false);
|
| 175 |
</script>
|
| 176 |
</body>
|
| 177 |
</html>
|
|
|
|
| 2 |
<html lang="es">
|
| 3 |
<head>
|
| 4 |
<meta charset="UTF-8">
|
| 5 |
+
<title>Mini OutRun con Sprites</title>
|
| 6 |
<style>
|
| 7 |
+
body { margin:0; background:#6dd5fa; overflow:hidden; }
|
| 8 |
+
canvas { display:block; width:100vw; height:80vh; background:skyblue; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
#controls {
|
| 10 |
+
position: fixed; bottom: 0; width: 100%;
|
| 11 |
+
display: flex; justify-content: space-around;
|
| 12 |
+
padding: 10px; background: rgba(0,0,0,0.3);
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
}
|
| 14 |
.btn {
|
| 15 |
+
width:70px; height:70px; border-radius:50%; border:none;
|
| 16 |
+
background:rgba(255,255,255,0.7); font-size:24px; font-weight:bold;
|
| 17 |
+
user-select:none;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
}
|
| 19 |
+
@media (max-width:600px){ .btn{width:60px;height:60px;font-size:20px;} }
|
| 20 |
</style>
|
| 21 |
</head>
|
| 22 |
<body>
|
|
|
|
| 29 |
</div>
|
| 30 |
|
| 31 |
<script>
|
| 32 |
+
const canvas=document.getElementById("game");
|
| 33 |
+
const ctx=canvas.getContext("2d");
|
| 34 |
+
function resize(){canvas.width=window.innerWidth;canvas.height=window.innerHeight*0.8;}
|
| 35 |
+
resize();window.addEventListener("resize",resize);
|
| 36 |
+
|
| 37 |
+
let roadWidth=2000, segL=200, camD=0.84;
|
| 38 |
+
let playerX=0, pos=0, speed=200;
|
| 39 |
+
let keys={left:false,right:false,up:false,down:false};
|
| 40 |
+
let segments=[];
|
| 41 |
|
| 42 |
+
// tiny sprites (palmera y auto rival) en base64
|
| 43 |
+
const palmImg=new Image();
|
| 44 |
+
palmImg.src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA8AAAAfCAYAAABu7W0UAAAAvUlEQVR42mNgoBUwAqMxIBGZgzFQK7cA7UdGZwC4nSU+g4EYw0kAgSlQJBcwAHEBzgdwARygzMCaQoUSFhHI6wYSkVQEyNUBKg5g4g9iM5FeA4QhOgPBIRLUI2GtgSAUM1FQ3wB8ghIUIgGkCaAgvQGB8A+FPCAGYqwYIIhQwoVgghu4jICcZsLoAEwXwCRfDYoA0aYVAYhtGAr6RhEkJgfAoWYg1AdH2AgBa7z6EKZnCw8AAAAASUVORK5CYII=";
|
| 45 |
+
const carImg=new Image();
|
| 46 |
+
carImg.src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAPCAYAAADkmO9VAAAAkElEQVR42mNggAJGIHGQOwNjW3kMxWwGCqL9DAwMjExkBgYGigLwMKCOQpwHTGA2gWhBkGBbJBcT6E4gLUEMpHEIkA0MIsg1IAXxAS1I4jQbkmQWI6QAKxFqIA+RaQqQkBiI1IMqGiNA/AOQHzGpAxNJoEZkY8kQDCsBlwQxFCDEhRAAbfRAQGyqkIwgxgWAIMV/YOot9MNQAAAAASUVORK5CYII=";
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
+
// generar segmentos
|
| 49 |
for(let i=0;i<500;i++){
|
| 50 |
+
segments.push({i,curve:Math.sin(i/30)*2,y:i*segL,sprites:[]});
|
| 51 |
+
if(i%20===0) segments[i].sprites.push({img:palmImg,offset:-2});
|
| 52 |
+
if(i%25===0) segments[i].sprites.push({img:palmImg,offset:2});
|
| 53 |
+
if(i%40===0) segments[i].sprites.push({img:carImg,offset:(Math.random()*2-1)});
|
| 54 |
}
|
| 55 |
|
| 56 |
+
function project(p,camX,camY,camZ){
|
| 57 |
+
let dz=p.z-camZ,dx=p.x-camX,dy=p.y-camY;
|
| 58 |
+
let scale=camD/dz;
|
|
|
|
|
|
|
| 59 |
return {
|
| 60 |
+
x:(1+scale*dx)*canvas.width/2,
|
| 61 |
+
y:(1-scale*dy)*canvas.height/2,
|
| 62 |
+
w:scale*roadWidth/2, scale
|
| 63 |
};
|
| 64 |
}
|
| 65 |
|
| 66 |
+
function drawSegment(p1,p2,color){
|
| 67 |
+
ctx.fillStyle=color.grass;
|
| 68 |
+
ctx.fillRect(0,p2.y,canvas.width,p1.y-p2.y);
|
| 69 |
+
ctx.fillStyle=color.road;
|
| 70 |
ctx.beginPath();
|
| 71 |
+
ctx.moveTo(p1.x-p1.w,p1.y);
|
| 72 |
+
ctx.lineTo(p1.x+p1.w,p1.y);
|
| 73 |
+
ctx.lineTo(p2.x+p2.w,p2.y);
|
| 74 |
+
ctx.lineTo(p2.x-p2.w,p2.y);
|
| 75 |
+
ctx.closePath();ctx.fill();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
}
|
| 77 |
|
| 78 |
function frame(dt){
|
| 79 |
ctx.clearRect(0,0,canvas.width,canvas.height);
|
| 80 |
+
pos+=speed*dt; while(pos>=segments.length*segL) pos-=segments.length*segL;
|
| 81 |
+
let base=Math.floor(pos/segL), camY=1000, camZ=pos+500;
|
| 82 |
+
let x=0,dx=0, maxY=canvas.height;
|
|
|
|
|
|
|
|
|
|
| 83 |
for(let n=0;n<300;n++){
|
| 84 |
+
let seg=segments[(base+n)%segments.length];
|
| 85 |
+
seg.z=seg.y-(pos); x+=dx; dx+=seg.curve*0.001;
|
| 86 |
+
let p1=project({x:x,y:0,z:seg.z},playerX*roadWidth,camY,camZ);
|
| 87 |
+
let p2=project({x:x+dx,y:0,z:seg.z+segL},playerX*roadWidth,camY,camZ);
|
| 88 |
+
if(p1.y>=p2.y || p2.y>=maxY) continue;
|
| 89 |
+
let color={road:n%2?"#707070":"#696969",grass:n%2?"#10aa10":"#009900"};
|
| 90 |
+
drawSegment(p1,p2,color); maxY=p2.y;
|
| 91 |
+
seg.sprites.forEach(s=>{
|
| 92 |
+
let spriteX=p1.x+p1.w*s.offset;
|
| 93 |
+
let spriteY=p1.y;
|
| 94 |
+
let size=80*p1.scale;
|
| 95 |
+
ctx.drawImage(s.img,spriteX-size/2,spriteY-size,size,size);
|
| 96 |
+
});
|
| 97 |
}
|
| 98 |
+
// dibujar coche del jugador
|
| 99 |
ctx.fillStyle="red";
|
| 100 |
+
ctx.fillRect(canvas.width/2-20+playerX*200,canvas.height-80,40,60);
|
| 101 |
}
|
| 102 |
|
| 103 |
let last=0;
|
| 104 |
function loop(ts){
|
| 105 |
+
let dt=(ts-last)/1000;if(dt>0.05)dt=0.05;last=ts;
|
| 106 |
+
if(keys.left)playerX-=0.02;
|
| 107 |
+
if(keys.right)playerX+=0.02;
|
| 108 |
+
if(keys.up)speed+=5;
|
| 109 |
+
if(keys.down)speed-=5;
|
| 110 |
+
if(speed<0)speed=0;if(speed>2000)speed=2000;
|
| 111 |
+
frame(dt);requestAnimationFrame(loop);
|
|
|
|
|
|
|
| 112 |
}
|
| 113 |
requestAnimationFrame(loop);
|
| 114 |
|
| 115 |
+
// controles
|
| 116 |
window.addEventListener("keydown",e=>{
|
| 117 |
+
if(e.key==="ArrowLeft")keys.left=true;
|
| 118 |
+
if(e.key==="ArrowRight")keys.right=true;
|
| 119 |
+
if(e.key==="ArrowUp")keys.up=true;
|
| 120 |
+
if(e.key==="ArrowDown")keys.down=true;
|
| 121 |
});
|
| 122 |
window.addEventListener("keyup",e=>{
|
| 123 |
+
if(e.key==="ArrowLeft")keys.left=false;
|
| 124 |
+
if(e.key==="ArrowRight")keys.right=false;
|
| 125 |
+
if(e.key==="ArrowUp")keys.up=false;
|
| 126 |
+
if(e.key==="ArrowDown")keys.down=false;
|
| 127 |
+
});
|
| 128 |
+
["left","right","up","down"].forEach(id=>{
|
| 129 |
+
const b=document.getElementById(id);
|
| 130 |
+
b.addEventListener("touchstart",()=>keys[id]=true);
|
| 131 |
+
b.addEventListener("touchend",()=>keys[id]=false);
|
| 132 |
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
</script>
|
| 134 |
</body>
|
| 135 |
</html>
|