From 2cb2ca4e251ab1da49c502aa978169d9e8c56ccd Mon Sep 17 00:00:00 2001 From: Kalyax Date: Sat, 17 Sep 2022 23:00:22 +0200 Subject: [PATCH] hotfixes --- charaters/personnage.py | 23 ++++++----- listener.py => graphics/key_listener.py | 0 graphics/layers.py | 41 +++++++++++--------- graphics/writer.py | 51 ++++++++++++++++--------- main.py | 21 ++++------ 5 files changed, 76 insertions(+), 60 deletions(-) rename listener.py => graphics/key_listener.py (100%) diff --git a/charaters/personnage.py b/charaters/personnage.py index 952ac49..7fc28b1 100644 --- a/charaters/personnage.py +++ b/charaters/personnage.py @@ -1,6 +1,7 @@ from random import randint +from enum import Enum -class StatsSet(): +class StatsSet: #HP, ATK%, DEF%, CRIT, CRITRATE%, XPCOEF def __init__(self, hp, atkP, defP, crit, critrateP, xpcoef): self.hp = hp @@ -10,22 +11,20 @@ class StatsSet(): self.critrateP = critrateP self.xpcoef = xpcoef -class ClassType: + +class ClassType(Enum): GUERRIER = StatsSet(20, 10, 12, 5, 10, 8) MAGICIEN = StatsSet(16, 12, 6, 8, 15, 7) - #VOLEUR = - #ELF = - @staticmethod - def class_name(class_set): - if class_set == ClassType.GUERRIER: - return "GUERRIER" - elif class_set == ClassType.MAGICIEN: - return "MAGICIEN" + #TODO: Changer stats + VOLEUR = StatsSet(16, 12, 6, 8, 15, 7) + ELF = StatsSet(16, 12, 6, 8, 15, 7) + class Personnage: def __init__(self, nom, class_type): self.nom = nom - self.class_type = class_type + self.class_name = class_type.name + self.class_type = class_type.value self.__hp = self.class_type.hp self.stats = StatsSet(0, 0, 0, 0, 0, 0) @@ -61,7 +60,7 @@ class Personnage: def affiche_caracteristiques(self): return (f"Nom: {self.nom}", - f"Type de classe: {ClassType.class_name(self.class_type)}", + f"Type de classe: {self.class_name}", f"Vie: {self.__hp}", f"Expérience: {self.__xp}", "Stats (classe + personnage):", diff --git a/listener.py b/graphics/key_listener.py similarity index 100% rename from listener.py rename to graphics/key_listener.py diff --git a/graphics/layers.py b/graphics/layers.py index 1182457..588dc7b 100644 --- a/graphics/layers.py +++ b/graphics/layers.py @@ -1,3 +1,5 @@ +from charaters.personnage import Personnage, ClassType + from graphics.colors import Colors from graphics.writer import Layer @@ -5,15 +7,16 @@ from getkey import keys from re import match class GUI(Layer): - def __init__(self, screen, z_index, buttons, personnage): - super().__init__(screen, z_index) - screen.add_key_handler(self.key_handler) + def __init__(self, screen, z_index, buttons): + super().__init__(screen, z_index, "gui") self.buttons = buttons self.current = 0 - self.__personnage = personnage + self.handle_keys = False + self.personnage = Personnage(" ", ClassType.GUERRIER) def draw(self): + super().draw() x = 1 for button in self.buttons: color = (Colors.WHITEBG, Colors.BLACK) @@ -22,7 +25,7 @@ class GUI(Layer): self.put_string(button, x, self.y-10, color) x += len(button)+1 - stats = self.__personnage.affiche_caracteristiques() + stats = self.personnage.affiche_caracteristiques() for i in range(4): self.put_string(stats[i], 0, self.y-8+i, [Colors.RED2]) for i in range(7): @@ -36,30 +39,32 @@ class GUI(Layer): elif key == keys.LEFT and self.current > 0: self.current -= 1 + class PopUp(Layer): def __init__(self, screen, z_index): - super().__init__(screen, z_index) - screen.add_key_handler(self.key_handler) + super().__init__(screen, z_index, "popup") + #screen.add_key_handler(self.key_handler) - self.classes = ["GUERRIER", "MAGICIEN", "VOLEUR", "ELF"] - self.choosen_class = 0 - self.name = "" + self.__classes = ["GUERRIER", "MAGICIEN", "VOLEUR", "ELF"] + self.__choosen_class = 0 + self.__username = "" def draw(self): + super().draw() #bg self.rect(self.x//3, self.y//6, self.x//3 + self.x//4, self.y//4 + self.y//3, Colors.WHITEBG) #name self.put_string("Nom: [A-z]", self.x//3 + 1, self.y//6 + 1, [Colors.WHITEBG, Colors.BLACK]) self.rect(self.x//3 + 1, self.y//6 + 2, self.x//3 + self.x//4 - 1, self.y//6 + 2, Colors.BLACKBG) - self.put_string(self.name, self.x//3 + 1, self.y//6 + 2, [Colors.WHITE, Colors.BLACKBG]) + self.put_string(self.__username, self.x//3 + 1, self.y//6 + 2, [Colors.WHITE, Colors.BLACKBG]) #Classes self.put_string("Classe perso.: SHIFT+[1-4]", self.x//3 + 1, self.y//6 + 4, [Colors.WHITEBG, Colors.BLACK]) y = 0 - for user_class in self.classes: + for user_class in self.__classes: colors = [Colors.GREYBG, Colors.WHITE] - if y == self.choosen_class: + if y == self.__choosen_class: colors = [Colors.REDBG, Colors.BLACK] self.put_string(user_class, self.x//3 + 2, self.y//6 + 5 + y, colors) y += 1 @@ -70,10 +75,12 @@ class PopUp(Layer): def key_handler(self, key): if match("[a-zA-Z]", key): - self.name += key + self.__username += key elif key == keys.BACKSPACE: - self.name = self.name[:-1] + self.__username = self.__username[:-1] elif match("[1-4]", key): - self.choosen_class = int(key)-1 + self.__choosen_class = int(key)-1 elif key == keys.TAB: - print("TAB") \ No newline at end of file + self.screen.get_layer("gui").handle_keys = True + self.screen.del_layer("popup") + self.screen.get_layer("gui").personnage = Personnage(self.__username, ClassType[self.__classes[self.__choosen_class]]) \ No newline at end of file diff --git a/graphics/writer.py b/graphics/writer.py index 199e96d..52a4bbd 100644 --- a/graphics/writer.py +++ b/graphics/writer.py @@ -2,14 +2,16 @@ import os from graphics.colors import Colors class Layer: - def __init__(self, screen, z_index): + def __init__(self, screen, z_index, name): + self.name = name self.frame = {} self.z_index = z_index self.x = screen.x self.y = screen.y - - def clear(self): - self.frame = {} + self.screen = screen + self.handle_keys = True + + screen.set_layer(self) def put_char(self, char, x, y, colors=[]): if x > self.x or x < 0 or y > self.y or y < 0: @@ -32,9 +34,16 @@ class Layer: for x in range(x1, x2+1): for y in range(y1, y2+1): self.frame[(x, y)] = "".join(colors) + template + Colors.RESET + + def draw(self): + self.frame = {} + + def key_handler(self): + pass class Screen: + """Moteur graphique basé sur des claques""" def __init__(self): size = os.get_terminal_size() self.x = size.columns @@ -44,33 +53,39 @@ class Screen: self.handlers = [] - def set_layer(self, name, layer: Layer): - self.__layers[name] = layer + def set_layer(self, layer): + self.__layers[layer.name] = layer + + def del_layer(self, name): + del self.__layers[name] + + def get_layer(self, name): + if not name in self.__layers.keys(): + raise ValueError("unknown key") + return self.__layers[name] def __combine_layers(self): self.frame = {} layers = sorted(self.__layers.values(), key=lambda layer: layer.z_index) for layer in layers: + layer.draw() self.frame.update(layer.frame) def draw(self): self.__combine_layers() - content = "" + buffer = "" for y in range(self.y): for x in range(self.x): if (x, y) in self.frame: - content += self.frame[(x, y)] + buffer += self.frame[(x, y)] else: - content += " " - content += "\n" + buffer += " " + buffer += "\n" print("\033[H\033[J", end="") - print(content) - - - - def add_key_handler(self, handler): - self.handlers.append(handler) + print(buffer) def send_key(self, key): - for handler in self.handlers: - handler(key) \ No newline at end of file + """Envoie les touches reçues à tous les calques actifs""" + for layer in list(self.__layers.values()): + if layer.handle_keys == True: + layer.key_handler(key) \ No newline at end of file diff --git a/main.py b/main.py index a850641..cd7a208 100644 --- a/main.py +++ b/main.py @@ -1,24 +1,19 @@ from charaters.personnage import Personnage, ClassType import graphics.layers as layers -from graphics.writer import * -import listener +from graphics.writer import Screen +import graphics.key_listener as listener from time import sleep -personnage = Personnage("Pierre", ClassType.GUERRIER) - -screen = Screen() -gui = layers.GUI(screen, 2, ["Attaquer", "Inventaire"], personnage) -popup = layers.PopUp(screen, 1) - -def draw(): - screen.set_layer("gui", gui.draw()) - screen.set_layer("popup", popup.draw()) - if __name__ == "__main__": + #Initialise la partie graphique + screen = Screen() + layers.PopUp(screen, 1) + layers.GUI(screen, 2, ["Attaquer", "Inventaire"]) + listener.build_thread(screen).start() + while True: - draw() screen.draw() sleep(0.4) \ No newline at end of file