load_library :cell
attr_reader :cells, :black, :colours, :spore1, :spore2, :spore3, :spore4
MAX_CELLS = 8000
RUNS_PER_LOOP = 10000
def setup
size(640, 360)
frame_rate(24)
@black = color(0, 0, 0)
@spore1 = color(128, 172, 255)
@spore2 = color(64, 128, 255)
@spore3 = color(255, 128, 172)
@spore4 = color(255, 64, 128)
@colours =[spore1, spore2, spore3, spore4]
reset!
end
def reset!
clear_screen
seed
end
def seed
@cells = []
MAX_CELLS.times do
cx = rand(0 ... width)
cy = rand(0 ... height)
if (getpix(cx, cy) == black)
setpix(cx, cy, colours.sample)
cells << Cell.new(self, cx, cy)
end
end
end
def draw
RUNS_PER_LOOP.times do
cells.sample.run
end
end
def clear_screen
background(0)
end
def setpix(x, y, c)
while (x < 0)
x += width
end
while (x > (width - 1))
x -= width
end
while (y < 0)
y += height
end
while (y > (height - 1))
y -= height
end
set(x, y, c)
end
def getpix(x, y)
while (x < 0)
x += width
end
while (x > (width - 1))
x -= width
end
while (y < 0)
y += height
end
while (y > (height - 1))
y -= height
end
get(x, y)
end
def mouse_pressed
reset!
end
class Cell
attr_reader :outer, :x, :y, :width, :height, :black
attr_reader :spore1, :spore2, :spore3, :spore4
def initialize(outer, xin = 0, yin = 0)
@outer, @x, @y = outer, xin, yin
@width, @height, @black = outer.width, outer.height, outer.black
@spore1, @spore2, @spore3, @spore4 = *outer.colours
end
def run
while (x < 0)
@x += width
end
while (x > (width - 1))
@x -= width
end
while (y < 0)
@y += height
end
while (y > (height - 1))
@y -= height
end
my_color = outer.getpix(x, y)
if (my_color == spore1)
if (outer.getpix(x - 1, y + 1) == black && outer.getpix(x + 1, y + 1) == black && outer.getpix(x, y + 1) == black)
move(0, 1)
elsif (outer.getpix(x - 1, y) == spore2 && outer.getpix(x - 1, y - 1) != black)
move(0, -1)
elsif (outer.getpix(x - 1, y) == spore2 && outer.getpix(x - 1, y - 1) == black)
move(-1, -1)
elsif (outer.getpix(x + 1, y) == spore1 && outer.getpix(x + 1, y - 1) != black)
move(0, -1)
elsif (outer.getpix(x + 1, y) == spore1 && outer.getpix(x + 1, y - 1) == black)
move(1, -1)
else
move(rand(1 .. 2), 0)
end
elsif (my_color == spore2)
if (outer.getpix(x - 1, y + 1) == black && outer.getpix(x + 1, y + 1) == black && outer.getpix(x, y + 1) == black)
move(0, 1)
elsif (outer.getpix(x + 1, y) == spore1 && outer.getpix(x + 1, y - 1) != black)
move(0, -1)
elsif (outer.getpix(x + 1, y) == spore1 && outer.getpix(x + 1, y - 1) == black)
move(1, -1)
elsif (outer.getpix(x - 1, y) == spore2 && outer.getpix(x - 1, y - 1) != black)
move(0, -1)
elsif (outer.getpix(x - 1, y) == spore2 && outer.getpix(x - 1, y - 1) == black)
move(-1, -1)
else
move(rand(1 .. 2), 0)
end
elsif (my_color == spore3)
if (outer.getpix(x - 1, y - 1) == black && outer.getpix(x + 1, y - 1) == black && outer.getpix(x, y - 1) == black)
move(0, -1)
elsif (outer.getpix(x - 1, y) == spore4 && outer.getpix(x - 1, y + 1) != black)
move(0, 1)
elsif (outer.getpix(x - 1, y) == spore4 && outer.getpix(x - 1, y + 1) == black)
move(-1, 1)
elsif (outer.getpix(x + 1, y) == spore3 && outer.getpix(x + 1, y + 1) != black)
move(0, 1)
elsif (outer.getpix(x + 1, y) == spore3 && outer.getpix(x + 1, y + 1) == black)
move(1, 1)
else
move(rand(1 .. 2), 0)
end
elsif (my_color == spore4)
if (outer.getpix(x - 1, y - 1) == black && outer.getpix(x + 1, y - 1) == black && outer.getpix(x, y - 1) == black)
move(0, -1)
elsif (outer.getpix(x + 1, y) == spore3 && outer.getpix(x + 1, y + 1) != black)
move(0, 1)
elsif (outer.getpix(x + 1, y) == spore3 && outer.getpix(x + 1, y + 1) == black)
move(1, 1)
elsif (outer.getpix(x - 1, y) == spore4 && outer.getpix(x - 1, y + 1) != black)
move(0, 1)
elsif (outer.getpix(x - 1, y) == spore4 && outer.getpix(x - 1, y + 1) == black)
move(-1, 1)
else
move(rand(1 .. 2), 0)
end
end
end
def move(dx, dy)
if (outer.getpix(x + dx, y + dy) == black)
outer.setpix(x + dx, y + dy, outer.getpix(x, y))
outer.setpix(x, y, black)
@x += dx
@y += dy
end
end
end
No comments:
Post a Comment