pico-8 cartridge // http://www.pico-8.com version 16 __lua__ -- amico8 input test -- tests: joystick/keys, mouse, stat(1) cpu timer -- (stat(100) is the load() breadcrumb on amico8/pico-8, not a timer) local mx,my,mb = 64,64,0 local trail = {} -- mouse trail (last 32 positions) local frame = 0 -- button names and positions (d-pad + o/x) local bname = {"left","right","up","down","o","x"} local bkey = {"\x8e","\x8f","\x8c","\x8d","o","x"} -- arrow glyphs or letters -- small 5x5 cross for d-pad rendering local function draw_dpad(cx,cy) -- up local col = btn(2) and 11 or 5 rectfill(cx-2, cy-7, cx+2, cy-4, col) -- down col = btn(3) and 11 or 5 rectfill(cx-2, cy+4, cx+2, cy+7, col) -- left col = btn(0) and 11 or 5 rectfill(cx-7, cy-2, cx-4, cy+2, col) -- right col = btn(1) and 11 or 5 rectfill(cx+4, cy-2, cx+7, cy+2, col) -- center dot rectfill(cx-2, cy-2, cx+2, cy+2, 6) end local function draw_buttons(cx,cy) -- o button (z/c) local col = btn(4) and 11 or 5 circfill(cx-8, cy, 5, col) print("o", cx-10, cy-2, 7) -- x button (x/v) col = btn(5) and 11 or 5 circfill(cx+8, cy, 5, col) print("x", cx+6, cy-2, 7) end function _init() -- draw a small mouse cursor sprite into slot 1 (address 0x0040) -- 4x4 white dot for i=0,31 do poke(0x40+i, 0) end -- col 7 (white): nibble 7 poke(0x40, 0x77) poke(0x41, 0x77) poke(0x48, 0x77) poke(0x49, 0x77) end function _update60() frame = frame + 1 mx = stat(32) my = stat(33) mb = stat(34) -- record trail add(trail, {x=mx, y=my}) if #trail > 32 then del(trail, trail[1]) end end function _draw() cls(0) -- title print("amico8 input test", 18, 2, 7) line(0,9,127,9,5) -- === JOYSTICK SECTION === print("joystick", 2, 12, 6) -- d-pad at (24,34) draw_dpad(24, 34) -- o/x buttons at right draw_buttons(80, 34) -- btn() bitmask readout local raw = btn() print("btn()="..raw, 2, 48, 5) -- individual button states as text local bs = "" for b=0,5 do bs = bs..(btn(b) and bname[b+1].." " or "") end if bs == "" then bs = "(none)" end print(bs, 2, 55, 7) line(0,64,127,64,5) -- === MOUSE SECTION === print("mouse", 2, 67, 6) -- mouse coordinate readout print("x="..mx.." y="..my, 2, 75, 7) -- mouse button states local mbl = (mb&1)~=0 and "L" or "l" local mbr = (mb&2)~=0 and "R" or "r" local mbm = (mb&4)~=0 and "M" or "m" local mbc = (mb&1)~=0 and 11 or 5 print("btns: "..mbl.." "..mbr.." "..mbm, 2, 83, 7) -- draw mouse trail (fading) for i=1,#trail do local p = trail[i] local c = 1+flr((i/#trail)*3) -- dark blue → blue → dark grey pset(p.x, p.y, c) end -- draw mouse cursor (small cross) local cx,cy = mx,my local cc = (mb~=0) and 10 or 7 line(cx-3, cy, cx+3, cy, cc) line(cx, cy-3, cx, cy+3, cc) pset(cx, cy, 7) -- mini 64x32 mouse area map (bottom right) -- shows position within the 128x128 field scaled to 64x32 box local bx,by = 64,68 rect(bx, by, bx+63, by+31, 5) local px = bx + flr(mx/2) local py = by + flr(my/4) if px >= bx and px <= bx+63 and py >= by and py <= by+31 then pset(px, py, 10) circfill(px, py, 1, (mb~=0) and 10 or 7) end print("pos map", bx+1, by+24, 5) line(0,102,127,102,5) -- === TIMER SECTION === -- stat(1) = cpu fraction used so far this frame (live, counts UP, 1=full -- 60fps frame). time a 1k-add loop by its stat(1) delta. print("timer", 2, 105, 6) local t0 = stat(1) local dummy = 0 for i=1,1000 do dummy=dummy+i end local dt = stat(1) - t0 if dt < 0 then dt = 0 end if dt > 1.9 then dt = 1.9 end -- fix32 guard: dt*16667 < 32767 -- frame budget = 16667us at 60fps print("1k-loop ~"..(dt*16667).."us", 2, 112, 5) print("cpu "..flr(stat(1)*100).."% frm "..frame, 2, 120, 5) end