Paste: game of life v2
Author: | daniel gabriele |
Mode: | python |
Date: | Sat, 6 Jun 2009 17:53:03 |
Plain Text |
'''
Conway's Game of Life '''
import random
import itertools
import pygame
pygame.init()
colors = pygame.color.THECOLORS
ON, OFF = 1, 0
class Cell (object):
def __init__ (self, **kwargs):
self.size = (25, 25)
self.oncolor = colors['green']
self.offcolor = colors['black']
self.state = OFF
self.position = None
self.relpos = None
for k in kwargs:
setattr(self, k, kwargs[k])
self._init_default_config()
@property
def width (self):
return self.size[0]
@property
def height (self):
return self.size[1]
def _init_default_config (self):
self.image = pygame.Surface(self.size)
self.image.set_alpha(0)
self.rect = self.image.get_rect()
if self.state:
self.image.fill(self.oncolor)
else:
self.image.fill(self.offcolor)
if hasattr(self, 'position'):
self.rect.topleft = self.position
def update (self):
if self.state:
self.image.fill(self.oncolor)
self.image.set_alpha(255)
else:
self.image.fill(self.offcolor)
def __repr__ (self):
return "<cell>"
class Grid (object):
def __init__ (self, gridsize, cellsize):
self.w, self.h = gridsize
self.cw, self.ch = cellsize
self.n = self.w * self.h
self.cells = {}
relx = rely = 0
absx = absy = 0
for i in xrange(self.n):
cell = Cell( size=(self.cw, self.ch),
position=(absx, absy),
relpos=(relx, rely),
)
if i % 2 == 0 or i % 5.5 == 0: cell.state = ON
cell.history = cell.state
self.cells[(relx, rely)] = cell
absx += cell.width
rely += 1
if (i + 1) % self.w == 0 and i != 0:
absx = 0
relx += 1
absy += cell.height
rely = 0
def update (self):
for cell in self.cells.itervalues():
cell.history = cell.state
cell.update()
x, y = cell.relpos
neighbors = self.get_neighbors(x, y)
cell.nhood = sum([1 for n in neighbors if self.cells[n].history])
if cell.nhood < 4 and cell.nhood > 1 and cell.history:
cell.state = ON
elif cell.nhood == 3 and not cell.history:
cell.state = ON
elif cell.nhood == 4 or cell.nhood < 2 and cell.history:
cell.state = OFF
screen.blit(cell.image, cell.position)
def get_neighbors (self, x, y):
for point in itertools.product(range(-1,2), range(-1,2)):
_x, _y = point[0]+x, point[1]+y
if (x, y) == (_x, _y):
continue
if _x < 0:
_x = self.w-1
elif _x == self.w:
_x = self.w-1
if _y < 0:
_y = self.h-1
elif _y == self.h:
_y = self.h-1
yield (_x, _y)
def randcolor (self, lower=180, upper=240, alpha=255):
cvals = range(lower, upper)
r, g, b = [random.choice(cvals) for i in range(3)]
return pygame.Color(r, g, b, alpha)
if __name__ == '__main__':
grid = Grid((50, 50), (14,14))
screen = pygame.display.set_mode((grid.w*grid.cw, grid.h*grid.ch))
bg = pygame.Surface(screen.get_size())
clock = pygame.time.Clock()
running = True
bg.fill((0,0,0))
bg.set_alpha(10)
while running:
clock.tick(36)
for e in pygame.event.get():
if not hasattr(e, 'key'):
continue
if e.key == pygame.K_q or e.key == pygame.K_ESCAPE:
running = False
grid.update()
pygame.display.flip()
New Annotation