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 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
@ -23,3 +24,12 @@ class AsciiAnimation:
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())
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
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

View file

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