public
Created

Array-oriented version of the game of life

  • Download Gist
life.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
from numpy.random import rand
from numpy import r_, ix_, uint8, roll
import matplotlib.pyplot as plt
import time
 
size = 200
GRID = (rand(size,size) > 0.75).astype(uint8)
# Rotate indices because the world is round
indx = r_[0:size]
up = roll(indx, -1)
down = roll(indx, 1)
 
fig = plt.figure()
ax = fig.add_subplot(111)
img = ax.imshow(GRID,interpolation='nearest',cmap='gray')
 
def update_image(idleevent):
if update_image.i==2000:
return False
 
global GRID
neighbors = GRID[up,:] + GRID[down,:] + GRID[:,up] + GRID[:,down] + \
GRID[ix_(up,up)] + GRID[ix_(up,down)] + GRID[ix_(down,up)] + \
GRID[ix_(down,down)]
GRID = (neighbors == 3) | (GRID & (neighbors==2))
 
img.set_data(GRID)
fig.canvas.draw_idle()
time.sleep(0.02)
update_image.i += 1
 
update_image.i = 0
 
import wx
wx.EVT_IDLE(wx.GetApp(), update_image)
plt.show()

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.