Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 80 lines (64 sloc) 2.62 KB
#!/usr/bin/env python
import life
import numpy
import pygame
import argparse
import pyopencl as cl
import sys
life =
rulesets = [ 'conway', 'conway_fading', 'quad', 'quad_fading', 'quad2', 'rule101' ]
def get_ruleset(l, s):
return (getattr(l, s + '_init'),
getattr(l, s + '_steps'),
getattr(l, s + '_render'),
getattr(l, s + '_uninit'))
def next_ruleset(l, s, d):
ruleset = rulesets[(rulesets.index(s) + d) % len(rulesets)]
return ruleset, get_ruleset(l, ruleset)
parser = argparse.ArgumentParser(description='The Game of Life!')
parser.add_argument('--variant', metavar='RULES', type=str, choices=rulesets, default='conway',
help='The variant of the game to run')
parser.add_argument('--width', metavar='INT', type=int, default=800,
help='Width of the world')
parser.add_argument('--height', metavar='INT', type=int, default=600,
help='Height of the world')
parser.add_argument('--steps', metavar='INT', type=int, default=1,
help='Number of simulation steps to perform per frame')
args = parser.parse_args()
screen = pygame.display.set_mode(size)
surface = pygame.Surface(size, depth=32)
initworld = numpy.random.choice([True, False], size=size)
ruleset = args.variant
life_init, life_steps, life_render, life_uninit = get_ruleset(life, ruleset)
life_state = life_init(initworld)
def render():
frame = life_render(life_state)
# We get back a PyOpenCL array. It is mostly compatible with
# Numpy, but Pygame really wants a proper Numpy array, so we use
# the get() method to obtain that.
pygame.surfarray.blit_array(surface, frame.get())
screen.blit(surface, (0, 0))
def switch_rules(d):
global ruleset, life_state, life_init, life_steps, life_render, life_uninit
bools = life_uninit(life_state)
ruleset, (life_init, life_steps, life_render, life_uninit) = next_ruleset(life, ruleset, d)
life_state = life_init(bools)
while running:
life_state = life_steps(steps, life_state)
for event in pygame.event.get():
if event.type == pygame.QUIT:
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
if event.key == pygame.K_LEFT:
if event.key == pygame.K_ESCAPE:
if event.unicode == 'r':
life_state = life_init(numpy.random.choice([True, False], size=size))