From 496942e8d8a4cd6457badf34486bb191ccecbe4d Mon Sep 17 00:00:00 2001 From: Kalyax Date: Tue, 20 Sep 2022 21:38:29 +0200 Subject: [PATCH] =?UTF-8?q?cette=20fois=20=C3=A7a=20respecte=20les=20r?= =?UTF-8?q?=C3=A8gles?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- game/core.py | 55 +++++++++----------- game/personnage.py | 126 +++++++++++++++++---------------------------- graphics/layers.py | 58 ++++++++------------- main.py | 2 +- 4 files changed, 93 insertions(+), 148 deletions(-) diff --git a/game/core.py b/game/core.py index 042e81d..e49c72f 100644 --- a/game/core.py +++ b/game/core.py @@ -1,4 +1,4 @@ -from game.personnage import Personnage, ClassType, Item, Material +from game.personnage import * from graphics.layers import Sprite, PopUp @@ -10,54 +10,45 @@ class Game: self.ennemy = None def late_init(self): - self.personnage = Personnage(" ", ClassType.GUERRIER, "player") + self.personnage = Personnage(" ", "Guerrier", "player") self.ennemy = self.rand_ennemy() def rand_ennemy(self): - classes = ["GUERRIER", "MAGICIEN", "VOLEUR", "ELFE"] + classes = ["Guerrier", "Magicien", "Voleur", "Elfe"] noms = ["Bill Gates", "Dark Vador", "Gargamel", "G-Man"] - return Personnage(noms[randint(0, 3)], ClassType[classes[randint(0, 3)]], "opponant") + return Personnage(noms[randint(0, 3)], classes[randint(0, 3)], "OPPONENT") def init_personnage(self, nom, class_type): - self.personnage = Personnage(nom, ClassType[class_type], "player") + self.personnage = Personnage(nom, class_type, "PLAYER") def attack(self, attacker, victim): atk_jet = attacker.jet_attaque() def_jet = victim.jet_defense() if (atk_jet >= def_jet): rand = randint(1, 8) - victim.change_hp(rand + rand * ((attacker.class_type.atkP + attacker.arme.stats.atkP)//100)) + victim.change_pdv(rand) else: rand = randint(1, 4) - attacker.change_hp(rand + rand * ((victim.class_type.defP + victim.arme.stats.defP)//100)) + attacker.change_pdv(rand) - if self.personnage.get_hp() == 0: + if self.personnage.get_pdv() == 0: for i in range(50): PopUp(1, "Vous avez perdu! Relancez le jeu pour continuer...", True) - if self.ennemy.get_hp() == 0: + if self.ennemy.get_pdv() == 0: self.ennemy = self.rand_ennemy() potions = "" - for i in range(randint(0, 3)): - random = randint(5, 25) - self.personnage.potions.append(Item(Material.POTION, random)) - potions += f"- Potion de {random}HP\n" - - random = randint(0, 4) - arme = "" - stat = randint(1, 4) - stats = self.personnage.arme.stats - if random == 0: - stats.hp += stat - arme = f"+ {stat} HP" - elif random == 1: - stats.atkP += stat - arme = f"+ {stat} ATK%" - elif random == 2: - stats.defP += stat - arme = f"+ {stat} DEF%" - elif random == 3: - stats.initiative += stat - arme = f"+ {stat} INITIATIVE" - - PopUp(1, f"Ennemi vaincu!\n{potions}\n{arme}") \ No newline at end of file + inv = self.personnage.inventaire + if len(inv) >= 6: + potions = f"- Pas de potion car inventaire plein\n" + else: + maxpot = 3 + if len(inv) > 3: + maxpot = 6-len(inv) + for i in range(randint(0, maxpot)): + random = randint(5, 25) + inv.append(Material.POTION) + potions += f"- Potion de 10HP\n" + + PopUp(1, f"Ennemi vaincu!\n{potions}") + self.personnage.change_exp(1) \ No newline at end of file diff --git a/game/personnage.py b/game/personnage.py index 9efa29b..0d38f64 100644 --- a/game/personnage.py +++ b/game/personnage.py @@ -4,105 +4,73 @@ from enum import Enum from graphics.engine import Screen from graphics.layers import Sprite -class StatsSet: - #HP, ATK, DEF, INITIATIVE, XPCOEF - def __init__(self, hp, atkP, defP, initiative, xpcoef): - self.hp = hp - self.atkP = atkP - self.defP = defP - self.initiative = initiative - self.xpcoef = xpcoef +class Category: + def __init__(self, nom, __expcoef, inventaire): + self.nom = nom + self.pdv = __expcoef + self.inventaire = inventaire -class ClassType(Enum): - GUERRIER = StatsSet(20, 10, 15, 5, 8) - MAGICIEN = StatsSet(15, 15, 6, 5, 7) - VOLEUR = StatsSet(9, 30, 9, 7, 9) - ELFE = StatsSet(13, 22, 1, 5, 10) +class Material: + POTION = "POTION" + EPEE = "EPEE" + BATON = "BATON" + DAGUE = "DAGUE" + ARC = "ARC" class Personnage: - def __init__(self, nom, class_type, place): + def __init__(self, nom, cat, personnage_type): self.nom = nom - self.class_name = class_type.name - self.class_type = class_type.value - - self.__xp = 1 - - self.potions = [Item(Material.POTION, 10)] - self.arme = Item(*ClassItem[self.class_name].value) - - self.__hp = self.class_type.hp + self.arme.stats.hp - - Sprite(3, self.class_name, place) + self.__pdv = 20 + self.__exp = 1 + self.cat = cat + if cat == "Guerrier": + self.inventaire = [Material.EPEE, Material.POTION] + self.__expcoef = 10 + elif cat == "Magicien": + self.inventaire = [Material.BATON, Material.POTION] + self.__expcoef = 10 + elif cat == "Voleur": + self.inventaire = [Material.DAGUE, Material.POTION] + self.__expcoef = 3 + elif cat == "Elfe": + self.inventaire = [Material.ARC, Material.POTION] + self.__expcoef = 8 + + Sprite(3, cat, personnage_type) def jet_attaque(self): damage = randint(1, 20) - self.change_exp(self.class_type.xpcoef * self.__xp) - return damage + self.class_type.initiative + self.arme.stats.initiative + self.change_exp(self.__expcoef * self.__exp) + return damage def jet_defense(self): damage = randint(1, 20) - self.change_exp(self.class_type.xpcoef * self.__xp) - return damage + self.class_type.initiative + self.arme.stats.initiative + self.change_exp(self.__expcoef * self.__exp) + return damage - def get_hp(self): - return self.__hp - def change_hp(self, nb_hp): - if self.__hp - nb_hp < 0: - self.__hp = 0 - elif self.__hp - nb_hp > self.class_type.hp + self.arme.stats.hp: - self.__hp = self.class_type.hp + self.arme.stats.hp + def get_pdv(self): + return self.__pdv + def change_pdv(self, nb_pdv): + if self.__pdv - nb_pdv < 0: + self.__pdv = 0 else: - self.__hp -= nb_hp + self.__pdv -= nb_pdv def get_xp(self): - return self.__xp + return self.__exp def change_exp(self, nb_exp): if nb_exp > 0: - self.__xp += nb_exp + self.__exp += nb_exp else: raise ValueError("nb_exp attends un nombre positif") def affiche_caracteristiques(self): return (f"Nom: {self.nom}", - f"Type de classe: {self.class_name}", - f"Vie: {self.__hp}", - f"Expérience: {self.__xp}", - "Stats (classe + inventaire):", - f"- HP: {self.class_type.hp} + {self.arme.stats.hp}", - f"- ATK%: {self.class_type.atkP} + {self.arme.stats.atkP}", - f"- DEF%: {self.class_type.defP} + {self.arme.stats.defP}", - f"- INITIATIVE: {self.class_type.initiative} + {self.arme.stats.initiative}", - f"- XPCOEF: {self.class_type.xpcoef} + {self.arme.stats.xpcoef}" + f"Type de classe: {self.cat}", + f"Vie: {self.__pdv}", + f"Expérience: {self.__exp}", + f"XPCOEF: {self.__expcoef}" ) - def reduced_stats(self): - return(f"Ennemi: {self.nom}", - f"Type: {self.class_name}", - f"Vie: {self.__hp}" - ) - - -class Item(): - def __init__(self, material, stats, meta=None): - self.material = material - self.meta = material.value if meta == None else meta - self.stats = stats - -class ItemMeta: - def __init__(self, name, description): - self.name = name - self.description = description - -class Material(Enum): - POTION = ItemMeta("Potion", "TODO") - EPEE = ItemMeta("Épée", "TODO") - BATON = ItemMeta("Bâton", "TODO") - DAGUE = ItemMeta("Dague", "TODO") - ARC = ItemMeta("Arc", "TODO") - -class ClassItem(Enum): - #HP, ATK, DEF, INITIATIVE, XPCOEF - GUERRIER = (Material.EPEE, StatsSet(3, 5, 2, 6, 0)) - MAGICIEN = (Material.BATON, StatsSet(8, 1, 2, 3, 0)) - VOLEUR = (Material.DAGUE, StatsSet(1, 9, 1, 1, 0)) - ELFE = (Material.ARC, StatsSet(4, 2, 5, 3, 0)) \ No newline at end of file + def affiche_inventaire(self): + return ["- "+item for item in self.inventaire] \ No newline at end of file diff --git a/graphics/layers.py b/graphics/layers.py index 5c17a9d..c3b6720 100644 --- a/graphics/layers.py +++ b/graphics/layers.py @@ -27,13 +27,11 @@ class GUI(Layer): x += len(button)+1 stats = Screen.instance.game.personnage.affiche_caracteristiques() - for i in range(4): + for i in range(5): self.put_string(stats[i], 1, self.y-8+i, [Colors.RED2]) - for i in range(6): - self.put_string(stats[i+4], (self.x-2)//3, self.y-8+i, [Colors.RED2]) - ennemy_stats = Screen.instance.game.ennemy.reduced_stats() - for i in range(3): + ennemy_stats = Screen.instance.game.ennemy.affiche_caracteristiques() + for i in range(5): self.put_string(ennemy_stats[i], self.x-(self.x-2)//3, self.y-8+i, [Colors.BLUE2]) else: @@ -42,24 +40,11 @@ class GUI(Layer): color = (Colors.REDBG, Colors.BLACK) if self.__buttons[self.__current] == button else (Colors.WHITEBG, Colors.BLACK) self.put_string(button, x, self.y-10, color) x += len(button)+1 - - if self.__current == 0: - self.put_string(f"ENTREE pour retourner en arrière", 1, self.y-8, [Colors.RED2]) - elif self.__current == 1: - potions = Screen.instance.game.personnage.potions - self.put_string(f"Nombre total de potions: {len(potions)}", 1, self.y-8, [Colors.RED2]) - if len(potions) > 0: - self.put_string(f"Rénération de la prochaine potion: {potions[0].stats}", 1, self.y-7, [Colors.RED2]) - self.put_string(f"ENTEE pour utiliser", 1, self.y-5, [Colors.RED2]) - elif self.__current == 2: - arme = Screen.instance.game.personnage.arme - self.put_string(f"Nom: {arme.meta.name}", 1, self.y-8, [Colors.RED2]) - self.put_string(f"Stats:", 1, self.y-7, [Colors.RED2]) - self.put_string(f"- HP: {arme.stats.hp}", 1, self.y-6, [Colors.RED2]) - self.put_string(f"- ATK%: {arme.stats.atkP}", 1, self.y-5, [Colors.RED2]) - self.put_string(f"- DEF%: {arme.stats.defP}", 1, self.y-4, [Colors.RED2]) - self.put_string(f"- INITIATIVE: {arme.stats.initiative}", 1, self.y-3, [Colors.RED2]) - self.put_string(f"- XPCOEF: {arme.stats.xpcoef}", 1, self.y-2, [Colors.RED2]) + + self.put_string("Votre inventaire:", 1, self.y-8, [Colors.RED2]) + stats = Screen.instance.game.personnage.affiche_inventaire() + for i in range(len(stats)): + self.put_string(stats[i], 1, self.y-7+i, [Colors.RED2]) return self @@ -76,15 +61,18 @@ class GUI(Layer): Screen.instance.game.attack(attacker, victim) elif self.__current == 1: self.__inventaire = True - self.__buttons = ["Retour", "Potion", "Arme"] + self.__buttons = ["Retour", "Utiliser une potion"] else: if self.__current == 0: self.__inventaire = False self.__buttons = ["Attaquer", "Inventaire"] - elif self.__current == 1 and len(Screen.instance.game.personnage.potions) > 0: - personnage = Screen.instance.game.personnage - personnage.change_hp(-1*personnage.potions[0].stats) - del personnage.potions[0] + elif self.__current == 1: + perso = Screen.instance.game.personnage + for item in range(len(perso.inventaire)): + if perso.inventaire[item] == "POTION": + perso.change_pdv(-10) + del perso.inventaire[item] + break @@ -92,7 +80,7 @@ class StartPopUp(Layer): """Calque du menu de lancement""" def __init__(self, z_index): super().__init__(z_index, "popup") - self.__classes = ["GUERRIER", "MAGICIEN", "VOLEUR", "ELFE"] + self.__classes = ["Guerrier", "Magicien", "Voleur", "Elfe"] self.__choosen_class = 0 self.__username = "" self.__missing_un = False @@ -109,7 +97,7 @@ class StartPopUp(Layer): self.put_string("Classe perso.: (Flèches)", self.x//3 + 1, self.y//6 + 4, [Colors.WHITEBG, Colors.BLACK]) y = 0 for user_class in self.__classes: - colors = colors = [Colors.REDBG, Colors.BLACK] if y == self.__choosen_class else [Colors.GREYBG, Colors.WHITE] + colors = [Colors.REDBG, Colors.BLACK] if y == self.__choosen_class else [Colors.GREYBG, Colors.WHITE] self.put_string(user_class, self.x//3 + 2, self.y//6 + 5 + y, colors) y += 1 @@ -170,17 +158,15 @@ class PopUp(Layer): Screen.instance.del_layer("popup") class Sprite(Layer): - def __init__(self, z_index, sprite_name, place): - if place != "player" and place != "opponant": - raise ValueError("wrong sprite name") - super().__init__(z_index, place) - self.place = place + def __init__(self, z_index, sprite_name, personnage_type): + super().__init__(z_index, personnage_type) + self.personnage_type = personnage_type self.sprite_name = sprite_name def draw(self): super().draw() with open(f"game/sprites/{self.sprite_name}.txt", "r") as sprite: - pos = (1, 1) if self.place == "player" else (Screen.instance.x*2//3+1, 1) + pos = (1, 1) if self.personnage_type == "PLAYER" else (Screen.instance.x*2//3+1, 1) self.put_string(sprite.read(), *pos) def key_handler(self, key): diff --git a/main.py b/main.py index a0cab77..24b582e 100644 --- a/main.py +++ b/main.py @@ -1,4 +1,4 @@ -from game.personnage import Personnage, ClassType +from game.personnage import * from game.core import Game import graphics.layers as layers