Instantly share code, notes, and snippets.
Created
February 3, 2019 00:47
-
Save omgmog/241e325c116f8af200ce059763825228 to your computer and use it in GitHub Desktop.
Pico-8 menu system
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pico-8 cartridge // http://www.pico-8.com | |
version 16 | |
__lua__ | |
-- init | |
function _init() | |
_sfx = { | |
select = 0, | |
move = 1, | |
no = 2 | |
} | |
_menu_start = { | |
selection = 1, | |
items = { | |
{"new game", game}, | |
{"new game", game}, | |
{"new game", game}, | |
{"new game", game}, | |
{"credits", credits} | |
}, | |
guide="❎/x to select" | |
} | |
_menu_game = { | |
selection = 1, | |
items = { | |
{"game over", die}, | |
{"congratulations", win}, | |
{"---"}, | |
{"back", start} | |
} | |
} | |
_menu_credits = { | |
selection = 1, | |
items = { | |
{"this sub menu contains lots of"}, | |
{"strings without actions bound "}, | |
{"to them, so you can just show "}, | |
{"multiple lines of text."}, | |
{"30 characters is a rough limit"}, | |
{"---"}, | |
{"back", start} | |
}, | |
guide="enjoy ♥" | |
} | |
start() | |
end | |
-->8 | |
--update | |
function _update() | |
_upd() | |
end | |
function update_start() | |
menu(_menu_start, "u") | |
end | |
function update_game() | |
menu(_menu_game, "u") | |
end | |
function update_credits() | |
menu(_menu_credits, "u") | |
end | |
-->8 | |
--draw | |
function _draw() | |
cls(13) | |
_drw() | |
end | |
function draw_start() | |
menu(_menu_start, "d") | |
end | |
function draw_game() | |
menu(_menu_game, "d") | |
end | |
function draw_credits() | |
menu(_menu_credits, "d") | |
end | |
-->8 | |
--functions | |
function start() | |
_upd=update_start | |
_drw=draw_start | |
end | |
function game() | |
_upd=update_game | |
_drw=draw_game | |
end | |
function credits() | |
_upd=update_credits | |
_drw=draw_credits | |
end | |
-->8 | |
--utils | |
function menu(m, mode) | |
local ic=#m.items | |
if mode == "u" then | |
if btnp(2) or btnp(3) then | |
sfx(_sfx.move) | |
end | |
if btnp(2) then | |
-- up | |
m.selection -= 1 | |
if m.selection < 1 then | |
m.selection = 1 | |
end | |
end | |
if btnp(3) then | |
-- down | |
m.selection += 1 | |
if m.selection > ic then | |
m.selection = ic | |
end | |
end | |
if btnp(5) then | |
if m.items[m.selection][2] then | |
m.items[m.selection][2]() | |
sfx(_sfx.select) | |
else | |
sfx(_sfx.no) | |
end | |
end | |
end | |
if mode == "d" then | |
local padding={4,4,0,6}-- t/r/b/l | |
local lineheight=8 | |
-- find longest string | |
local widest_str=0 | |
for i,item in pairs(m.items) do | |
if #item[1] > widest_str then | |
widest_str = #item[1] | |
end | |
end | |
local width = (widest_str*4)+padding[2]+padding[4] | |
local height = (ic*lineheight)+padding[1]+padding[3] | |
local xoff = 64 - (width/2) | |
local yoff = 64 - (height/2) | |
local x0 = xoff | |
local y0 = yoff | |
local x1 = xoff + width | |
local y1 = yoff + height | |
-- box | |
rect(x0-2,y0+1,x1+2,y1+2, 5) | |
rect(x0-1,y0-1,x1+1,y1+1,0) | |
rectfill(x0,y0,x1,y1,1) | |
rect(x0,y0,x1,y1,7) | |
-- aditional text | |
if m.guide then | |
local _w = #m.guide*4 | |
local _x = 64 - (_w/2) | |
local _y = y1+padding[1]+2 | |
printo(m.guide,_x,_y, 0, 7) | |
end | |
-- items | |
for i,item in pairs(m.items) do | |
if i == m.selection then | |
-- selection highlight | |
printo( | |
item[1], | |
xoff+padding[4], | |
yoff+padding[1]+((i-1)*lineheight), | |
0,7 | |
) | |
if m.items[m.selection][2] then | |
palt(0,false) | |
palt(1,true) | |
local xadj=(5 - time()%3) | |
spr(1,x0-xadj,y0+padding[1]+((i-1)*lineheight) - 1) | |
pal() | |
end | |
else | |
-- normal item | |
printo( | |
item[1], | |
xoff+padding[4], | |
yoff+padding[1]+((i-1)*lineheight), | |
0,6 | |
) | |
end | |
end | |
end | |
end | |
function printo(str,x,y,c1,c2) | |
for xoff=-1,1 do | |
for yoff=-1,1 do | |
print(str,x+xoff,y+yoff,c1) | |
end | |
end | |
print(str,x,y,c2) | |
end | |
__gfx__ | |
00000000155511110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 | |
00000000557665550000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 | |
00700700777777770000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 | |
00077000777766550000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 | |
00077000667765000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 | |
00700700556665010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 | |
00000000005550110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 | |
00000000110001110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 | |
__sfx__ | |
000500003423036230000003023032230342203622038210342000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 | |
000200000805008050000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 | |
000300000315003130031100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 |
Author
omgmog
commented
Feb 3, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment