player.py

982 B · python · 37 lines

1import os2import sys3import time4from .models import Life56FILL = "\u2B1B\uFE0F"7VOID = "\u2B1C\uFE0F"8FPS = 4910def clear_console():11    os.system("clear")1213def resize_console(rows, cols):14    cols = 32 if cols < 32 else cols15    command = "\x1b[8;{rows};{cols}t".format(rows=rows, cols=cols + cols)16    sys.stdout.write(command)1718def print_grid(rows, cols, grid):19    clear_console()20    output_str = ""21    for row in range(rows):22        output_str += "".join([FILL if c else VOID for c in grid[row]])23        output_str += "\n\r"24    print(output_str, end="")2526def run_animation(result: Life):27    rows, cols = result.grids[0].height, result.grids[0].width28    resize_console(rows, cols)29    delay = 1 / FPS30    for i, grid in enumerate(result.grids):31        print_grid(rows, cols, grid.types.tolist())32        print(f"Generation: {i + 1} / {result.count} - {result.fate.value}")33        time.sleep(delay)3435def play(result: Life):36    while True:37        run_animation(result)