This repository has been archived on 2022-12-03. You can view files and clone it, but cannot push or open issues or pull requests.
texty/lib/game.py

82 lines
3.2 KiB
Python
Raw Normal View History

2021-11-02 17:40:54 +01:00
import yaml
from yaml.loader import SafeLoader
from colorama import Fore, Back, Style
2022-01-25 17:16:07 +01:00
import re
2022-01-26 09:23:30 +01:00
from .ascii import AsciiAnimation
2022-01-25 17:16:07 +01:00
from time import sleep
2022-01-25 17:30:09 +01:00
from os import system
2021-11-02 17:40:54 +01:00
class Game:
2021-11-26 22:13:30 +01:00
def __init__(self,data:dict):
self.name = data["meta"]["name"]
self.author = data["meta"]["creator"]
2021-11-02 17:40:54 +01:00
self.current = "start"
self.nodes = {}
2021-11-26 22:13:30 +01:00
for k in data["game"]:
self.nodes.update({k:data["game"][k]})
2021-11-02 17:40:54 +01:00
2021-11-26 22:13:30 +01:00
def make_selection(self,selection:int) -> bool:
if(type(selection) != int or selection >= len(self.nodes[self.current]["actions"]) or selection < 0):
2021-11-02 17:40:54 +01:00
print("Invalid selection")
2021-11-03 10:16:56 +01:00
return False
2021-11-02 17:40:54 +01:00
else:
2021-11-26 22:13:30 +01:00
self.current = self.nodes[self.current]["actions"][selection]
2021-11-03 10:16:56 +01:00
return True
2021-11-02 17:40:54 +01:00
2022-01-25 17:16:07 +01:00
def print_text(self):
2021-11-02 17:40:54 +01:00
'''
Used to print out the current prompt with the options
'''
2022-01-25 17:16:07 +01:00
animated = re.search(r"(?!{).+(?=})",self.nodes[self.current]["text"]) # find the animated text
if(animated != None):
self.print_animated(animated.group(0))
2022-01-25 17:30:09 +01:00
self.nodes[self.current]["text"] = self.nodes[self.current]["text"].replace("{"+animated.group(0)+"}","") # remove the animated text from the text prompt
2021-12-16 10:12:00 +01:00
print(self.parse_colors(self.nodes[self.current]["text"]))
2021-11-02 17:40:54 +01:00
print("")
ostring = ""
2021-11-26 22:13:30 +01:00
if("actions" in self.nodes[self.current].keys()):
for i,option in enumerate(self.nodes[self.current]["actions"]):
ostring+=f"{i} - {self.nodes[option]['description']}\n"
print(ostring)
2021-11-03 10:16:56 +01:00
sel = input("Make a selection (number): ")
2021-11-26 22:13:30 +01:00
isWrong = self.make_selection(int(sel))
while isWrong == False:
sel = input("Make a selection (number): ")
isWrong = self.make_selection(sel)
2022-01-25 17:16:07 +01:00
self.print_text()
def print_animated(self,animid):
'''
Used to print out animated text,
currently only prints out the first occurence of an animated text
(in curly braces)
'''
2022-01-26 09:23:30 +01:00
animation = AsciiAnimation()
animation.load_ascii(animid)
2022-01-25 17:16:07 +01:00
for frame in animation:
2022-01-25 17:30:09 +01:00
system("cls||clear")
2022-01-25 17:16:07 +01:00
print(frame)
sleep(0.2)
2022-01-25 17:30:09 +01:00
print()
2022-01-25 17:16:07 +01:00
2021-11-03 10:16:56 +01:00
def parse_colors(self,text:str) -> str:
'''
Used to convert color codes in string to colors from the colorama lib
'''
2021-12-02 11:20:39 +01:00
newText = text.replace("&b",Fore.CYAN).replace("&c",Fore.RED).replace("&e", Fore.YELLOW).replace("&a",Fore.GREEN).replace("&9",Fore.BLUE).replace("&r",Fore.RESET).replace("&f",Fore.WHITE).replace("&5",Fore.MAGENTA).replace("\n",Fore.RESET + "\n") # replace color codes and newlines with colorama
2021-11-26 22:13:30 +01:00
newText += Fore.RESET # reset color at the end of the text
return newText
2021-11-02 17:40:54 +01:00
def load(file_path):
'''Loads the game from a YAML file to a Game class'''
2021-11-26 22:13:30 +01:00
try:
with open(file_path) as f:
data = yaml.load(f,Loader=SafeLoader)
g = Game(data)
return g
except Exception as e:
2021-12-16 10:12:00 +01:00
print(f"{Back.RED}{Fore.WHITE}An exception has occured while loading the game from the YAML file:{Fore.RESET}{Back.RESET}")
2021-11-26 22:13:30 +01:00
print(e)
2022-01-26 09:23:30 +01:00
return None