shabbycat [Lexaloffle Blog Feed]https://www.lexaloffle.com/bbs/?uid=26964 A question about timing in Pico 8 <p>Heyo people!</p> <p>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.</p> <p>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:</p> <div> <div class=scrollable_with_touch style="width:100%; max-width:800px; overflow:auto; margin-bottom:12px"> <table style="width:100%" cellspacing=0 cellpadding=0> <tr><td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> <td background=/gfx/code_bg0.png> <div style="font-family : courier; color: #000000; display:absolute; padding-left:10px; padding-top:4px; padding-bottom:4px; "> <pre>if band(mousebtn,1) &gt; 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</pre></div></td> <td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> </tr></table></div></div> <p>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.</p> <p>I've tried wrapping it in a timer...</p> <div> <div class=scrollable_with_touch style="width:100%; max-width:800px; overflow:auto; margin-bottom:12px"> <table style="width:100%" cellspacing=0 cellpadding=0> <tr><td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> <td background=/gfx/code_bg0.png> <div style="font-family : courier; color: #000000; display:absolute; padding-left:10px; padding-top:4px; padding-bottom:4px; "> <pre>t=time() -- upon button click get time for i=1,flr(dist) do if time()-t&gt;1 then local dirx=lpx+cos(angle)*i local diry=lpy+sin(angle)*i add(ropes, {x=dirx,y=diry}) t=time() end end</pre></div></td> <td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> </tr></table></div></div> <p>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.</p> <p>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.</p> <p>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.</p> <p>-- I feel like I'll be posting a lot here since I'm just starting, apologies if my questions begin to grate.</p> https://www.lexaloffle.com/bbs/?tid=53671 https://www.lexaloffle.com/bbs/?tid=53671 Sun, 06 Aug 2023 21:38:11 UTC Trajectory help? <p>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.</p> <p>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!</p> <p>Thanks!</p> <p>The I suspect the problem lies somewhere here...</p> <div> <div class=scrollable_with_touch style="width:100%; max-width:800px; overflow:auto; margin-bottom:12px"> <table style="width:100%" cellspacing=0 cellpadding=0> <tr><td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> <td background=/gfx/code_bg0.png> <div style="font-family : courier; color: #000000; display:absolute; padding-left:10px; padding-top:4px; padding-bottom:4px; "> <pre>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) &gt; 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</pre></div></td> <td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> </tr></table></div></div> <p> <table><tr><td> <a href="/bbs/?pid=132778#p"> <img src="/bbs/thumbs/pico8_turtgame-0.png" style="height:256px"></a> </td><td width=10></td><td valign=top> <a href="/bbs/?pid=132778#p"> turtgame</a><br><br> by <a href="/bbs/?uid=26964"> shabbycat</a> <br><br><br> <a href="/bbs/?pid=132778#p"> [Click to Play]</a> </td></tr></table> </p> https://www.lexaloffle.com/bbs/?tid=53657 https://www.lexaloffle.com/bbs/?tid=53657 Sun, 06 Aug 2023 02:20:54 UTC