From 47179b6969809f611ad7b88dba96ea372b09b868 Mon Sep 17 00:00:00 2001 From: Kalyax Date: Sat, 17 Sep 2022 17:29:24 +0200 Subject: [PATCH] key_handler et popup; TODO: draw_handler --- charaters/personnage.py | 29 +++++++++------ graphics/layer/menu.py | 20 ----------- graphics/layers.py | 79 +++++++++++++++++++++++++++++++++++++++++ graphics/writer.py | 21 +++++++---- listener.py | 9 +++-- main.py | 32 +++++++---------- 6 files changed, 128 insertions(+), 62 deletions(-) delete mode 100644 graphics/layer/menu.py create mode 100644 graphics/layers.py diff --git a/charaters/personnage.py b/charaters/personnage.py index bccde6d..952ac49 100644 --- a/charaters/personnage.py +++ b/charaters/personnage.py @@ -15,6 +15,12 @@ class ClassType: 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" class Personnage: def __init__(self, nom, class_type): @@ -54,14 +60,15 @@ class Personnage: raise ValueError("nb_exp attends un nombre positif") def affiche_caracteristiques(self): - print(f"Nom: {self.nom}") - print(f"Type de classe: {self.class_type.name}") - print(f"Vie: {self.__hp}") - print(f"Expérience: {self.__xp}") - print("Stats (classe + personnage):") - print(f"- HP: {self.class_type.hp} + {self.stats.hp}") - print(f"- ATK%: {self.class_type.atkP} + {self.stats.atkP}") - print(f"- DEF%: {self.class_type.defP} + {self.stats.defP}") - print(f"- CRIT: {self.class_type.crit} + {self.stats.crit}") - print(f"- CRITRATE%: {self.class_type.critrateP} + {self.stats.critrateP}") - print(f"- XPCOEF: {self.class_type.xpcoef} + {self.stats.xpcoef}") \ No newline at end of file + return (f"Nom: {self.nom}", + f"Type de classe: {ClassType.class_name(self.class_type)}", + f"Vie: {self.__hp}", + f"Expérience: {self.__xp}", + "Stats (classe + personnage):", + f"- HP: {self.class_type.hp} + {self.stats.hp}", + f"- ATK%: {self.class_type.atkP} + {self.stats.atkP}", + f"- DEF%: {self.class_type.defP} + {self.stats.defP}", + f"- CRIT: {self.class_type.crit} + {self.stats.crit}", + f"- CRITRATE%: {self.class_type.critrateP} + {self.stats.critrateP}", + f"- XPCOEF: {self.class_type.xpcoef} + {self.stats.xpcoef}" + ) \ No newline at end of file diff --git a/graphics/layer/menu.py b/graphics/layer/menu.py deleted file mode 100644 index bb448cb..0000000 --- a/graphics/layer/menu.py +++ /dev/null @@ -1,20 +0,0 @@ -from graphics.colors import Colors -from graphics.writer import Layer - -class Menu: - def __init__(self, cli, buttons): - self.cli = cli - #self.stats = stats - self.buttons = buttons - self.current = 0 - - def draw_layer(self): - layer = Layer(self.cli, 1) - x = 1 - for button in self.buttons: - color = (Colors.WHITEBG, Colors.BLACK) - if self.buttons[self.current] == button: - color = (Colors.REDBG, Colors.BLACK) - layer.put_string(button, x, self.cli.y-10, color) - x += 11 - return layer \ No newline at end of file diff --git a/graphics/layers.py b/graphics/layers.py new file mode 100644 index 0000000..1182457 --- /dev/null +++ b/graphics/layers.py @@ -0,0 +1,79 @@ +from graphics.colors import Colors +from graphics.writer import Layer + +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) + self.buttons = buttons + self.current = 0 + + self.__personnage = personnage + + def draw(self): + x = 1 + for button in self.buttons: + color = (Colors.WHITEBG, Colors.BLACK) + if self.buttons[self.current] == button: + color = (Colors.REDBG, Colors.BLACK) + self.put_string(button, x, self.y-10, color) + x += len(button)+1 + + 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): + self.put_string(stats[i+4], 40, self.y-8+i, [Colors.RED2]) + + return self + + def key_handler(self, key): + if key == keys.RIGHT and self.current < len(self.buttons)-1: + self.current += 1 + 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) + + self.classes = ["GUERRIER", "MAGICIEN", "VOLEUR", "ELF"] + self.choosen_class = 0 + self.name = "" + + def draw(self): + #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]) + + #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: + colors = [Colors.GREYBG, Colors.WHITE] + 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 + + self.put_string("TAB pour confirmer", self.x//3 + 1, self.y//4 + self.y//3 -1, [Colors.WHITEBG, Colors.BLACK]) + + return self + + def key_handler(self, key): + if match("[a-zA-Z]", key): + self.name += key + elif key == keys.BACKSPACE: + self.name = self.name[:-1] + elif match("[1-4]", key): + self.choosen_class = int(key)-1 + elif key == keys.TAB: + print("TAB") \ No newline at end of file diff --git a/graphics/writer.py b/graphics/writer.py index 8e3563a..199e96d 100644 --- a/graphics/writer.py +++ b/graphics/writer.py @@ -1,5 +1,4 @@ import os -from typing import List from graphics.colors import Colors class Layer: @@ -12,12 +11,12 @@ class Layer: def clear(self): self.frame = {} - def put_char(self, char, x, y, colors: List[Colors]): + def put_char(self, char, x, y, colors=[]): if x > self.x or x < 0 or y > self.y or y < 0: raise ValueError("out of range pixel") self.frame[(x, y)] = "".join(colors) + char + Colors.RESET - def put_string(self, string, x, y, colors: List[Colors]): + def put_string(self, string, x, y, colors=[]): if x > self.x or x < 0 or y > self.y or y < 0: raise ValueError("out of range pixel") string = string.split("\n") @@ -25,7 +24,7 @@ class Layer: for string_x in range(len(string[string_y])): self.frame[(x+string_x, y+string_y)] = "".join(colors) + string[string_y][string_x] + Colors.RESET - def rect(self, x1, y1, x2, y2, colors: List[Colors], template=' '): + def rect(self, x1, y1, x2, y2, colors=[], template=' '): if (x1 > self.x or x1 < 0 or y1 > self.y or y1 < 0) or (x2 > self.x or x2 < 0 or y2 > self.y or y2 < 0): raise ValueError("out of range pixel") if len(template) > 1: @@ -43,6 +42,8 @@ class Screen: self.frame = {} self.__layers = {} + self.handlers = [] + def set_layer(self, name, layer: Layer): self.__layers[name] = layer @@ -56,7 +57,6 @@ class Screen: self.__combine_layers() content = "" for y in range(self.y): - #content += "\n" for x in range(self.x): if (x, y) in self.frame: content += self.frame[(x, y)] @@ -64,4 +64,13 @@ class Screen: content += " " content += "\n" print("\033[H\033[J", end="") - print(content) \ No newline at end of file + print(content) + + + + def add_key_handler(self, handler): + self.handlers.append(handler) + + def send_key(self, key): + for handler in self.handlers: + handler(key) \ No newline at end of file diff --git a/listener.py b/listener.py index caabe68..45e4d4e 100644 --- a/listener.py +++ b/listener.py @@ -1,11 +1,10 @@ from threading import Thread from getkey import getkey -from main import key_handler - -def __listener(): +def __listener(screen): while True: key = getkey() - key_handler(key) + screen.send_key(key) -key_listener = Thread(target=__listener) +def build_thread(screen): + return Thread(target=__listener, args=(screen,)) diff --git a/main.py b/main.py index c4f8804..a850641 100644 --- a/main.py +++ b/main.py @@ -1,32 +1,24 @@ -from charaters.personnage import Personnage +from charaters.personnage import Personnage, ClassType -from graphics.colors import Colors -from graphics.layer.menu import Menu +import graphics.layers as layers from graphics.writer import * - -from getkey import keys +import listener from time import sleep -game = Screen() -menu = Menu(game, ["Inventaire", "Teset"]) +personnage = Personnage("Pierre", ClassType.GUERRIER) -def key_handler(key): - print(key) - if key == keys.RIGHT and menu.current < len(menu.buttons)-1: - main.menu.current += 1 - elif key == keys.LEFT and menu.current > 0: - menu.current -= 1 - print(menu.current) +screen = Screen() +gui = layers.GUI(screen, 2, ["Attaquer", "Inventaire"], personnage) +popup = layers.PopUp(screen, 1) def draw(): - game.set_layer("menu", menu.draw_layer()) + screen.set_layer("gui", gui.draw()) + screen.set_layer("popup", popup.draw()) if __name__ == "__main__": - from listener import key_listener - #key_listener.start() + listener.build_thread(screen).start() while True: draw() - game.draw() - print("t") - sleep(1) \ No newline at end of file + screen.draw() + sleep(0.4) \ No newline at end of file