Dino Run
by BlazingDolphin53870 lines62.6 KB
<!DOCTYPE html>
<html lang="en"><head><meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no">
<title>Dino Run ยท Yathin Looops</title>
<meta name="description" content="The no-internet dino game in full color. Pick Easy, Normal or Hard and survive 2 hours to win. Free, by Yathin Pedamallu.">
<meta property="og:title" content="Dino Run">
<meta property="og:description" content="The no-internet dino game in full color. Three levels. Can you survive 2 hours?">
<meta property="og:image" content="https://yathinlooops.pages.dev/promo/dino-run-free-app-2.jpg?v=3">
<link rel="icon" href="dinorun_icon.png">
<style>
*{box-sizing:border-box}
html,body{margin:0;background:#fff;color:#535353;font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,Helvetica,Arial,sans-serif;-webkit-user-select:none;user-select:none;-webkit-tap-highlight-color:transparent}
body{min-height:100vh;display:flex;flex-direction:column;align-items:center;justify-content:center;padding:14px 8px}
#mute{position:fixed;top:10px;left:10px;width:44px;height:44px;border-radius:50%;border:1px solid #ddd;background:#fff;font-size:18px;cursor:pointer;z-index:5;line-height:1}
.hint{width:100%;margin-bottom:14px}
.hint .row{display:flex;align-items:center;gap:10px}
.hint b{font-size:20px;color:#202124}
.hint p{margin:6px 0 0;color:#757575;font-size:14px}
#icon{width:33px;height:28px;image-rendering:pixelated}
#wrap{position:relative;touch-action:none;cursor:pointer;outline:none}
#c{display:block;image-rendering:pixelated;image-rendering:crisp-edges;touch-action:none;outline:none}
#boost{position:absolute;right:10px;bottom:10px;width:58px;height:58px;border-radius:50%;border:2px solid rgba(255,255,255,.75);background:rgba(32,33,36,.35);color:#ffe14d;font-size:26px;display:none;touch-action:none;z-index:3;-webkit-user-select:none;user-select:none}
#boost.on{background:rgba(255,190,0,.6)}
#boost.ultra{background:rgba(255,0,200,.55);border-color:#4dfcff;color:#4dfcff}
.touch #boost{display:block}
#again{position:absolute;left:50%;transform:translateX(-50%);bottom:8%;display:none;padding:8px 18px;font:700 14px/1 monospace;letter-spacing:2px;background:#9cff3a;color:#14210a;border:3px solid #14210a;box-shadow:3px 3px 0 #14210a;cursor:pointer;z-index:4}
#levels{display:flex;gap:10px;justify-content:center;width:100%;margin-top:12px;visibility:visible}
#levels button{flex:1;max-width:170px;min-height:48px;padding:10px 6px;font:700 16px/1 monospace;letter-spacing:2px;background:#fff;color:#14210a;border:3px solid #14210a;box-shadow:3px 3px 0 #14210a,inset 0 -6px 0 var(--lc);cursor:pointer;touch-action:manipulation;-webkit-user-select:none;user-select:none}
#levels button[aria-pressed="true"]{background:var(--lc);transform:translate(2px,2px);box-shadow:1px 1px 0 #14210a}
#levels button:focus-visible{outline:3px solid #5ec8ff;outline-offset:2px}
.keys{margin-top:12px;color:#6b6b6b;font-size:13px;text-align:center;line-height:1.5}
footer{margin-top:16px;color:#6b6b6b;font-size:12px;text-align:center}
</style></head><body>
<button id="mute" aria-label="Mute sound">๐</button>
<div class="hint" id="hint"><div class="row"><canvas id="icon" width="22" height="19"></canvas><b>No internet</b></div><p>Pick a level, then press Space (or tap) to play.</p></div>
<div id="wrap" tabindex="0"><canvas id="c" tabindex="0"></canvas><button id="boost" aria-label="Super speed">โก</button><button id="again">PLAY AGAIN</button></div>
<div id="levels" role="group" aria-label="Difficulty level"><button type="button" data-l="0" style="--lc:#9cff3a" aria-pressed="false">EASY</button><button type="button" data-l="1" style="--lc:#ffd23f" aria-pressed="true">NORMAL</button><button type="button" data-l="2" style="--lc:#ff3b5c" aria-pressed="false">HARD</button></div>
<div class="keys">Space / โ jump ยท โ duck ยท Shift super speed ยท both Shifts ULTRA speed ยท tap to jump, hold โก for super speed (two fingers = ultra) on phones</div>
<footer><a href="https://yathinlooops.pages.dev/" target="_blank" rel="noopener" style="color:inherit">โ Yathin Looops</a> ยท A fan recreation of the Chrome offline game. Not affiliated with Google.</footer>
<script>
(()=>{
'use strict';
const W=600,H=180,GY=150,WIN_T=2*3600;
/* difficulty levels: start speed, acceleration, speed caps per stage, stage times (s), birds from (s), gravity, jump velocity */
const LV=[
{n:'EASY',c:'#9cff3a',s0:3.3,acc:0,caps:[3.3,3.3,3.3],st:[Infinity,Infinity],bird:Infinity,g:.45,jv:-9},
{n:'NORMAL',c:'#ffd23f',s0:6,acc:.001,caps:[13,16,19],st:[40*60,67*60],bird:20,g:.6,jv:-10},
{n:'HARD',c:'#ff3b5c',s0:7.8,acc:.002,caps:[15,18,21],st:[20*60,45*60],bird:5,g:.6,jv:-10}];
let lvl=1;
const cv=document.getElementById('c'),ctx=cv.getContext('2d'),wrap=document.getElementById('wrap');
const boostBtn=document.getElementById('boost'),againBtn=document.getElementById('again'),muteBtn=document.getElementById('mute');
let S=1;
function resize(){
const cw=Math.max(320,Math.min(900,innerWidth-16,Math.max(320,(innerHeight-140)*W/H)));
const dpr=window.devicePixelRatio||1;
cv.style.width=cw+'px';cv.style.height=(cw*H/W)+'px';
cv.width=Math.round(cw*dpr);cv.height=Math.round(cw*H/W*dpr);
S=cv.width/W;
document.getElementById('hint').style.maxWidth=cw+'px';
document.getElementById('levels').style.maxWidth=cw+'px';
}
addEventListener('resize',resize);resize();
if('ontouchstart' in window||matchMedia('(pointer:coarse)').matches)document.body.classList.add('touch');
const rnd=(a,b)=>a+Math.random()*(b-a),ri=(a,b)=>Math.floor(rnd(a,b+1)),pick=a=>a[Math.floor(Math.random()*a.length)];
const hex=h=>[1,3,5].map(i=>parseInt(h.substr(i,2),16));
const mix=(a,b,t)=>a.map((v,i)=>v+(b[i]-v)*t);
const rgb=(c,a)=>a===undefined?`rgb(${c[0]|0},${c[1]|0},${c[2]|0})`:`rgba(${c[0]|0},${c[1]|0},${c[2]|0},${a})`;
function spr(rows,pal){
const h=rows.length,w=Math.max(...rows.map(r=>r.length));
const c=document.createElement('canvas');c.width=w;c.height=h;const x=c.getContext('2d');
rows.forEach((r,j)=>{for(let i=0;i<r.length;i++){const p=pal[r[i]];if(p){x.fillStyle=p;x.fillRect(i,j,1,1);}}});
return c;
}
function darken(src,a){const c=document.createElement('canvas');c.width=src.width;c.height=src.height;const x=c.getContext('2d');x.drawImage(src,0,0);x.globalCompositeOperation='source-atop';x.fillStyle=`rgba(14,10,48,${a})`;x.fillRect(0,0,c.width,c.height);return c;}
/* ---------- original pixel art ---------- */
const DT=[
"............hhhhhhh...",
"...........hgggggggg..",
"...........gkkkkwkggg.",
"...........gkkkkkkggg.",
"...........gggggggggg.",
"...........ggggg......",
"...........gggggggg...",
"g.........ggggg.......",
"g........hgggggg......",
"gg.....hhgggggggdd....",
"ggg..hhggggggg..d.....",
"gggghgggggggggg.......",
".gggggggggggggd.......",
"..ggggggggggddd.......",
"...dggggggddd.........",
"....ddddddd..........."];
const LEGS=[
["....dd...dd...","....d.....d...","....dd....dd.."],
["...ddd...dd...","..dd......d...","..........dd.."],
["....dd...dd...",".....d...d....",".....dd..dd..."],
["....dd...ddd..","....d......dd.","....dd........"],
["....dd...dd...","....dd...d....","....d....dd..."]];
const DK=[
"................hhhhhhh.....",
"hh...........hhgggggggggg...",
"gggghhhhhhhhhggkkkkwkgggg...",
".gggggggggggggggkkkkkkgggg..",
"..ggggggggggggggggggggggg...",
"...dggggggggggggggg.........",
"....ddddggggggggdgggggg.....",
".....ddddddddd..d..........."];
const DKL=[["......dd...dd...............","......d.....d..............."],
[".....ddd...dd...............","..........dd................"]];
const PD={g:'#9aa1ab',d:'#626a75',h:'#cdd3da',k:'#15161a',w:'#ffffff'};
const PM={g:'#535353',d:'#535353',h:'#535353',k:'#535353',w:'#ffffff'};
const mkD=(l,p)=>spr(DT.concat(l),p||PD);
const D_STAND=mkD(LEGS[0]),D_RUN=[mkD(LEGS[1]),mkD(LEGS[2]),mkD(LEGS[3]),mkD(LEGS[4])],D_MONO=mkD(LEGS[0],PM);
const D_DUCK=[spr(DK.concat(DKL[0]),PD),spr(DK.concat(DKL[1]),PD)];
const CR=DT.concat(LEGS[0]).map(r=>r.split(''));
for(let y=2;y<4;y++)for(let x=12;x<18;x++)CR[y][x]='g';
[[13,1],[15,1],[14,2],[13,3],[15,3]].forEach(([x,y])=>CR[y][x]='k');
[[14,1],[13,2],[15,2],[14,3]].forEach(([x,y])=>CR[y][x]='w');
for(let x=17;x<21;x++)CR[4][x]='.';CR[5][16]='.';
const D_CRASH=spr(CR.map(r=>r.join('')),PD);
const DOWN_T=[
"......................",
"......................",
"...........hhhhh......",
"..........hgggggh.....",
"..........gggggggg....",
"..........ggkkkkggg...",
"..........ggkkwkggg...",
"g.........ggkkkkggg...",
"g........hggggggggg...",
"gg.....hhggggg.gggg...",
"ggg..hhggggggg.gggg...",
"gggghgggggggggd.ggg...",
".gggggggggggggd.gdg...",
"..ggggggggggddd.......",
"...dggggggddd.........",
"....ddddddd..........."];
const ROAR_T=[
"...........hhhhhhh....",
"..........hggggggggggg",
"..........gkkkkwkggggg",
"..........gkkkkkkgwwww",
"..........ggggggrrr...",
"..........gggggrrrr...",
"..........gggggrrr....",
"g.........ggggggwww...",
"g........hggggggggggg.",
"gg.....hhggggggggggg..",
"ggg..hhgggggggd.......",
"gggghgggggggggdd......",
".gggggggggggggd.......",
"..ggggggggggddd.......",
"...dggggggddd.........",
"....ddddddd..........."];
const D_DOWN=spr(DOWN_T.concat(LEGS[0]),PD),D_ROAR=spr(ROAR_T.concat(LEGS[0]),Object.assign({r:'#7a1a2a'},PD));
{const ic=document.getElementById('icon').getContext('2d');ic.drawImage(D_MONO,0,0);}
const DB_STAND=[{x:24,y:2,w:18,h:12},{x:12,y:14,w:22,h:14},{x:2,y:16,w:10,h:6},{x:8,y:30,w:16,h:7}];
const DB_DUCK=[{x:6,y:2,w:48,h:12},{x:12,y:14,w:16,h:6}];
const BU=[
"..........tt..............",
"..........ttt.............",
"..........tdtt............",
"..........tddtt...........",
"...ttcc...tddttt..........",
"..ttttcc..tdddttt.........",
".tkttt....ttddtttt........",
"ooottttttttttttttttttt....",
"...ttyyyyyyttttttttttttttt",
"......ttttttttttttttttt...",
"","","","",""];
const BD=["","","","",
"...ttcc...................",
"..ttttcc..................",
".tkttt....................",
"ooottttttttttttttttttt....",
"...ttyyyyyyttttttttttttttt",
"......ttttttttttttttttt...",
".........tddtttt..........",
".........tddttt...........",
".........tdtt.............",
".........ttt..............",
".........tt..............."];
const PB={t:'#1fb5a8',d:'#11766e',o:'#ff9a1f',c:'#ff5a3d',k:'#111',y:'#ffd166'};
const BIRD=[spr(BU,PB),spr(BD,PB)];
const BB=[[{x:0,y:8,w:16,h:8},{x:6,y:14,w:44,h:6},{x:22,y:2,w:10,h:12}],[{x:0,y:8,w:16,h:8},{x:6,y:14,w:44,h:6},{x:20,y:20,w:10,h:8}]];
function makeCactus(h,tw,nArms,flower){
const c=document.createElement('canvas'),w=tw+8;c.width=w;c.height=h;const x=c.getContext('2d');
const G='#0f9d8a',D='#0a6b5e',L='#6ff0d6',SP='#ffffff',tx=4,boxes=[],armSp=[];
const col=(px,py,pw,ph)=>{x.fillStyle=G;x.fillRect(px,py,pw,ph);x.fillStyle=D;x.fillRect(px+pw-1,py,1,ph);if(pw>2){x.fillStyle=L;x.fillRect(px+1,py+1,1,ph-1);}x.clearRect(px,py,1,1);x.clearRect(px+pw-1,py,1,1);};
col(tx,0,tw,h);
boxes.push({x:tx*2+1,y:2,w:tw*2-2,h:h*2-2});
const sides=nArms===2?[-1,1]:nArms===1?[pick([-1,1])]:[];
for(const sd of sides){
const aw=tw>=6?3:2,al=ri(Math.max(2,h*.18|0),Math.max(3,h*.3|0)),ay=ri(Math.floor(h*.35),Math.floor(h*.6));
const vx=sd<0?tx-aw-1:tx+tw+1,cx=sd<0?vx:tx+tw;
x.fillStyle=G;x.fillRect(cx,ay,aw+1,aw);x.fillStyle=D;x.fillRect(cx,ay+aw-1,aw+1,1);
col(vx,ay-al,aw,al+aw);
const ax0=sd<0?vx:tx+tw,ax1=sd<0?tx:vx+aw;
boxes.push({x:ax0*2+1,y:(ay-al)*2+1,w:(ax1-ax0)*2-2,h:(al+aw)*2-2});
armSp.push([vx+(aw>>1),ay-al+1]);
}
/* body silhouette mask, taken before spines so the outline hugs the body */
const mask=x.getImageData(0,0,w,h).data;
x.fillStyle=SP;for(const [sx,sy] of armSp)x.fillRect(sx,sy,1,1);
for(let y=3;y<h-1;y+=3){const g=(y/3|0)%2;x.fillRect(tx+(g?tw-2:1),y,1,1);if(y%6===0)x.fillRect(g?tx+tw:tx-1,y+1,1,1);}
if(flower){x.fillStyle='#ff6fae';x.fillRect(tx+(tw>>1)-1,0,3,1);x.fillStyle='#ffd23f';x.fillRect(tx+(tw>>1),0,1,1);}
/* draw copy padded 1px left/right/top, with a 1px near-black outline around the body */
const o=document.createElement('canvas');o.width=w+2;o.height=h+1;const ox=o.getContext('2d');
const op=(px,py)=>px>=0&&py>=0&&px<w&&py<h&&mask[(py*w+px)*4+3]>0;
ox.fillStyle='#1a1a1a';
for(let py=-1;py<h;py++)for(let px=-1;px<=w;px++){if(op(px,py))continue;
if(op(px-1,py)||op(px+1,py)||op(px,py-1)||op(px,py+1))ox.fillRect(px+1,py+1,1,1);}
ox.drawImage(c,1,1);
/* slightly lighter variant, blended in at night */
const l=document.createElement('canvas');l.width=o.width;l.height=o.height;const lx=l.getContext('2d');
lx.drawImage(c,1,1);lx.globalCompositeOperation='source-atop';lx.fillStyle='rgba(190,255,240,.28)';lx.fillRect(0,0,l.width,l.height);
c.boxes=boxes;c.o=o;c.lt=l;return c;
}
const SMALL=[],LARGE=[],SQUAT=[];
for(let i=0;i<8;i++){SMALL.push(makeCactus(ri(13,16),4,ri(0,2)));LARGE.push(makeCactus(ri(20,24),6,ri(1,2)));}
for(let i=0;i<3;i++)SQUAT.push(makeCactus(ri(8,9),7,0,true));
const SMALL_MONO=null;
const TINY=[];for(let i=0;i<6;i++)TINY.push(makeCactus(ri(10,12),4,ri(0,1)));
const CLOUD=spr(["....wwww......","..wwwwwwww....",".wwwwwwwwwww..","wwwwwwwwwwwwww","llllllllllllll"],{w:'#ffffff',l:'#dfe6f5'});
const WIFI_ROWS=["...ooooo...",".oo.....oo.","o.........o","...ooooo...","..o.....o..","...........","....ooo....","...........",".....o....."];
const WIFI=spr(WIFI_ROWS,{o:'#ffffff'}),WIFI_G=spr(WIFI_ROWS,{o:'#7dff8a'}),WIFI_F=spr(WIFI_ROWS,{o:'#ff8c1a'}),WIFI_H=spr(WIFI_ROWS,{o:'#fff3b0'});
const RESTART=spr(["....xxxx....","..xx....xx.x",".x........xx","x.......xxxx","x...........","x...........","x..........x",".x........x.","..xx....xx..","....xxxx...."],{x:'#14210a'});
const RESTART_IN=spr(["....xxxx....","..xx....xx.x",".x........xx","x.......xxxx","x...........","x...........","x..........x",".x........x.","..xx....xx..","....xxxx...."],{x:'#9cff3a'});
function circ(n,f){const c=document.createElement('canvas');c.width=c.height=n;const x=c.getContext('2d');const r=n/2;for(let j=0;j<n;j++)for(let i=0;i<n;i++){const d=Math.hypot(i+.5-r,j+.5-r);const col=f(d,i,j,r);if(col){x.fillStyle=col;x.fillRect(i,j,1,1);}}return c;}
const SUN=circ(15,(d)=>d>7.3?0:d<3.5?'#fff6b0':d<5.8?'#ffd23f':'#ffab2e');
const MOON=circ(13,(d,i,j)=>{if(d>6.2)return 0;if((Math.hypot(i-4,j-4)<1.6)||(Math.hypot(i-8,j-8)<2)||(Math.hypot(i-9,j-3)<1))return '#d6cfa8';return '#f6f1d3';});
/* parallax tiles (art px, drawn x2) */
function tile(w,h,fn){const c=document.createElement('canvas');c.width=w;c.height=h;fn(c.getContext('2d'),w,h);return c;}
const HILLS=tile(300,30,(x,w,h)=>{
const hs=new Array(w).fill(5);let i=0;
while(i<w-8){const v=ri(6,24);i+=v;const top=ri(10,25),mw=ri(16,40),sl=ri(2,5);
for(let k=0;k<mw+sl*2&&i<w-4;k++,i++){let hh=top;if(k<sl)hh=5+(top-5)*(k+1)/(sl+1);else if(k>=mw+sl)hh=5+(top-5)*(mw+sl*2-k)/(sl+1);hs[i]=Math.round(hh);}}
for(let c=0;c<w;c++){const t=h-hs[c];x.fillStyle='#a184c9';x.fillRect(c,t,1,h-t);x.fillStyle='#bfa6e0';x.fillRect(c,t,1,2);x.fillStyle='#8f71b8';for(let b=t+6;b<h;b+=6)x.fillRect(c,b,1,1);}
});
const BUSH=tile(300,9,(x,w,h)=>{
x.fillStyle='#2f9e44';x.fillRect(0,h-2,w,2);
for(let k=0;k<26;k++){const cx=ri(3,w-4),r=ri(2,4);for(let j=-r;j<=0;j++)for(let i=-r;i<=r;i++)if(i*i+j*j<=r*r+1){x.fillStyle=(j===-r||(j===-r+1&&Math.abs(i)>r-2))?'#5cc96a':'#2f9e44';x.fillRect(cx+i,h-2+j,1,1);}}
});
const GROUND=tile(300,17,(x,w,h)=>{
const band=(y,hh,c)=>{x.fillStyle=c;x.fillRect(0,y,w,hh);};
band(2,1,'#8bea6d');band(3,2,'#5fd35f');band(5,1,'#3a9e3a');band(6,4,'#9c6b3c');band(10,4,'#86582f');band(14,3,'#6e4523');
for(let i=0;i<w;i++){if(Math.random()<.18){x.fillStyle=Math.random()<.5?'#5fd35f':'#8bea6d';x.fillRect(i,1,1,1);if(Math.random()<.3)x.fillRect(i,0,1,1);}}
for(let k=0;k<90;k++){x.fillStyle=Math.random()<.6?'#5a3a1e':'#b08458';x.fillRect(ri(0,w-2),ri(7,16),Math.random()<.5?2:1,1);}
x.fillStyle='#4e3119';for(let k=0;k<14;k++){let cx=ri(0,w-6),cy=ri(8,14);for(let s=0;s<4;s++){x.fillRect(cx,cy,1,1);cx++;cy+=Math.random()<.5?1:0;if(cy>16)break;}}
});
const HILLS_N=darken(HILLS,.55),BUSH_N=darken(BUSH,.5),GROUND_N=darken(GROUND,.45);
const STARS=[];for(let i=0;i<46;i++)STARS.push({x:rnd(0,W),y:rnd(4,105),p:rnd(0,6),s:Math.random()<.2?2:1});
/* ---------- destroyed earth (after the mega impact) ---------- */
const D_FIRES=[];
const D_HILLS=tile(300,30,(x,w,h)=>{
const hs=new Array(w).fill(4);let i=0;
while(i<w-8){const v=ri(6,22);i+=v;const top=ri(9,23),mw=ri(14,36),sl=ri(2,5);
for(let k=0;k<mw+sl*2&&i<w-4;k++,i++){let hh=top;if(k<sl)hh=4+(top-4)*(k+1)/(sl+1);else if(k>=mw+sl)hh=4+(top-4)*(mw+sl*2-k)/(sl+1);hs[i]=Math.max(3,Math.round(hh)+ri(-2,1));}
if(i<w-4&&Math.random()<.6){const nx=i-ri(4,12);for(let q=0;q<ri(2,4);q++)if(hs[nx+q])hs[nx+q]=Math.max(3,hs[nx+q]-ri(3,7));}}
for(let c=0;c<w;c++){const t=h-hs[c];x.fillStyle='#1f1416';x.fillRect(c,t,1,h-t);x.fillStyle='#3a2220';x.fillRect(c,t+1,1,1);x.fillStyle='#5a1e14';x.fillRect(c,t,1,1);x.fillStyle='#140c0e';for(let b=t+5;b<h;b+=5)x.fillRect(c,b,1,1);
if(Math.random()<.04){x.fillStyle='#7a2a14';x.fillRect(c,t+ri(2,Math.max(2,h-t-1)),1,1);}}
let last=-40;for(let c=6;c<w-6;c++){if(hs[c]>=11&&c-last>34&&Math.random()<.2){D_FIRES.push({x:c*2,y:GY-58+(h-hs[c])*2,p:rnd(0,6)});last=c;}}
});
const D_RUBBLE=tile(300,9,(x,w,h)=>{
x.fillStyle='#1a1212';x.fillRect(0,h-2,w,2);
for(let k=0;k<22;k++){const cx=ri(3,w-4),r=ri(1,3);for(let j=-r;j<=0;j++)for(let i=-r-1;i<=r+1;i++)if(i*i+j*j*2<=r*r+1){x.fillStyle=j===-r?'#2e2020':'#1a1212';x.fillRect(cx+i,h-2+j,1,1);}}
for(let k=0;k<16;k++){x.fillStyle=Math.random()<.5?'#c2401a':'#5a1e10';x.fillRect(ri(0,w-1),ri(h-3,h-1),1,1);}
});
const D_GROUND=tile(300,17,(x,w,h)=>{
const band=(y,hh,c)=>{x.fillStyle=c;x.fillRect(0,y,w,hh);};
band(2,1,'#2a2220');band(3,3,'#1e1817');band(6,4,'#171212');band(10,4,'#110d0d');band(14,3,'#0b0808');
for(let i=0;i<w;i++){if(Math.random()<.22){x.fillStyle=Math.random()<.5?'#2a2220':'#3a302c';x.fillRect(i,1,1,1);if(Math.random()<.25)x.fillRect(i,0,1,1);}}
for(let k=0;k<110;k++){x.fillStyle=Math.random()<.7?'#3a302c':'#4a1a10';x.fillRect(ri(0,w-2),ri(4,16),Math.random()<.5?2:1,1);}
});
function crackTiles(){
const a=document.createElement('canvas'),g=document.createElement('canvas');a.width=g.width=300;a.height=g.height=17;
const x=a.getContext('2d'),y=g.getContext('2d');y.fillStyle='rgba(255,60,10,.28)';
for(let k=0;k<9;k++){let cx=ri(0,296),cy=ri(2,4);const dir=pick([-1,1]);
for(let s=0;s<ri(8,16)&&cy<16;s++){
x.fillStyle=s%3===1?'#ffb347':'#ff5a1a';x.fillRect(cx,cy,1,1);y.fillRect(cx-1,cy-1,3,3);
if(Math.random()<.25){const bx=cx+pick([-1,1])*ri(1,3);x.fillStyle='#c2300e';x.fillRect(Math.min(cx,bx),cy,Math.abs(bx-cx)+1,1);}
const r=Math.random();if(r<.45)cy++;else if(r<.8)cx+=dir;else{cx-=dir;cy++;}}}
return {c:a,g};
}
const D_CRACK_A=crackTiles(),D_CRACK_B=crackTiles();
const D_STUMPS=[
spr(["..e...",".bcb..",".bcb.a",".bcb.b",".bcbbb",".bcb..","bbcb..","c.cb..","bbcb..",".bcb..",".bcb..","bbcbb."],{b:'#1c1412',c:'#3a2a24',e:'#ff6a1a',a:'#5a5250'}),
spr(["a.e.","bcbb","bcb.","bcb.","bcbb","bccb"],{b:'#1c1412',c:'#3a2a24',e:'#ff6a1a',a:'#5a5250'}),
spr(["a....e.","b..bcb.","bbbbcb.","...bcbb","...bcb.","...bcb.","...bcb.","..bbcbb"],{b:'#1c1412',c:'#3a2a24',e:'#ff6a1a',a:'#5a5250'})];
const D_STUMP_AT=[{s:0,x:236},{s:1,x:338},{s:2,x:452},{s:1,x:556}];
const SMOKE_C=spr(["....dddd.......","..ddggggdd.....",".dggggllggdddd.","dggglllggggggdd","ddggggggggggggd","..ddddddddddd.."],{d:'#2a2426',g:'#3a3234',l:'#4c4244'});
const CRATER=tile(60,16,(x,w,h)=>{
for(let j=0;j<h;j++)for(let i=0;i<w;i++){
const dx=(i+.5-30)/22,dy=(j+.5-6)/4.5,e=dx*dx+dy*dy,ax=Math.abs(i+.5-30);
let col=0;
if(e<1){if(j<6)col=e>.7?'#5a1e10':'#241614';else col=e<.18?(j===6?'#ffb347':'#ff5a1a'):e<.42?'#7a2410':'#140c0c';}
else{const mh=Math.max(0,Math.round(4*(1-Math.abs(ax-25)/7)));if(j<=4&&j>=4-mh)col=j===4-mh?'#4a3028':'#2a1e1a';}
if(col){x.fillStyle=col;x.fillRect(i,j,1,1);}}
});
const CRATER_LIP=tile(60,16,(x,w,h)=>{
for(let j=6;j<h;j++)for(let i=0;i<w;i++){const dx=(i+.5-30)/22,dy=(j+.5-6)/4.5,e=dx*dx+dy*dy;
if(e>=.72&&e<1.25){x.fillStyle=e<.85?'#3a2a24':(i*7+j*3)%13===0?'#c2401a':'#211715';x.fillRect(i,j,1,1);}}
});
const ROCK=(()=>{
const r=document.createElement('canvas'),g=document.createElement('canvas');r.width=g.width=22;r.height=g.height=18;
const x=r.getContext('2d'),y=g.getContext('2d');
WIFI_ROWS.forEach((row,j)=>{for(let i=0;i<row.length;i++){if(row[i]!=='o')continue;
for(let b=0;b<2;b++)for(let a=0;a<2;a++){const px=i*2+a,py=j*2+b;
x.fillStyle=(a===0&&b===0)?'#5a423a':(a+b===2)?'#20140f':'#3b2a26';x.fillRect(px,py,1,1);
if((px*7+py*13)%9===0||(j===8)){x.fillStyle='#ff7a1a';x.fillRect(px,py,1,1);y.fillStyle=(px+py)%2?'#ffb347':'#ff5a1a';y.fillRect(px,py,1,1);}}}});
return {r,g};
})();
/* ---------- pixel font ---------- */
const F={'0':'75557','1':'26227','2':'71747','3':'71317','4':'55711','5':'74717','6':'74757','7':'71222','8':'75757','9':'75717',
A:'25755',B:'65656',C:'34443',D:'65556',E:'74647',F:'74644',G:'34553',H:'55755',I:'72227',J:'11152',K:'55655',L:'44447',M:'57755',N:'65555',O:'25552',P:'65644',Q:'25563',R:'65655',S:'34216',T:'72222',U:'55557',V:'55552',W:'55775',X:'55255',Y:'55222',Z:'71247',
',':'00024',':':'02020','!':'22202','.':'00002','-':'00700','/':'11244',' ':'00000'};
function txt(s,x,y,sc,col,al,out){
s=String(s).toUpperCase();const cw=4*sc,w=s.length*cw-sc;
let x0=al===1?x-w/2:al===2?x-w:x;x0=Math.round(x0);
const pass=(c,o)=>{ctx.fillStyle=c;for(let i=0;i<s.length;i++){const g=F[s[i]]||F[' '];for(let r=0;r<5;r++){const b=+g[r];if(!b)continue;for(let k=0;k<3;k++)if(b&(4>>k))ctx.fillRect(x0+i*cw+k*sc-o,y+r*sc-o,sc+o*2,sc+o*2);}}};
if(out)pass(out,Math.max(1,Math.round(sc/2)));
pass(col,0);
return w;
}
const fmtScore=n=>{const s=String(Math.floor(n)).padStart(7,'0');return s.replace(/\B(?=(\d{3})+(?!\d))/g,',');};
const fmtTime=t=>{t=Math.floor(t);const h=Math.floor(t/3600),m=Math.floor(t%3600/60),s=t%60;return h+':'+String(m).padStart(2,'0')+':'+String(s).padStart(2,'0');};
/* ---------- audio ---------- */
let AC=null,muted=false;
function ac(){try{if(!AC)AC=new(window.AudioContext||window.webkitAudioContext)();if(AC.state==='suspended')AC.resume();}catch(e){}}
function tone(f,d,type,v,f2,delay){if(muted||!AC)return;try{const t=AC.currentTime+(delay||0),o=AC.createOscillator(),g=AC.createGain();o.type=type||'square';o.frequency.setValueAtTime(f,t);if(f2)o.frequency.exponentialRampToValueAtTime(f2,t+d);g.gain.setValueAtTime(v||.06,t);g.gain.exponentialRampToValueAtTime(.0001,t+d);o.connect(g);g.connect(AC.destination);o.start(t);o.stop(t+d+.03);}catch(e){}}
let NB=null;
function noise(d,v,f1,f2,ft){if(muted||!AC)return;try{if(!NB){NB=AC.createBuffer(1,AC.sampleRate*2,AC.sampleRate);const a=NB.getChannelData(0);for(let i=0;i<a.length;i++)a[i]=Math.random()*2-1;}
const t=AC.currentTime,s=AC.createBufferSource(),fl=AC.createBiquadFilter(),g=AC.createGain();s.buffer=NB;fl.type=ft||'bandpass';fl.frequency.setValueAtTime(f1,t);fl.frequency.exponentialRampToValueAtTime(f2,t+d);
g.gain.setValueAtTime(.0001,t);g.gain.exponentialRampToValueAtTime(v,t+d*.25);g.gain.exponentialRampToValueAtTime(.0001,t+d);s.connect(fl);fl.connect(g);g.connect(AC.destination);s.start(t);s.stop(t+d+.05);}catch(e){}}
const sfx={jump:()=>tone(520,.09,'square',.05,800),point:()=>{tone(880,.07,'square',.05);tone(1320,.1,'square',.05,0,.09);},crash:()=>{tone(180,.28,'square',.07,55);noise(.25,.15,1200,200);},
whoosh:()=>noise(.8,.25,2600,300),boom:()=>{noise(1.5,.7,900,50,'lowpass');tone(90,1.1,'sine',.45,28);},thunder:()=>noise(1.2,.25,500,60,'lowpass'),win:()=>[523,659,784,1047].forEach((f,i)=>tone(f,.18,'square',.05,0,i*.13)),
look:()=>tone(330,.12,'square',.035,250),
boing:()=>{tone(260,.16,'sine',.09,560);tone(560,.22,'sine',.07,330,.14);},
roar:()=>{tone(190,1.0,'sawtooth',.14,48);tone(96,1.1,'sawtooth',.12,36);noise(.9,.3,900,120,'lowpass');},
vanish:()=>{tone(1100,.3,'square',.04,180);noise(.35,.12,3200,700);},
xboom:()=>{if(muted||!AC)return;try{if(!NB){NB=AC.createBuffer(1,AC.sampleRate*2,AC.sampleRate);const a=NB.getChannelData(0);for(let i=0;i<a.length;i++)a[i]=Math.random()*2-1;}
const t=AC.currentTime,s=AC.createBufferSource(),fl=AC.createBiquadFilter(),g=AC.createGain();s.buffer=NB;fl.type='lowpass';fl.frequency.setValueAtTime(5000,t);fl.frequency.exponentialRampToValueAtTime(90,t+1.3);
g.gain.setValueAtTime(1.1,t);g.gain.exponentialRampToValueAtTime(.25,t+.25);g.gain.exponentialRampToValueAtTime(.0001,t+1.4);s.connect(fl);fl.connect(g);g.connect(AC.destination);s.start(t);s.stop(t+1.45);
const o=AC.createOscillator(),og=AC.createGain();o.type='sine';o.frequency.setValueAtTime(120,t);o.frequency.exponentialRampToValueAtTime(30,t+.7);og.gain.setValueAtTime(.0001,t);og.gain.exponentialRampToValueAtTime(.9,t+.01);og.gain.exponentialRampToValueAtTime(.0001,t+.9);o.connect(og);og.connect(AC.destination);o.start(t);o.stop(t+.95);}catch(e){}
tone(220,.25,'square',.09,50);noise(.5,.35,2400,400);},
mega:()=>{sfx.boom();noise(2.6,.9,600,30,'lowpass');tone(55,2.2,'sine',.5,18);tone(140,.5,'square',.08,40);}};
let rumbleN=null;
function rumble(dur){if(muted||!AC)return;try{
if(!NB){NB=AC.createBuffer(1,AC.sampleRate*2,AC.sampleRate);const a=NB.getChannelData(0);for(let i=0;i<a.length;i++)a[i]=Math.random()*2-1;}
const t=AC.currentTime,o=AC.createOscillator(),o2=AC.createOscillator(),n=AC.createBufferSource(),fl=AC.createBiquadFilter(),g=AC.createGain();
o.type='sawtooth';o.frequency.setValueAtTime(30,t);o.frequency.exponentialRampToValueAtTime(72,t+dur);
o2.type='sine';o2.frequency.setValueAtTime(42,t);o2.frequency.exponentialRampToValueAtTime(95,t+dur);
n.buffer=NB;n.loop=true;fl.type='lowpass';fl.frequency.setValueAtTime(180,t);fl.frequency.exponentialRampToValueAtTime(900,t+dur);
g.gain.setValueAtTime(.0001,t);g.gain.exponentialRampToValueAtTime(.35,t+dur);g.gain.exponentialRampToValueAtTime(.0001,t+dur+.08);
o.connect(fl);o2.connect(fl);n.connect(fl);fl.connect(g);g.connect(AC.destination);
[o,o2,n].forEach(s=>{s.start(t);s.stop(t+dur+.12);});rumbleN=g;}catch(e){}}
function stopRumble(){if(rumbleN&&AC){try{const t=AC.currentTime;rumbleN.gain.cancelScheduledValues(t);rumbleN.gain.setValueAtTime(.0001,t);}catch(e){}}rumbleN=null;}
let ambN=null,CB=null;
function ambStart(){if(!AC||ambN)return;try{
if(!NB){NB=AC.createBuffer(1,AC.sampleRate*2,AC.sampleRate);const a=NB.getChannelData(0);for(let i=0;i<a.length;i++)a[i]=Math.random()*2-1;}
if(!CB){const sr=AC.sampleRate;CB=AC.createBuffer(1,sr*3,sr);const a=CB.getChannelData(0);for(let k=0;k<a.length;k++)if(Math.random()<.0009){const len=ri(20,300),amp=rnd(.2,1);for(let q=0;q<len&&k+q<a.length;q++)a[k+q]+=(Math.random()*2-1)*amp*Math.pow(1-q/len,2);}}
const t=AC.currentTime,m=AC.createGain();m.gain.setValueAtTime(0,t);m.gain.linearRampToValueAtTime(muted?0:1,t+.6);
const n=AC.createBufferSource(),lp=AC.createBiquadFilter(),rg=AC.createGain(),lfo=AC.createOscillator(),lg=AC.createGain();
n.buffer=NB;n.loop=true;lp.type='lowpass';lp.frequency.value=120;rg.gain.value=.45;lfo.frequency.value=.13;lg.gain.value=.18;
const o=AC.createOscillator(),og=AC.createGain();o.type='sine';o.frequency.value=41;og.gain.value=.04;
const cs=AC.createBufferSource(),hp=AC.createBiquadFilter(),cg=AC.createGain();cs.buffer=CB;cs.loop=true;hp.type='highpass';hp.frequency.value=700;cg.gain.value=.12;
n.connect(lp);lp.connect(rg);rg.connect(m);lfo.connect(lg);lg.connect(rg.gain);o.connect(og);og.connect(m);cs.connect(hp);hp.connect(cg);cg.connect(m);m.connect(AC.destination);
[n,lfo,o,cs].forEach(s=>s.start(t));ambN={m,src:[n,lfo,o,cs]};}catch(e){}}
function ambStop(){if(!ambN)return;try{const t=AC.currentTime,g=ambN.m.gain;g.cancelScheduledValues(t);g.setValueAtTime(g.value,t);g.linearRampToValueAtTime(0,t+.15);ambN.src.forEach(s=>s.stop(t+.2));}catch(e){}ambN=null;}
function ambMute(){if(!ambN)return;try{const t=AC.currentTime,g=ambN.m.gain;g.cancelScheduledValues(t);g.setValueAtTime(muted?0:1,t);}catch(e){}}
muteBtn.addEventListener('click',e=>{e.stopPropagation();try{window.focus();}catch(_){}muted=!muted;muteBtn.textContent=muted?'๐':'๐';muteBtn.blur();ambMute();});
/* ---------- state ---------- */
let inv=0;const hi=[0,0,0];
/* EASY lives, drawn as pixel hearts in the HUD */
const LIVES=10;let lives=LIVES;
const HEART_ROWS=[".kk.kk.","kwrkrrk","krrrrrk",".krrrk.","..krk..","...k..."];
const HEART=spr(HEART_ROWS,{k:'#14210a',r:'#ff3b3b',w:'#ffb3b3'}),HEART_E=spr(HEART_ROWS,{k:'#14210a',r:'#8f8f8f',w:'#8f8f8f'});
let state='start',paused=false,score=0,playTime=0,cycleT=0,tier=0,speed=6,eff=6,boostKey=false,boostTouch=false,duckKey=false,duckTouch=false,jumpHeld=false;
let obstacles=[],parts=[],clouds=[],meteors=[],nextIn=90,gOff=0,bOff=0,hOff=0,T=0,wipe=1,banner=null,blinkT=0,lastMile=0,overAt=0,winAt=0;
let flash=0,shake=0,lightT=5,bolt=null,ct=0,nightForce=0,csFired={},connA=0,speedLines=[],rings=[],superOn=false,boostMix=0;
/* ULTRA SPEED: Shift keys tracked one by one (both = ultra); โก pointers counted (2+ = ultra) */
let shiftL=false,shiftR=false,ultraOn=false,ultraMix=0;const bPtrs=new Set();
function syncShift(){boostKey=shiftL||shiftR;}
function syncBoostBtn(){boostTouch=bPtrs.size>0;boostBtn.classList.toggle('on',boostTouch);boostBtn.classList.toggle('ultra',bPtrs.size>=2);}
function clearShift(){shiftL=shiftR=false;syncShift();}
/* dino explosion: xb holds its clock; XB is how much later every beat after it runs */
let xb=null;const XB=.4;
function hn(i,j,k){const s=Math.sin(i*127.1+j*311.7+k*74.7)*43758.5453;return s-Math.floor(s);}
const d={y:0,vy:0,air:false,released:true,anim:0,sq:0,dustT:0,look:0,gone:false};
for(let i=0;i<4;i++)clouds.push({x:rnd(0,W),y:rnd(12,70)});
let doom=0,doomOn=false,ash=[],smoke=[],dclouds=[];
for(let i=0;i<5;i++)dclouds.push({x:rnd(0,W),y:rnd(6,72),v:rnd(.08,.2)});
function doomBegin(){if(doomOn)return;doomOn=true;ash=[];smoke=[];
for(let i=0;i<60;i++)ash.push({x:rnd(0,W),y:rnd(0,GY+20),vx:rnd(-.5,-.1),vy:rnd(.2,.6),e:Math.random()<.45,l:rnd(0,3),m:rnd(5,8),ph:rnd(0,6)});
ac();ambStart();}
function resetRun(){score=0;playTime=0;cycleT=0;tier=0;speed=LV[lvl].s0;eff=speed;boostMix=0;ultraMix=0;inv=0;lives=LIVES;obstacles=[];parts=[];meteors=[];rings=[];nextIn=lvl===0?150:90;banner=null;blinkT=0;lastMile=0;flash=0;shake=0;lightT=5;bolt=null;nightForce=0;connA=0;csFired={};stopRumble();xb=null;
doom=0;doomOn=false;ash=[];smoke=[];ambStop();
Object.assign(d,{y:0,vy:0,air:false,released:true,anim:0,sq:0,look:0,gone:false});againBtn.style.display='none';}
function startRun(){resetRun();state='run';wipe=0;}
function restart(){resetRun();state='run';}
function doJump(){if(d.air)return;d.air=true;d.vy=LV[lvl].jv;d.released=false;jumpHeld=true;d.sq=1;sfx.jump();}
function crash(){state='over';overAt=performance.now();hi[lvl]=Math.max(hi[lvl],score);sfx.crash();shake=.4;}
/* EASY: a hit costs a life; the cactus pops away in teal pixels, the dino blinks and the run goes on. The last life ends the run */
function popCactus(o){
lives=Math.max(0,lives-1);
if(lives<=0){crash();return;}
for(const p of o.ps){const s=p.s,x=o.x+p.dx,y=p.y,w=s.width*2,h=s.height*2;
for(let i=0;i<28;i++)parts.push({x:x+rnd(0,w),y:y+rnd(0,h),vx:rnd(-1,2.6),vy:rnd(-3.6,-1),l:0,m:rnd(.5,1),s:pick([2,2,3]),c:pick(['#0f9d8a','#6ff0d6','#0a6b5e','#1fb5a8']),g:.15});}
obstacles=obstacles.filter(q=>q!==o);inv=1;sfx.boing();
}
function startCutscene(){state='cutscene';ct=0;csFired={};obstacles.forEach(o=>{for(let i=0;i<8;i++)puff(o.x+o.w/2+rnd(-10,10),GY-10-rnd(0,20),'#d9b98c');});obstacles=[];inv=0;hi[lvl]=Math.max(hi[lvl],score);}
function toWin(){state='win';winAt=performance.now();d.gone=true;meteors=[];stopRumble();score=Math.floor(WIN_T*300);hi[lvl]=Math.max(hi[lvl],score);nightForce=1;againBtn.style.display='block';sfx.win();doomBegin();}
function puff(x,y,c,vx,vy,life,sz){parts.push({x,y,vx:vx!==undefined?vx:rnd(-.6,.6),vy:vy!==undefined?vy:rnd(-.8,-.2),l:0,m:life||rnd(.3,.6),s:sz||rnd(2,4),c,g:0});}
/* ---------- input ---------- */
function press(){
ac();const now=performance.now();
if(paused){paused=false;return;}
if(state==='start'){startRun();doJump();}
else if(state==='run')doJump();
else if(state==='over'){if(now-overAt>600)restart();}
else if(state==='cutscene'){if(ct>2)toWin();}
else if(state==='win'){if(now-winAt>800)restart();}
}
function release(){jumpHeld=false;}
/* level picker */
const levelsEl=document.getElementById('levels'),lvBtns=[...levelsEl.querySelectorAll('button')];
const pickable=()=>state==='start'||state==='over'||state==='win';
function setLevel(i){lvl=i;lives=LIVES;lvBtns.forEach((b,j)=>b.setAttribute('aria-pressed',j===i?'true':'false'));document.body.classList.toggle('easy',i===0);}
lvBtns.forEach(b=>{
b.addEventListener('pointerdown',e=>{e.stopPropagation();});
['touchstart','touchend'].forEach(t=>b.addEventListener(t,e=>e.stopPropagation(),{passive:true}));
b.addEventListener('click',e=>{e.preventDefault();e.stopPropagation();ac();if(pickable())setLevel(+b.dataset.l);grabFocus();});
});
let lvUI=null;
function syncUI(){const v=pickable();if(v!==lvUI){lvUI=v;levelsEl.style.visibility=v?'visible':'hidden';}}
const LVK={Digit1:0,Digit2:1,Digit3:2,Numpad1:0,Numpad2:1,Numpad3:2};
const NOJ=['Tab','ShiftLeft','ShiftRight','ControlLeft','ControlRight','AltLeft','AltRight','MetaLeft','MetaRight','CapsLock'];
document.addEventListener('keydown',e=>{const k=e.code;
if(e.target&&e.target.closest&&e.target.closest('#levels')&&(k==='Space'||k==='Enter'||k==='NumpadEnter'))return;
if(k in LVK&&pickable()){e.preventDefault();if(!e.repeat)setLevel(LVK[k]);return;}
if(lvl===0&&(state==='run'||state==='start')&&!NOJ.includes(k)&&!e.ctrlKey&&!e.metaKey&&!e.altKey){
if(['Space','ArrowUp','ArrowDown','ArrowLeft','ArrowRight','KeyW','KeyS','Enter'].includes(k))e.preventDefault();
if(!e.repeat)press();return;}
if(['Space','ArrowUp','ArrowDown','KeyW','KeyS'].includes(k))e.preventDefault();
if(k==='Space'||k==='ArrowUp'||k==='KeyW'){if(!e.repeat)press();}
else if(k==='Enter'){if(state==='over'||state==='win'||paused)press();}
else if(k==='ArrowDown'||k==='KeyS'){ac();duckKey=true;if(paused)paused=false;}
else if(k==='ShiftLeft'){shiftL=true;syncShift();}
else if(k==='ShiftRight'){shiftR=true;syncShift();}
else if(!e.shiftKey&&boostKey)clearShift();
},{capture:true});
document.addEventListener('keyup',e=>{const k=e.code;
if(lvl===0&&!NOJ.includes(k))release();
if(k==='Space'||k==='ArrowUp'||k==='KeyW')release();
else if(k==='ArrowDown'||k==='KeyS')duckKey=false;
else if(k==='ShiftLeft'){shiftL=false;syncShift();}
else if(k==='ShiftRight'){shiftR=false;syncShift();}
/* no Shift held per the event: drop any Shift whose keyup was missed */
if(!e.shiftKey&&boostKey)clearShift();
},{capture:true});
function grabFocus(){try{window.focus();}catch(_){}try{cv.focus({preventScroll:true});}catch(_){}}
addEventListener('blur',()=>{clearShift();bPtrs.clear();syncBoostBtn();duckKey=duckTouch=false;jumpHeld=false;});
let ptr=null;
cv.addEventListener('pointerdown',e=>{e.preventDefault();ptr={id:e.pointerId,y:e.clientY,ducked:false};grabFocus();press();});
document.body.addEventListener('pointerdown',e=>{const t=e.target;if(t===cv||(t.closest&&t.closest('button')))return;grabFocus();if(state==='start'||state==='over'||(state==='run'&&lvl===0))press();});
addEventListener('pointermove',e=>{if(ptr&&e.pointerId===ptr.id&&!ptr.ducked&&e.clientY-ptr.y>24){ptr.ducked=true;duckTouch=true;jumpHeld=false;}});
const pend=e=>{if(ptr&&e.pointerId===ptr.id){release();duckTouch=false;ptr=null;}};
addEventListener('pointerup',pend);addEventListener('pointercancel',pend);
cv.addEventListener('touchstart',e=>e.preventDefault(),{passive:false});
cv.addEventListener('touchmove',e=>e.preventDefault(),{passive:false});
cv.addEventListener('contextmenu',e=>e.preventDefault());
const bOn=e=>{e.preventDefault();e.stopPropagation();try{window.focus();}catch(_){}ac();bPtrs.add(e.pointerId);syncBoostBtn();try{boostBtn.setPointerCapture(e.pointerId);}catch(_){}};
const bOff2=e=>{e.stopPropagation();bPtrs.delete(e.pointerId);syncBoostBtn();};
boostBtn.addEventListener('pointerdown',bOn);
['pointerup','pointercancel','lostpointercapture','pointerleave'].forEach(t=>boostBtn.addEventListener(t,bOff2));
['touchstart','touchend','click','contextmenu'].forEach(t=>boostBtn.addEventListener(t,e=>{e.preventDefault();e.stopPropagation();},{passive:false}));
againBtn.addEventListener('pointerdown',e=>{e.stopPropagation();try{window.focus();}catch(_){}});
againBtn.addEventListener('click',e=>{e.stopPropagation();try{window.focus();}catch(_){}ac();restart();});
grabFocus();
document.addEventListener('visibilitychange',()=>{clearShift();bPtrs.clear();syncBoostBtn();if(document.hidden&&(state==='run'||state==='cutscene'))paused=true;});
/* ---------- spawning ---------- */
function makeCluster(){
const dist=[[.5,.35,.15],[.3,.4,.3],[.25,.4,.35]][tier];
const r=Math.random(),n=r<dist[0]?1:r<dist[0]+dist[1]?2:3,big=Math.random()<.45;
const ps=[],boxes=[];let x=0;
for(let i=0;i<n;i++){
const s=(!big&&Math.random()<.22)?pick(SQUAT):pick(big?LARGE:SMALL);
const y=GY+2-s.height*2;ps.push({s,dx:x,y});
s.boxes.forEach(b=>boxes.push({x:x+b.x,y:y+b.y,w:b.w,h:b.h}));
x+=s.width*2-6;
}
return {k:'c',x:W+10,w:x+6,ps,boxes};
}
function makeBird(){const lv=pick([GY-26,GY-50,GY-80]);return {k:'b',x:W+10,y:lv,w:52,fl:rnd(0,1)};}
function makeTiny(){const s=pick(TINY),y=GY+2-s.height*2;return {k:'c',x:W+10,w:s.width*2,ps:[{s,dx:0,y}],boxes:s.boxes.map(b=>({x:b.x,y:y+b.y,w:b.w,h:b.h}))};}
function spawn(){
if(lvl===0){obstacles.push(makeTiny());nextIn=rnd(155,240);return;}
const pB=playTime>=LV[lvl].bird?[.22,.38,.45][tier]:0;
const o=Math.random()<pB?makeBird():makeCluster();obstacles.push(o);
const gf=[1,.9,.82][tier];
let base=(o.w+24)/eff+32*gf;
if(tier===2&&Math.random()<.25)base=(o.w+24)/eff+26;
nextIn=base*(1+Math.random()*.7);
}
/* ---------- update ---------- */
function dinoPhys(f){
if(d.air){
const fast=(duckKey||duckTouch)&&state==='run';
d.vy+=LV[lvl].g*(fast?3:1)*f;d.y+=d.vy*f;
if(!jumpHeld&&!d.released){if(d.y<-30){d.released=true;if(d.vy<-5)d.vy=-5;}}
if(d.y>=0){d.y=0;d.vy=0;d.air=false;d.sq=-1;for(let i=0;i<6;i++)puff(50+rnd(4,36),GY,'#e3c79a',rnd(-1.2,1.2),rnd(-.9,-.2));}
}
d.sq*=Math.pow(.82,f);if(Math.abs(d.sq)<.02)d.sq=0;
}
function hit(o){
const ducking=!d.air&&(duckKey||duckTouch);
const db=ducking?DB_DUCK:DB_STAND,dh=ducking?20:38,dx=50,dy=GY+1-dh+d.y;
const ob=o.k==='c'?o.boxes:BB[Math.floor(o.fl*2)%2].map(b=>({x:b.x,y:o.y+b.y,w:b.w,h:b.h}));
for(const a of db)for(const b of ob){
const ax=dx+a.x,ay=dy+a.y,bx=o.x+b.x,by=b.y;
if(ax<bx+b.w&&ax+a.w>bx&&ay<by+b.h&&ay+a.h>by)return true;
}
return false;
}
function update(f,sec){
T+=sec;
for(const p of parts){p.l+=sec;p.x+=p.vx*f;p.y+=p.vy*f;p.vy+=p.g*f;}
parts=parts.filter(p=>p.l<p.m);
if(flash>0)flash=Math.max(0,flash-sec*2.2);
if(shake>0)shake=Math.max(0,shake-sec);
for(const r of rings){r.r+=sec*420;r.a-=sec*.9;}
rings=rings.filter(r=>r.a>0);
if(wipe<1)wipe=Math.min(1,wipe+sec/.55);
if(state==='run'){
playTime+=sec;cycleT+=sec*(tier===2?2:1);
const L=LV[lvl],nt=playTime>=L.st[1]?2:playTime>=L.st[0]?1:0;
if(nt!==tier){tier=nt;banner={t:nt===1?'SPEED UP!':'MAX SPEED!',a:2};sfx.point();}
const cap=L.caps[tier];if(speed<cap)speed=Math.min(cap,speed+L.acc*f);
/* SUPER SPEED: max(2 ร speed, 16) on every level, easing up over 0.2 s and back down over 0.3 s.
EASY's jump needs no change: at boosted speed the cactus passes faster, so the timing window to clear it only widens */
const boost=boostKey||boostTouch;superOn=boost;
boostMix=boost?Math.min(1,boostMix+sec/.2):Math.max(0,boostMix-sec/.3);
const bm=boostMix*boostMix*(3-2*boostMix);eff=speed+(Math.max(2*speed,16)-speed)*bm;
/* ULTRA SPEED: both Shifts, or 2+ fingers on โก = max(3 ร speed, 24), same 0.2 s up / 0.3 s down; letting go of one falls back to super.
EASY's jump was checked against the hitboxes at 24: a 0.4 s window to start the jump clears a small cactus, so jump velocity is unchanged */
const ultra=boost&&((shiftL&&shiftR)||bPtrs.size>=2);ultraOn=ultra;
ultraMix=ultra?Math.min(1,ultraMix+sec/.2):Math.max(0,ultraMix-sec/.3);
const um=ultraMix*ultraMix*(3-2*ultraMix);eff+=(Math.max(3*speed,24)-Math.max(2*speed,16))*um;
/* EASY stays fair while boosting: cacti at least 1.2 s apart at the current speed.
A too-close one still off screen waits further back; one already on screen sinks into the sand */
if(lvl===0){const gap=72*eff;
for(let i=1;i<obstacles.length;i++){const a=obstacles[i-1],o=obstacles[i];
if(o.x-a.x<gap){if(o.x>W)o.x=a.x+gap;else{for(let j=0;j<8;j++)puff(o.x+o.w/2+rnd(-10,10),GY-10-rnd(0,20),'#d9b98c');obstacles.splice(i,1);i--;}}}}
if(inv>0)inv=Math.max(0,inv-sec);
score=Math.floor(playTime*300);
const mile=Math.floor(score/30000);if(mile>lastMile){lastMile=mile;blinkT=1;sfx.point();}
if(playTime>=WIN_T){playTime=WIN_T;score=WIN_T*300;startCutscene();return;}
gOff+=eff*f;bOff+=eff*.45*f;hOff+=eff*.12*f;
nextIn-=f;if(nextIn<=0)spawn();
for(const o of obstacles){o.x-=(o.k==='b'?eff+.6:eff)*f;if(o.k==='b')o.fl+=sec*3.2;}
obstacles=obstacles.filter(o=>o.x+o.w>-20);
dinoPhys(f);
if(!d.air){d.anim+=f*Math.min(1.8,.55+eff/14);d.dustT-=f;if(d.dustT<=0){d.dustT=7;puff(54,GY,'#e3c79a',-rnd(.6,1.4),rnd(-.5,-.1),.35,2);}}
if(ultra){for(let n=0;n<3;n++)if(Math.random()<.9*f)speedLines.push({x:rnd(0,W*.6),y:GY-rnd(2,120)+d.y*.3,l:rnd(40,110),a:1,c:pick(['77,252,255','255,77,240','77,252,255','255,77,240','255,255,255'])});}
else if(boost&&Math.random()<.85*f)speedLines.push({x:rnd(0,46),y:GY-rnd(4,36)+d.y,l:rnd(16,40),a:1,c:Math.random()<.45?'255,225,77':'255,255,255'});
for(const o of obstacles){if(o.x<110&&o.x+o.w>40&&hit(o)){if(lvl===0){if(inv<=0)popCactus(o);}else crash();break;}}
if(tier===2){lightT-=sec;if(lightT<=0){lightT=rnd(3,9);flash=.7;bolt={x:rnd(60,540),a:.25,seed:Math.random()};sfx.thunder();}}
if(blinkT>0)blinkT=Math.max(0,blinkT-sec);
if(banner){banner.a-=sec;if(banner.a<=0)banner=null;}
}else if(state==='cutscene'){
ct+=sec;nightForce=Math.min(1,ct/2.5);
const sp=Math.max(0,1-ct/1.6);eff=speed*sp;
gOff+=eff*f;bOff+=eff*.45*f;hOff+=eff*.12*f;
if(sp>.1)d.anim+=f*.6*sp;
if(ct>5.6)d.look=Math.max(0,d.look-sec*5);else if(ct>1.8)d.look=Math.min(1,d.look+sec*3);
dinoPhys(f);
const fire=(k,t,fn)=>{if(ct>=t&&!csFired[k]){csFired[k]=1;fn();}};
[2.8,3.4,4.0,4.5,5.0].forEach((t,i)=>fire('s'+i,t,()=>{meteors.push({x:rnd(320,720),y:-20,vx:-rnd(4,5.5),vy:rnd(3,4),sz:1.5,big:false});sfx.whoosh();}));
/* dino moment: look down, roar, flicker, vanish */
fire('down',5.6,()=>sfx.look());
fire('roar',6.4,()=>{sfx.roar();shake=.6;for(let i=0;i<14;i++)puff(86,GY-24,pick(['#ffffff','#dfe6f5','#cdd3da']),rnd(1,3.5),rnd(-2,.3),rnd(.3,.7),rnd(2,3));});
if(ct>6.4&&ct<7.3)shake=Math.max(shake,.3);
fire('vanish',8.4,()=>{d.gone=true;sfx.xboom();xb={t:0};flash=.35;shake=.6;
const cx=72,cy=GY-19;
/* grey dino chunks, three per body pixel */
DT.concat(LEGS[0]).forEach((row,j)=>{for(let i=0;i<row.length;i++){const ch=row[i];if(ch==='.'||ch==='k'||ch==='w')continue;
for(let n=0;n<3;n++){const a=rnd(0,Math.PI*2),v=rnd(2,7.5);
parts.push({x:50+i*2,y:GY+1-38+j*2,vx:Math.cos(a)*v+(i-11)*.12,vy:Math.sin(a)*v-rnd(1.5,4),l:0,m:rnd(1,2.2),s:pick([2,2,3,4]),c:pick(['#9aa1ab','#626a75','#cdd3da','#7d848e']),g:.16});}}});
/* a few chunks arcing high across the screen */
for(let i=0;i<28;i++)parts.push({x:cx+rnd(-14,14),y:cy+rnd(-12,12),vx:rnd(-5,9),vy:rnd(-12,-8),l:0,m:rnd(2.4,3.2),s:pick([3,4]),c:pick(['#9aa1ab','#626a75','#cdd3da']),g:.17});
/* fiery sparks */
for(let i=0;i<220;i++){const a=rnd(0,Math.PI*2),v=rnd(3,11);
parts.push({x:cx+rnd(-8,8),y:cy+rnd(-8,8),vx:Math.cos(a)*v,vy:Math.sin(a)*v-rnd(0,3),l:0,m:rnd(.4,1.2),s:pick([2,2,3]),c:pick(['#ffffff','#fff3b0','#ffde59','#ffb02e','#ff8c1a','#ff3d1a']),g:.09});}
/* sunglasses pixels, tumbling */
for(let i=0;i<6;i++)parts.push({x:76+rnd(0,10),y:GY-30+rnd(0,4),vx:rnd(-3,5),vy:rnd(-10,-6),l:0,m:rnd(2.2,3),s:3,c:'#15161a',g:.18,tb:rnd(0,6)});});
if(xb){xb.t+=sec;
if(xb.t>.45&&xb.t<1.3&&Math.random()<.8*f)puff(72+rnd(-34,34),GY-19-rnd(0,50),pick(['#3a3448','#4b4458','#5c5568','#6e6878']),rnd(-.6,.6),-rnd(.4,1.2),rnd(.9,1.6),rnd(4,7));
if(xb.t>2.4)xb=null;}
/* mega meteoroid */
fire('mega',9.2+XB,()=>{meteors.push({mega:true,t:0,sx:W+90,sy:-200,tx:80,ty:GY-40,x:W+90,y:-200});rumble(1.6);});
for(const m of meteors){
if(m.mega){m.t+=sec/1.6;const u=Math.min(1,m.t),e=Math.pow(u,1.5);m.x=m.sx+(m.tx-m.sx)*e;m.y=m.sy+(m.ty-m.sy)*e;
if(!m.hit){for(let i=0;i<5;i++)puff(m.x+rnd(-45,45),m.y+rnd(-35,35),pick(['#fff3b0','#ffde59','#ff8c1a','#ff3d1a']),rnd(1,3.5),rnd(-2.2,-.4),rnd(.3,.8),rnd(2,5));
if(Math.random()<.5*f)parts.push({x:m.x+rnd(-30,30),y:m.y+rnd(-20,30),vx:rnd(-1,3),vy:rnd(-1,2),l:0,m:rnd(.4,.9),s:2,c:'#fff3b0',g:.06});}
if(u>=1&&!m.hit){m.hit=true;flash=1.5;shake=1.8;stopRumble();sfx.mega();doomBegin();
rings.push({x:m.tx,r:6,a:1,w:4},{x:m.tx,r:-70,a:1.1,w:2});
for(let i=0;i<70;i++)parts.push({x:m.tx+rnd(-20,20),y:GY-rnd(0,6),vx:rnd(-5,5),vy:rnd(-7,-1.5),l:0,m:rnd(.8,1.8),s:rnd(2,5),c:pick(['#6e4523','#9c6b3c','#5a3a1e','#8a8a8a','#5fd35f']),g:.2});
for(let i=0;i<60;i++)puff(m.tx+rnd(-24,24),GY-rnd(0,14),pick(['#ffffff','#fff3b0','#ffde59','#ff8c1a','#ff3d1a']),rnd(-6,6),rnd(-6,-.5),rnd(.5,1.4),rnd(3,6));
for(let i=0;i<24;i++)puff(m.tx+rnd(-40,40),GY-rnd(0,20),pick(['#3a3448','#4b4458','#5c5568']),rnd(-1.2,1.2),rnd(-1.2,-.3),rnd(1.2,2.2),rnd(4,7));}}
else{m.x+=m.vx*f;m.y+=m.vy*f;
if(!m.hit)puff(m.x+rnd(0,6)*m.sz,m.y+rnd(0,4)*m.sz,pick(['#ffde59','#ff8c1a','#ff3d1a']),rnd(.5,1.5),rnd(-1,-.3),rnd(.25,.6),rnd(2,3));}
}
meteors=meteors.filter(m=>!m.mega?m.y<GY:!m.hit);
if(ct>11.6+XB)connA=Math.min(1,connA+sec);
if(ct>=12.9+XB)toWin();
}else if(state==='win'){
for(let i=0;i<2;i++){const p={x:rnd(0,W),y:-4,vx:rnd(-.6,.6),vy:rnd(.8,1.8),l:0,m:4,s:rnd(2,3),c:pick(['#ff5a8a','#ffd23f','#7dff8a','#5ec8ff','#c08bff','#ff9a1f']),g:.01};parts.push(p);}
}
if(doomOn){
if(flash<.3)doom=Math.min(1,doom+sec/.6);
if(ash.length<130&&Math.random()<.9*f)ash.push({x:rnd(0,W+40),y:-4,vx:rnd(-.5,-.1),vy:rnd(.2,.6),e:Math.random()<.45,l:0,m:rnd(5,8),ph:rnd(0,6)});
if(ash.length<140&&Math.random()<.15*f)ash.push({x:rnd(50,102),y:GY-2,vx:rnd(-.3,.3),vy:-rnd(.4,1),e:true,l:0,m:rnd(1,2),ph:rnd(0,6)});
for(const a of ash){a.l+=sec;a.x+=(a.vx+Math.sin(T*2+a.ph)*.2)*f;a.y+=a.vy*f;}
ash=ash.filter(a=>a.l<a.m&&a.y<GY+30&&a.x>-10);
if(smoke.length<220){
if(Math.random()<.35*f)smoke.push({x:76+rnd(-14,14),y:GY-4,vx:rnd(-.35,-.05),vy:-rnd(.35,.6),l:0,m:rnd(2.5,4),s:rnd(3,5),hot:true});
for(let i=0;i<Math.min(3,D_FIRES.length);i++){const fp=D_FIRES[i];if(Math.random()<.1*f)smoke.push({x:fp.x+rnd(-2,2),y:fp.y-4,vx:rnd(-.3,-.05),vy:-rnd(.3,.5),l:0,m:rnd(3,4.5),s:rnd(2,4),hot:false});}
for(const st of D_STUMP_AT){if(Math.random()<.04*f)smoke.push({x:st.x+4,y:GY-20,vx:rnd(-.25,-.05),vy:-rnd(.25,.45),l:0,m:rnd(2,3),s:2,hot:false});}}
for(const p of smoke){p.l+=sec;p.x+=p.vx*f;p.y+=p.vy*f;}
smoke=smoke.filter(p=>p.l<p.m);
for(const c of dclouds){c.x-=c.v*f;if(c.x<-50){c.x=W+rnd(10,80);c.y=rnd(6,72);}}
}
for(const c of clouds){c.x-=(state==='start'?.15:.25+eff*.06)*f;if(c.x<-40){c.x=W+rnd(10,120);c.y=rnd(12,70);}}
for(const s of speedLines){s.x-=eff*.9*f;s.a-=sec*3;}
speedLines=speedLines.filter(s=>s.a>0&&s.x+s.l>-10);
if(bolt){bolt.a-=sec;if(bolt.a<=0)bolt=null;}
}
/* ---------- render ---------- */
const DAYT=hex('#5ec8ff'),DAYB=hex('#dccbff'),NIGT=hex('#0b1033'),NIGB=hex('#3a2a6b'),WARMT=hex('#ff7a59'),WARMB=hex('#ffc2d6');
const HDT=hex('#ff5e3a'),HDB=hex('#ffb36b'),HNT=hex('#2a0d1f'),HNB=hex('#6a2340'),IDT=hex('#4a0f3a'),IDB=hex('#b0304a'),INT=hex('#14051a'),INB=hex('#4a0f2e');
function cyc(){
const p=cycleT%510;let n,warm,su=-1,mu=-1;
if(p<300){n=0;warm=0;}else if(p<315){const u=(p-300)/15;n=u;warm=Math.sin(Math.PI*u);}else if(p<495){n=1;warm=0;}else{const u=(p-495)/15;n=1-u;warm=Math.sin(Math.PI*u);}
if(p>=495)su=(p-495)/15*.12;else if(p<300)su=.12+p/300*.76;else if(p<315)su=.88+(p-300)/15*.12;
if(p>=300&&p<315)mu=(p-300)/15*.12;else if(p>=315&&p<495)mu=.12+(p-315)/180*.76;else if(p>=495)mu=.88+(p-495)/15*.12;
return {n,warm,su,mu};
}
const tileDraw=(img,imgN,off,y,n)=>{const o=Math.round((off%600)/2)*2;for(const x of [-o,600-o]){ctx.drawImage(img,x,y,600,img.height*2);if(n>0){ctx.globalAlpha=n;ctx.drawImage(imgN,x,y,600,img.height*2);ctx.globalAlpha=1;}}};
function drawWorld(){
if(doom<1)drawNormal();
if(doom>0)drawDoom();
}
function drawDoom(){
const A=doom,pl=.5+.5*Math.sin(T*1.6);
ctx.globalAlpha=A;
const g=ctx.createLinearGradient(0,0,0,GY);g.addColorStop(0,'#0e0606');g.addColorStop(.4,'#2e0a0a');g.addColorStop(.75,'#5a1410');g.addColorStop(1,'#4a2416');
ctx.fillStyle=g;ctx.fillRect(0,0,W,GY+4);
for(let i=0;i<STARS.length;i+=2){const s=STARS[i];ctx.fillStyle=`rgba(255,180,150,${A*(.12+.1*Math.sin(T*1.5+s.p))})`;ctx.fillRect(Math.round(s.x),Math.round(s.y*.8),1,1);}
ctx.globalAlpha=A*.75;for(const c of dclouds)ctx.drawImage(SMOKE_C,Math.round(c.x),Math.round(c.y),45,18);
ctx.globalAlpha=A;
ctx.drawImage(D_HILLS,0,GY-58,600,60);
for(const fp of D_FIRES){const fl=Math.sin(T*11+fp.p);
ctx.fillStyle=`rgba(255,90,20,${A*(.12+.05*fl)})`;ctx.fillRect(fp.x-8,fp.y-10,16,12);
for(let k=-1;k<=1;k++){const hh=2*Math.max(1,Math.round(3-Math.abs(k)*1.5+Math.sin(T*14+fp.p+k*2)*1.2));
ctx.fillStyle='#ff3d1a';ctx.fillRect(fp.x+k*2,fp.y-hh,2,hh);
if(hh>2){ctx.fillStyle='#ffb347';ctx.fillRect(fp.x+k*2,fp.y-hh+2,2,hh-2);}}
if(fl>.2){ctx.fillStyle='#fff3b0';ctx.fillRect(fp.x,fp.y-2,2,2);}}
ctx.drawImage(D_RUBBLE,0,GY-14,600,18);
for(const st of D_STUMP_AT){const im=D_STUMPS[st.s];ctx.drawImage(im,st.x,GY+1-im.height*2,im.width*2,im.height*2);}
ctx.drawImage(D_GROUND,0,GY-4,600,34);
ctx.globalAlpha=A*(.3+.5*pl);ctx.drawImage(D_CRACK_A.g,0,GY-4,600,34);
ctx.globalAlpha=A*(.55+.45*pl);ctx.drawImage(D_CRACK_A.c,0,GY-4,600,34);
const pl2=.5+.5*Math.sin(T*1.6+2.4);
ctx.globalAlpha=A*(.3+.5*pl2);ctx.drawImage(D_CRACK_B.g,0,GY-4,600,34);
ctx.globalAlpha=A*(.55+.45*pl2);ctx.drawImage(D_CRACK_B.c,0,GY-4,600,34);
ctx.globalAlpha=A;ctx.drawImage(CRATER,16,GY-8,120,32);
ctx.save();ctx.beginPath();ctx.rect(0,0,W,GY+6);ctx.clip();ctx.translate(76,GY+24);ctx.rotate(-.12);
ctx.drawImage(ROCK.r,-33,-54,66,54);
ctx.shadowColor='#ff5a00';ctx.shadowBlur=(6+6*pl)*S;ctx.globalAlpha=A*(.55+.45*pl);ctx.drawImage(ROCK.g,-33,-54,66,54);
ctx.restore();
ctx.globalAlpha=A;ctx.drawImage(CRATER_LIP,16,GY-8,120,32);
for(const p of smoke){const u=p.l/p.m,sz=Math.round((p.s+p.l*5)/2)*2;
ctx.globalAlpha=A*(1-u)*.5;ctx.fillStyle=p.hot&&p.l<.35?'#7a3418':u<.4?'#2e2a2c':u<.7?'#3c3638':'#4a4446';
ctx.fillRect(Math.round(p.x-sz/2),Math.round(p.y-sz/2),sz,sz);}
for(const a of ash){const fa=Math.min(1,a.l*2)*Math.min(1,(a.m-a.l));
if(a.e){ctx.globalAlpha=A*fa*(.6+.4*Math.sin(T*9+a.ph));ctx.fillStyle=a.ph>3?'#ffb347':'#ff6a1a';}
else{ctx.globalAlpha=A*fa*.7;ctx.fillStyle=a.ph>3?'#8a8280':'#6a6260';}
ctx.fillRect(Math.round(a.x),Math.round(a.y),2,2);}
ctx.globalAlpha=1;
}
function drawNormal(){
const c=cyc();let n=Math.max(c.n,nightForce);const warm=c.warm*(1-nightForce);
let top=mix(mix(DAYT,NIGT,n),WARMT,warm*.75),bot=mix(mix(DAYB,NIGB,n),WARMB,warm*.75);
const tv=state==='win'||state==='cutscene'?2:tier;
if(tv===1){top=mix(top,mix(HDT,HNT,n),.6);bot=mix(bot,mix(HDB,HNB,n),.6);}
else if(tv===2){top=mix(top,mix(IDT,INT,n),.72);bot=mix(bot,mix(IDB,INB,n),.72);}
const g=ctx.createLinearGradient(0,0,0,GY);g.addColorStop(0,rgb(top));g.addColorStop(1,rgb(bot));
ctx.fillStyle=g;ctx.fillRect(0,0,W,GY+4);
for(let i=0;i<STARS.length;i++){const s=STARS[i];const base=Math.max(n,i<6?.35:0);if(base<=0)continue;const a=base*(.55+.45*Math.sin(T*2+s.p));ctx.fillStyle=`rgba(255,255,240,${a})`;ctx.fillRect(Math.round(s.x),Math.round(s.y),s.s,s.s);}
const arcP=u=>[40+520*u,GY-4-118*Math.sin(Math.PI*u)];
if(c.su>=0&&nightForce<1){const [x,y]=arcP(c.su);ctx.globalAlpha=1-nightForce;ctx.fillStyle='rgba(255,220,120,.18)';ctx.beginPath();ctx.arc(x,y,24,0,7);ctx.fill();ctx.drawImage(SUN,Math.round(x-15),Math.round(y-15),30,30);ctx.globalAlpha=1;}
if(c.mu>=0){const [x,y]=arcP(c.mu);ctx.drawImage(MOON,Math.round(x-13),Math.round(y-13),26,26);}
else if(nightForce>0){ctx.globalAlpha=nightForce;ctx.drawImage(MOON,480,24,26,26);ctx.globalAlpha=1;}
if(bolt){ctx.strokeStyle=`rgba(255,255,255,${Math.min(1,bolt.a*4)})`;ctx.lineWidth=2;ctx.beginPath();let x=bolt.x,y=0;ctx.moveTo(x,y);let s=bolt.seed;while(y<GY-30){s=(s*9301+49297)%233280/233280;x+=(s-.5)*24;y+=12;ctx.lineTo(Math.round(x),y);}ctx.stroke();}
ctx.globalAlpha=1-n*.5;for(const cl of clouds)ctx.drawImage(CLOUD,Math.round(cl.x),Math.round(cl.y),28,10);ctx.globalAlpha=1;
tileDraw(HILLS,HILLS_N,hOff,GY-58,n);
tileDraw(BUSH,BUSH_N,bOff,GY-14,n);
tileDraw(GROUND,GROUND_N,gOff,GY-4,n);
}
function drawDino(){
if(d.gone)return;
const ducking=state==='run'&&!d.air&&(duckKey||duckTouch);
let img,pose=null,rot=d.look>0?-.32*d.look:0;
if(state==='cutscene'){
if(ct>=7.4){const u=Math.min(1,ct-7.4),ph=3*u+9*u*u;if(Math.floor(ph*2)%2===1)return;}
else if(ct>=6.4){pose=D_ROAR;rot=-.2+Math.sin(T*60)*.03;}
else if(ct>=5.6)pose=D_DOWN;
}
if(pose)img=pose;
else if(state==='over')img=D_CRASH;
else if(ducking)img=D_DUCK[Math.floor(d.anim/5)%2];
else if(d.air||state==='win'||(state==='cutscene'&&eff<.8))img=D_STAND;
else img=D_RUN[Math.floor(d.anim/5)%4];
const w=img.width*2,h=img.height*2,x=50,y=GY+1-h+d.y;
const ht=-d.y,sw=Math.max(8,18-ht/7);
ctx.fillStyle=`rgba(20,10,30,${Math.max(.08,.28-ht/400)})`;ctx.beginPath();ctx.ellipse(x+w*.45,GY+2,sw,3,0,0,7);ctx.fill();
const sq=d.sq,sx=1-sq*.12,sy=1+sq*.16;
ctx.save();ctx.translate(x+w/2,y+h);
if(inv>0&&state==='run'&&Math.floor(inv*10)%2===0)ctx.globalAlpha=.3;
if(rot)ctx.rotate(rot);
ctx.scale(sx,sy);ctx.drawImage(img,-w/2,-h,w,h);ctx.restore();
}
function drawObstacles(){
const nl=state==='start'?0:Math.max(cyc().n,nightForce);
for(const o of obstacles){
if(o.k==='c')for(const p of o.ps){const s=p.s,x=Math.round(o.x+p.dx)-2,y=p.y-2,w=s.o.width*2,h=s.o.height*2;
ctx.drawImage(s.o,x,y,w,h);
if(nl>0){ctx.globalAlpha=nl;ctx.drawImage(s.lt,x,y,w,h);ctx.globalAlpha=1;}}
else{const b=BIRD[Math.floor(o.fl*2)%2];ctx.drawImage(b,Math.round(o.x),o.y,52,30);}
}
}
function drawParts(){for(const p of parts){ctx.globalAlpha=Math.max(0,1-p.l/p.m);ctx.fillStyle=p.c;
if(p.tb!==undefined){const ph=Math.floor(p.l*14+p.tb)%4,x=Math.round(p.x),y=Math.round(p.y);
if(ph%2===0)ctx.fillRect(x-p.s,y,p.s*2,p.s);else ctx.fillRect(x,y-p.s,p.s,p.s*2);
if(ph===0){ctx.fillStyle='#ffffff';ctx.fillRect(x,y,1,1);}continue;}
ctx.fillRect(Math.round(p.x),Math.round(p.y),p.s,p.s);}ctx.globalAlpha=1;}
function drawBlast(){if(!xb||state!=='cutscene')return;const t=xb.t,cx=72,cy0=GY-19;
/* white-yellow core flash */
if(t<.3){const a=1-t/.3,r=24+110*t/.3,g=ctx.createRadialGradient(cx,cy0,0,cx,cy0,r);g.addColorStop(0,`rgba(255,255,255,${a})`);g.addColorStop(.4,`rgba(255,246,170,${a*.85})`);g.addColorStop(1,'rgba(255,200,60,0)');ctx.fillStyle=g;ctx.fillRect(cx-r,cy0-r,r*2,r*2);}
/* pixel fireball, growing to ~3x the dino's height, then cooling into smoke */
if(t<1.4){const P=4,grow=1-Math.pow(1-Math.min(1,t/.35),3),R=10+47*grow+6*Math.max(0,t-.35),cy=cy0-R*.35,h=Math.min(1,t/1),al=t<.9?1:Math.max(0,1-(t-.9)/.5),fr=Math.floor(t*14);
const hot=['#ffffff','#fff3b0','#ffde59','#ffb02e','#ff8c1a','#ff3d1a','#b3261a'],cool=['#8a8494','#6e6878','#5c5568','#4b4458','#3a3448'];
ctx.globalAlpha=al;const n=Math.ceil(R*1.25/P);
for(let j=-n;j<=n;j++)for(let i=-n;i<=n;i++){const x=cx+i*P,y=Math.round(cy+j*P);if(y>GY+2)continue;
const dd=Math.hypot(i*P,j*P)/R,rr=1+(hn(i,j,fr)-.5)*.45;if(dd>rr)continue;
const q=Math.min(.999,dd/rr),shift=h*1.6,k=q+shift+hn(j,i,fr)*.15;
ctx.fillStyle=k<1?hot[Math.min(6,Math.floor((q+h*.8)*7))]:cool[Math.min(4,Math.floor((k-1)*3.2))];ctx.fillRect(Math.round(x-P/2),y,P,P);}
ctx.globalAlpha=1;}
/* pixel shockwave ring */
if(t<.75){const u=t/.75,r=12+260*(1-Math.pow(1-u,2)),a=1-u,P=3,n=Math.max(24,Math.floor(r*2*Math.PI/(P*2)));
for(let i=0;i<n;i++){const an=i/n*Math.PI*2,x=Math.round((cx+Math.cos(an)*r)/P)*P,y=Math.round((cy0+Math.sin(an)*r*.8)/P)*P;if(y>GY+2)continue;
ctx.globalAlpha=a;ctx.fillStyle=i%3?'#fff3b0':'#ff8c1a';ctx.fillRect(x,y,P,P);
ctx.globalAlpha=a*.5;ctx.fillStyle='#ffffff';const r2=r*.86,x2=Math.round((cx+Math.cos(an)*r2)/P)*P,y2=Math.round((cy0+Math.sin(an)*r2*.8)/P)*P;if(y2<=GY+2&&i%2===0)ctx.fillRect(x2,y2,P,P);}
ctx.globalAlpha=1;}
}
function drawMeteors(){
for(const m of meteors){if(m.hit)continue;
if(m.mega){const w=154,h=126,ux=m.sx-m.tx,uy=m.sy-m.ty,L=Math.hypot(ux,uy),nx=ux/L,ny=uy/L,px=-ny,py=nx,tl=320;
const g=ctx.createLinearGradient(m.x,m.y,m.x+nx*tl,m.y+ny*tl);g.addColorStop(0,'rgba(255,236,140,.85)');g.addColorStop(.35,'rgba(255,140,30,.55)');g.addColorStop(1,'rgba(255,50,20,0)');
ctx.fillStyle=g;ctx.beginPath();ctx.moveTo(m.x+px*66,m.y+py*66);ctx.lineTo(m.x+nx*tl,m.y+ny*tl);ctx.lineTo(m.x-px*66,m.y-py*66);ctx.closePath();ctx.fill();
ctx.fillStyle='rgba(255,120,30,.22)';ctx.beginPath();ctx.arc(m.x,m.y,84,0,7);ctx.fill();
const ix=Math.round(m.x-w/2),iy=Math.round(m.y-h/2);
ctx.save();ctx.shadowColor='#ff6a00';ctx.shadowBlur=26*S;ctx.drawImage(WIFI_F,ix,iy,w,h);ctx.restore();
ctx.globalAlpha=.5+.3*Math.sin(T*30);ctx.drawImage(WIFI_H,ix,iy,w,h);ctx.globalAlpha=1;continue;}
const w=11*2*m.sz/2,h=9*2*m.sz/2;
ctx.save();ctx.shadowColor='#ffb02e';ctx.shadowBlur=10*S;ctx.drawImage(WIFI,Math.round(m.x),Math.round(m.y),w,h);ctx.restore();}
}
function hud(){
const col=state==='start'?'#535353':'#9cff3a',out='#14210a';
if(!(blinkT>0&&Math.floor(blinkT*8)%2===0)){txt(fmtScore(score),592,6,2,col,2,out);}
const sw=fmtScore(score).length*8-2;
if(hi[lvl]>0){ctx.globalAlpha=.8;txt('HI '+fmtScore(hi[lvl]),592-sw-14,6,2,col,2,out);ctx.globalAlpha=1;}
const tw=txt(fmtTime(playTime),592,22,2,'#ffffff',2,out);
txt(LV[lvl].n,592-tw-12,22,2,LV[lvl].c,2,out);
if(lvl===0){const hw=HEART.width*3,hh=HEART.height*3,gap=2,x0=592-(LIVES*(hw+gap)-gap);
for(let i=0;i<LIVES;i++)ctx.drawImage(i<lives?HEART:HEART_E,x0+i*(hw+gap),38,hw,hh);}
/* SUPER SPEED label, just above the โก button (bottom right) while boosting */
if(state==='run'&&!paused&&superOn&&(boostKey||boostTouch)){const ly=Math.round(H-(76/S)-12);
if(ultraOn)txt('ULTRA SPEED!!',W-8,Math.max(56,ly),2,Math.floor(performance.now()/90)%2?'#4dfcff':'#ff4df0',2,'#14210a');else
txt('SUPER SPEED!',W-8,Math.max(56,ly),2,Math.floor(performance.now()/120)%2?'#ffe14d':'#ffffff',2,'#14210a');}
}
function render(){
ctx.setTransform(S,0,0,S,0,0);ctx.imageSmoothingEnabled=false;
ctx.clearRect(0,0,W,H);
if(state==='start'){
ctx.fillStyle='#fff';ctx.fillRect(0,0,W,H);
ctx.fillStyle='#535353';ctx.fillRect(0,GY+1,W,1);
for(let i=0;i<40;i++){ctx.fillRect((i*53)%W,GY+4+(i%3)*2,i%2?2:1,1);}
ctx.drawImage(D_MONO,50,GY+1-38,44,38);
return;
}
ctx.save();
if(shake>0)ctx.translate(Math.round(rnd(-1,1)*shake*6),Math.round(rnd(-1,1)*shake*6));
if(xb&&state==='cutscene'&&xb.t<.6){const k=Math.pow(1-xb.t/.6,1.5)*12;ctx.translate(Math.round(rnd(-1,1)*k),Math.round(rnd(-1,1)*k));}
drawWorld();drawObstacles();
for(const s of speedLines){ctx.fillStyle=`rgba(${s.c||'255,255,255'},${s.a*.75})`;ctx.fillRect(Math.round(s.x-s.l),Math.round(s.y),Math.round(s.l),2);}
/* ULTRA: cyan/magenta tint and smeared streaks at the screen edges, fading with the ease */
if(ultraMix>0&&state==='run'){const ua=ultraMix*ultraMix*(3-2*ultraMix);
let g=ctx.createLinearGradient(0,0,70,0);g.addColorStop(0,`rgba(77,252,255,${.32*ua})`);g.addColorStop(1,'rgba(77,252,255,0)');ctx.fillStyle=g;ctx.fillRect(0,0,70,H);
g=ctx.createLinearGradient(W,0,W-70,0);g.addColorStop(0,`rgba(255,77,240,${.32*ua})`);g.addColorStop(1,'rgba(255,77,240,0)');ctx.fillStyle=g;ctx.fillRect(W-70,0,70,H);
for(let i=0;i<10;i++){const yy=(i*37+Math.floor(T*900))%H,ll=20+((i*53)%40);ctx.fillStyle=`rgba(255,255,255,${.18*ua})`;ctx.fillRect(0,yy,ll,1);ctx.fillRect(W-ll,(yy+H/2)%H,ll,1);}}
drawDino();drawBlast();drawParts();drawMeteors();
for(const r of rings){if(r.r<=0)continue;const a=Math.min(1,r.a);ctx.lineWidth=r.w;
ctx.strokeStyle=`rgba(255,236,190,${a})`;ctx.beginPath();ctx.ellipse(r.x,GY,r.r,r.r*.22,0,0,7);ctx.stroke();
ctx.strokeStyle=`rgba(255,160,60,${a*.6})`;ctx.beginPath();ctx.arc(r.x,GY,r.r*.75,Math.PI,2*Math.PI);ctx.stroke();}
if(state==='cutscene'&&ct>=6.4&&ct<7.6){const a=ct-6.4,k=1-Math.pow(1-Math.min(1,a/.25),3),sc=Math.max(1,Math.round(1+5*k));
ctx.globalAlpha=a>1?Math.max(0,1-(a-1)/.2):1;txt('ROAR!',Math.round(100+70*k+rnd(-1,1)),Math.round(112-62*k+rnd(-1,1)),sc,'#9cff3a',1,'#14210a');ctx.globalAlpha=1;}
if(state==='cutscene'&&ct>=8.4&&ct<9.2){const a=ct-8.4,u=Math.min(1,a/.14),k=u<1?1.25*u:1.25-.25*Math.min(1,(a-.14)/.1),sc=Math.max(1,Math.round(8*k)),bx=Math.round(150+rnd(-2,2)*(1-a)),by=Math.round(70-sc*2.5+rnd(-2,2)*(1-a));
ctx.globalAlpha=a>.62?Math.max(0,1-(a-.62)/.18):1;
txt('BOOM!',bx,by,sc,'#ff8c1a',1,'#2a0a00');txt('BOOM!',bx,by-Math.max(1,Math.round(sc/3)),sc,'#ffde59',1);ctx.globalAlpha=1;}
ctx.restore();
if(state==='cutscene'&&ct>=9.2+XB&&ct<10.8+XB&&Math.floor((ct-9.2-XB)*6)%2===0){ctx.fillStyle='rgba(255,30,60,.22)';ctx.fillRect(0,44,W,32);txt('MEGA METEOROID',W/2,50,4,'#ff3b5c',1,'#1a0710');}
if(connA>0&&state==='cutscene'){ctx.globalAlpha=connA;ctx.save();ctx.shadowColor='#7dff8a';ctx.shadowBlur=14*S;ctx.drawImage(WIFI_G,W/2-33,14,66,54);ctx.restore();txt('CONNECTED',W/2,78,4,'#7dff8a',1,'#0b2a10');ctx.globalAlpha=1;}
if(flash>0){ctx.fillStyle=`rgba(255,255,255,${flash})`;ctx.fillRect(0,0,W,H);}
if(state==='run'||state==='over')hud();
if(state==='cutscene'){hud();if(ct>2)txt('SPACE / TAP TO SKIP',592,166,1.5,'#ffffff',2,'#14210a');}
if(banner&&Math.floor(banner.a*6)%2===0){txt(banner.t,W/2,58,5,banner.t[0]==='S'?'#ffb02e':'#ff3b5c',1,'#1a0710');}
if(state==='over'){txt('GAME OVER',W/2,46,3,'#9cff3a',1,'#14210a');ctx.drawImage(RESTART,W/2-14,72,28,24);}
if(state==='win'){
ctx.fillStyle=doom>0?`rgba(${10+6*doom|0},${8-4*doom|0},${30-26*doom|0},${.55-.27*doom})`:'rgba(10,8,30,.55)';ctx.fillRect(0,0,W,H);drawParts();
txt('YOU WIN!',W/2,20,6,'#ffd23f',1,'#3a1a00');
txt('SCORE '+fmtScore(score),W/2,62,2,'#9cff3a',1,'#14210a');
txt('TIME '+fmtTime(WIN_T),W/2,80,2,'#ffffff',1,'#14210a');
}
if(paused){ctx.fillStyle='rgba(0,0,0,.4)';ctx.fillRect(0,0,W,H);txt('PAUSED',W/2,62,4,'#ffffff',1,'#000');txt('TAP OR PRESS A KEY',W/2,92,2,'#ffffff',1,'#000');}
if(wipe<1){
const wx=Math.round(wipe*W);
ctx.fillStyle='#fff';ctx.fillRect(wx,0,W-wx,H);ctx.fillStyle='#535353';ctx.fillRect(wx,GY+1,W-wx,1);
const g=ctx.createLinearGradient(wx-24,0,wx,0);g.addColorStop(0,'rgba(156,255,58,0)');g.addColorStop(1,'rgba(156,255,58,.9)');ctx.fillStyle=g;ctx.fillRect(wx-24,0,24,H);
}
}
let last=performance.now();
function loop(now){
let ms=now-last;last=now;if(ms>50)ms=50;if(ms<0)ms=0;
if(!paused)update(ms/16.667,ms/1000);
render();syncUI();
requestAnimationFrame(loop);
}
requestAnimationFrame(loop);
})();
</script>
</body></html>
Game Source: Dino Run
Creator: BlazingDolphin53
Libraries: none
Complexity: complex (870 lines, 62.6 KB)
The full source code is displayed above on this page.
Remix Instructions
To remix this game, copy the source code above and modify it. Add a ARCADELAB header at the top with "remix_of: dino-run-blazingdolphin53" to link back to the original. Then publish at arcadelab.ai/publish.