From eaf7c8ab17d2ebfa82cd531352fff4f88eda4467 Mon Sep 17 00:00:00 2001 From: Kevin Dean Date: Sun, 22 May 2016 19:55:09 -0500 Subject: [PATCH 1/7] symbol set and framerate are now command line options --- maze.py | 59 +++++++++++++++++++++++++++++++++------------------------ 1 file changed, 34 insertions(+), 25 deletions(-) diff --git a/maze.py b/maze.py index ddfc50d..d609f1b 100755 --- a/maze.py +++ b/maze.py @@ -6,13 +6,14 @@ from random import choice, randint from sys import argv, exit from time import sleep +from argparse import ArgumentParser setlocale(LC_ALL, '') # Symbols -ARCHIVE = [ +ARCHIVE = { # ASCII lines - { + 'ASCII' : { '0000': ' ', '0001': '-', '0010': '|', @@ -32,7 +33,7 @@ }, # Thick lines - { + 'THICK' : { '0000': ' ', '0001': '╸', '0010': '╻', @@ -52,7 +53,7 @@ }, # Thin lines - { + 'THIN' : { '0000': ' ', '0001': '╴', '0010': '╷', @@ -72,7 +73,7 @@ }, # Double lines - { + 'DOUBLE' : { '0000': ' ', '0001': '═', '0010': '║', @@ -90,15 +91,14 @@ '1110': '╠', '1111': '╬', }, -] -SYMBOLS = [ARCHIVE[3]] - +} # Init -def init(screen): +def init(screen, all_symbols): rows, cols = screen.getmaxyx() matrix = { + 'symbols':all_symbols, 'grid': [[None for _ in range(rows)] for _ in range(cols)], 'cols': cols, 'rows': rows @@ -111,9 +111,9 @@ def init(screen): screen.clear() - def redraw(matrix, seq): + def redraw(all_symbols, matrix, seq): for point in seq: - render(screen, matrix, point) + render(screen, all_symbols, matrix, point) mazes = [] # for color in [1,2,4]: @@ -167,7 +167,7 @@ def get_color(n): # Render matrix -def render(screen, matrix, loc): +def render(screen, all_symbols, matrix, loc): def in_bounds(matrix, col, row): cols = len(matrix) @@ -193,13 +193,13 @@ def in_bounds(matrix, col, row): try: if matrix[ncol][nrow] is None: screen.addstr(nrow, ncol, - SYMBOLS[matrix[ncol][nrow][2]]['0000']) + all_symbols[matrix[ncol][nrow][2]]['0000']) else: key = '' for val in matrix[ncol][nrow][0]: key += str(val) screen.addstr(nrow, ncol, - SYMBOLS[matrix[ncol][nrow][2]][key], + all_symbols[matrix[ncol][nrow][2]][key], get_color(matrix[ncol][nrow][1])) except: """ @@ -217,13 +217,14 @@ def __init__(self, matrix, color=None, symbol=None): self._matrix = matrix['grid'] self._cols = matrix['cols'] self._rows = matrix['rows'] + self._all_symbols = matrix['symbols'] col = randint(0, self._cols-1) row = randint(0, self._rows-1) self._color = 0 if color is None else color if symbol is None: - self._symbol = randint(0, len(SYMBOLS)-1) + self._symbol = randint(0, len(self._all_symbols)-1) else: - self._symbol = symbol % len(SYMBOLS) + self._symbol = symbol % len(self._all_symbols) if self._matrix[col][row] is None: self._matrix[col][row] = [[0, 0, 0, 0], self._color, self._symbol] self._stack = [(col, row)] @@ -296,14 +297,14 @@ def _step(self): self._color, self._symbol] self._matrix[next['col']][next['row']][0][(next['dir']+2) % 4] = 1 self._stack.append((next['col'], next['row'])) - self._redraw(self._matrix, [self._stack[-1], self._stack[-2]]) + self._redraw(self._all_symbols, self._matrix, [self._stack[-1], self._stack[-2]]) return True # fill(matrix, next['col'], next['row']) # break # Run -def run(): +def run(max_framerate, all_symbols): initscr() start_color() use_default_colors() @@ -316,7 +317,9 @@ def run(): init_colors() - matrix, mazes = init(screen) + frame_sleep_time = 1.0 / max_framerate + + matrix, mazes = init(screen, all_symbols) (running, paused, quitting) = range(0, 3) state = running @@ -329,7 +332,7 @@ def run(): if len(mazes) < 1: matrix, mazes = init(screen) - sleep(0.01) + sleep(frame_sleep_time) event = screen.getch() @@ -353,11 +356,17 @@ def run(): # Main -def main(argv=None): - if argv is None: - argv = "" - argc = len(argv) - run() +def main(): + + argparser = ArgumentParser(description='simple curses pipes') + argparser.add_argument('--symbol_set', nargs='*', type=str, choices=ARCHIVE.keys(), default=['ASCII']) + argparser.add_argument('--max_framerate', type=int, choices=[1, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100], default=100) + args = argparser.parse_args() + + run( + max_framerate=args.max_framerate, + all_symbols=[ARCHIVE.get(syms) for syms in args.symbol_set] + ) return 0 From e54b189b992931865dda3c7abc841ade3712e54a Mon Sep 17 00:00:00 2001 From: Kevin Dean Date: Sun, 22 May 2016 20:19:49 -0500 Subject: [PATCH 2/7] color sets and number of pipes to seed with configurable --- maze.py | 67 +++++++++++++++++++++++++++++++-------------------------- 1 file changed, 36 insertions(+), 31 deletions(-) diff --git a/maze.py b/maze.py index d609f1b..07132b0 100755 --- a/maze.py +++ b/maze.py @@ -3,7 +3,8 @@ from curses import * from locale import setlocale, LC_ALL -from random import choice, randint +from random import choice, randint, shuffle +from math import ceil from sys import argv, exit from time import sleep from argparse import ArgumentParser @@ -93,8 +94,21 @@ }, } +COLOR_SETS = ['basic', 'rainbow', 'drab', 'random'] + +def get_color_palette(color_set): + if color_set == 'random': + color_set = choice(COLOR_SETS) + + if color_set == 'rainbow': + return [1, 2, 3, 4, 5, 9, 10, 11, 12, 13] + elif color_set == 'drab': + return [0, 7, 8, 15] + else: + return [1, 2, 4] + # Init -def init(screen, all_symbols): +def init(screen, all_symbols, color_set, seed_count): rows, cols = screen.getmaxyx() matrix = { @@ -116,29 +130,15 @@ def redraw(all_symbols, matrix, seq): render(screen, all_symbols, matrix, point) mazes = [] - # for color in [1,2,4]: - type = randint(0, 2) - if type == 0: - for _ in range(25): - maze = Maze(matrix, - color=choice([1, 2, 3, 4, 5, 9, 10, 11, 12, 13])) - maze.redraw(redraw) - mazes.append(maze) - - elif type == 1: - for _ in range(25): - maze = Maze(matrix, color=choice([0, 7, 8, 15])) - maze.redraw(redraw) - mazes.append(maze) - - elif type == 2: - maze = Maze(matrix, color=1) - maze.redraw(redraw) - mazes.append(maze) - maze = Maze(matrix, color=2) - maze.redraw(redraw) - mazes.append(maze) - maze = Maze(matrix, color=4) + + base_palette = get_color_palette(color_set) + + full_palette = base_palette * ceil(seed_count / len(base_palette)) + shuffle(full_palette) + + for i in range(seed_count): + maze = Maze(matrix, + color=full_palette[i]) maze.redraw(redraw) mazes.append(maze) @@ -304,7 +304,7 @@ def _step(self): # Run -def run(max_framerate, all_symbols): +def run(max_framerate, all_symbols, color_set, seed_count): initscr() start_color() use_default_colors() @@ -319,7 +319,7 @@ def run(max_framerate, all_symbols): frame_sleep_time = 1.0 / max_framerate - matrix, mazes = init(screen, all_symbols) + matrix, mazes = init(screen, all_symbols, color_set, seed_count) (running, paused, quitting) = range(0, 3) state = running @@ -330,7 +330,7 @@ def run(max_framerate, all_symbols): mazes = set([maze for maze in mazes if maze.step()]) if len(mazes) < 1: - matrix, mazes = init(screen) + matrix, mazes = init(screen, all_symbols, color_set, seed_count) sleep(frame_sleep_time) @@ -343,10 +343,10 @@ def run(max_framerate, all_symbols): elif event == ord('p'): state = running if state == paused else paused elif event == ord(' '): - matrix, mazes = init(screen) + matrix, mazes = init(screen, all_symbols, color_set, seed_count) state = running elif event == KEY_RESIZE: - matrix, mazes = init(screen) + matrix, mazes = init(screen, all_symbols, color_set, seed_count) state = running screen.clear() @@ -361,11 +361,16 @@ def main(): argparser = ArgumentParser(description='simple curses pipes') argparser.add_argument('--symbol_set', nargs='*', type=str, choices=ARCHIVE.keys(), default=['ASCII']) argparser.add_argument('--max_framerate', type=int, choices=[1, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100], default=100) + argparser.add_argument('--color_set', type=str, choices=COLOR_SETS, default='random', help='Change the color set pipes are drawn with, defaults to random') + argparser.add_argument('--seed_count', type=int, default=randint(5, 15), help='Number of pipes to draw simultaneously, defaults to a random number between 5 and 25') + args = argparser.parse_args() run( max_framerate=args.max_framerate, - all_symbols=[ARCHIVE.get(syms) for syms in args.symbol_set] + all_symbols=[ARCHIVE.get(syms) for syms in args.symbol_set], + color_set=args.color_set, + seed_count=args.seed_count ) return 0 From 5ab07697dc00446b5303cd1a98a65251e86421d7 Mon Sep 17 00:00:00 2001 From: Kevin Dean Date: Sun, 22 May 2016 20:30:26 -0500 Subject: [PATCH 3/7] allow setting min and max number of pipes --- maze.py | 48 ++++++++++++++++++++++++++++-------------------- 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/maze.py b/maze.py index 07132b0..3f21382 100755 --- a/maze.py +++ b/maze.py @@ -94,21 +94,28 @@ }, } -COLOR_SETS = ['basic', 'rainbow', 'drab', 'random'] +COLOR_SETS = ['basic', 'rainbow', 'drab'] -def get_color_palette(color_set): - if color_set == 'random': +def get_color_palette(color_set, colors_needed): + if color_set == 'random' or color_set not in COLOR_SETS: color_set = choice(COLOR_SETS) + base_palette = None + if color_set == 'rainbow': - return [1, 2, 3, 4, 5, 9, 10, 11, 12, 13] + base_palette = [1, 2, 3, 4, 5, 9, 10, 11, 12, 13] elif color_set == 'drab': - return [0, 7, 8, 15] - else: - return [1, 2, 4] + base_palette = [0, 7, 8, 15] + elif color_set == 'basic': + base_palette = [1, 2, 4] + + full_palette = base_palette * ceil(colors_needed / len(base_palette)) + shuffle(full_palette) + + return full_palette # Init -def init(screen, all_symbols, color_set, seed_count): +def init(screen, all_symbols, color_set, min_pipes, max_pipes): rows, cols = screen.getmaxyx() matrix = { @@ -131,14 +138,13 @@ def redraw(all_symbols, matrix, seq): mazes = [] - base_palette = get_color_palette(color_set) + seed_count = randint(min_pipes, max_pipes) - full_palette = base_palette * ceil(seed_count / len(base_palette)) - shuffle(full_palette) + palette = get_color_palette(color_set, seed_count) for i in range(seed_count): maze = Maze(matrix, - color=full_palette[i]) + color=palette[i]) maze.redraw(redraw) mazes.append(maze) @@ -304,7 +310,7 @@ def _step(self): # Run -def run(max_framerate, all_symbols, color_set, seed_count): +def run(max_framerate, all_symbols, color_set, min_pipes, max_pipes): initscr() start_color() use_default_colors() @@ -319,7 +325,7 @@ def run(max_framerate, all_symbols, color_set, seed_count): frame_sleep_time = 1.0 / max_framerate - matrix, mazes = init(screen, all_symbols, color_set, seed_count) + matrix, mazes = init(screen, all_symbols, color_set, min_pipes, max_pipes) (running, paused, quitting) = range(0, 3) state = running @@ -330,7 +336,7 @@ def run(max_framerate, all_symbols, color_set, seed_count): mazes = set([maze for maze in mazes if maze.step()]) if len(mazes) < 1: - matrix, mazes = init(screen, all_symbols, color_set, seed_count) + matrix, mazes = init(screen, all_symbols, color_set, min_pipes, max_pipes) sleep(frame_sleep_time) @@ -343,10 +349,10 @@ def run(max_framerate, all_symbols, color_set, seed_count): elif event == ord('p'): state = running if state == paused else paused elif event == ord(' '): - matrix, mazes = init(screen, all_symbols, color_set, seed_count) + matrix, mazes = init(screen, all_symbols, color_set, min_pipes, max_pipes) state = running elif event == KEY_RESIZE: - matrix, mazes = init(screen, all_symbols, color_set, seed_count) + matrix, mazes = init(screen, all_symbols, color_set, min_pipes, max_pipes) state = running screen.clear() @@ -361,8 +367,9 @@ def main(): argparser = ArgumentParser(description='simple curses pipes') argparser.add_argument('--symbol_set', nargs='*', type=str, choices=ARCHIVE.keys(), default=['ASCII']) argparser.add_argument('--max_framerate', type=int, choices=[1, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100], default=100) - argparser.add_argument('--color_set', type=str, choices=COLOR_SETS, default='random', help='Change the color set pipes are drawn with, defaults to random') - argparser.add_argument('--seed_count', type=int, default=randint(5, 15), help='Number of pipes to draw simultaneously, defaults to a random number between 5 and 25') + argparser.add_argument('--color_set', type=str, choices=COLOR_SETS + ['random'], default='random', help='Change the color set pipes are drawn with, defaults to random') + argparser.add_argument('--min_pipes', type=int, default=5, help="Minimum number of pipes to draw each round") + argparser.add_argument('--max_pipes', type=int, default=15, help="Maximum number of pipes to draw each round") args = argparser.parse_args() @@ -370,7 +377,8 @@ def main(): max_framerate=args.max_framerate, all_symbols=[ARCHIVE.get(syms) for syms in args.symbol_set], color_set=args.color_set, - seed_count=args.seed_count + min_pipes=args.min_pipes, + max_pipes=args.max_pipes ) return 0 From 8018a97aae5bab74c3574c690b06070cf4db688e Mon Sep 17 00:00:00 2001 From: Kevin Dean Date: Sun, 22 May 2016 20:33:46 -0500 Subject: [PATCH 4/7] adding some extra comments --- maze.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/maze.py b/maze.py index 3f21382..8294a81 100755 --- a/maze.py +++ b/maze.py @@ -94,8 +94,9 @@ }, } -COLOR_SETS = ['basic', 'rainbow', 'drab'] +COLOR_SETS = ['basic', 'rainbow', 'drab'] #possible kinds of color sets, user can also use 'random' to pick a different one each iteration +# Create a randomized color palette based on the given color set with at least 'colors_needed' values def get_color_palette(color_set, colors_needed): if color_set == 'random' or color_set not in COLOR_SETS: color_set = choice(COLOR_SETS) From 4b3122d79654a614ac47ca40e8fcfc1abf422c0e Mon Sep 17 00:00:00 2001 From: Kevin Dean Date: Sun, 22 May 2016 20:37:36 -0500 Subject: [PATCH 5/7] modifying verbiage --- maze.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/maze.py b/maze.py index 8294a81..80cff5f 100755 --- a/maze.py +++ b/maze.py @@ -369,8 +369,8 @@ def main(): argparser.add_argument('--symbol_set', nargs='*', type=str, choices=ARCHIVE.keys(), default=['ASCII']) argparser.add_argument('--max_framerate', type=int, choices=[1, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100], default=100) argparser.add_argument('--color_set', type=str, choices=COLOR_SETS + ['random'], default='random', help='Change the color set pipes are drawn with, defaults to random') - argparser.add_argument('--min_pipes', type=int, default=5, help="Minimum number of pipes to draw each round") - argparser.add_argument('--max_pipes', type=int, default=15, help="Maximum number of pipes to draw each round") + argparser.add_argument('--min_pipes', type=int, default=5, help="Minimum number of pipes to draw each round, defaults to 5") + argparser.add_argument('--max_pipes', type=int, default=25, help="Maximum number of pipes to draw each round, defaults to 25") args = argparser.parse_args() From e8b39a8beede0d1eec6b38a522c32cf251695c85 Mon Sep 17 00:00:00 2001 From: Kevin Dean Date: Sun, 22 May 2016 20:45:11 -0500 Subject: [PATCH 6/7] modifying verbiage --- maze.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/maze.py b/maze.py index 80cff5f..3434fee 100755 --- a/maze.py +++ b/maze.py @@ -364,10 +364,9 @@ def run(max_framerate, all_symbols, color_set, min_pipes, max_pipes): # Main def main(): - - argparser = ArgumentParser(description='simple curses pipes') - argparser.add_argument('--symbol_set', nargs='*', type=str, choices=ARCHIVE.keys(), default=['ASCII']) - argparser.add_argument('--max_framerate', type=int, choices=[1, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100], default=100) + argparser = ArgumentParser(description='Simple curses pipes') + argparser.add_argument('--symbol_set', nargs='*', choices=list(ARCHIVE.keys()), default=['ASCII'], help='One or more allowable symbol sets to use, defaults to ASCII symbols') + argparser.add_argument('--max_framerate', type=int, choices=[1, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100], default=100, help='Maximum allowable drawing rate') argparser.add_argument('--color_set', type=str, choices=COLOR_SETS + ['random'], default='random', help='Change the color set pipes are drawn with, defaults to random') argparser.add_argument('--min_pipes', type=int, default=5, help="Minimum number of pipes to draw each round, defaults to 5") argparser.add_argument('--max_pipes', type=int, default=25, help="Maximum number of pipes to draw each round, defaults to 25") From a3707f85cf08f1e12a7114b5ea24900440b3376d Mon Sep 17 00:00:00 2001 From: Kevin Dean Date: Sun, 22 May 2016 20:47:47 -0500 Subject: [PATCH 7/7] modified exist routine to turn echo back on and turn off keypad and nodelay --- maze.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/maze.py b/maze.py index 3434fee..565cc1c 100755 --- a/maze.py +++ b/maze.py @@ -357,6 +357,9 @@ def run(max_framerate, all_symbols, color_set, min_pipes, max_pipes): state = running screen.clear() + screen.keypad(0) + screen.nodelay(0) + echo() endwin() return 0