Fix saving, move anim playing and work on fight

This commit is contained in:
Matyáš Caras 2022-04-17 20:03:52 +02:00
parent 3251dbe4a6
commit 3e9cd33e98
5 changed files with 40 additions and 13 deletions

View file

@ -1,6 +1,7 @@
from tempfile import TemporaryDirectory from tempfile import TemporaryDirectory
from time import sleep
from zipfile import ZipFile from zipfile import ZipFile
from os import listdir from os import listdir, system
import yaml import yaml
class AsciiAnimation: class AsciiAnimation:
def __init__(self) -> None: def __init__(self) -> None:
@ -9,7 +10,7 @@ class AsciiAnimation:
def load_ascii(self,name:str): def load_ascii(self,name:str):
""" """
Returns ASCII by name Loads art from .asc file
""" """
with TemporaryDirectory() as tmpdir: # we enter a temporary directory with TemporaryDirectory() as tmpdir: # we enter a temporary directory
@ -23,3 +24,12 @@ class AsciiAnimation:
self.speed = data["speed"] self.speed = data["speed"]
with open(f"{tmpdir}/ascii/{name}/{f}",encoding="utf-8") as f: # add all frames into list with open(f"{tmpdir}/ascii/{name}/{f}",encoding="utf-8") as f: # add all frames into list
self.frames.append(f.read()) 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)

21
lib/fight.py Normal file
View file

@ -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()

View file

@ -1,4 +1,3 @@
import enum
import yaml import yaml
from yaml.loader import SafeLoader from yaml.loader import SafeLoader
from colorama import Fore, Back from colorama import Fore, Back
@ -17,8 +16,8 @@ class Game: # the game class keeps information about the loaded game
self.current = "start" self.current = "start"
self.nodes = {} self.nodes = {}
self.inventory = [] self.inventory = []
self.save = SaveManager()
self.id = data["meta"]["id"] self.id = data["meta"]["id"]
self.save = SaveManager(self.id)
for k in data["game"]: for k in data["game"]:
self.nodes.update({k:data["game"][k]}) 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']) print(self.lang['quitting'])
exit() exit()
else: # Display continue 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 selection = m.selected
system("cls||clear") system("cls||clear")
if(selection == 0): 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 def print_animated(self,animid): # prints the first found occurence of an ascii animation
animation = AsciiAnimation() animation = AsciiAnimation()
animation.load_ascii(animid) animation.load_ascii(animid)
for frame in animation.frames: animation.play()
system("cls||clear")
print(frame)
sleep(animation.speed)
print() print()
def parse_colors(self,text:str) -> str: # Converts color codes into terminal colors def parse_colors(self,text:str) -> str: # Converts color codes into terminal colors

View file

@ -2,8 +2,8 @@ from os import path
import yaml import yaml
class SaveManager: # manages save and configuration files class SaveManager: # manages save and configuration files
def __init__(self): def __init__(self,id:str):
self.id = "" # game ID self.id = id # game ID
self.currentPrompt = "" # Current prompt self.currentPrompt = "" # Current prompt
self.inventory = [] # Items in inventory self.inventory = [] # Items in inventory