def main(): cell_size = 10 grid_size = 80, 50 grid = build_life_grid(*grid_size) pg.display.init() pg.display.set_mode(([x * cell_size for x in grid_size]), 0, 0) surface = pg.display.get_surface() clock = pg.time.Clock() close = False pause = False one_frame = False speed = 15 while not close: if not pause or one_frame: surface.fill((20, 20, 20)) y_pos = 0 for row in grid: x_pos = 0 for cell in row: if cell: surface.fill((125, 125, 125), pg.Rect(x_pos, y_pos, cell_size, cell_size)) x_pos += cell_size y_pos += cell_size pg.display.flip() conway(grid) if one_frame: one_frame = False clock.tick(speed) events = pg.event.get() for event in events: if event.type == pg.QUIT: pg.quit() close = True break if event.type == pg.MOUSEBUTTONDOWN: if event.button == 3: one_frame = True elif event.button == 1: pause = not pause elif event.button == 4: speed += 1 elif event.button == 5: speed -= 1 if speed <= 0: speed = 1
def simulate(): H = 40 V = 30 C = conway(H, V, 'zeros') C.setPos(10, 10, 1) C.setPos(9, 10, 1) C.setPos(12, 10, 1) C.setPos(10, 9, 1) C.setPos(11, 9, 1) C.setPos(10, 11, 1) C.setPos(11, 11, 1) C.setPos(10, 20, 1) C.setPos(11, 21, 1) C.setPos(12, 19, 1) C.setPos(12, 20, 1) C.setPos(12, 21, 1) n = 0 while True: C.evolve(rule) os.system('clear') C.printDisp() print("STEP:", n) #sleep(0.1) n += 1 return True
def test_block(): block = [(0,0), (0,1), (1,0), (1,1)] result = conway(block, generations=2) assert( set(result) == set(block) )
def test_blinker(): blinker = [(-1,0), (0,0), (1,0)] result = conway(blinker, generations=2) assert( set(result) == set(blinker) )