allenxch [Lexaloffle Blog Feed]https://www.lexaloffle.com/bbs/?uid=38719 Size-optimized(not really) pong <p> <table><tr><td> <a href="/bbs/?pid=122848#p"> <img src="/bbs/thumbs/pico8_fademuteju-0.png" style="height:256px"></a> </td><td width=10></td><td valign=top> <a href="/bbs/?pid=122848#p"> fademuteju</a><br><br> by <a href="/bbs/?uid=38719"> allenxch</a> <br><br><br> <a href="/bbs/?pid=122848#p"> [Click to Play]</a> </td></tr></table> </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>--init t,f,b,w,h=tonum,flip,btn,128,128 w,h,x,y,y0,y1=w/2,h/2,0,0,0,0 r,s,l,dx,dy=3,w-5,20,1,1color(7) --update while abs(x)&lt;w do x,y=x+dx,y+dy y0,y1=y0+t(b(3,1))-t(b(2,1)), y1+t(b(3,0))-t(b(2,0)) dx,dy=(x&lt;r-s and abs(y-y0)&lt;l or x&gt;s-r and abs(y-y1)&lt;l)and-dx or dx,(abs(y)&lt;h-r)and dy or-dy --draw line(w-s,y0-l/2+h,w-s,y0+l/2+h) line(w+s,y1-l/2+h,w+s,y1+l/2+h) circfill(x+w,y+h,r)f()cls()end </pre></div></td> <td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> </tr></table></div></div> https://www.lexaloffle.com/bbs/?tid=50764 https://www.lexaloffle.com/bbs/?tid=50764 Thu, 22 Dec 2022 15:55:24 UTC Size-optimized Snake Game <p> <table><tr><td> <a href="/bbs/?pid=111066#p"> <img src="/bbs/thumbs/pico8_minsnakegame-3.png" style="height:256px"></a> </td><td width=10></td><td valign=top> <a href="/bbs/?pid=111066#p"> minsnakegame</a><br><br> by <a href="/bbs/?uid=38719"> allenxch</a> <br><br><br> <a href="/bbs/?pid=111066#p"> [Click to Play]</a> </td></tr></table> <br /> An attempt at size-optimizing the classic game of snake.<br /> The final code was 416 in characters.</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>m={}x=3y=2r=1c=0o=0d=2^7f=flip l=3w=16h=w::_::cls()for i=1,w*h do m[i]=m[i]or 0if(m[i]&gt;0)m[i]-=1 if(o)k=flr(rnd(w*h-l))+1o=nil if(m[i]==0)k-=1m[i]=(k==0)and -8/3 or 0 p=(i%w)*d/w q=flr((i-1)/h)*d/h rect(p,q,p+d/w-1,q+d/h-1,3*min(m[i],1))?l-3,0,0,7 end b=btn()t=(b&amp;2)/2-(b&amp;1)if(t~=0)r,c=t,0 t=(b&amp;8)/8-(b&amp;4)/4if(t~=0)c,r=t,0 x,y=(x+r)%w,(y+c)%h n=x+w*y+1if(m[n]&lt;0)l+=1o=0 if(m[n]&lt;=0)m[n]=l f()f()f()goto _ ?&quot;game over&quot;</pre></div></td> <td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> </tr></table></div></div> <p>Here is the commented version.<br /> <table><tr><td> <a href="/bbs/?pid=111066#p"> <img src="/bbs/thumbs/pico8_minsnakegame_commented-0.png" style="height:256px"></a> </td><td width=10></td><td valign=top> <a href="/bbs/?pid=111066#p"> minsnakegame_commented</a><br><br> by <a href="/bbs/?uid=38719"> allenxch</a> <br><br><br> <a href="/bbs/?pid=111066#p"> [Click to Play]</a> </td></tr></table> <br /> Feel free to post any suggestions!</p> https://www.lexaloffle.com/bbs/?tid=47590 https://www.lexaloffle.com/bbs/?tid=47590 Fri, 29 Apr 2022 17:47:18 UTC Convenience Request: Anonymous Function Shorthands <p>This is an anonymous function shorthand suggestion &amp; discussion for pico8.</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>sum=function(a,b)return a+b 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>Right now, anonymous function definitions look like this.<br /> This is way too long for such a simple function!<br /> I know that Lua doesn't have its own short function; although efforts were made(<a href="http://lua-users.org/wiki/ShortAnonymousFunctions">as seen here</a>), none stood out to be practical.<br /> But as there is already a ton of shorts in pico8, why not add one more?</p> <p>Of course, then we have to decide the syntaxes for this short.<br /> Below I list three function shorts from other languages:</p> <h2>Verbose Lambda</h2> <p>As implemented in Python and Java.</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>lambda p1=default,p2:expression sum=lambda a,b:a+b</pre></div></td> <td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> </tr></table></div></div> <p>(example from Python)<br /> Allows default values, which makes the assignment of local values very concise.<br /> From my experience of size-optimization in pico8, one pattern troubles me: nobody uses local variables.<br /> Of course, size-optimized code is supposed to be purely procedural and I get that; still, they can be better with such a shorthand.</p> <h2>The arrow</h2> <p>Choice of Javascript.</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>(p1=deafault,p2=&gt;expression) (a,b=&gt;a+b)</pre></div></td> <td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> </tr></table></div></div> <p>Nice and short. (Don't need 2 characters for the arrow tho, one will do the trick)<br /> Just like the Lambda method, it also allows default values.</p> <p>&gt; Personally, I think the Elixir <code>fn(p1)-&gt; expression end</code> fits the Lua syntax better.<br /> &gt; But why arrow again when there are already parentheses to separate the parameters and the expression??<br /> &gt; Also, the additional <code>end</code> fails to capture the expression-like nature of anonymous functions. It's not a control structure(although it can be used as one).</p> <p>Overall, I think this is the best choice.</p> <h2>The &amp; shorthand</h2> <p>From Elixir.</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>sum = &amp;(&amp;1 + &amp;2)</pre></div></td> <td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> </tr></table></div></div> <p>(example from <a href="https://elixirschool.com/en/lessons/basics/functions/">https://elixirschool.com/en/lessons/basics/functions/</a>)<br /> This is the shortest so far.<br /> Instead of naming the parameters, why not just number them?<br /> This shorthand has several problems tho:</p> <ol> <li><del>doesn't align with the De Bruijn index.</del></li> <li>no default values.</li> <li>if I do <code>&amp;(&amp;2)</code>, does this just evaluate to <code>function(x)return function(y)return y end end</code>? As you can see in this example, I just skipped <code>&amp;1</code> entirely. I do not think this is great at all for intuitive and readable code. It's confusing and not as elegant as shorthands should be. (Just an opinion, feel free to tell me how wrong I am.)</li> </ol> <p>So I guess I won't consider this as an option.</p> <h2>Conclusion</h2> <p>A functional shorthand would make the pico8 experience even better.<br /> I am not sure if this has been requested before, but it might shape how we code in pico8 by a lot.<br /> Feel free to comment below on your favorite shorthand. I am sure there are even better syntaxes than the ones I have listed above.</p> https://www.lexaloffle.com/bbs/?tid=47523 https://www.lexaloffle.com/bbs/?tid=47523 Mon, 25 Apr 2022 12:57:07 UTC Help on string indexing with metatable <p>I was trying to get string indexing using brackets to work on pico-8.<br /> I quickly found this <a href="http://lua-users.org/wiki/StringIndexing">discussion on lua-uers.org</a> that discusses this problem in Lua 5.1 and 5.2.(See below)</p> <p>However, after trying several solutions provided in the discussion I still can't get any to work in pico-8.<br /> They work on my other Lua IDEs, but somehow pico-8 always returns runtime errors. </p> <p>Current 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>--from lua-users.org getmetatable('').__index = function(str,i) return string.sub(str,i,i) end getmetatable('').__call = function(str,i,j) if type(i)~='table' then return string.sub(str,i,j) else local t={} for k,v in ipairs(i) do t[k]=string.sub(str,v,v) end return table.concat(t) end end --demo a=&quot;1234123512345&quot; print(a[4])</pre></div></td> <td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> </tr></table></div></div> <p>Current results:</p> <img style="margin-bottom:16px" border=0 src="/media/38719/微信图片_20220417194908.png" alt="" /> <p>Does anybody know how to correctly implement this in pico-8?</p> https://www.lexaloffle.com/bbs/?tid=47409 https://www.lexaloffle.com/bbs/?tid=47409 Sun, 17 Apr 2022 11:49:14 UTC