Skip to content

Instantly share code, notes, and snippets.

@aparrish
Created April 13, 2011 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aparrish/917624 to your computer and use it in GitHub Desktop.
Save aparrish/917624 to your computer and use it in GitHub Desktop.
simple cellular automata + text in processing.py
# requires processing.py: https://github.com/jdf/processing.py
#
# you'll need pcsenior.ttf (from here: http://www.zone38.net/font/) in a data/ directory
# (though you can also use any font you'd like)
from random import randrange
class Cell(object):
def __init__(self, status):
self.current = status
self.next = False
def die(self):
self.next = False
def live(self):
self.next = True
def maintain(self):
self.next = self.current
def step(self):
self.current = self.next
def alive(self):
return self.current
def __str__(self):
if self.current:
return '#'
else:
return '.'
class World(object):
def __init__(self, xsize, ysize, randomly=True):
self.grid = list()
self.xsize = xsize
self.ysize = ysize
for i in range(xsize):
tmp = list()
for j in range(ysize):
if randrange(3) == 0 and randomly:
tmp.append(Cell(True))
else:
tmp.append(Cell(False))
self.grid.append(tmp)
def get(self, xpos, ypos):
return self.grid[xpos][ypos]
def set(self, xpos, ypos, newcell):
self.grid[xpos][ypos] = newcell
def step(self):
# calculate what the next state should be
for i in range(self.xsize):
for j in range(self.ysize):
ncount = self.sum_neighbors(i, j)
cell = self.grid[i][j]
if cell.alive() and ncount in [1, 2, 3, 4, 5]:
cell.maintain()
elif not(cell.alive()) and ncount in [3]:
cell.live()
else:
cell.die()
# move each cell to its next state
for i in range(self.xsize):
for j in range(self.ysize):
self.grid[i][j].step()
def sum_neighbors(self, x, y):
total = 0
for i in range(x - 1, x + 2):
for j in range(y - 1, y + 2):
xindex = i % self.xsize
yindex = j % self.ysize
if self.grid[xindex][yindex].alive():
total += 1
if self.grid[x][y].alive():
total -= 1
return total
def __str__(self):
output = ''
for y in range(self.ysize):
for x in range(self.xsize):
output += str(self.grid[x][y])
output += "\n"
return output
world = World(100, 100)
font = None
current_index = 0
def blit_to_world(world, font, fsize, s):
pg = createGraphics(world.xsize, world.ysize, JAVA2D)
pg.beginDraw()
pg.background(0)
pg.fill(255)
pg.textFont(font)
pg.textSize(fsize)
pg.textAlign(CENTER, CENTER)
pg.text(s, pg.width/2, pg.height/2)
pg.endDraw()
for x in range(world.xsize):
for y in range(world.ysize):
if red(pg.get(x, y)) == 0:
world.set(x, y, Cell(False))
else:
world.set(x, y, Cell(True))
def next_letter():
global current_index, world
world = World(100, 100)
blit_to_world(world, font, 16, 'abcdefghijklmnopqrstuvwxyz'[current_index])
current_index += 1
def setup():
global font
size(400, 400)
font = createFont("pcsenior.ttf", 8)
def draw():
if frameCount % 100 == 1:
background(0)
next_letter()
noStroke()
fill(0, 32)
rect(0, 0, width, height)
rwidth = width / float(world.xsize)
rheight = height / float(world.ysize)
for x in range(world.xsize):
for y in range(world.ysize):
if world.get(x, y).alive():
pushMatrix()
translate(x*rwidth, y*rheight)
fill(255)
rect(0, 0, rwidth, rheight)
popMatrix()
world.step()
saveFrame("output-#####.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment