76 lines
1.8 KiB
Text
76 lines
1.8 KiB
Text
colors = [color.black, color.yellow, color.orange,
|
|
color.brown, color.red, color.fuchsia,
|
|
color.purple, color.blue, color.navy]
|
|
|
|
rows = 48; rowRange = range(0, rows-1)
|
|
cols = 72; colRange = range(0, cols-1)
|
|
particlesOfSand = rows * cols
|
|
divBase = particlesOfSand / (colors.len - 4)
|
|
deltas = [[0,-1],[-1, 0], [1, 0],[0, 1]]
|
|
|
|
displayGrid = function(grid, td)
|
|
for y in globals.rowRange
|
|
for x in globals.colRange
|
|
colorIx = grid[y][x]
|
|
// determine the rest of the colors if > 3 by division
|
|
if colorIx > 3 then colorIx = (colorIx - 3) / divBase + 4
|
|
td.setCell x,y, colorIx
|
|
end for
|
|
end for
|
|
end function
|
|
|
|
clear
|
|
|
|
// Prepare a tile display
|
|
// Generate image used for the tiles from the defined above.
|
|
// The colors are to indicate height of a sand pile.
|
|
img = Image.create(colors.len, 1);
|
|
for i in range(0, colors.len - 1)
|
|
img.setPixel(i, 0, colors[i])
|
|
end for
|
|
|
|
grid = []
|
|
for y in rowRange
|
|
row = []
|
|
for x in colRange
|
|
row.push(0)
|
|
end for
|
|
grid.push(row)
|
|
end for
|
|
|
|
grid[rows/2][cols/2] = particlesOfSand
|
|
|
|
display(4).mode = displayMode.tile
|
|
td = display(4)
|
|
td.cellSize = 640/48 // size of cells on screen
|
|
td.extent = [cols, rows]
|
|
td.overlap = 0 // adds a small gap between cells
|
|
td.tileSet = img; td.tileSetTileSize = 1
|
|
td.clear 0
|
|
|
|
toTopple = []
|
|
for y in rowRange
|
|
for x in colRange
|
|
if grid[y][x] > 3 and toTopple.indexOf([x,y]) == null then toTopple.push([x,y])
|
|
end for
|
|
end for
|
|
tt = time
|
|
while toTopple.len > 0
|
|
nextGen = []
|
|
for cell in toTopple
|
|
x = cell[0]; y = cell[1]
|
|
grid[y][x] -= 4
|
|
for delta in deltas
|
|
x1 = (x + delta[0]) % cols; y1 = (y + delta[1]) % rows
|
|
grid[y1][x1] += 1
|
|
end for
|
|
end for
|
|
for y in rowRange
|
|
for x in colRange
|
|
if grid[y][x] > 3 and nextGen.indexOf([x,y]) == null then nextGen.push([x,y])
|
|
end for
|
|
end for
|
|
toTopple = nextGen
|
|
displayGrid(grid, td)
|
|
end while
|
|
key.get()
|