200,
    200,  # tree color
    255,
    0,
    255
]  # path color

for i in range(256):
    rgb = hls_to_rgb((i / 360.0) % 1, 0.5, 1.0)
    palette += [int(round(255 * x)) for x in rgb]

surface.set_palette(palette)

# create a maze instance and set its position in the image
size = (surface.width, surface.height)
mask = generate_text_mask(size, 'UST', './resources/ubuntu.ttf', 350)
maze = gm.Maze(111, 67, mask=mask).scale(4).translate((75, 56))

# create an animation environment
anim = gm.Animation(surface)

# here `trans_index=1` is for compatible with eye of gnome in linux,
# you may always use the default 0 for chrome and firefox.
anim.pause(100, trans_index=1)

# paint the background region that contains the maze
left, top, width, height = 70, 50, 450, 280
anim.paint(left, top, width, height, 0)
anim.pause(100)

# Run the maze generation algorithm.
Example #2
0
# -*- coding: utf-8 -*-
"""
This example shows how to use a mask pattern in the maze.
"""
import gifmaze as gm
from algorithms import random_dfs
from gentext import generate_text_mask

width, height = 600, 400
surface = gm.GIFSurface(width, height, bg_color=0)
surface.set_palette([0, 0, 0, 255, 255, 255])

mask = generate_text_mask((width, height), 'UNIX', './resources/ubuntu.ttf',
                          280)
maze = gm.Maze(147, 97, mask=mask).scale(4).translate((6, 6))

anim = gm.Animation(surface)
anim.pause(200)
anim.run(random_dfs, maze, start=(0, 0), speed=20, delay=5)
anim.pause(500)

surface.save('random_dfs.gif')
surface.close()
Example #3
0
author neozhaoliang
"""
import gifmaze as gm
from algorithms import kruskal, dfs, random_dfs
from algorithms import prim
from gentext import generate_text_mask

width, height = 800, 320
# define the surface to drawn on
surface = gm.GIFSurface(width, height, bg_color=0)
surface.set_palette([0, 0, 0, 235, 235, 235, 150, 200, 100, 161, 35,
                     6])  # 78, 205, 196,   161,35,6

# define the maze
mask = generate_text_mask(
    (width, height), "Je t'aime", '‪C:\Windows\Fonts\GILC____.TTF', 240
)  # C:/Windows/Fonts/STZHONGS.TTF ./resources/ubuntu.ttf 147, 97  fontsize 480
maze = gm.Maze(int(width / 4 - 3), int(height / 4 - 3),
               mask=mask).scale(4).translate((6, 6))

# define the animation environment
anim = gm.Animation(surface)
anim.pause(200)
# run the algorithm
anim.run(prim, maze, speed=50, delay=2)
anim.pause(300)

start = (0, 0)
end = (maze.width - 1, maze.height - 1)

# run the maze solving algorithm.
Example #4
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('-width',
                        type=int,
                        default=121,
                        help='width of the maze')
    parser.add_argument('-height',
                        type=int,
                        default=97,
                        help='height of the maze')
    parser.add_argument('-margin',
                        type=int,
                        default=2,
                        help='border of the maze')
    parser.add_argument('-scale',
                        type=int,
                        default=5,
                        help='size of a cell in pixels')
    parser.add_argument(
        '-loop',
        type=int,
        default=0,
        help='number of loops of the animation, default to 0 (loop infinitely)'
    )
    parser.add_argument(
        '-bits',
        metavar='b',
        type=int,
        default=8,
        help='an interger beteween 2-8 represents the color depth of the image,\
                        this parameter determines the size of the global color table.'
    )
    parser.add_argument('-filename',
                        type=str,
                        default='wilson.gif',
                        help='output file name')
    args = parser.parse_args()

    # define your favorite global color table here.
    mypalette = [0, 0, 0, 200, 200, 200, 255, 0, 255]
    # GIF files allows at most 256 colors in the global color table,
    # redundant colors will be discarded when the encoder is initialized.
    for i in range(256):
        rgb = hls_to_rgb((i / 360.0) % 1, 0.5, 1.0)
        mypalette += map(lambda x: int(round(255 * x)), rgb)

    # you may use a binary image instance of PIL's Image class here as the mask image,
    # this image must preserve the connectivity of the grid graph.
    from gentext import generate_text_mask
    mask = generate_text_mask(args.width, args.height, 'UST',
                              '../resources/ubuntu.ttf', 60)
    maze = Maze(args.width, args.height, args.margin, mask=mask)
    canvas = maze.add_canvas(scale=args.scale,
                             min_bits=args.bits,
                             palette=mypalette,
                             loop=args.loop,
                             filename=args.filename)

    # here we need to paint the blank background because the region that has not been
    # covered by any frame will be set to transparent by decoders.
    # Comment out this line and watch the result if you don't understand this.
    canvas.paint_background(wall_color=0)

    # pad one second delay, get ready!
    canvas.pad_delay_frame(delay=100)

    # you may adjust the `speed` parameter for different algorithms.
    canvas.set_control_params(delay=2,
                              speed=50,
                              trans_index=3,
                              wall_color=0,
                              tree_color=1,
                              path_color=2)

    start = (args.margin, args.margin)
    end = (args.width - args.margin - 1, args.height - args.margin - 1)

    # the maze generation animation.
    # try prim(maze, start) or kruskal(maze) or random_dfs(maze) here!
    wilson(maze, start)

    # pad three seconds delay to help to see the resulting maze clearly.
    canvas.pad_delay_frame(delay=300)

    # in the path finding animation the walls are unchanged throughout,
    # hence it's safe to use color 0 as the transparent color.
    canvas.set_control_params(delay=5,
                              speed=30,
                              trans_index=0,
                              wall_color=0,
                              tree_color=0,
                              path_color=2,
                              fill_color=3)

    # the maze solving animation.
    # try dfs(maze, start, end) or astar(maze, start, end) here!
    bfs(maze, start, end)

    # pad five seconds delay to help to see the resulting path clearly.
    canvas.pad_delay_frame(delay=500)

    # finally finish the animation and close the file.
    canvas.save()
Example #5
0
    255,
    0,
    255
]  # path color

for i in range(256):
    rgb = hls_to_rgb((i / 360.0) % 1, 0.5, 1.0)
    palette += [int(round(255 * x)) for x in rgb]

surface.set_palette(palette)

# --------------------------------------------------------
# create a maze instance and set its position in the image
# --------------------------------------------------------
size = (surface.width, surface.height)
mask = generate_text_mask(size, 'UST', 'ubuntu.ttf', 350)
maze = gm.Maze(117, 73, mask=mask).scale(4).translate((69, 49))

# ------------------------------------------------------------------
# create an animation instance to write the animation to the surface
# ------------------------------------------------------------------
anim = gm.Animation(surface)

# here `trans_index=1` is for compatible with eye of chrome in linux.
# you may always use the default 0 for chrome and firefox.
anim.pause(100, trans_index=1)

# paint the background region that contains the maze.
left, top, width, height = 66, 47, 475, 297
anim.paint(left, top, width, height, 0)
anim.pause(100)
Example #6
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('-width',
                        type=int,
                        default=121,
                        help='width of the maze')
    parser.add_argument('-height',
                        type=int,
                        default=97,
                        help='height of the maze')
    parser.add_argument('-margin',
                        type=int,
                        default=2,
                        help='border of the maze')
    parser.add_argument('-scale',
                        type=int,
                        default=5,
                        help='size of a cell in pixels')
    parser.add_argument(
        '-loop',
        type=int,
        default=0,
        help='number of loops of the animation, default to 0 (loop infinitely)'
    )
    parser.add_argument(
        '-bits',
        metavar='b',
        type=int,
        default=8,
        help=
        'an interger beteween 2-8 represents the minimal number of bits needed to\
                        represent the colors, this parameter determines the size of the global color table.'
    )
    parser.add_argument('-algo',
                        metavar='a',
                        type=str,
                        default='bfs',
                        help='choose which maze-solving algorithm to run.')
    parser.add_argument('-filename',
                        type=str,
                        default='wilson.gif',
                        help='output file name')

    args = parser.parse_args()

    if (args.width * args.height % 2 == 0):
        raise ValueError(
            'The width and height of the maze must both be odd integers!')

    # comment out the following two lines if you don't want to show text.
    from gentext import generate_text_mask
    mask = generate_text_mask(args.width, args.height, 'UST', 'ubuntu.ttf', 60)

    anim = WilsonAlgoAnimation(args.width,
                               args.height,
                               args.margin,
                               args.scale,
                               args.bits,
                               PALETTE,
                               args.loop,
                               mask=mask)

    # here we need to paint the blank background because the region that has not been
    # covered by any frame will be set to transparent by decoders.
    # comment this line and watch the result if you don't understand this.
    anim.paint_background(wc=0)

    # pad one second delay, get ready!
    anim.pad_delay_frame(100)

    # in the Wilson's algorithm animation no cells are filled,
    # hence it's safe to use color 3 as the transparent color.
    anim.run_wilson_algorithm(speed=50,
                              delay=2,
                              trans_index=3,
                              wc=0,
                              tc=1,
                              pc=2)

    # pad three seconds delay to help to see the resulting maze clearly.
    anim.pad_delay_frame(300)

    # in the dfs/bfs algorithm animation the walls are unchanged throughout,
    # hence it's safe to use color 0 as the transparent color.
    if args.algo == 'bfs':
        anim.run_bfs_algorithm(speed=50,
                               delay=5,
                               trans_index=0,
                               wc=0,
                               tc=0,
                               pc=2,
                               fc=3)

    elif args.algo == 'dfs':
        anim.run_dfs_algorithm(speed=10,
                               delay=5,
                               trans_index=0,
                               wc=0,
                               tc=0,
                               pc=2,
                               fc=3)

    # more elif for algorithms implemented by the user ...
    else:
        pass

    # pad five seconds delay to help to see the resulting path clearly.
    anim.pad_delay_frame(500)

    # finally save the bytestream in 'wb' mode.
    anim.write_to_gif(args.filename)