Created
September 18, 2018 22:03
-
-
Save rross101/80966e16d7067d103370791f3b704ee2 to your computer and use it in GitHub Desktop.
Bounce for Norns
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- bounce by declutter; can't remember who first created this app in Max for the monome | |
--- to-do: | |
--- sound on collision | |
--- multiple balls | |
g = grid.connect() | |
function init() | |
--- set up variables and grid | |
position = {1,1} | |
v = {1,1} | |
map = {} | |
for i = 1,16 do | |
map[i] = {} | |
for j = 1,8 do | |
map[i][j] = 0 | |
end | |
end | |
--- initial state to make it more interesting | |
map[4][1] = 1 | |
map[4][2] = 1 | |
map[4][3] = 1 | |
map[9][6] = 1 | |
map[9][7] = 1 | |
map[9][8] = 1 | |
---timing | |
counter = metro.alloc() | |
counter.time = 0.1 | |
counter.count = -1 | |
counter.callback = count | |
counter:start() | |
end | |
function count() | |
--- move ball | |
position[1] = position[1] + v[1] | |
position[2] = position[2] + v[2] | |
--- detect edges | |
--- this is ugly; there must be a prettier way | |
if (position[1] == 16) or (position[1] == 1) or (map[position[1]+1][position[2]] == 1) or (map[position[1]-1][position[2]] == 1) then v[1] = (-1)*v[1] end | |
if (position[2] == 8) or (position[2] == 1) or (map[position[1]][position[2]+1] == 1) or (map[position[1]][position[2]-1] == 1) then v[2] = (-1)*v[2] end | |
--- redraw grid | |
g.all(0) | |
for i = 1,16 do | |
for j = 1,8 do | |
g.led(i,j,(map[i][j] == 1) and 5 or 0) | |
end | |
end | |
g.led(position[1],position[2],15) | |
g.refresh() | |
end | |
g.event = function(x,y,z) | |
--- key presses build walls | |
if z == 1 then | |
map[x][y] = 1 - map[x][y] | |
end | |
g.refresh() | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@trentgill somehow I missed this comment. This is excellent - will do some looking at it. Thank you.