def main(seed_file, grid_size, offsets, cutoff, frames): """ parameters ---------- :seed_file: the pattern file. :grid_size: (width, height) of the grid in the life world. :offsets: (left, top) position of the initial pattern with respect to the grid. :cutoff: cutoff of the image relative to the actual grid. :frames: number of frames in the animation. """ seed = np.array(parse(seed_file), dtype=np.bool) grid = np.zeros(grid_size).astype(np.bool) grid[offsets[0]:seed.shape[0] + offsets[0], offsets[1]:seed.shape[1] + offsets[1]] = seed pattern_name = os.path.splitext(os.path.basename(seed_file))[0] ncols, nrows = grid_size maze, surface, anim = create_animation_for_size(ncols - 2 * cutoff, nrows - 2 * cutoff, cell_size, lw, margin, wall_init=1) surface.set_palette([0, 0, 0, 200, 200, 200, 255, 255, 255]) def conway(maze, encode_func, grid, frames): bar = tqdm(total=frames, desc="Running the {} pattern".format(pattern_name)) def copy(grid, maze): for x in range(cutoff, grid.shape[0] - cutoff): for y in range(cutoff, grid.shape[1] - cutoff): maze.mark_cell((2 * (x - cutoff), 2 * (y - cutoff)), 2 + grid[x][y]) for _ in range(frames): copy(grid, maze) yield encode_func(maze) grid = evolve(grid) bar.update(1) bar.close() anim.run(conway, maze, grid=grid, frames=frames, mcl=2, delay=10, cmap={ 0: 2, 3: 0 }) anim.pause(500) anim.save(pattern_name + ".gif")
def example1(): """The most simple maze animation example. """ maze, surface, anim = create_animation_for_size(width, height, cell_size, lw, margin) surface.set_palette([0, 0, 0, 255, 255, 255]) anim.pause(100) anim.run(algo.random_dfs, maze, speed=30, delay=5, mcl=2) anim.pause(500) anim.save("random_dfs.gif")
def example2(): """This example shows how to use a mask image in the maze. """ _, surface, anim = create_animation_for_size(width, height, cell_size, lw, margin) surface.set_palette([0, 0, 0, 255, 255, 255]) mask = generate_text_mask((surface.width, surface.height), "UNIX", "./resources/ubuntu.ttf", 280) maze = (Maze(width, height, mask=mask).scale(cell_size).translate( (margin, margin)).setlinewidth(lw)) anim.pause(100) anim.run(algo.kruskal, maze, speed=30, delay=5, mcl=2) anim.pause(500) anim.save("kruskal.gif")
def example3(): """This example shows how to insert a background image at the beginning of the gif file while the animation is running. """ maze, surface, anim = create_animation_for_size(width, height, cell_size, lw, margin) surface.set_palette( [0, 0, 0, 255, 255, 255, 50, 50, 50, 150, 200, 100, 255, 0, 255]) anim.pause(100) anim.run(algo.prim, maze, speed=30, delay=5, trans_index=0, mcl=2) anim.pause(300) anim.insert_frame(encode_maze(maze, cmap={1: 2}, mcl=2)) anim.run(algo.dfs, maze, speed=10, trans_index=0, cmap={1: 1, 2: 4}, mcl=3) anim.pause(500) anim.save("prim-dfs.gif")
def example1(): """A basic maze animation example. Three ingredients for making an animation: maze: for running algorithms. surface: for maintaining information of the gif image. anim: for encoding the maze into frames. """ maze, surface, anim = create_animation_for_size(width, height, cell_size, lw, margin) # surface: set global palette surface.set_palette([0, 0, 0, 255, 255, 255]) anim.pause(100) # anim: encode the maze anim.run(algo.random_dfs, maze, speed=30, delay=5, mcl=2) anim.pause(500) anim.save("random_dfs.gif")
maze.mark_cell((2 * ant.x, 2 * ant.y), 2) ant.turn("left") else: grid[ant.x][ant.y] = 0 maze.mark_cell((2 * ant.x, 2 * ant.y), 3) ant.turn("right") maze.mark_cell((2 * ant.x, 2 * ant.y), 4) count += 1 if count % speed == 0: yield encode_func(maze) maze, surface, anim = create_animation_for_size( ncols, nrows, cell_size, lw, margin, bg_color=1, wall_init=1 ) surface.set_palette( [ 255, 255, 255, # white spaces 0, 0, 0, # grid line color 0, 0, 255, # dead cells 0, 255, 0, # live cells 255, 0, 0 # current position ] ) anim.show_grid(maze, bg_color=0, line_color=1) anim.pause(100) anim.run(langton, maze, speed=5, delay=3, steps=11500, mcl=3) anim.pause(500) anim.save("langton_ant.gif")
for i in range(len(pixels) - 1): maze.mark_cell(pixels[i], color_pixel(i)) maze.mark_space(pixels[i], pixels[i + 1], color_pixel(i)) if i % speed == 0: yield encode_func(maze) maze.mark_cell(pixels[-1], color_pixel(len(pixels) - 1)) yield encode_func(maze) order = 6 curve_size = 1 << order cell_size = 4 lw = 4 margin = 6 pixels = tuple(pixels_hilbert(curve_size)) maze, surface, anim = create_animation_for_size(curve_size, curve_size, cell_size, lw, margin) colors = [0, 0, 0] for i in range(1, 256): rgb = hls_to_rgb(i / 256.0, 0.5, 1.0) colors += [int(round(255 * x)) for x in rgb] surface.set_palette(colors) anim.pause(100) anim.run(hilbert, maze, speed=5, delay=5, pixels=pixels) anim.pause(500) anim.save("hilbert_curve.gif")