Skip to content

Instantly share code, notes, and snippets.

@pancelor
Last active Sep 29, 2021
Embed
What would you like to do?
--[[
this is ~1400 characters and doesn't include auto-moving cards, sfx, proper card suits, etc.
(it's an earlier version of my pico1k jam entry)
It's not nearly as compressed as my <1024 character version (see https://pancelor.itch.io/free-cell-1k)
but it is similar enough that it might help you decipher the finished version.
Hopefully my comments help too.
]]
function rectwh(x,y,w,h,c) rect(x,y,x+w-1,y+h-1,c) end
function rectfillwh(x,y,w,h,c) rectfill(x,y,x+w-1,y+h-1,c) end
--t: number of frames since game start
--B: list of the cards that are flying in during the opening animation
--s: a list of "stacks". The game has 16 stacks:
-- 0-7: the main play area
-- 8-11: the free cells
-- 12-15: the scoring area
t=-1A=24365w=13h=17s={}B={}C=fillp
p=poke
p(A,3)p(A-29,8,5,-5)p(A-20,1,-10,3) --enable mouse; edit palette
for i=0,51do
s[i]={}
a=i%13+1b=i\13+1
add(B,{
x0=58,y0=1,
name="\f"..(i\26)..sub("a234567890jqk",a,a)..sub("♥◆♪⧗",b,b),
id=i+i\13*3, --cards have ids 0-12, 16-28, 32-44, and 48-60. Useful for bitwise logic-checking later
},rnd(i+1)\1+1)
end
function set_home(cr,i)
--moves card cr to stack i
assert(i<16,i)
del(s[cr.home],add(s[i],cr))
cr.home=i
cr.x1,cr.y1,z=stack_base(i)
cr.y1+=z
end
function stack_base(i)
--returns the coordinates of the next place a card should go in stack i
-- (returns x,y,z, where x,y is the base of the stack and x,y+x is the top of the stack)
a=i\8return i*(15-a)+a*(i\12*14-114)+4,23-a*22,(1-a)*max(#s[i]*6-6)
end
function draw_card(cr)
--bitplane shadows! search for "0x5f5e" in https://pico-8.fandom.com/wiki/Memory
-- basically, this adds 8 to every color the rectwh draws over (instead of actually drawing over it)
p(A+49,248)rectwh(cr.x0-1,cr.y0-1,14,h,8)p(A+49,-1)
rectfillwh(cr.x0,cr.y0,w,h,7)
print(cr.name,cr.x0+1,cr.y0+1)
end
::_:: --setup is done; time to define the main game loop
t+=1ml=mb mb=btn(5)mx=stat(32)my=stat(33)
if #B>0then --continue starting animation
set_home(deli(B),t%8)
end
for i=0,15do
st=s[i]
x,y,z=stack_base(i)
C(▒)rectwh(x,y,w,h,2)C() --draw base pad
for cr in all(st)do
--move each card from its current pos (x0,y0) towards its target pos (x1,y1)
dx,dy=cr.x1-cr.x0,cr.y1-cr.y0
div=dx^2+dy^2<1 and 1 or 2
cr.x0+=dx/div
cr.y0+=dy/div
if(cr~=crh)draw_card(cr)
end
top=st[#st]
if(i<8)y+=z
-- crh is the card we're holding
-- top is the top card of the stack we're hovering the mouse over
-- crh.id (and top.id) are integer ids for the cards; the cards altogether have ids 0-12, 16-28, 32-44, and 48-60
if x<=mx and mx<x+w and y<=my and my<y+h then --if mouse is colliding,
if crh and not mb and ( --if we're holding a card and just released the mouse button,
i<12 and not top --(you can place any card in a bottom or top left stack if the stack is empty)
or i<8 and 1+crh.id^^32|16==top.id|16 --(bottom cards stack descending w/ alternating colors)
or i>11 and (top and crh.id==top.id+1 or crh.id&15<1) --(scoring cards stack ascending w/ same suit)
) then
set_home(crh,i)
end
if(mb and not ml and i<12)crh=top --pick up the card
end
end
if crh then
-- move and draw the held card
crh.x0=mx-6
crh.y0=my-8
draw_card(crh)
if(not mb)crh=nil
end
?"\^1\^c3",mx-4,my-1,9 --draw the mouse; flip(); cls()
goto _
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment