Work on equipping items

This commit is contained in:
Matyáš Caras 2022-04-19 12:36:05 +02:00 committed by GitHub
parent 8d58c9647a
commit 1cb78c4b8a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 90 additions and 13 deletions

View file

@ -33,7 +33,7 @@ def main():
g = load(f"./games/{file}",l)
games.append(g)
except Exception as e:
print(f"{Back.RED}{Fore.RED}{l['error_loading']} {file}:")
print(f"{Back.RED}{Fore.WHITE}{l['error_loading']} {file}{Fore.RESET}{Back.RESET}:")
print(e)
# PRINT OUT GAME SELECT
@ -42,7 +42,8 @@ def main():
else:
names = []
for n in games:
names.append(n.name)
if(n is not None):
names.append(n.name)
m = MenuManager(names,f" TEXTY \n{l['available']}")
games[m.selected].main_menu()

View file

@ -2,6 +2,13 @@ meta: # make sure every key is in lowercase
name: "My Text Adventure" # Game name
id: "examplegame" # An original game ID (please try to be original)
creator: "hernik" # Your name as a creator
equippable:
- iron_sword:
atk: 3
starter: true
- chainmail:
def: 2
starter: true
game: # here goes all the game logic
start: # the starting point always HAS to be named "start" (lowercase), the order and name of the rest does not matter

View file

@ -1,21 +1,60 @@
import math
from lib.game import Item
from .ascii import *
from colorama import Fore
import keyboard
class FightHandler:
def __init__(self,name:str,hp:int,attacks:list,img:str="") -> None:
def __init__(self,message:str,name:str,hp:int,attacks:list,lang:dict,eq:Item,img:str="") -> None:
self.selected = 0
keyboard.add_hotkey("up",self.up)
keyboard.add_hotkey("down",self.down)
keyboard.add_hotkey("enter",self.attack)
self.name = name
self.max = hp # starting HP
self.hp = self.max # current HP
self.attacks = attacks
self.img = img
self.lang = lang
self.message = message
self.equipped = eq
input()
def up(self):
if self.selected == 0:
self.selected = 2
else:
self.selected -= 1
system("cls||clear")
self.show()
def down(self):
if self.selected == 2:
self.selected = 0
else:
self.selected += 1
system("cls||clear")
self.show()
def show(self):
p = math.trunc(self.hp/self.max*10)
h = "🟥"*p
if str(p).endswith(".5"):
h += ""
print(self.message)
print(f"{self.name} {h} {self.hp}/{self.max}")
if self.img != "":
anim = AsciiAnimation()
anim.load_ascii(self.img)
anim.play()
s = [self.lang["attack"],self.lang["defend"],self.lang["inventory"]]
for selection in s:
if(self.selected == self.selections.index(selection)):
print(f"{Fore.RED}{Fore.RESET} {selection}")
else:
print(f" {selection}")
def attack(self):
self.hp
input()

View file

@ -11,13 +11,30 @@ from os import system
class Game: # the game class keeps information about the loaded game
def __init__(self,data:dict):
self.name = data["meta"]["name"]
self.author = data["meta"]["creator"]
self.current = "start"
self.nodes = {}
self.inventory = []
self.id = data["meta"]["id"]
self.save = SaveManager(self.id)
self.name = data["meta"]["name"] # Game name
self.author = data["meta"]["creator"] # Game creator
self.current = "start" # Current prompt
self.nodes = {} # All nodes
self.inventory = [] # Player's inventory
self.id = data["meta"]["id"] # Game ID
self.save = SaveManager(self.id) # saving
self.equipped = {"weapon":None,"armor":None} # Items equipped by player
if "equippable" in data["meta"].keys():
self.equippable = [] # Items that can be equipped by player
for item in data["meta"]["equippable"]:
name = list(item.keys())[0]
if "def" in item[name].keys() and "atk" in item[name].keys():
self.equippable.append(Item(name,item[name]["atk"],item[name]["def"]))
elif "def" in item[name].keys():
self.equippable.append(Item(name=name,defense=item[name]["def"]))
elif "atk" in item[name].keys():
self.equippable.append(Item(name,item[name]["atk"]))
if("starter" in item[name].keys()): # if starter, equip and add to inventory
if item[name]["starter"]:
i = next((x for x in self.equippable if x.name == list(item.keys())[0]))
self.inventory.append(i)
self.equipped[i.type] = i
for k in data["game"]:
self.nodes.update({k:data["game"][k]})
@ -128,7 +145,7 @@ class Game: # the game class keeps information about the loaded game
def show_inventory(self):
if len(self.inventory) == 0:
MenuManager([self.lang["return"]],f" YOUR INVENTORY \n")
MenuManager([self.lang["return"]],f" {self.lang['inside_inv']} \n")
else:
s = ""
for i,item in enumerate(self.inventory):
@ -136,7 +153,7 @@ class Game: # the game class keeps information about the loaded game
s += f"- {item}"
else:
s += f"- {item}\n"
MenuManager([self.lang["return"]],f" YOUR INVENTORY \n{s}")
MenuManager([self.lang["return"]],f" {self.lang['inside_inv']} \n{s}")
self.print_text()
def print_animated(self,animid): # prints the first found occurence of an ascii animation
@ -158,6 +175,16 @@ def load(file_path,lang): # starts to load the game from YAML
g.lang = lang
return g
except Exception as e:
print(f"{Back.RED}{Fore.WHITE}{g.lang['error_loading']}{Fore.RESET}{Back.RESET}")
print(f"{Back.RED}{Fore.WHITE}ERROR{Fore.RESET}{Back.RESET}")
print(e)
return None
class Item:
def __init__(self,name:str,attack:int = 0,defense:int = 0) -> None:
self.name = name
if attack == 0 and defense > 0:
self.type = "armor"
else:
self.type = "weapon"
self.attack = attack
self.defense = defense

View file

@ -24,5 +24,8 @@ selection: 'Make a selection (number):'
not_number: 'Not a number selection'
invalid: 'Invalid selection'
attack: "Attack"
defend: "Defend"
error_loading: 'An exception has occured while loading the game from the YAML file'
no_action: 'Error: No action "$action" found in the game file.'