import sys from OpenGL.GLUT import * from OpenGL.GLU import * from OpenGL.GL import * from copy import deepcopy as copy from random import randint MAX = 10 class Vertex: def __init__(self): self.__right = 0 self.__top = 0 self.__left = 0 self.__down = 0 self.__neighbors = [self.__right, self.__top, self.__left , self.__down] def clear(self): self.__right = 0 self.__top = 0 self.__left = 0 self.__down = 0 def set_right(self, right): self.__right = right def set_top(self, top ): self.__top = top def set_left(self, left ): self.__left = left def set_down(self, down ): self.__down = down def set_neighbors(self, neighbors): self.__neighbors = neighbors def get_right(self): return self.__right def get_top(self): return self.__top def get_left(self): return self.__left def get_down(self): return self.__down def get_neighbors(self): return self.__neighbors lattice = [[Vertex( ) for i in xrange(MAX)] for j in xrange(MAX)] old_lattice = copy(lattice) #[[Vertex( ) for i in xrange(MAX)] # for j in xrange(MAX)] def collide(i, j): global lattice, old_lattice if old_lattice[i][j].get_left( ) == 1 and old_lattice[i][j].get_right( ) == 1 and\ old_lattice[i][j].get_top( ) == 0 and old_lattice[i][j].get_down( ) == 0: lattice[i][j].set_top(1) lattice[i][j].set_bottom(1) elif old_lattice[i][j].get_left( ) == 0 and old_lattice[i][j].get_right( ) == 0 and\ old_lattice[i][j].get_top( ) == 1 and old_lattice[i][j].get_down( ) == 1: lattice[i][j].set_left(1) lattice[i][j].set_right(1) def propigate(i, j): global lattice, old_lattice old_lattice[i][j].set_left(lattice[i][j-1].get_left( )) old_lattice[i][j].set_right(lattice[i][j+1].get_left( )) old_lattice[i][j].set_down(lattice[i+1][j].get_left( )) old_lattice[i][j].set_top(lattice[i-1][j].get_left( )) def next( ): global lattice, old_lattice for i in xrange(MAX): for j in xrange(MAX): collide(i,j) clear(old_lattice) for i in xrange(MAX-1): for j in xrange(MAX-1): propigate(i,j) lattice = copy(old_lattice) def clear(m): for i in xrange(MAX): for j in xrange(MAX): m[i][j].clear( ) def display( ): global lattice, old_lattice glClear(GL_COLOR_BUFFER_BIT) glBegin(GL_POINTS) for i in xrange(MAX): for j in xrange(MAX): a = lattice[i][j].get_neighbors( ) for k in xrange(4): if a[k] == 0: glColor(1.0,0,0) else: glColor(1,1,1) glVertex(100 + (i*3 + k%2)*2, 100 + (3*j + k%2 + 1)*2) glEnd() glutSwapBuffers( ) def idle( ): glutPostRedisplay( ) def init( ): next( ) glClear(0) glColor(1,1,1) glMatrixMode(GL_PROJECTION) glLoadIdentity( ) gluOrtho2D(0, 400, 0, 400) glPointSize(4) if __name__ == "__main__": glutInit(sys.argv) glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB) glutInitWindowSize(400, 400) glutInitWindowPosition(0,0) glutCreateWindow("hpp") init( ) glutDisplayFunc(display) glutIdleFunc(idle) glutMainLoop( )