From 10fc929417dee11cd08e1893a3b0582f045532eb Mon Sep 17 00:00:00 2001 From: anthony-melin Date: Thu, 16 Jul 2026 19:38:51 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20parameterized=20action=20menu?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.py | 61 ++++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 44 insertions(+), 17 deletions(-) diff --git a/app.py b/app.py index 851c840..f44471c 100644 --- a/app.py +++ b/app.py @@ -1,16 +1,45 @@ +import os +from enum import Enum + from character_builder import CharacterBuilder from level_one import Level -import os -def action_menu(): - """ Menu Action du niveau """ - print("\n------ Menu Action ------\n") - print("Pièce Suivante :\t\t 1") - print("Combat : \t\t\t 2") - print("Fuir : \t\t\t\t 3") - print("Quitter :\t\t\t 4") - return int(input("\nChoix : ")) +class Action(Enum): + PIECE_SUIVANTE = (1, "Pièce suivante") + COMBAT = (2, "Combat") + FUIR = (3, "Fuir") + QUITTER = (4, "Quitter") + + @property + def code(self): + return self.value[0] + + @property + def libelle(self): + return self.value[1] + + +def action_menu(actions_autorisees: list[Action] = None) -> Action: + """Affiche le menu et retourne une action valide.""" + + actions_autorisees = list(Action) if actions_autorisees is None else actions_autorisees + + while True: + print("\n------ Menu Action ------\n") + + for action in actions_autorisees: + print(f"{action.libelle:<20} : {action.code}") + + try: + choix = int(input("\nChoix : ")) + for action in actions_autorisees: + if action.code == choix: + return action + + print("Mauvais choix") + except ValueError: + print("Veuillez saisir un nombre.") # Exemple d'utilisation @@ -27,8 +56,8 @@ if __name__ == "__main__": while level.exit_level: os.system('cls' if os.name == 'nt' else 'clear') - match action_menu(): - case 1: + match action_menu([Action.PIECE_SUIVANTE, Action.QUITTER]): + case Action.PIECE_SUIVANTE: if level.current_room_index >= number_room: print("fin du donjon !") break @@ -37,22 +66,20 @@ if __name__ == "__main__": if room is not None: for mob in room: print(f"Un {mob.name} est devant toi !") - match action_menu(): - case 2: + match action_menu([Action.COMBAT, Action.FUIR]): + case Action.COMBAT: level.combat(mob, player) if player.current_pv <= 0: print("!!!!!!!!! Joueur mort !!!!!!!!!!") break if mob.current_pv == 0: print(f"Le {mob.name} est mort !!") - case 3: + case Action.FUIR: print("Tu fuis la pièce") else: print("\n ------ Pièce vide ------ \n") player.current_pv = player.max_pv level.current_room_index += 1 - case 4: + case Action.QUITTER: level.exit_level = False print(f"Au revoir {player.name} !") - case _: - print("Mauvais choix")