Skip to content

Instantly share code, notes, and snippets.

@simonbyrne
Created June 22, 2017 20:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simonbyrne/97f5692282734d921322883b5daa467b to your computer and use it in GitHub Desktop.
Save simonbyrne/97f5692282734d921322883b5daa467b to your computer and use it in GitHub Desktop.
snake
using SenseHat
function newblob(snake)
while true
x = rand(1:8)
y = rand(1:8)
if (x,y) ∉ snake
return (x,y)
end
end
end
function opposite(d)
if d == Stick.UP
Stick.DOWN
elseif d == Stick.DOWN
Stick.UP
elseif d == Stick.LEFT
Stick.RIGHT
elseif d == Stick.RIGHT
Stick.LEFT
end
end
function main()
led_clear()
LED = led_matrix()
snake_col = RGB565(1,1,1)
blob_col = RGB565(1,0,0)
dir = Stick.RIGHT
snake = [(2,5),(3,5)]
for (x,y) in snake
LED[x,y] = snake_col
end
blob_x, blob_y = newblob(snake)
LED[blob_x, blob_y] = blob_col
@schedule for e in sticktask()
if e.state == Stick.PRESS && e.direc != Stick.MIDDLE && e.direc != opposite(dir)
dir = e.direc
end
end
x,y = snake[end]
delay = 0.5
while true
sleep(delay)
if dir == Stick.RIGHT
x += 1
elseif dir == Stick.LEFT
x -= 1
elseif dir == Stick.UP
y -= 1
elseif dir == Stick.DOWN
y += 1
end
if (x,y) == (blob_x, blob_y)
blob_x, blob_y = newblob(snake)
LED[blob_x, blob_y] = blob_col
else
x_old, y_old = shift!(snake)
LED[x_old, y_old] = RGB565(0,0,0)
end
if x < 1 || x > 8 || y < 1 || y > 8 || (x,y) in snake
break
end
push!(snake, (x,y))
LED[x, y] = snake_col
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment