import random
from processing.core import PApplet
scale = 10
cols, rows = 100, 100
world = []
for n in range(rows):
world.append([0]*cols)
aliveFill = 128
backgroundFill = 0
def drawCell(x, y, alive):
if alive == 1:
p.fill(aliveFill)
else:
p.fill(backgroundFill)
p.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
class Processor(PApplet):
def __init__(self):
global p
p = self
def setup(self):
p.size(1000,1000)
p.frameRate(60)
p.background(0)
p.translate(5,5)
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(self):
p.translate(5,5)
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
import pawt
pawt.test(Processor())