from pyprocessing import *
import random
scale = 10
cols, rows = 50, 40
world = []
for n in range(cols):
world.append([0]*rows)
aliveFill = 128
backgroundFill = 0
def drawCell(x, y, alive):
if alive == 1:
fill(aliveFill)
else:
fill(backgroundFill)
ellipse(x*scale, y*scale, 8, 8)
def checkNeighbours(x, y):
neighbours = 0
for dx in [-1,0,1]:
for dy in [-1,0,1]:
if dy==0 and dx==0:
continue
if (x+dx >= 0 and x+dx < cols) and (y+dy >= 0 and y+dy < rows) and (world[x+dx][y+dy] == 1 or world[x+dx][y+dy] == -1):
neighbours = neighbours + 1
return neighbours
def setup():
size(500,400)
frameRate(5)
background(0)
for x in range(cols):
for y in range(rows):
world[x][y] = random.randint(0,1)
if world[x][y] == 1:
drawCell(x, y, 1)
def draw():
print "test"
for x in range(cols):
for y in range(rows):
num = checkNeighbours(x,y)
if world[x][y] == 0 and num == 3:
world[x][y] = 8
drawCell(x, y, 1)
elif world[x][y] == 1 and (num < 2 or num > 3):
world[x][y] = -1
drawCell(x, y, 0)
for x in range(cols):
for y in range(rows):
if world[x][y] == 8:
world[x][y] = 1
elif world[x][y] == -1:
world[x][y] = 0
run()