Skip to content

Instantly share code, notes, and snippets.

@kevinthompson
Last active July 5, 2025 04:18
Show Gist options
  • Save kevinthompson/d0e8bb3dc17c2c79a5c32c13e1ad5741 to your computer and use it in GitHub Desktop.
Save kevinthompson/d0e8bb3dc17c2c79a5c32c13e1ad5741 to your computer and use it in GitHub Desktop.
PICO-8 Button Functions
-- button functions
--------------------
-- return true if button released since last frame
function btnr(b)
local bit = shl(1,b)
return band(bit, pbtn) == bit and btn() == band(bnot(bit), btn())
end
-- return count of frames button has been held down
function btnf(b)
return (bfr or {})[b+1] or 0
end
-- call this at the end of update
function update_button_state()
-- track button frames
bfr = bfr or {}
for b=0,5 do
bfr[b+1] = btn(b) and (bfr[b+1] or 0) + 1 or 0
end
-- store previous button state
pbtn = btn()
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment