Ascii now a class

This commit is contained in:
Matyáš Caras 2022-01-26 09:23:30 +01:00
parent 0b6d9b287c
commit 2a7581336b
3 changed files with 23 additions and 16 deletions

View file

@ -2,7 +2,7 @@ from sys import argv
from lib.game import * from lib.game import *
from colorama import init from colorama import init
def main(): def main(): # TODO: Maybe a menu for available text games?
init() init()
if len(argv)<2: if len(argv)<2:
print("You need to specify a path to a YAML file") print("You need to specify a path to a YAML file")

View file

@ -2,16 +2,22 @@ from tempfile import TemporaryDirectory
from zipfile import ZipFile from zipfile import ZipFile
from os import listdir from os import listdir
def ascii_art(name): class AsciiAnimation:
""" def __init__(self) -> None:
Returns ASCII by name self.frames = []
""" self.speed = 0.2
a = []
with TemporaryDirectory() as tmpdir: def load_ascii(self,name:str):
with ZipFile(f"./assets/{name}.asc","r") as z: """
z.extractall(f"{tmpdir}/ascii/{name}") Returns ASCII by name
for f in listdir(f"{tmpdir}/ascii/{name}"): """
with open(f"{tmpdir}/ascii/{name}/{f}") as f:
a.append(f.read()) with TemporaryDirectory() as tmpdir: # we enter a temporary directory
return a 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())

View file

@ -2,7 +2,7 @@ import yaml
from yaml.loader import SafeLoader from yaml.loader import SafeLoader
from colorama import Fore, Back, Style from colorama import Fore, Back, Style
import re import re
from .ascii import ascii_art from .ascii import AsciiAnimation
from time import sleep from time import sleep
from os import system from os import system
@ -51,7 +51,8 @@ class Game:
currently only prints out the first occurence of an animated text currently only prints out the first occurence of an animated text
(in curly braces) (in curly braces)
''' '''
animation = ascii_art(animid) animation = AsciiAnimation()
animation.load_ascii(animid)
for frame in animation: for frame in animation:
system("cls||clear") system("cls||clear")
print(frame) print(frame)
@ -78,4 +79,4 @@ def load(file_path):
except Exception as e: 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(f"{Back.RED}{Fore.WHITE}An exception has occured while loading the game from the YAML file:{Fore.RESET}{Back.RESET}")
print(e) print(e)
return None return None