#!/usr/bin/env python from numpy import * from numpy.random import randint import pygame import sys def draw_states(states): screen.fill((200,200,220)) n = shape(states)[-2] for i in range(n): for j in range(n): pygame.draw.circle(screen, (0,255,0), (int(pix2site*i),int(pix2site*j)), int(.2*pix2site)) if states[i,j][0]: pygame.draw.circle(screen, (240,0,0), (int(pix2site*(i-.25)),int(pix2site*j)), int(.1*pix2site)) if states[i,j][1]: pygame.draw.circle(screen, (240,0,0), (int(pix2site*i),int(pix2site*(j-.25))), int(.1*pix2site)) if states[i,j][2]: pygame.draw.circle(screen, (240,0,0), (int(pix2site*i),int(pix2site*(j+.25))), int(.1*pix2site)) if states[i,j][3]: pygame.draw.circle(screen, (240,0,0), (int(pix2site*(i+.25)),int(pix2site*j)), int(.1*pix2site)) pygame.display.update() ''' we'd like to generate a rule table to do look-ups into. for HPP, state at each site is given by 4 bits: 1 | 0-O-3 | 2 0000:0000 0001:1000 0010:0100 0011:1100 0100:0010 0101:1010 0110:1001 #collision 0111:1110 1000:0001 1001:0110 #collision 1010:0101 1011:1101 1100:0011 1101:1011 1110:0111 1111:1111 ''' rules = array([[0,0,0,0],[1,0,0,0],[0,1,0,0],[1,1,0,0], [0,0,1,0],[1,0,1,0],[1,0,0,1],[1,1,1,0], [0,0,0,1],[0,1,1,0],[0,1,0,1],[1,1,0,1], [0,0,1,1],[1,0,1,1],[0,1,1,1],[1,1,1,1]], dtype=int8).reshape(2,2,2,2,4) def apply_rules(world): return rules[tuple([world[...,i] for i in range(shape(world)[-1])])] n = 2*20 #world has n**2 sites pix2site = 30 sr = 5 #radius of seeded region #world = zeros((n,n,4),dtype=int8) world = randint(0,2,(n,n,4)) temp = world world[n/2-sr:n/2+sr, n/2-sr:n/2+sr] = array([1,1,1,1],dtype=int8) pygame.init() screen = pygame.display.set_mode((pix2site*n,pix2site*n)) clock = pygame.time.Clock() i=0 while(True): msElapsed = clock.tick(10) for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit(); sys.exit(); world = apply_rules(world) draw_states(world) pygame.image.save(screen,"frames/hpp1/%04d.bmp"%i) i+=1 #transport temp[:-1,:,3] = world[1:,:,0] temp[1:,:, 0] = world[:-1,:,3] temp[:,:-1,1] = world[:,1:,2] temp[:,1:, 2] = world[:,:-1,1] world = temp draw_states(world) pygame.image.save(screen,"frames/hpp1/%04d.bmp"%i) i+=1