Refactor input code
This commit is contained in:
124
maths-attacks.p8
124
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,56 +70,9 @@ 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 = {
|
||||
keys = {
|
||||
{ --1
|
||||
dx=0,dy=0,
|
||||
up=true,down=false,
|
||||
@ -167,12 +118,43 @@ function draw_keypad()
|
||||
up=false,down=true,
|
||||
left=false,right=true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function get_input()
|
||||
result=""
|
||||
if btn_pressed(❎) then
|
||||
for k,v in pairs(keys) do
|
||||
if btn(⬆️)==v.up and
|
||||
if input_matches(v) then
|
||||
result=tostr(k)
|
||||
end
|
||||
end
|
||||
elseif btn_held(❎) then
|
||||
result = "fire!"
|
||||
elseif btn_pressed(🅾️) then
|
||||
result = "0"
|
||||
elseif btn_held(🅾️) then
|
||||
result = "clear!"
|
||||
end
|
||||
return result
|
||||
end
|
||||
|
||||
function input_matches(v)
|
||||
return btn(⬆️)==v.up and
|
||||
btn(⬇️)==v.down and
|
||||
btn(⬅️)==v.left and
|
||||
btn(➡️)==v.right then
|
||||
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
|
||||
rectfill(kx-1,ky-1,kx+3*delta,ky+3*delta,0)
|
||||
|
||||
for k,v in pairs(keys) do
|
||||
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
|
||||
|
Reference in New Issue
Block a user