Skip to content

Instantly share code, notes, and snippets.

@dotxnc
Last active February 12, 2017 03:55
Show Gist options
  • Save dotxnc/3b00b7866c19a0eb28c1af479e144ad6 to your computer and use it in GitHub Desktop.
Save dotxnc/3b00b7866c19a0eb28c1af479e144ad6 to your computer and use it in GitHub Desktop.
#!/usr/bin/luajit
local wpi = require ("wiringlua")
local ADDR = 0x27
local BLEN = 1
local fd = 0
function write_word(data)
local temp = data
if BLEN == 1 then
temp = bit.bor(temp, 0x08)
else
temp = bit.band(temp, 0xF7)
end
wpi.i2c.write(fd, temp)
end
function send_command(comm)
local buf
buf = bit.band(comm, 0xF0)
buf = bit.bor(buf, 0x04)
write_word(buf)
wpi.timer.delay(1)
buf = bit.band(buf, 0xFB)
write_word(buf)
buf = bit.lshift(bit.band(comm, 0x0F), 4)
buf = bit.bor(buf, 0x04)
write_word(buf)
wpi.timer.delay(1)
buf = bit.band(buf, 0xFB)
write_word(buf)
end
function send_data(data)
local buf
buf = bit.band(data, 0xF0)
buf = bit.bor(buf, 0x05)
write_word(buf)
wpi.timer.delay(1)
buf = bit.band(buf, 0xFB)
write_word(buf)
buf = bit.lshift(bit.band(data, 0x0F), 4)
buf = bit.bor(buf, 0x05)
write_word(buf)
wpi.timer.delay(1)
buf = bit.band(buf, 0xFB)
write_word(buf)
end
function init()
send_command(0x33)
wpi.timer.delay(5)
send_command(0x32)
wpi.timer.delay(5)
send_command(0x28)
wpi.timer.delay(5)
send_command(0x0C)
wpi.timer.delay(5)
send_command(0x01)
wpi.i2c.write(fd, 0x08)
end
function clear()
send_command(0x01)
end
function write(x, y, string)
local addr, tmp
if x < 0 then x = 0 end
if x > 15 then x = 15 end
if y < 0 then y = 0 end
if y > 1 then y = 1 end
addr = 0x80 + 0x40 * y + x
send_command(addr)
tmp = #string
for i=1,#string do
send_data(string.byte(string:sub(i,i)))
end
end
fd = wpi.i2c.setup(ADDR)
init()
print("FD = " .. fd)
-- button input
local button = 0 -- pin number
wpi.setup()
wpi.setMode(button, wpi.input)
-- notification leds
local green = 5
local blue = 1
wpi.setMode(green, wpi.output)
wpi.setMode(blue, wpi.output)
wpi.write(green, wpi.high)
--wpi.write(blue, wpi.high)
-- game loop
local ticrate = 1/25
local nexttime = wpi.timer.getMillis()
local running = true
local enemies = {}
local pos=1
local npos=0
local begin=true
local gameover=false
local still=false
local pressed = false
local dt = 0
local frames = 0
local move = 0
local speed = 15
local score = 0
local spawn = 100
local add = 0
while running do
pressed = false
if wpi.read(button) == 0 then
if not still then
still=true
pressed=true
else
pressed=false
end
else
pressed=false
still=false
end
if begin then
write(0, 0, "---MAIN MENU----")
write(2, 1, "TAP TO BEGIN")
-- TODO: if tapped then begin = false end
if pressed then
print("beginning " .. wpi.read(button))
begin = false
clear()
wpi.write(green, wpi.low)
wpi.write(blue, wpi.high)
end
elseif gameover then
write(0, 0, "GAME OVER")
write(0, 1, "SCORE: " .. score)
--write(12, 1, "" .. frames)
if pressed and frames > 10 then
wpi.write(green, wpi.high)
enemies = {}
gameover = false
begin = true
score = 0
add = 0
speed = 15
spawn = 100
clear()
end
else
-- Game code starts here
--clear()
if pressed then
if pos==0 then npos = 1 else npos = 0 end
end
if npos ~= pos then
write(0, pos, " ")
pos = npos
write(0, pos, "@")
end
-- spawn enemies (fuuuuck)
if frames > spawn then
table.insert(enemies, {x=15, y=math.random(0, 1)})
frames = 0
end
-- move enemies and draw
if move > speed then
for i,v in ipairs(enemies) do
v.x = v.x - 1
write(v.x+1, v.y, " ")
write(v.x, v.y, "#")
if v.x == 0 and v.y == pos then gameover = true frames=0 wpi.write(blue, wpi.low) clear() end
if v.x < 0 then
score = score + 1
add = add + 1
write(v.x, v.y, " ")
if v.y == pos then
write(v.x, v.y, "@")
end
table.remove(enemies, i)
end
end
move = 0
end
end
--if add == 2 and speed > 5 then speed = speed - 2 add = 0 end
if add == 2 then
if speed > 0 then speed = speed - 2 end
if spawn > 15 then spawn = spawn - 10 end
add = 0
end
if speed < 1 then speed = 0 end
frames = frames + 1
move = move + 1
wpi.timer.delay(10)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment