pico-8 cartridge // http://www.pico-8.com version 37 __lua__ -- maths attacks -- -- by djib -- function _init() cls() init_btn_status() end function _update() update_btn_status(❎) update_btn_status(🅾️) end function _draw() cls() rect(0,0,127,127,1) draw_keypad() result=get_input() if result!="" then print(get_input()) end end -->8 -- button management -- function init_btn_status() -- no repeating delay for keyboard poke(0x5f5d,255) -- true when pressed btn_down_status={} -- true when quickly released btn_up_status={} -- true when held btn_held_status={} end function update_btn_status(button) -- clear held status if btn_held_status[button] then btn_held_status[button]=false -- button was already pressed -- elseif btn_down_status[button] then -- button was held if btnp(button) then btn_held_status[button]=true btn_down_status[button]=false end -- button no longer pressed if not btn(button) then btn_up_status[button]=true btn_down_status[button]=false end else if btnp(button) then btn_down_status[button]=true end if not btn(button) then btn_held_status[button]=false btn_down_status[button]=false btn_up_status[button]=false end end end function btn_held(button) return btn_held_status[button] end function btn_pressed(button) return btn_up_status[button] end -->8 -- game logic -- function get_input() result="" if btn_pressed(❎) then if btn(⬆️) then if btn(⬅️) then result = "1" elseif btn(➡️) then result = "3" else result = "2" end elseif btn(⬇️) then if btn(⬅️) then result = "7" elseif btn(➡️) then result = "9" else result = "8" end else if btn(⬅️) then result = "4" elseif btn(➡️) then result = "6" else result = "5" end end elseif btn_held(❎) then result = "clear!" elseif btn_pressed(🅾️) then result = "0" elseif btn_held(🅾️) then result = "fire!" end return result end -->8 -- keypad input -- function draw_keypad() -- size of keys local delta=8 -- keypad location local kx=(127-delta*3)/2 local ky=127-delta*3 local keys = { { --1 dx=0,dy=0, up=true,down=false, left=true,right=false }, { --2 dx=1,dy=0, up=true,down=false, left=false,right=false }, { --3 dx=2,dy=0, up=true,down=false, left=false,right=true }, { --4 dx=0,dy=1, up=false,down=false, left=true,right=false }, { --5 dx=1,dy=1, up=false,down=false, left=false,right=false }, { --6 dx=2,dy=1, up=false,down=false, left=false,right=true }, { --7 dx=0,dy=2, up=false,down=true, left=true,right=false }, { --8 dx=1,dy=2, up=false,down=true, left=false,right=false }, { --9 dx=2,dy=2, up=false,down=true, left=false,right=true } } for k,v in pairs(keys) do if btn(⬆️)==v.up and btn(⬇️)==v.down and btn(⬅️)==v.left and btn(➡️)==v.right then offset=16 else offset=0 end spr(k+offset, kx+v.dx*delta, ky+v.dy*delta ) end end __gfx__ 00000000666666606666666066666660666666606666666066666660666666606666666066666660000000000000000000000000000000000000000000000000 00000000671177656711176567111765671717656711176567111765671117656711176567111765000000000000000000000000000000000000000000000000 00000000677177656777176567771765671717656717776567177765677717656717176567171765000000000000000000000000000000000000000000000000 00000000677177656711176567711765671117656711176567111765677117656711176567111765000000000000000000000000000000000000000000000000 00000000677177656717776567771765677717656777176567171765677717656717176567771765000000000000000000000000000000000000000000000000 00000000671117656711176567111765677717656711176567111765677717656711176567771765000000000000000000000000000000000000000000000000 00000000666666656666666566666665666666656666666566666665666666656666666566666665000000000000000000000000000000000000000000000000 00000000055555550555555505555555055555550555555505555555055555550555555505555555000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000066666660666666606666666066666660666666606666666066666660666666606666666000000000000000000000000000000000000000000000000 00000000067117760671117606711176067171760671117606711176067111760671117606711176000000000000000000000000000000000000000000000000 00000000067717760677717606777176067171760671777606717776067771760671717606717176000000000000000000000000000000000000000000000000 00000000067717760671117606771176067111760671117606711176067711760671117606711176000000000000000000000000000000000000000000000000 00000000067717760671777606777176067771760677717606717176067771760671717606777176000000000000000000000000000000000000000000000000 00000000067111760671117606711176067771760671117606711176067771760671117606777176000000000000000000000000000000000000000000000000 00000000066666660666666606666666066666660666666606666666066666660666666606666666000000000000000000000000000000000000000000000000