Log In  
Follow
allenxch

Cart #fademuteju-0 | 2022-12-22 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA

--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)<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<r-s and abs(y-y0)<l or 
x>s-r and abs(y-y1)<l)and-dx or
dx,(abs(y)<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
0 comments



Cart #minsnakegame-3 | 2022-04-29 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA
1


An attempt at size-optimizing the classic game of snake.
The final code was 416 in characters.

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]>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&2)/2-(b&1)if(t~=0)r,c=t,0
t=(b&8)/8-(b&4)/4if(t~=0)c,r=t,0
x,y=(x+r)%w,(y+c)%h
n=x+w*y+1if(m[n]<0)l+=1o=0
if(m[n]<=0)m[n]=l f()f()f()goto _
?"game over"

Here is the commented version.

Cart #minsnakegame_commented-0 | 2022-04-29 | Code ▽ | Embed ▽ | No License
1

[ Continue Reading.. ]

1
0 comments



This is an anonymous function shorthand suggestion & discussion for pico8.

sum=function(a,b)return a+b end

Right now, anonymous function definitions look like this.
This is way too long for such a simple function!
I know that Lua doesn't have its own short function; although efforts were made(as seen here), none stood out to be practical.
But as there is already a ton of shorts in pico8, why not add one more?

Of course, then we have to decide the syntaxes for this short.
Below I list three function shorts from other languages:

Verbose Lambda

As implemented in Python and Java.

lambda p1=default,p2:expression

sum=lambda a,b:a+b

(example from Python)
Allows default values, which makes the assignment of local values very concise.
From my experience of size-optimization in pico8, one pattern troubles me: nobody uses local variables.
Of course, size-optimized code is supposed to be purely procedural and I get that; still, they can be better with such a shorthand.

[ Continue Reading.. ]

4
23 comments



I was trying to get string indexing using brackets to work on pico-8.
I quickly found this discussion on lua-uers.org that discusses this problem in Lua 5.1 and 5.2.(See below)

However, after trying several solutions provided in the discussion I still can't get any to work in pico-8.
They work on my other Lua IDEs, but somehow pico-8 always returns runtime errors.

Current code:

--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="1234123512345"
print(a[4])

Current results:

Does anybody know how to correctly implement this in pico-8?

5 comments