Ascii now a class
This commit is contained in:
parent
0b6d9b287c
commit
2a7581336b
3 changed files with 23 additions and 16 deletions
|
@ -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")
|
||||
|
|
30
lib/ascii.py
30
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())
|
|
@ -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
|
Reference in a new issue