# -*- coding: utf-8 -*- """ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Make GIF Animation of Prim's Algorithm ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ """ import gifmaze as gm from algorithms import prim width, height = 600, 400 # define the surface to drawn on surface = gm.GIFSurface(width, height, bg_color=0) surface.set_palette([0, 0, 0, 255, 255, 255]) # define the maze maze = gm.Maze(147, 97, mask=None).scale(4).translate((6, 6)) # define the animation environment anim = gm.Animation(surface) anim.pause(200) # run the algorithm anim.run(prim, maze, start=(0, 0), speed=30, delay=5) anim.pause(500) # save the result surface.save('prim.gif') surface.close()
import gifmaze as gm from gifmaze.algorithms import random_dfs surface = gm.GIFSurface(600, 400, bg_color=0) surface.set_palette([0, 0, 0, 255, 255, 255]) maze = gm.Maze(149, 99, mask=None).scale(4).translate((2, 2)) anim = gm.Animation(surface) anim.pause(100) anim.run(random_dfs, maze, speed=10, delay=5, trans_index=None, cmap={ 0: 0, 1: 1 }, mcl=2, start=(0, 0)) anim.pause(300) surface.save('d:\\random_dfs.gif') surface.close()
yield render(maze) maze.mark_cell(pixels[-1], color_pixel(len(pixels) - 1)) if maze.num_changes > 0: yield render(maze) order = 6 # order of the curve curve_size = (1 << order) # width & height of the curve maze_size = 2 * curve_size - 1 # pad one space between two adjacent vertices cell_size = 4 margin = 6 image_size = cell_size * maze_size + 2 * margin pixels = tuple(pixels_hilbert(curve_size)) # (x, y) coordinate of the pixels colors = [0, 0, 0] # global color table for i in range(255): rgb = hls_to_rgb((i / 256.0) % 1, 0.5, 1.0) colors += [int(round(255 * x)) for x in rgb] surface = gm.GIFSurface(image_size, image_size, bg_color=0) surface.set_palette(colors) maze = gm.Maze(maze_size, maze_size, mask=None).scale(cell_size).translate((margin, margin)) anim = gm.Animation(surface) anim.pause(100) anim.run(hilbert, maze, speed=20, delay=5, pixels=pixels) anim.pause(500) surface.save("hilbert.gif") surface.close()
# -*- coding: utf-8 -*- """ This script shows how to animate an algorithm on a maze. """ import gifmaze as gm from gifmaze.algorithms import prim # firstly define the size and color_depth of the image. width, height = 600, 400 color_depth = 2 # define a surface to draw on. surface = gm.GIFSurface(width, height, color_depth, bg_color=0) # define the colors of the wall, tree and transparent channel. surface.set_palette([0, 0, 0, 255, 255, 255, 255, 0, 255, 0, 0, 0]) # next define an animation environment to run the algorithm. anim = gm.Animation(surface) # set the speed, delay, and transparent color we want. anim.set_control(speed=20, delay=5, trans_index=3) # now we need to add a maze instance. # `region=8` means the maze is padded with border of 8 pixels. maze = anim.create_maze_in_region(cell_size=5, region=8, mask=None) # pad two seconds delay, get ready! anim.pad_delay_frame(200) # the animation runs here.
#!/usr/bin/python # -*- coding: utf-8 -*- #闻到 http://wendao123.cn #使用gifmaze这个库制作gif迷宫 #github地址:https://github.com/neozhaoliang/gifmaze import gifmaze as gm from gifmaze.algorithms import prim #首先我们需要构建一个GIFSurface对象(类似cairo的ImageSurface类), # 我们的动画将会画在这个对象上。同时,我们需要指定图片的大小 #这里bg_color=0意味着全局颜色表中的第0个颜色被用作背景颜色。 surface = gm.GIFSurface(width=600, height=400, bg_color=0) #只要你还没有最后保存图片,您可以随时定义全局颜色表格,并且必须至少指定一个RGB三元组。 surface.set_palette([0, 0, 0, 255, 255, 255]) #然后我们构建一个环境,生成的动画基于这个环境构建(类似cairo的Context类) anim = gm.Animation(surface) #然后我们设置这个动画的控制参数, #这定义了一个尺寸为149x99的迷宫,缩放为4(所以它占据了596x396像素), # 并且向右平移了2个像素,向底部平移了2个像素,使其位于图像的中心。 maze = gm.Maze(149, 99, None).scale(4).translate((2, 2)) #这里speed控制动画的速度, # delay控制连续帧之间的延迟, # trans_index是透明色彩索引, # min_code_length是将动画编码成帧的最小代码长度, # start是运行的Prim算法的起始单元格(它是单元格的位置迷宫,而不是图像中的像素)。 # cmap控制细胞如何映射到颜色,即{细胞:颜色} cmap={0: 0, 1: 1}意味着单元格的值为0(墙壁)用0索引颜色(黑色)着色,单元格的值为1(树)用1索引颜色(白色)着色。
#utf-8 import gifmaze as gm maze = anim.create_maze_in_region(cell_size=5, region=8, mask=None) from gifmaze.algorithms import prim surface = gm.GIFSurface(width=600, height=400, color_depth=2, bg_color=0) surface.set_palette([0, 0, 0, 255, 255, 255, 255, 0, 255, 0, 0, 0]) anim = gm.Animation(surface) anim.set_control(speed=20, delay=5, trans_index=0) anim.pad_delay_frame(200) prim(maze, start=(0, 0)) anim.pad_delay_frame(500) surface.save("p.gif") surface.close()