Download gravity-well.html from boundbomb/single-file-html5-games: direct link, hf CLI and curl.
- Browser
- Download file 15.5 kB
-
https://huggingface.co/datasets/boundbomb/single-file-html5-games/resolve/main/gravity-well.html
- Command line
-
hf download hf://datasets/boundbomb/single-file-html5-games/gravity-well.html
-
curl -L -o gravity-well.html https://huggingface.co/datasets/boundbomb/single-file-html5-games/resolve/main/gravity-well.html
15.5 kB
| <html lang="en"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> | |
| <title>GRAVITY WELL</title> | |
| <style> | |
| html,body{margin:0;padding:0;background:#05060f;height:100%;overflow:hidden;font-family:'Segoe UI',Arial,sans-serif;} | |
| #wrap{position:absolute;inset:0;display:flex;align-items:center;justify-content:center;} | |
| canvas{background:#05060f;touch-action:none;cursor:crosshair;max-width:100%;max-height:100%;} | |
| </style> | |
| </head> | |
| <body> | |
| <div id="wrap"><canvas id="c" width="960" height="540"></canvas></div> | |
| <script> | |
| ; | |
| const cv=document.getElementById('c'),cx=cv.getContext('2d'); | |
| const W=960,H=540; | |
| function resize(){const s=Math.min(window.innerWidth/W,window.innerHeight/H);cv.style.width=(W*s)+'px';cv.style.height=(H*s)+'px';} | |
| window.addEventListener('resize',resize);resize(); | |
| // ---------- levels ---------- | |
| // planets: x,y,r (surface gravity is constant G_SURF; pull ~ r^2/d^2) | |
| // spawn: launch point. goal: x,y,r. shots: allowed shots. | |
| const G_SURF=950; | |
| const LEVELS=[ | |
| {name:"FIRST CONTACT",spawn:{x:110,y:430},goal:{x:820,y:120,r:26},shots:5,planets:[{x:480,y:275,r:40}]}, | |
| {name:"TWIN SUNS",spawn:{x:100,y:270},goal:{x:860,y:270,r:26},shots:5,planets:[{x:430,y:170,r:34},{x:430,y:370,r:34}]}, | |
| {name:"THE HOOK",spawn:{x:120,y:120},goal:{x:800,y:430,r:26},shots:5,planets:[{x:560,y:300,r:48}]}, | |
| {name:"CORRIDOR",spawn:{x:480,y:470},goal:{x:480,y:70,r:26},shots:5,planets:[{x:250,y:270,r:38},{x:710,y:270,r:38}]}, | |
| {name:"FAR SIDE",spawn:{x:110,y:470},goal:{x:110,y:70,r:24},shots:5,planets:[{x:520,y:270,r:52}]}, | |
| {name:"SLINGSHOT",spawn:{x:850,y:460},goal:{x:140,y:110,r:26},shots:5,planets:[{x:300,y:400,r:30},{x:620,y:180,r:44}]}, | |
| {name:"ASTEROID BELT",spawn:{x:110,y:460},goal:{x:850,y:90,r:26},shots:6,planets:[{x:330,y:350,r:24},{x:480,y:240,r:24},{x:640,y:140,r:24},{x:820,y:400,r:30}]}, | |
| {name:"BINARY DANCE",spawn:{x:480,y:490},goal:{x:480,y:60,r:24},shots:6,planets:[{x:340,y:270,r:40},{x:620,y:270,r:40}]}, | |
| {name:"THE WELL",spawn:{x:120,y:100},goal:{x:840,y:440,r:24},shots:6,planets:[{x:480,y:270,r:58},{x:200,y:440,r:26},{x:760,y:110,r:26}]}, | |
| {name:"GRAVITY MASTER",spawn:{x:100,y:500},goal:{x:880,y:60,r:22},shots:7,planets:[{x:300,y:200,r:30},{x:560,y:380,r:36},{x:720,y:150,r:30},{x:480,y:120,r:22}]} | |
| ]; | |
| // ---------- state ---------- | |
| let screen="title"; // title | select | play | win | done | |
| let li=0,shotsLeft=0,ball=null,trail=[],particles=[],dragging=false,dragPt=null; | |
| let progress=JSON.parse(localStorage.getItem('gw_progress')||'{}'); | |
| let stars=0,flightT=0,flash=0,mute=false; | |
| let buttons=[]; | |
| let bgStars=[];for(let i=0;i<160;i++)bgStars.push({x:Math.random()*W,y:Math.random()*H,r:Math.random()*1.4+0.3,tw:Math.random()*6.28}); | |
| // ---------- audio ---------- | |
| let AC=null; | |
| function beep(f,d,type,vol){if(mute)return;try{if(!AC)AC=new (window.AudioContext||window.webkitAudioContext)();const o=AC.createOscillator(),g=AC.createGain();o.type=type||'sine';o.frequency.value=f;g.gain.setValueAtTime(vol||0.12,AC.currentTime);g.gain.exponentialRampToValueAtTime(0.001,AC.currentTime+d);o.connect(g);g.connect(AC.destination);o.start();o.stop(AC.currentTime+d);}catch(e){}} | |
| function sfx(n){if(n==='launch'){beep(180,0.25,'sawtooth',0.08);beep(360,0.18,'sine',0.06);}if(n==='crash'){beep(70,0.4,'square',0.12);beep(45,0.5,'sawtooth',0.08);}if(n==='goal'){beep(520,0.15,'sine',0.12);setTimeout(()=>beep(660,0.15,'sine',0.12),110);setTimeout(()=>beep(880,0.3,'sine',0.12),220);}if(n==='click'){beep(600,0.06,'sine',0.06);}} | |
| // ---------- physics ---------- | |
| function planets(){return LEVELS[li].planets;} | |
| function accel(x,y){let ax=0,ay=0;for(const p of planets()){const dx=p.x-x,dy=p.y-y;const d2=dx*dx+dy*dy;const d=Math.sqrt(d2)||1;const a=G_SURF*p.r*p.r/d2;ax+=a*dx/d;ay+=a*dy/d;}return[ax,ay];} | |
| function step(b,dt){const[ax,ay]=accel(b.x,b.y);b.vx+=ax*dt;b.vy+=ay*dt;b.x+=b.vx*dt;b.y+=b.vy*dt;} | |
| function hitPlanet(x,y){for(const p of planets()){const dx=x-p.x,dy=y-p.y;if(dx*dx+dy*dy<(p.r+3)*(p.r+3))return p;}return null;} | |
| function inGoal(x,y){const g=LEVELS[li].goal;const dx=x-g.x,dy=y-g.y;return dx*dx+dy*dy<g.r*g.r;} | |
| function outOfWorld(x,y){return x<-80||x>W+80||y<-80||y>H+80;} | |
| function simulate(x0,y0,vx,vy,maxT){const b={x:x0,y:y0,vx:vx,vy:vy};const dt=1/240;const pts=[{x:b.x,y:b.y}];let t=0;while(t<maxT){step(b,dt);t+=dt;if(t*240%4<1)pts.push({x:b.x,y:b.y});if(inGoal(b.x,b.y))return{res:'goal',pts:pts};if(hitPlanet(b.x,b.y))return{res:'crash',pts:pts};if(outOfWorld(b.x,b.y))return{res:'out',pts:pts};}return{res:'timeout',pts:pts};} | |
| // ---------- flow ---------- | |
| function startLevel(i){li=i;shotsLeft=LEVELS[i].shots;ball=null;trail=[];particles=[];dragging=false;screen='play';flash=0;} | |
| function launch(vx,vy){ball={x:LEVELS[li].spawn.x,y:LEVELS[li].spawn.y,vx:vx,vy:vy};trail=[{x:ball.x,y:ball.y}];flightT=0;sfx('launch');} | |
| function explode(x,y,color){for(let i=0;i<40;i++){const a=Math.random()*6.28,s=60+Math.random()*220;particles.push({x:x,y:y,vx:Math.cos(a)*s,vy:Math.sin(a)*s,life:0.7+Math.random()*0.5,c:color});}} | |
| // ---------- input ---------- | |
| function ptr(e){const r=cv.getBoundingClientRect();const p=e.touches?e.touches[0]:e;return{x:(p.clientX-r.left)*W/r.width,y:(p.clientY-r.top)*H/r.height};} | |
| function down(e){const p=ptr(e); | |
| if(screen==='title'||screen==='select'||screen==='win'||screen==='done'){for(const b of buttons){if(p.x>b.x&&p.x<b.x+b.w&&p.y>b.y&&p.y<b.y+b.h){sfx('click');b.fn();return;}}} | |
| if(screen!=='play')return; | |
| // HUD buttons | |
| for(const b of buttons){if(p.x>b.x&&p.x<b.x+b.w&&p.y>b.y&&p.y<b.y+b.h){sfx('click');b.fn();return;}} | |
| if(ball)return; // in flight | |
| const s=LEVELS[li].spawn;const dx=p.x-s.x,dy=p.y-s.y; | |
| if(dx*dx+dy*dy<90*90){dragging=true;dragPt=p;} | |
| } | |
| function move(e){if(dragging){dragPt=ptr(e);e.preventDefault&&e.preventDefault();}} | |
| function up(e){if(!dragging)return;dragging=false;const s=LEVELS[li].spawn;let dx=s.x-dragPt.x,dy=s.y-dragPt.y;const len=Math.hypot(dx,dy);if(len<12)return;const cap=Math.min(len,140);const sp=cap*6;launch(dx/len*sp,dy/len*sp);} | |
| cv.addEventListener('mousedown',down);cv.addEventListener('mousemove',move);window.addEventListener('mouseup',up); | |
| cv.addEventListener('touchstart',function(e){down(e);e.preventDefault();},{passive:false}); | |
| cv.addEventListener('touchmove',function(e){move(e);e.preventDefault();},{passive:false}); | |
| cv.addEventListener('touchend',function(e){up(e);e.preventDefault();},{passive:false}); | |
| window.addEventListener('keydown',e=>{if(e.key==='m'||e.key==='M')mute=!mute;if(e.key==='r'||e.key==='R'){if(screen==='play')startLevel(li);}}); | |
| // ---------- render helpers ---------- | |
| function btn(x,y,w,h,label,fn,style){buttons.push({x:x,y:y,w:w,h:h,fn:fn}); | |
| cx.fillStyle=style==='ghost'?'rgba(40,48,80,0.55)':'#2a3560';cx.strokeStyle='#5f74c9';cx.lineWidth=2; | |
| roundRect(x,y,w,h,10);cx.fill();cx.stroke(); | |
| cx.fillStyle='#eaf0ff';cx.font='bold 20px Segoe UI,Arial';cx.textAlign='center';cx.textBaseline='middle';cx.fillText(label,x+w/2,y+h/2);} | |
| function roundRect(x,y,w,h,r){cx.beginPath();cx.moveTo(x+r,y);cx.arcTo(x+w,y,x+w,y+h,r);cx.arcTo(x+w,y+h,x,y+h,r);cx.arcTo(x,y+h,x,y,r);cx.arcTo(x,y,x+w,y,r);cx.closePath();} | |
| function drawBg(t){cx.fillStyle='#05060f';cx.fillRect(0,0,W,H);for(const s of bgStars){const a=0.35+0.35*Math.sin(t*0.001+s.tw);cx.fillStyle='rgba(180,200,255,'+a.toFixed(2)+')';cx.fillRect(s.x,s.y,s.r,s.r);}} | |
| function drawPlanets(t){for(const p of planets()){ | |
| const g=cx.createRadialGradient(p.x,p.y,p.r*0.2,p.x,p.y,p.r*1.9); | |
| g.addColorStop(0,'rgba(90,120,255,0.85)');g.addColorStop(0.55,'rgba(50,60,160,0.55)');g.addColorStop(1,'rgba(20,24,60,0)'); | |
| cx.fillStyle=g;cx.beginPath();cx.arc(p.x,p.y,p.r*1.9,0,6.29);cx.fill(); | |
| const g2=cx.createRadialGradient(p.x-p.r*0.35,p.y-p.r*0.35,p.r*0.1,p.x,p.y,p.r); | |
| g2.addColorStop(0,'#9db4ff');g2.addColorStop(1,'#323d8f'); | |
| cx.fillStyle=g2;cx.beginPath();cx.arc(p.x,p.y,p.r,0,6.29);cx.fill(); | |
| cx.strokeStyle='rgba(150,170,255,0.25)';cx.setLineDash([4,8]);cx.beginPath();cx.arc(p.x,p.y,p.r*1.6,0,6.29);cx.stroke();cx.setLineDash([]); | |
| }} | |
| function drawGoal(t){const g=LEVELS[li].goal;const p=1+0.12*Math.sin(t*0.006); | |
| const gr=cx.createRadialGradient(g.x,g.y,2,g.x,g.y,g.r*2.2); | |
| gr.addColorStop(0,'rgba(120,255,170,0.9)');gr.addColorStop(0.5,'rgba(60,220,130,0.35)');gr.addColorStop(1,'rgba(0,0,0,0)'); | |
| cx.fillStyle=gr;cx.beginPath();cx.arc(g.x,g.y,g.r*2.2,0,6.29);cx.fill(); | |
| cx.fillStyle='#8cffb0';cx.beginPath();cx.arc(g.x,g.y,g.r*0.45*p,0,6.29);cx.fill(); | |
| cx.strokeStyle='#57e389';cx.lineWidth=2;cx.beginPath();cx.arc(g.x,g.y,g.r,0,6.29);cx.stroke();} | |
| function drawSpawn(){const s=LEVELS[li].spawn;cx.fillStyle='#3a466e';cx.beginPath();cx.arc(s.x,s.y,14,0,6.29);cx.fill();cx.strokeStyle='#7d90d8';cx.lineWidth=2;cx.stroke(); | |
| if(!ball){cx.fillStyle='#ffd166';cx.beginPath();cx.arc(s.x,s.y,7,0,6.29);cx.fill();}} | |
| // ---------- screens ---------- | |
| function drawTitle(t){buttons=[]; | |
| cx.textAlign='center';cx.textBaseline='middle'; | |
| cx.fillStyle='#8cffb0';cx.font='bold 64px Segoe UI,Arial'; | |
| cx.shadowColor='#57e389';cx.shadowBlur=26;cx.fillText('GRAVITY WELL',W/2,170);cx.shadowBlur=0; | |
| cx.fillStyle='#aebaf0';cx.font='20px Segoe UI,Arial'; | |
| cx.fillText('Slingshot your ship through gravity fields. Reach the green beacon.',W/2,240); | |
| cx.fillText('Drag from the launch pad, aim with the predicted path, release to fire.',W/2,270); | |
| btn(W/2-110,330,220,56,'PLAY',()=>{screen='select';}); | |
| btn(W/2-110,400,220,44,mute?'SOUND: OFF':'SOUND: ON',()=>{mute=!mute;},'ghost'); | |
| cx.fillStyle='#5f6b9e';cx.font='14px Segoe UI,Arial';cx.fillText('10 levels | R = restart level | M = mute',W/2,480);} | |
| function drawSelect(t){buttons=[]; | |
| cx.textAlign='center';cx.textBaseline='middle';cx.fillStyle='#eaf0ff';cx.font='bold 34px Segoe UI,Arial';cx.fillText('SELECT LEVEL',W/2,70); | |
| const cols=5,bw=130,bh=84,gx=26,gy=26,x0=(W-(cols*bw+(cols-1)*gx))/2,y0=140; | |
| for(let i=0;i<LEVELS.length;i++){const col=i%cols,row=(i-col)/cols;const x=x0+col*(bw+gx),y=y0+row*(bh+gy); | |
| const st=progress[i]||0; | |
| buttons.push({x:x,y:y,w:bw,h:bh,fn:(function(n){return function(){startLevel(n);};})(i)}); | |
| cx.fillStyle='#141a33';cx.strokeStyle=st>0?'#57e389':'#3a4466';cx.lineWidth=2;roundRect(x,y,bw,bh,10);cx.fill();cx.stroke(); | |
| cx.fillStyle='#eaf0ff';cx.font='bold 22px Segoe UI,Arial';cx.fillText(String(i+1),x+bw/2,y+26); | |
| cx.font='16px Segoe UI,Arial';let s='';for(let k=0;k<3;k++)s+=k<st?'\u2605':'\u2606'; | |
| cx.fillStyle=st>0?'#ffd166':'#46507a';cx.fillText(s,x+bw/2,y+56);} | |
| const tot=Object.values(progress).reduce((a,b)=>a+b,0); | |
| cx.fillStyle='#8ea0d8';cx.font='16px Segoe UI,Arial';cx.fillText('Total stars: '+tot+' / '+(LEVELS.length*3),W/2,360); | |
| btn(W/2-90,420,180,46,'BACK',()=>{screen='title';},'ghost');} | |
| function drawHUD(){const L=LEVELS[li];buttons=[]; | |
| cx.textAlign='left';cx.textBaseline='middle';cx.fillStyle='#cdd7ff';cx.font='bold 16px Segoe UI,Arial'; | |
| cx.fillText('LEVEL '+(li+1)+' / '+LEVELS.length+' - '+L.name,16,24); | |
| cx.textAlign='right';cx.fillStyle='#ffd166'; | |
| let s='Shots ';for(let k=0;k<L.shots;k++)s+=k<shotsLeft?'\u25CF':'\u25CB'; | |
| cx.fillText(s,W-170,24); | |
| btn(W-140,8,56,32,'R',()=>{startLevel(li);},'ghost'); | |
| btn(W-76,8,60,32,'MENU',()=>{screen='select';},'ghost');} | |
| function drawWin(){buttons=[];const used=LEVELS[li].shots-shotsLeft; | |
| const st=Math.max(1,Math.min(3,4-used)); | |
| if((progress[li]||0)<st){progress[li]=st;localStorage.setItem('gw_progress',JSON.stringify(progress));} | |
| cx.fillStyle='rgba(5,8,20,0.72)';cx.fillRect(0,0,W,H); | |
| cx.textAlign='center';cx.textBaseline='middle'; | |
| cx.fillStyle='#8cffb0';cx.font='bold 44px Segoe UI,Arial';cx.shadowColor='#57e389';cx.shadowBlur=20; | |
| cx.fillText('BEACON REACHED!',W/2,180);cx.shadowBlur=0; | |
| cx.font='40px Segoe UI,Arial';let s='';for(let k=0;k<3;k++)s+=k<st?'\u2605':'\u2606'; | |
| cx.fillStyle='#ffd166';cx.fillText(s,W/2,250); | |
| cx.fillStyle='#cdd7ff';cx.font='18px Segoe UI,Arial';cx.fillText('Shots used: '+used+' / '+LEVELS[li].shots,W/2,300); | |
| if(li+1<LEVELS.length){btn(W/2-190,360,180,52,'NEXT',()=>{startLevel(li+1);});} | |
| else{btn(W/2-190,360,180,52,'FINISH',()=>{screen='done';});} | |
| btn(W/2+10,360,180,52,'REPLAY',()=>{startLevel(li);},'ghost');} | |
| function drawDone(){buttons=[]; | |
| cx.textAlign='center';cx.textBaseline='middle'; | |
| const tot=Object.values(progress).reduce((a,b)=>a+b,0); | |
| cx.fillStyle='#8cffb0';cx.font='bold 46px Segoe UI,Arial';cx.shadowColor='#57e389';cx.shadowBlur=22; | |
| cx.fillText('ALL BEACONS REACHED',W/2,190);cx.shadowBlur=0; | |
| cx.fillStyle='#ffd166';cx.font='26px Segoe UI,Arial';cx.fillText('Total stars: '+tot+' / '+(LEVELS.length*3),W/2,260); | |
| cx.fillStyle='#cdd7ff';cx.font='18px Segoe UI,Arial';cx.fillText(tot>=LEVELS.length*3?'Perfect run. Gravity itself is impressed.':'Replay levels to chase 3 stars each.',W/2,310); | |
| btn(W/2-110,380,220,52,'LEVEL SELECT',()=>{screen='select';});} | |
| // ---------- main loop ---------- | |
| let last=0; | |
| function frame(t){requestAnimationFrame(frame);const dt=Math.min(0.033,(t-last)/1000||0.016);last=t; | |
| drawBg(t); | |
| if(screen==='title'){drawTitle(t);drawPlanetsTitle(t);return;} | |
| if(screen==='select'){drawSelect(t);return;} | |
| if(screen==='done'){drawDone();return;} | |
| // play & win share the world render | |
| drawPlanets(t);drawGoal(t);drawSpawn(); | |
| // flight | |
| if(ball){flightT+=dt;const sub=Math.ceil(dt/(1/240));const h=dt/sub; | |
| for(let i=0;i<sub&&ball;i++){step(ball,h);} | |
| trail.push({x:ball.x,y:ball.y});if(trail.length>220)trail.shift(); | |
| if(inGoal(ball.x,ball.y)){explode(ball.x,ball.y,'#8cffb0');sfx('goal');ball=null;screen='win';} | |
| else{const p=hitPlanet(ball.x,ball.y); | |
| if(p){explode(ball.x,ball.y,'#ff8c66');sfx('crash');ball=null;shotsLeft--;if(shotsLeft<=0){flash=1;startLevel(li);}} | |
| else if(outOfWorld(ball.x,ball.y)){ball=null;shotsLeft--;if(shotsLeft<=0){flash=1;startLevel(li);}} | |
| else if(flightT>12){ball=null;shotsLeft--;if(shotsLeft<=0){flash=1;startLevel(li);}}}} | |
| // trail | |
| if(trail.length>1){cx.strokeStyle='rgba(255,209,102,0.55)';cx.lineWidth=2;cx.beginPath();cx.moveTo(trail[0].x,trail[0].y);for(const q of trail)cx.lineTo(q.x,q.y);cx.stroke();} | |
| // ball | |
| if(ball){cx.fillStyle='#ffd166';cx.shadowColor='#ffb347';cx.shadowBlur=14;cx.beginPath();cx.arc(ball.x,ball.y,7,0,6.29);cx.fill();cx.shadowBlur=0;} | |
| // aim preview | |
| if(dragging&&dragPt&&!ball){const s=LEVELS[li].spawn;let dx=s.x-dragPt.x,dy=s.y-dragPt.y;const len=Math.hypot(dx,dy)||1;const cap=Math.min(len,140);const sp=cap*6; | |
| const sim=simulate(s.x,s.y,dx/len*sp,dy/len*sp,6); | |
| cx.fillStyle=sim.res==='goal'?'rgba(140,255,176,0.85)':'rgba(200,210,255,0.6)'; | |
| for(let i=0;i<sim.pts.length;i+=3){const q=sim.pts[i];cx.beginPath();cx.arc(q.x,q.y,2.4,0,6.29);cx.fill();} | |
| cx.strokeStyle='rgba(255,255,255,0.35)';cx.lineWidth=2;cx.beginPath();cx.moveTo(s.x,s.y);cx.lineTo(dragPt.x,dragPt.y);cx.stroke();} | |
| // particles | |
| for(let i=particles.length-1;i>=0;i--){const p=particles[i];p.life-=dt;p.x+=p.vx*dt;p.y+=p.vy*dt;p.vx*=0.985;p.vy*=0.985; | |
| if(p.life<=0){particles.splice(i,1);continue;} | |
| cx.globalAlpha=Math.min(1,p.life*1.4);cx.fillStyle=p.c;cx.fillRect(p.x-2,p.y-2,4,4);cx.globalAlpha=1;} | |
| if(screen==='play')drawHUD(); | |
| if(screen==='win')drawWin(); | |
| if(flash>0){flash-=dt;cx.fillStyle='rgba(255,80,80,'+(flash*0.35).toFixed(2)+')';cx.fillRect(0,0,W,H);} | |
| } | |
| function drawPlanetsTitle(t){const g=cx.createRadialGradient(W/2,620,40,W/2,620,420);g.addColorStop(0,'rgba(70,90,220,0.5)');g.addColorStop(1,'rgba(0,0,0,0)');cx.fillStyle=g;cx.beginPath();cx.arc(W/2,620,420,0,6.29);cx.fill();} | |
| requestAnimationFrame(frame); | |
| </script> | |
| </body> | |
| </html> | |