Skip to content

Instantly share code, notes, and snippets.

@limtbk
Forked from dmitmel/game_of_life.pyde
Last active December 2, 2015 21:32
Show Gist options
  • Save limtbk/f4ef56cc621b6f89f517 to your computer and use it in GitHub Desktop.
Save limtbk/f4ef56cc621b6f89f517 to your computer and use it in GitHub Desktop.
Game of life
s1x = 30
s1y = 30
s2 = 20
gen = []
next_gen = []
clicks = []
zero = 50
one = 250
cell_stroke = 60
earsing = 0
adding = 1
changing = 2
kClick = False
population = 0
generation = 0
keyDelay = 0
maxKeyDelay = 40
maxFrames = 40
mouseMode = adding
def grid_filler(sx, sy, grid, item):
for x in range(sx):
grid.append([])
for y in range(sy):
grid[x].append(item)
grid_filler(s1x, s1y, gen, 0)
grid_filler(s1x, s1y, next_gen, 0)
grid_filler(s1x, s1y, clicks, False)
for x in range(s1x):
for y in range(s1y):
if gen[x][y] == one:
population += 1
def cell(x, y):
global s1x, s1y, gen
c = 0
if x >= 0 and x < s1x:
if y >= 0 and y < s1y:
c = gen[x][y]
return c;
def neighbours(x, y):
n = -cell(x, y)
for ix in range(3):
for iy in range(3):
n += cell(x + ix - 1, y + iy - 1)
return n
def show():
global s1x, s1y, s2, gen, next_gen, one, zero
for x in range(s1x):
for y in range(s1y):
if gen[x][y] == 0:
fill(zero)
stroke(cell_stroke)
else:
fill(one)
stroke(cell_stroke)
rect(s2 * x, s2 * y, s2, s2)
for x in range(s1x):
for y in range(s1y):
fill(128)
textAlign(CENTER, CENTER)
text(neighbours(x, y), s2 * (x+0.5), s2 * (y+0.5))
def mouseDraw():
global s1x, s1y, s2, gen, population, one, zero, next_gen, mouseMode, clicks, adding, earsing, changing
if mousePressed:
x = mouseX / s2
y = mouseY / s2
clicks[x][y] = True
if mouseMode == changing:
if gen[x][y] == 0:
mouseMode = adding
else:
mouseMode = earsing
if mouseMode == adding:
next_gen[x][y] = 1
population += 1
else:
next_gen[x][y] = 0
population -= 1
for x in range(s1x):
for y in range(s1y):
gen[x][y] = next_gen[x][y]
show()
def setup():
global s1x, s1y, s2
size(s1x * s2 + 1, s1y * s2 + 1)
show()
def draw():
global s1x, s1y, s2, gen, next_gen, zero, one, state,kClick, generation, population, keyDelay, maxKeyDelay, maxFrames, mouseMode, changing
mouseDraw()
show()
if not(mousePressed):
for x in range(s1x):
for y in range(s1y):
if clicks[x][y]: clicks[x][y] = False
mouseMode = changing
if not(keyPressed):
kClick = False
keyDelay = 0
if keyPressed: keyDelay += 1
if keyDelay > maxKeyDelay: keyDelay = maxKeyDelay
if keyPressed and (not(kClick) or keyDelay > maxKeyDelay - 1):
kClick = True
if keyDelay > maxKeyDelay - 1:
keyDelay = maxFrames
generation += 1
print 'current generation: ', generation
print 'current population: ', population
for x in range(s1x):
for y in range(s1y):
nCount = neighbours(x, y)
next_gen[x][y] = gen[x][y]
if nCount == 3:
next_gen[x][y] = 1
elif nCount < 2 or nCount > 3:
next_gen[x][y] = 0
if next_gen[x][y] == 0 and nCount == 3:
population += 1
elif next_gen[x][y] == 1 and (nCount < 2 or nCount > 3):
population -= 1
for x in range(s1x):
for y in range(s1y):
gen[x][y] = next_gen[x][y]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment