Browse Source

Refactor input code

master
djib 2 weeks ago
parent
commit
b0d6c8507f
  1. 182
      maths-attacks.p8

182
maths-attacks.p8

@ -4,7 +4,7 @@ __lua__
-- maths attacks --
-- by djib --
function _init()
cls()
cls(1)
init_btn_status()
end
@ -14,12 +14,10 @@ function _update()
end
function _draw()
cls()
rect(0,0,127,127,1)
draw_keypad()
result=get_input()
if result!="" then
print(get_input())
print(get_input(),0)
end
end
-->8
@ -72,107 +70,91 @@ function btn_pressed(button)
return btn_up_status[button]
end
-->8
-- game logic --
-- keypad input --
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
}
}
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"
for k,v in pairs(keys) do
if input_matches(v) then
result=tostr(k)
end
end
elseif btn_held(❎) then
result = "clear!"
result = "fire!"
elseif btn_pressed(🅾️) then
result = "0"
elseif btn_held(🅾️) then
result = "fire!"
result = "clear!"
end
return result
end
-->8
-- keypad input --
function input_matches(v)
return btn(⬆️)==v.up and
btn(⬇️)==v.down and
btn(⬅️)==v.left and
btn(➡️)==v.right
end
function draw_keypad()
-- size of keys
local delta=8
-- keypad location
local kx=(127-delta*3)/2
local ky=127-delta*3
local ky=127-delta*3
rectfill(kx-1,ky-1,kx+3*delta,ky+3*delta,0)
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
if input_matches(v) then
offset=16
else
offset=0
@ -183,20 +165,22 @@ function draw_keypad()
)
end
end
-->8
__gfx__
00000000666666606666666066666660666666606666666066666660666666606666666066666660000000000000000000000000000000000000000000000000
00000000671177656711176567111765671717656711176567111765671117656711176567111765000000000000000000000000000000000000000000000000
00000000677177656777176567771765671717656717776567177765677717656717176567171765000000000000000000000000000000000000000000000000
00000000677177656711176567711765671117656711176567111765677117656711176567111765000000000000000000000000000000000000000000000000
00000000677177656717776567771765677717656777176567171765677717656717176567771765000000000000000000000000000000000000000000000000
00000000671117656711176567111765677717656711176567111765677717656711176567771765000000000000000000000000000000000000000000000000
00000000666666656666666566666665666666656666666566666665666666656666666566666665000000000000000000000000000000000000000000000000
00000000055555550555555505555555055555550555555505555555055555550555555505555555000000000000000000000000000000000000000000000000
66666660666666606666666066666660666666606666666066666660666666606666666066666660000000000000000000000000000000000000000000000000
67111765671177656711176567111765671717656711176567111765671117656711176567111765000000000000000000000000000000000000000000000000
67171765677177656777176567771765671717656717776567177765677717656717176567171765000000000000000000000000000000000000000000000000
67171765677177656711176567711765671117656711176567111765677117656711176567111765000000000000000000000000000000000000000000000000
67171765677177656717776567771765677717656777176567171765677717656717176567771765000000000000000000000000000000000000000000000000
67111765671117656711176567111765677717656711176567111765677717656711176567771765000000000000000000000000000000000000000000000000
66666665666666656666666566666665666666656666666566666665666666656666666566666665000000000000000000000000000000000000000000000000
05555555055555550555555505555555055555550555555505555555055555550555555505555555000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000066666660666666606666666066666660666666606666666066666660666666606666666000000000000000000000000000000000000000000000000
00000000067117760671117606711176067171760671117606711176067111760671117606711176000000000000000000000000000000000000000000000000
00000000067717760677717606777176067171760671777606717776067771760671717606717176000000000000000000000000000000000000000000000000
00000000067717760671117606771176067111760671117606711176067711760671117606711176000000000000000000000000000000000000000000000000
00000000067717760671777606777176067771760677717606717176067771760671717606777176000000000000000000000000000000000000000000000000
00000000067111760671117606711176067771760671117606711176067771760671117606777176000000000000000000000000000000000000000000000000
00000000066666660666666606666666066666660666666606666666066666660666666606666666000000000000000000000000000000000000000000000000
06666666066666660666666606666666066666660666666606666666066666660666666606666666000000000000000000000000000000000000000000000000
06711176067117760671117606711176067171760671117606711176067111760671117606711176000000000000000000000000000000000000000000000000
06717176067717760677717606777176067171760671777606717776067771760671717606717176000000000000000000000000000000000000000000000000
06717176067717760671117606771176067111760671117606711176067711760671117606711176000000000000000000000000000000000000000000000000
06717176067717760671777606777176067771760677717606717176067771760671717606777176000000000000000000000000000000000000000000000000
06711176067111760671117606711176067771760671117606711176067771760671117606777176000000000000000000000000000000000000000000000000
06666666066666660666666606666666066666660666666606666666066666660666666606666666000000000000000000000000000000000000000000000000

Loading…
Cancel
Save