From 2a7581336b1fd6955609e82302f9193daf7e0063 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maty=C3=A1=C5=A1=20Caras?= Date: Wed, 26 Jan 2022 09:23:30 +0100 Subject: [PATCH] Ascii now a class --- __main__.py | 2 +- lib/ascii.py | 30 ++++++++++++++++++------------ lib/game.py | 7 ++++--- 3 files changed, 23 insertions(+), 16 deletions(-) diff --git a/__main__.py b/__main__.py index 555c3ae..0087c17 100644 --- a/__main__.py +++ b/__main__.py @@ -2,7 +2,7 @@ from sys import argv from lib.game import * from colorama import init -def main(): +def main(): # TODO: Maybe a menu for available text games? init() if len(argv)<2: print("You need to specify a path to a YAML file") diff --git a/lib/ascii.py b/lib/ascii.py index 9e5ef6a..f4b2b85 100644 --- a/lib/ascii.py +++ b/lib/ascii.py @@ -2,16 +2,22 @@ from tempfile import TemporaryDirectory from zipfile import ZipFile from os import listdir -def ascii_art(name): - """ - Returns ASCII by name - """ - a = [] +class AsciiAnimation: + def __init__(self) -> None: + self.frames = [] + self.speed = 0.2 - with TemporaryDirectory() as tmpdir: - with ZipFile(f"./assets/{name}.asc","r") as z: - z.extractall(f"{tmpdir}/ascii/{name}") - for f in listdir(f"{tmpdir}/ascii/{name}"): - with open(f"{tmpdir}/ascii/{name}/{f}") as f: - a.append(f.read()) - return a + def load_ascii(self,name:str): + """ + Returns ASCII by name + """ + + with TemporaryDirectory() as tmpdir: # we enter a temporary directory + with ZipFile(f"./assets/{name}.asc","r") as z: # extract the asc file + z.extractall(f"{tmpdir}/ascii/{name}") + for f in listdir(f"{tmpdir}/ascii/{name}"): # read all the files + if f.endswith("yaml"): + # TODO: load asc config file + pass + with open(f"{tmpdir}/ascii/{name}/{f}") as f: # add all frames into list + self.frames.append(f.read()) \ No newline at end of file diff --git a/lib/game.py b/lib/game.py index 882c602..20af2a0 100644 --- a/lib/game.py +++ b/lib/game.py @@ -2,7 +2,7 @@ import yaml from yaml.loader import SafeLoader from colorama import Fore, Back, Style import re -from .ascii import ascii_art +from .ascii import AsciiAnimation from time import sleep from os import system @@ -51,7 +51,8 @@ class Game: currently only prints out the first occurence of an animated text (in curly braces) ''' - animation = ascii_art(animid) + animation = AsciiAnimation() + animation.load_ascii(animid) for frame in animation: system("cls||clear") print(frame) @@ -78,4 +79,4 @@ def load(file_path): except Exception as e: print(f"{Back.RED}{Fore.WHITE}An exception has occured while loading the game from the YAML file:{Fore.RESET}{Back.RESET}") print(e) - return None + return None \ No newline at end of file