From e8eeebd2b920da45c296fff3de30c4f817682b46 Mon Sep 17 00:00:00 2001 From: Kalyax Date: Sun, 18 Sep 2022 13:02:59 +0200 Subject: [PATCH] =?UTF-8?q?d=C3=A9but=20fonctionnement=20jeu?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- game/core.py | 17 ++++ charaters/Inventaire.py => game/inventaire.py | 0 {charaters => game}/personnage.py | 15 +++- graphics/{writer.py => engine.py} | 88 ++++++++++--------- graphics/layers.py | 26 +++--- main.py | 10 ++- 6 files changed, 95 insertions(+), 61 deletions(-) create mode 100644 game/core.py rename charaters/Inventaire.py => game/inventaire.py (100%) rename {charaters => game}/personnage.py (84%) rename graphics/{writer.py => engine.py} (96%) diff --git a/game/core.py b/game/core.py new file mode 100644 index 0000000..ec99dde --- /dev/null +++ b/game/core.py @@ -0,0 +1,17 @@ +from game.personnage import Personnage, ClassType + +class Game: + def __init__(self): + self.personnage = Personnage(" ", ClassType.GUERRIER) + self.ennemy = Personnage("Zombie", ClassType.ZOMBIE) + + def attack(self, attacker, victim): + atk_dmg = attacker.jet_attaque() + def_dmg = victim.jet_defense() + victim.change_hp(atk_dmg) + attacker.change_hp(def_dmg) + if self.personnage.get_hp() == 0: + for i in range(50): + print("FIN") + if self.ennemy.get_hp() == 0: + self.ennemy = Personnage("Zombie", ClassType.ZOMBIE) \ No newline at end of file diff --git a/charaters/Inventaire.py b/game/inventaire.py similarity index 100% rename from charaters/Inventaire.py rename to game/inventaire.py diff --git a/charaters/personnage.py b/game/personnage.py similarity index 84% rename from charaters/personnage.py rename to game/personnage.py index 7fc28b1..4011e76 100644 --- a/charaters/personnage.py +++ b/game/personnage.py @@ -19,6 +19,9 @@ class ClassType(Enum): VOLEUR = StatsSet(16, 12, 6, 8, 15, 7) ELF = StatsSet(16, 12, 6, 8, 15, 7) + #Enemy + ZOMBIE = StatsSet(16, 12, 6, 8, 15, 7) + class Personnage: def __init__(self, nom, class_type): @@ -33,14 +36,15 @@ class Personnage: #self.element = def jet_attaque(self): + #TODO: ajouter crit damage = randint(1, 20) self.change_exp(self.class_type.xpcoef * self.__xp) - return damage + damage * (self.class_type.atkP + self.stats.atkP) + return damage + damage * (self.class_type.atkP//100 + self.stats.atkP//100) def jet_defense(self): damage = randint(1, 20) self.change_exp(self.class_type.xpcoef * self.__xp) - return damage + damage * (self.class_type.defP + self.stats.defP) + return damage + damage * (self.class_type.defP//100 + self.stats.defP//100) def get_hp(self): return self.__hp @@ -63,11 +67,14 @@ class Personnage: f"Type de classe: {self.class_name}", f"Vie: {self.__hp}", f"Expérience: {self.__xp}", - "Stats (classe + personnage):", + "Stats (classe + inventaire):", 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 + ) + + def reduced_stats(self): + return("Ennemi:", f"Type: {self.class_name}", f"Vie: {self.__hp}",) \ No newline at end of file diff --git a/graphics/writer.py b/graphics/engine.py similarity index 96% rename from graphics/writer.py rename to graphics/engine.py index 52a4bbd..6beb1e8 100644 --- a/graphics/writer.py +++ b/graphics/engine.py @@ -1,55 +1,16 @@ import os from graphics.colors import Colors -class Layer: - 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 - 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: - raise ValueError("out of range pixel") - self.frame[(x, y)] = "".join(colors) + char + Colors.RESET - - 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") - for string_y in range(len(string)): - 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=[], 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: - raise ValueError("template should be 1 char long") - 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): + def __init__(self, game): size = os.get_terminal_size() self.x = size.columns self.y = size.lines - 2 self.frame = {} self.__layers = {} + + self.game = game self.handlers = [] @@ -88,4 +49,45 @@ class Screen: """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 + layer.key_handler(key) + + +class Layer: + 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 + 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: + raise ValueError("out of range pixel") + self.frame[(x, y)] = "".join(colors) + char + Colors.RESET + + 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") + for string_y in range(len(string)): + 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=[], 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: + raise ValueError("template should be 1 char long") + 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 \ No newline at end of file diff --git a/graphics/layers.py b/graphics/layers.py index 588dc7b..423855d 100644 --- a/graphics/layers.py +++ b/graphics/layers.py @@ -1,7 +1,7 @@ -from charaters.personnage import Personnage, ClassType +from game.personnage import Personnage, ClassType from graphics.colors import Colors -from graphics.writer import Layer +from graphics.engine import Layer from getkey import keys from re import match @@ -13,7 +13,8 @@ class GUI(Layer): self.current = 0 self.handle_keys = False - self.personnage = Personnage(" ", ClassType.GUERRIER) + #self.personnage = Personnage(" ", ClassType.GUERRIER) + #self.ennemy = Personnage("Zombie", ClassType.ZOMBIE) def draw(self): super().draw() @@ -25,11 +26,15 @@ class GUI(Layer): self.put_string(button, x, self.y-10, color) x += len(button)+1 - stats = self.personnage.affiche_caracteristiques() + stats = self.screen.game.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]) + + ennemy_stats = self.screen.game.ennemy.reduced_stats() + for i in range(3): + self.put_string(ennemy_stats[i], 80, self.y-8+i, [Colors.BLUE2]) return self @@ -38,13 +43,15 @@ class GUI(Layer): self.current += 1 elif key == keys.LEFT and self.current > 0: self.current -= 1 + elif key == keys.ENTER and self.current == 0: + attacker = self.screen.game.personnage + victim = self.screen.game.ennemy + self.screen.game.attack(attacker, victim) -class PopUp(Layer): +class StartPopUp(Layer): def __init__(self, screen, z_index): super().__init__(screen, z_index, "popup") - #screen.add_key_handler(self.key_handler) - self.__classes = ["GUERRIER", "MAGICIEN", "VOLEUR", "ELF"] self.__choosen_class = 0 self.__username = "" @@ -53,12 +60,10 @@ class PopUp(Layer): 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.__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 @@ -83,4 +88,5 @@ class PopUp(Layer): elif key == keys.TAB: 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 + self.screen.game.personnage = Personnage(self.__username, ClassType[self.__classes[self.__choosen_class]]) + #self.screen.get_layer("gui").personnage = Personnage(self.__username, ClassType[self.__classes[self.__choosen_class]]) \ No newline at end of file diff --git a/main.py b/main.py index cd7a208..4a71994 100644 --- a/main.py +++ b/main.py @@ -1,15 +1,17 @@ -from charaters.personnage import Personnage, ClassType +from game.personnage import Personnage, ClassType +from game.core import Game import graphics.layers as layers -from graphics.writer import Screen +from graphics.engine import Screen import graphics.key_listener as listener from time import sleep if __name__ == "__main__": #Initialise la partie graphique - screen = Screen() - layers.PopUp(screen, 1) + game = Game() + screen = Screen(game) + layers.StartPopUp(screen, 1) layers.GUI(screen, 2, ["Attaquer", "Inventaire"]) listener.build_thread(screen).start()