Skip to content

Instantly share code, notes, and snippets.

@pancelor
Last active October 29, 2022 03:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pancelor/a3aadc5e8cdf809cf0a4972ac9598433 to your computer and use it in GitHub Desktop.
Save pancelor/a3aadc5e8cdf809cf0a4972ac9598433 to your computer and use it in GitHub Desktop.
dev version of mine1k. some comments, might be helpful

Hi! Here's the source code to my minesweeper demake. it's just 1024 bytes!

Q: this looks longer than 1024 characters..?

Yes, this file is much longer. This is the commented "dev" version -- the human-readable version of the code that I wrote by hand. the final submission is the result of running this code through some minification scripts that rename the variables and remove comments and whitespace.

To view the final 1024-byte code, open the minified version, press play, and then press escape twice to open the code editor.

Note that the 1024-byte codesize is measured by the size of the compressed cartridge, not raw character count. that's why you see so much repeated code here -- it compresses well

For more details, see the pico1k 2022 jam page

Q: still, the minified version on pico-8-edu.com says 1028 compressed bytes

I custom-compressed the code using shrinko8 -- the tool tends to compress slightly better than pico-8 itself. So the mine1k.p8.rom file is exactly 1024 bytes, but when pico8 decompresses it and then recompresses it as best as it can, it ends up at 1028 bytes. both files are valid compressed pico8 encodings, but one of them is a little smaller!

sprites

the sprites in the game are stored in the code as strings, and converted into sprites during startup. see JadeLombax's tutorial for more info -- I used a modified version of the RLE sprite compressor shown there

comments

The comments were written as I worked, with only my future self in mind. I haven't edited them to try and explain things. so, this "commented source code" might not be that helpful to you! but maybe it's a little bit helpful, so here you go. feel free to leave a comment on the itch page or tweet at me if you have questions about this code

devlog

I wrote a bit about this code here

numfont={"⁶.\0008llll8\0","⁶.\0「、「「「<\0","⁶.\0008d`8ᶜ|\0","⁶.\0008d0`d8\0","⁶.\000044|00\0","⁶.\0|ᶜ<`d8\0","⁶.\0008ᶜ<ll8\0","⁶.\0|`0「「「\0","⁶.\0008l8ll8\0","⁶.\0008llx`8\0","⁶.$\0B<\0\0\0\0","⁶.$\0<B\0\0\0\0","⁶.~l²<\0\0\0\0","⁶!5f000\0¹²","⁶!5f00²³⁴\t"}
for i=1,177do o=ord("$²¹\n¹\n¹⁴-¹$²⁴²\r²\n³ ⁷$²⁴¹³\r\n³ ⁷$²%⁴³ ⁷$²⁴」\n³ ⁷$²⁴²\r²\n³ ⁷$²¹\n¹\n¹⁴³ ⁷,.³+E¹ᶜ1³ᵉ⁙ᵉ⁷ᶜ-¹³⁸¹▶¹⁸⁷ᶜ³²▮⁸⁷³²¹#¹²⁷ᶜ³²▮⁸⁷³¹/\rᶜ³²▮⁸⁷³¹/\rᶜ³²⁷³⁸⁷³¹/\rᶜ³ᵉ³⁸⁷³¹/\rᶜ³⁸\r²⁷³²¹#¹²⁷ᶜ³+³⁸¹▶¹⁸⁷ᶜ1³ᵉ⁙ᵉ⁷B³CB",i)for i=0,o\6do sset(⬅️%25,⬅️\25,o%6)⬅️+=1end end
-- note: can remove the +1 in direct ord since first char is \0
-- cls()
-- -- hexdump(0x5f00,32)
-- ?"\^!5f1061=78*,3=0562911" --pal
-- -- hexdump(0x5f00,32)
-- poke(0x5f2e,1) --pal persist
-- spr(0,0,32,16,16)
-- stop()
gmine={}
gnum={}
gflag={}
gvis={}
revq={}
::o::
?"\^1\^c"
ghold={}
mbtnr=~stat(34)&mbtn
mbtnp=stat(34)&~mbtn
mbtn=stat(34)&3
i=mid(stat(32)\8,15)+stat(33)\8*16-32
sr=0 --spooky #2
--sum into sr
for ii=1,8do
jr=i+ord("•、。+-;<=",ii)-44
if(abs(i%16-jr%16)<2and jr==jr%224)sr+=gflag[jr]
end
-- if not dead/won, and cell is inbounds, do mouse interaction
-- if death==999and jr==jr%224then -- dunno why this loses 3CB, should be good
if death==999and gflag[i]then
-- LMB held: mark non-flag cells as preview-held
jr=i --spooky #1
if 0<1&mbtn then
ghold[jr]=gflag[jr]<1 --spooky #1
end
-- LMB+RMB held: enter splatmode, and show 3x3 preview-hold
if 2<mbtn then
splat=999
for ii=1,8do
jr=i+ord("•、。+-;<=",ii)-44
if(abs(i%16-jr%16)<2and jr==jr%224)ghold[jr]=gflag[jr]<1 -- (reason for spooky #1)
end
end
-- LMB released, and not splatmode: reveal cell
-- note: revq code deals already-revealed cells
if 1&mbtnr>splat then
-- if mbtnr&1>0and splat<1then
add(revq,i)
end
-- RMB released, and not splatmode or revealed: flag cell
if mbtnp&2>splat+gvis[i]then
-- if mbtnp&2>0and splat<1and not gvis[i]then
remain+=2*gflag[i]-1 -- could reverse this for next line; doesn't help currently
gflag[i]=1-gflag[i]
end
-- either released, and in splatmode: splat! (if flag count matches minecount, and cell is already revealed)
-- note: revq code deals already-revealed cells
if splat&gvis[i]&mbtnr>0and sr==gnum[i]then --spooky #2
-- if mbtnr>0and splat>0and sr==gnum[i]and gvis[i]then --note 999&3==3
for ii=1,8do
jr=i+ord("•、。+-;<=",ii)-44
if(abs(i%16-jr%16)<2and jr==jr%224)add(revq,jr)
end
end
-- none held: exit splatmode
if 1>mbtn then
splat=0
end
end
-- if mbtnp>mbtnp*i then --mbtnp>0 and i<0 (bug: i==0 resets)
-- if mbtnp>0and abs(i%16-8)<2then -- same CB but bad ux. also needs elseif
if mbtnr>i+25&238then
reset=8
end
-- levelgen
if reset>1then
reset=1
death=999
remain=0
time=0
for i=0,223do
gmine[i]=0
gflag[i]=0
gnum[i]=0
gvis[i]=0
gvis[-i]=0
if(rnd(9)>7)gmine[i]=1remain+=1
end
for i=0,223do
hidden=remain-223 --spooky order. must be after last remain calc
--sum into gnum
for ii=1,8do
jr=i+ord("•、。+-;<=",ii)-44
if(abs(i%16-jr%16)<2and jr==jr%224)gnum[i]+=gmine[jr]
end
end
end
time+=1/30
-- tophud
rect(0,0,223,15,3) --white --spooky: 0,223 is any 0,>=128
rect(0,14,223,15,1) --blue --spooky: 223 is any >=127
sspr(0,8,13,13,58,2)
?numfont[12-death\999],60,6
-- seg8 displays
for i=0,2do
?"\#9"..numfont[1+min(death,time)\10^i%10],117-6*i,3,4
?"\#9"..numfont[1+remain\10^i%10],15-6*i,3,4
end
-- reveal queue
for i=1,#revq do
i=deli(revq,1)
-- if tile is not flagged, reveal it
if 1>gflag[i]then
-- die to mines
if 0<gmine[i]then
?"\as8i6c2.c3g2c" --⁷
death=time
reset*=8
-- new reveals
elseif 1>gvis[i]then
?"\ac0"
reset=0
hidden+=1
if 0<hidden then
?"\acg"
death=time-1000
end
if 1>gnum[i]then
for ii=1,8do
jr=i+ord("•、。+-;<=",ii)-44
if(abs(i%16-jr%16)<2and jr==jr%224)add(revq,jr)
end
end
end
gvis[i]=3
end
end
-- draw cells
for i=0,223do
x=i%16*8 y=i\16*8+16
?"\^!5f1061=78*,3=0562911" --pal --spooky order
?"\^!5f2d1" --mouse. ⁶
?"\^!5f5e◝" --reset bitplanes
-- if 0<gvis[i] or (dev_look and btn(4,1))then
if 0<gvis[i]then
-- if visible, draw empty space or mine
if 0<gnum[i]then
?numfont[1+gnum[i]],x-1,y-1,ord("6741,2*5",gnum[i])
end
spr(gmine[i],x,y)
elseif ghold[i]then
-- preview-hold: show empty space
spr(0,x,y)
else
-- otherwise: show solid block (maybe with flag)
sspr(16,9*gflag[i],9,9,x-1,y-1)
end
?"\^!5f5e☉" --bitplanes
if gvis[i-16]<gvis[i]then
rect(x,y,x+8,y,8) -- bitplane shadow
end
if gvis[i-1]<gvis[i]then
rect(x,y,x,y+8,8) -- bitplane shadow
end
end
-- y=stat(33)+4
-- rect(stat(32)-1,y,stat(32)+4,y,8) -- bitplane shadow
-- y+=1
-- rect(stat(32),y,stat(32)+4,y,8) -- bitplane shadow
-- y+=1
-- rect(stat(32)+1,y,stat(32)+3,y,8) -- bitplane shadow
--mouse
?"⁶.\0\0\0\0\0??゛",stat(32)-1,stat(33)-1,8 --shadow
?"\^!5f5e◝" --reset bitplanes
?"⁶.²⁵\t■!」⁶\0",stat(32)-1,stat(33)-1,1 --border
?"⁶.\0²⁶ᵉ゛⁶⁸\0",stat(32)-1,stat(33)-1,3 --white
-- pset(stat(32),stat(33),4)
-- smiley hitbox debug
-- for i=0,255do
-- x=i%16*8 y=i\16*8
-- i-=32
-- if i-7&(255-1-16)==224then -- check < 3 too
-- rect(x,y,x+7,y+7,5)
-- end
-- end
goto o
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment