Follow
shabbycat
[ :: Read More :: ]

Heyo people!

So, I'm having some trouble getting my head around the timing of everything when it comes to Pico 8, Lua, and I guess programming in general. Something that troubles me is how for loops act when running a game.

Right now, I have a piece of code that displays several circles along a straight line, one circle being created for each pixel of distance between the player sprite and a mouse click location. Here is the code:

 ```if band(mousebtn,1) > 0 then ropes={} mouseclick=true end if mouseclick==true then --get last mouse coords local lcursx=mousex local lcursy=mousey --get last player coords local lpx=p.x+8 local lpy=p.y local dist, angle = gettraj(lcursx,lcursy) for i=1,flr(dist) do local dirx=lpx+cos(angle)*i local diry=lpy+sin(angle)*i add(ropes, {x=dirx,y=diry}) end mouseclick=false end```

Now, I've been messing around with this for loop for about 3 hours, trying to figure out how to create these 'rope' particles sequentially, one by one, over a specific span of time.

I've tried wrapping it in a timer...

 ```t=time() -- upon button click get time for i=1,flr(dist) do if time()-t>1 then local dirx=lpx+cos(angle)*i local diry=lpy+sin(angle)*i add(ropes, {x=dirx,y=diry}) t=time() end end```

I've also tried wrapping the timer around the add(ropes)... statement alone, doing all sorts of timey wimey stuff that I'm seeing in tutorials and in the forums, and nothing has worked. I wish I could post all of the iterations of failure but that would be very boring.

I've also considered creating each rope particle, and then having them move incrementally towards the mouse location, before freezing the entire table of particles in place. Perhaps that would give me greater control over the 'extending rope' effect.

So, I guess I'm just wondering, how does the for loop really work? Does placing a timer within the loop mean that one second should be passing each time the loop runs? Does the loop finish its full duration before continuing with any code below? And also, if you have any suggestions on how to get these little ropey fellas to spawn based on a timed sequence, please let me know.

-- I feel like I'll be posting a lot here since I'm just starting, apologies if my questions begin to grate.

P#132809 2023-08-06 21:38 ( Edited 2023-08-06 21:40)

[ :: Read More :: ]

Heyo people! I've been learning Pico 8 for a few weeks now, created a few little test games to learn the language, now I'm trying to start a game I intend to finish and polish - thought it might be useful to start interacting on the boards. I'm having some issues with the mathematics of this game I'm making.

Playing as a small turtle with a cactus on it's back, the main mode of interaction will be snatching other entities out of the air with rope-like tendrils that come from within the cactus. However, right now, I'm having a lot of difficulty trying to figure out why my tendrils are not lining up to the angle of the mouse's location (using mouse input). Sine, cosine, pi, all that stuff terrifies me, so if someone out there wouldn't mind taking a gander at the small amount of code I've written for this project so far, I'd appreciate any advice!

Thanks!

The I suspect the problem lies somewhere here...

 ```function gettraj(mx,my) --get x/y distances local hdist=p.x-(mx+4) local vdist=p.y-(my+4) --get diagonal distance local dist=sqrt(hdist^2+vdist^2) --get angle to cursor in degrees local angle=(atan2(vdist,hdist)*180/pi)+45 return dist, angle end function update_game() mousex=stat(32) mousey=stat(33) mousebtn=stat(34) if band(mousebtn,1) > 0 then --get last mouse coords local lcursx=mousex local lcursy=mousey --get last player coords local lpx=p.x+8 local lpy=p.y local dist, angle = gettraj(lcursx,lcursy) for i=1,flr(dist) do local dirx=lpx+cos(angle*pi/180)*i local diry=lpy-sin(angle*pi/180)*i add(ropes, {x=dirx,y=diry}) end end end```

Cart #turtgame-0 | 2023-08-06 | Code ▽ | No License

P#132778 2023-08-06 02:20 ( Edited 2023-08-06 02:24)