From 3e9cd33e98e6bed2ddbe339963ca1d681c48d1a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maty=C3=A1=C5=A1=20Caras?= Date: Sun, 17 Apr 2022 20:03:52 +0200 Subject: [PATCH] Fix saving, move anim playing and work on fight --- lib/ascii.py | 16 +++++++++++++--- lib/fight.py | 21 +++++++++++++++++++++ lib/game.py | 10 +++------- lib/save.py | 4 ++-- requirements.txt | 2 +- 5 files changed, 40 insertions(+), 13 deletions(-) create mode 100644 lib/fight.py diff --git a/lib/ascii.py b/lib/ascii.py index d7d1cc7..34de09b 100644 --- a/lib/ascii.py +++ b/lib/ascii.py @@ -1,6 +1,7 @@ from tempfile import TemporaryDirectory +from time import sleep from zipfile import ZipFile -from os import listdir +from os import listdir, system import yaml class AsciiAnimation: def __init__(self) -> None: @@ -9,7 +10,7 @@ class AsciiAnimation: def load_ascii(self,name:str): """ - Returns ASCII by name + Loads art from .asc file """ with TemporaryDirectory() as tmpdir: # we enter a temporary directory @@ -22,4 +23,13 @@ class AsciiAnimation: if(data["speed"] != None): self.speed = data["speed"] with open(f"{tmpdir}/ascii/{name}/{f}",encoding="utf-8") as f: # add all frames into list - self.frames.append(f.read()) \ No newline at end of file + self.frames.append(f.read()) + + def play(self): + """ + Plays the animation frame by frame + """ + for frame in self.frames: + system("cls||clear") + print(frame) + sleep(self.speed) \ No newline at end of file diff --git a/lib/fight.py b/lib/fight.py new file mode 100644 index 0000000..c1a198d --- /dev/null +++ b/lib/fight.py @@ -0,0 +1,21 @@ +import math +from .ascii import * + +class FightHandler: + def __init__(self,name:str,hp:int,attacks:list,img:str="") -> None: + self.name = name + self.max = hp # starting HP + self.hp = self.max # current HP + self.attacks = attacks + self.img = img + + def show(self): + p = math.trunc(self.hp/self.max*10) + h = "🟥"*p + if str(p).endswith(".5"): + h += "◾" + print(f"{self.name} {h} {self.hp}/{self.max}") + if self.img != "": + anim = AsciiAnimation() + anim.load_ascii(self.img) + anim.play() diff --git a/lib/game.py b/lib/game.py index 013d4d3..fc6e591 100644 --- a/lib/game.py +++ b/lib/game.py @@ -1,4 +1,3 @@ -import enum import yaml from yaml.loader import SafeLoader from colorama import Fore, Back @@ -17,8 +16,8 @@ class Game: # the game class keeps information about the loaded game self.current = "start" self.nodes = {} self.inventory = [] - self.save = SaveManager() self.id = data["meta"]["id"] + self.save = SaveManager(self.id) for k in data["game"]: self.nodes.update({k:data["game"][k]}) @@ -37,7 +36,7 @@ class Game: # the game class keeps information about the loaded game print(self.lang['quitting']) exit() else: # Display continue - m = MenuManager([self.lang['continue'],self.lang['new_name'],self.lang['options'],self.lang['quit']],f"{self.name}\n{self.lang['game_by'].replace('$author',self.author)}") + m = MenuManager([self.lang['continue'],self.lang['new_game'],self.lang['options'],self.lang['quit']],f"{self.name}\n{self.lang['game_by'].replace('$author',self.author)}") selection = m.selected system("cls||clear") if(selection == 0): @@ -143,10 +142,7 @@ class Game: # the game class keeps information about the loaded game def print_animated(self,animid): # prints the first found occurence of an ascii animation animation = AsciiAnimation() animation.load_ascii(animid) - for frame in animation.frames: - system("cls||clear") - print(frame) - sleep(animation.speed) + animation.play() print() def parse_colors(self,text:str) -> str: # Converts color codes into terminal colors diff --git a/lib/save.py b/lib/save.py index 389e830..80e8836 100644 --- a/lib/save.py +++ b/lib/save.py @@ -2,8 +2,8 @@ from os import path import yaml class SaveManager: # manages save and configuration files - def __init__(self): - self.id = "" # game ID + def __init__(self,id:str): + self.id = id # game ID self.currentPrompt = "" # Current prompt self.inventory = [] # Items in inventory diff --git a/requirements.txt b/requirements.txt index 4b2555a..3d6798a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,3 @@ pyyaml==6.0 colorama==0.4.4 -keyboard==0.13.5 +keyboard==0.13.5 \ No newline at end of file