Skip to content

Instantly share code, notes, and snippets.

@Gikkman
Last active October 22, 2018 20:08
Show Gist options
  • Select an option

  • Save Gikkman/d5cf36479b092926f0d0cb35509968e4 to your computer and use it in GitHub Desktop.

Select an option

Save Gikkman/d5cf36479b092926f0d0cb35509968e4 to your computer and use it in GitHub Desktop.
-- Array of controller modes. We will switch
-- between the three of these randomly
local mode = {"P1", "P2", "ZEN"}
local current = mode[1]
-- Pre-made array for resetting the P1 joypad
local reset = joypad.get(1)
for k,v in pairs(reset) do
reset[k] = ''
end
-- Seed randomness when we start
math.randomseed(os.time())
event.onframestart( function()
local p1 = joypad.get(1)
local p2 = joypad.get(2)
local output = {}
if current == "P1" then
output = p1
elseif current == "P2" then
output = p2
else
output = intersection(p1, p2)
end
gui.drawText(0,0, current)
-- gui.drawText(0,15, dump(output))
joypad.set(output,1)
end)
event.onframeend( function()
joypad.set(reset, 1)
end)
function intersection(p1, p2)
local ret = {}
for k,v in pairs(p1) do
ret[k] = p1[k] and p2[k]
end
return ret
end
-- Print all pressed buttons
function dump(o)
local s = ''
for k,v in pairs(o) do
if v then s = s .. tostring(k) .. ' ' end
end
return s
end
--------------------------------------
-- Main loop --
--------------------------------------
while true do
-- Switch mode randomly twice a second
-- This might become the same mode as we
-- already had
if emu.framecount() % 30 == 0 then
current = mode[ math.random(1,3) ]
end
emu.frameadvance()
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment