From 2f6b775a834825a73566e82c5f6f05843e131315 Mon Sep 17 00:00:00 2001 From: Luca Conte Date: Tue, 18 Mar 2025 22:21:53 +0100 Subject: [PATCH] initial commit --- index.html | 2 + rocket.png | Bin 0 -> 1141 bytes script.js | 169 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 171 insertions(+) create mode 100644 index.html create mode 100644 rocket.png create mode 100644 script.js diff --git a/index.html b/index.html new file mode 100644 index 0000000..7aa8a61 --- /dev/null +++ b/index.html @@ -0,0 +1,2 @@ + + diff --git a/rocket.png b/rocket.png new file mode 100644 index 0000000000000000000000000000000000000000..b0b5437394cfebe7e08bfd01dcba6752cb855a51 GIT binary patch literal 1141 zcmV-*1d98KP)02y>e zSad^gZEa<4bO1wgWnpw>WFU8GbZ8()Nlj2!fese{00Z4gL_t(&-tCxQY*SSn$3OS> zZfn`r(cQK-EIB?8fIdtTV5t%YV_m06Wp{1u*xIe@^ z6B82?6O)odP{Cb*eOiEtl2oFCTEGaGsz*fN9w1`S-l;-Z8=FhfD_c|;Ezmn7D*UFx z6Qw4mNrgaxVpL5`=YJmTIbvFYeZZ3iu5%U$0sR7el6RxNV8?*bnI!&z?}!Uwz#JX;I_f5w|oX4Y~+kvU?0nE2Lhl*M9f zA3TOAg54RSjxj`1OD9Hv?`Z=>70Lzpda;m;n)4RZRV)zz=fhq;ooiyw=|ZV*;6zh1 zTP79CVlm!JY~#_X@93$VphAc(w?r%8P$4A1xr`7Ccd%l|XE!iCXW`o8?VQ-*%lK~vB6WX-u^hO}Q!RhsuDparNa&@SBPz5DFe1JR;z97LAZG^CSeb=_#~d)PFXXd8 z8(&RsBqikuPm2ZB;Q-*RpIV6}HyBCMzo;NfFT;n<+}CfeaJw6UHZ6JUFxtJBQ}=IQ zEhjsG2`R1xh;ucTT(8xYojM(a0-Y}CS(pEPY8nPJ5ADF(MK3FWzWno~qtNp=L_dI^ zZ$=|;Q_gEE>Jy~fn_cexnDk4#arquxjqQs7o2C7b0M7M-lHIi$62%Jk=ethg`k|SN zsno6Bnx7}BEHvWp>^n`3J#@R}f-D5PB5CDj>P}^?>Z&m+Z4Ytv=y8-Y=c(P%fTyLI znd=IppZ6msrr6nXg<8iL7EyOU13y3j zElJ~6zrGKS@guz)$k(D+^Hg^5l4d`u?m4g$xEH8enGla_2#;&XsQCag0UpCI^ez?g z@7wrJt(kFWGx`v9%e3*ZM@yha`6AmcF3=}<4Yu(`Jy-=fterB9rt-bQMq>ZFqwd{v z`JjSZg-L^de!gU{gYK3KI!U{rGkj%=iHV7ciHV7ciBbLmfPUR4deiRp00000NkvXX Hu0mjfA`JwW literal 0 HcmV?d00001 diff --git a/script.js b/script.js new file mode 100644 index 0000000..230a6ea --- /dev/null +++ b/script.js @@ -0,0 +1,169 @@ +var player={x: 0, y: 0, mx: 0, my:0} +var lastpos=JSONClone(player); +var playerbreak=0.1; +var playeracceleration=3; +var gs=playeracceleration/playerbreak; + +var rockspeed=5; + +var debug=false; + +var frames=0; + +var debugtxts=[]; + +var particles=[]; + +var rocks=[]; + +var img=new Image(); +img.src="rocket.png"; + +function init() { + player={x: cv.width/2, y: cv.height/2, mx: 0, my:0} + setInterval(addRock,500); +} + +function addRock() { + var x = Math.random()*cv.width; + var y = Math.random()*cv.height; + + var tx=Math.random()*cv.width/2 + cv.width/4; + var ty=Math.random()*cv.height/2 + cv.width/4; + + var angle=-Math.atan2(tx-x, ty-y) + Math.PI/2; + rocks.push({x:x ,y:y, angle: angle}); +} + +function tick() { + player.mx*=1-playerbreak; + player.my*=1-playerbreak; + var accelerating=false; + if (keystateByName("w") || keystateByName("ArrowUp")) { + player.my-=playeracceleration; + accelerating=true; + } + if (keystateByName("s") || keystateByName("ArrowDown")) { + player.my+=playeracceleration; + accelerating=true; + } + if (keystateByName("a") || keystateByName("ArrowLeft")) { + player.mx-=playeracceleration; + accelerating=true; + } + if (keystateByName("d") || keystateByName("ArrowRight")) { + player.mx+=playeracceleration; + accelerating=true; + } + player.x+=player.mx; + player.y+=player.my; + + + debugtxts.push(Math.round(player.mx,2)); + debugtxts.push(Math.round(player.my,2)); + debugtxts.push(""); + debugtxts.push(Math.round(player.x,2)); + debugtxts.push(Math.round(player.y,2)); + + c.fillStyle="black"; + fillBackground(); + if (debug) { + c.strokeStyle="green"; + c.lineWidth=3; + c.beginPath(); + c.moveTo(lastpos.x, lastpos.y); + c.lineTo(player.x, player.y); + c.stroke(); + } + + var motionangle=-Math.atan2(player.mx,player.my)-Math.PI/2; + + if (debug) { + c.strokeStyle="blue"; + c.lineWidth=3; + c.beginPath(); + c.moveTo(player.x, player.y); + c.lineTo(player.x + Math.cos(motionangle)*100, player.y + Math.sin(motionangle)*100) + c.stroke(); + } + if (accelerating) { + particles.push({x:player.x, y: player.y, speed: 5 + Math.random()*6-3, color: "yellow", angle: motionangle + Math.random()*0.8-0.4, size: 10, maxage: FPS/2}); + particles.push({x:player.x, y: player.y, speed: 5 + Math.random()*6-3, color: "yellow", angle: motionangle + Math.random()*0.8-0.4, size: 10, maxage: FPS/2}); + particles.push({x:player.x, y: player.y, speed: 5 + Math.random()*6-3, color: "red", angle: motionangle + Math.random()*0.8-0.4, size: 10, maxage: FPS/2}); + particles.push({x:player.x, y: player.y, speed: 5 + Math.random()*6-3, color: "red", angle: motionangle + Math.random()*0.8-0.4, size: 10, maxage: FPS/2}); + } + + lastpos=JSONClone(player); + + for (var i=0; i=particles[i].maxage) { + particles.splice(1,i); + } else { + particles[i].age++; + particles[i].x+=Math.cos(particles[i].angle)*particles[i].speed; + particles[i].y+=Math.sin(particles[i].angle)*particles[i].speed; + c.globalAlpha=Math.abs(1-particles[i].age/particles[i].maxage); + c.fillStyle=particles[i].color; + c.fillRect(particles[i].x - particles[i].size/2, particles[i].y - particles[i].size/2, particles[i].size, particles[i].size) + c.globalAlpha=1; + } + } + + c.fillStyle="red"; + drawRotatedImage(img,player.x,player.y,motionangle+Math.PI,50,50); + + if (debug) { + c.fillStyle="white"; + for (var x=0; x1) { + return factorial(n-1)*n; + } else { + return 1; + } +} + +function onresize() { + tick(); +}