import random import time import datetime from text import read from text import input_read import math game_date = datetime.datetime(793, 7, 30, 12, 0, 0)#Défini la date de départ du jeu # La liste des poissons avec leur poids et leur valeur monétaire poissons = { 'carpe':{ "name": "carpe", "quantity": 0, "price": 3, "description": "Restaure 50 points de vie." }, 'truite': { "name": "truite", "quantity": 0, "price": 5, "description": "Restaure 50 points de vie." }, 'saumon': { "name": "saumon", "quantity": 0, "price": 10, "description": "Restaure 50 points de vie." }, 'poisson-chat': { "name": "poisson-chat", "quantity": 0, "price": 12, "description": "Restaure 50 points de vie." }, } #La liste des recettes possibles avec leurs résultats recipes = { "hache en silex" : { "name": "hache en silex", "ingredients": {"branche taillée": 1, "silex": 1, "ficelle": 2}, "result": { "name": "hache en silex", "quantity": 0, "price": 10, "description": "Restaure 50 points de vie." } }, "tissus propre": { "name": "tissus propre", "ingredients": {"tissus sale": 1, "eau": 1}, "result": { "name": "tissus propre", "quantity": 0, "price": 10, "description": "Restaure 50 points de vie." } } } #Définition de la classe Marchand class Marchand: def __init__(self, name): self.name = name self.body_parts = {} self.inventory = {} self.pieces_or = 50 self.map = {} def show_inventory(self): if not self.inventory: read("L'inventaire inventaire de {} est vide.".format(self.name)) else: print() read("{} propose de vendre :".format(self.name)) for item, item_data in self.inventory.items(): read(f"-{item_data['name'].capitalize()} x{item_data['quantity']}") read("il a {} pièces d'or.".format(self.pieces_or)) def add_inventory(self, item_add, quantity=1): if item_add["name"] in self.inventory: self.inventory[item_add["name"]]["quantity"] += quantity else: self.inventory[item_add["name"]] = { "name": item_add["name"], "quantity": quantity, "price": item_add["price"], "description": item_add["description"] } def remove_inventory(self, item_remove, quantity=1): if item_remove["name"] in self.inventory: self.inventory[item_remove["name"]]["quantity"] -= quantity if self.inventory[item_remove["name"]]["quantity"] <= 0: del self.inventory[item_remove["name"]] # Définition de la classe Personnage class Personnage: def __init__(self, name, sexe, age): self.name = name self.sexe = sexe self.age = age self.inventory = {} self.pieces_or = 10 self.body_parts = {} self.stats = {} self.map = {} def move (self): #Permet de bouger sur la map move = input("Move") if move == "z": self.map["y"] -= 1 elif move == "q": self.map["x"] -= 1 elif move == "s": self.map["y"] += 1 elif move == "d": self.map["x"] += 1 elif move == "menu": self.show_menu() else: print("Invalid key.") def show_menu (self): # Affichage du menu du jeu while True : print() read("Que souhaitez vous faire ?") print() read("1- Aller chez le marchand") read("2- Pecher") read("3- Crafter") read("4- Voir votre inventaire") read("5- Voir vos stats") read("6- Quitter le jeu") read("7- Attaquer le marchand") print() choice = input_read("Entrez votre choix (1-6) : ") if choice == "marchand" or choice == "1": personnage.aller_chez_le_marchand() elif choice == "peche" or choice == "2": personnage.pecher() if choice == "3": personnage.craft_item() elif choice == "inventaire" or choice == "4": personnage.navigate_inventory() elif choice == "stats" or choice == "5": personnage.show_stats() elif choice == "quit" or choice == "6": break elif choice == "mal" or choice == "7": personnage.attack(marchand1) def calcul_damage(self): return random.randint(1 + 1 * self.stats["Force"]["Niveau"], 3 + 2 * self.stats["Force"]["Niveau"]) def target_body_part(self, target): target_body_limb = random.choice(list(target.body_parts.keys())) target_body_part = random.choice(list(target.body_parts[target_body_limb]["sous_partie"].keys())) hit_parts = {target_body_limb: target_body_part} return hit_parts def damage_body_parts(self, target, hit_parts, damage): # necessite la création d'une liste tel que : {body_limb : body_part} for body_limb, body_part in hit_parts.items(): body_limb_hit = target.body_parts[body_limb] if body_limb_hit is not None: target.body_parts[body_limb]["points_de_vie"] -= damage read("{} -{}PV PV restants: {}".format(body_limb, damage, target.body_parts[body_limb]["points_de_vie"])) else: print(f"{body_limb} non trouvé") for body_limb, body_part in hit_parts.items(): body_part_hit = target.body_parts[body_limb]["sous_partie"][body_part] if body_part_hit is not None: target.body_parts[body_limb]["sous_partie"][body_part]["points_de_vie"] -= damage read("{} -{}PV PV restants: {}".format(body_part, damage, target.body_parts[body_limb]["sous_partie"][body_part][ "points_de_vie"])) else: print(f"{body_part} non trouvé") def attack(self, target): read("{} attaque {} !".format(self.name, target.name)) hit_parts = self.target_body_part(target) damage = self.calcul_damage() self.damage_body_parts(target, hit_parts, damage) def show_inventory(self): if not self.inventory: read("Votre inventaire est vide.") else: print() read("Inventaire :") for item, item_data in self.inventory.items(): read(f"-{item_data['name'].capitalize()} x{item_data['quantity']}") read("Vous avez {} pièces d'or.".format(self.pieces_or)) def navigate_inventory(self): while True: self.show_inventory() print() item_select = input_read("Selectionner un item ou quitter l'inventaire (0)") print() if item_select == "0": break if item_select not in self.inventory : read("Vous ne possedez pas cet item.") else: while True : action_item = input_read("Item selectionné : {} 1)Lire la description 2)Drop 3)Annuler".format(self.inventory[item_select]["name"])) print() if action_item == "1" : read(self.inventory[item_select]["description"]) print() if action_item == "2" : self.remove_inventory(self.inventory[item_select],1) break if action_item == "3" : break def add_inventory(self, item_add, quantity=1): if item_add["name"] in self.inventory: self.inventory[item_add["name"]]["quantity"] += quantity else: self.inventory[item_add["name"]] = { "name": item_add["name"], "quantity": quantity, "price": item_add["price"], "description": item_add["description"] } def remove_inventory(self, item_remove, quantity=1): if item_remove["name"] in self.inventory: self.inventory[item_remove["name"]]["quantity"] -= quantity if self.inventory[item_remove["name"]]["quantity"] <= 0: del self.inventory[item_remove["name"]] def show_stats (self) : for i in self.stats : read("{} Niveau {}".format(i,self.stats[i]["Niveau"])) def craft_item(self): while True: # Affiche les recettes disponibles read("Recettes :") for i, recipe in enumerate(recipes): read(f"{i + 1}. {recipes[recipe]['name']}") # Demande à l'utilisateur de choisir une recette recipe_choice = int( input_read( "Entrez le numéro de la recette que vous souhaitez crafter (ou '0' pour annuler) : ")) if recipe_choice == 0: break # Vérifie que l'index de recette est valide if recipe_choice < 0 or recipe_choice >= (len(recipes) + 1): read("Recette invalide.") else: recipe = list(recipes.values())[recipe_choice - 1] # Vérifie si tous les ingrédients sont disponibles dans l'inventaire for ingredient, quantity in recipe["ingredients"].items(): if ingredient not in self.inventory or self.inventory[ingredient]["quantity"] < quantity: read("Vous n'avez pas tous les ingrédients nécessaires.") return False for ingredient, quantity in recipe["ingredients"].items():#retire les ingredients de l'inventaire self.remove_inventory(self.inventory[ingredient], quantity) # Ajoute l'objet résultant à l'inventaire item_add = recipe["result"] self.add_inventory(item_add,1) read("Vous avez fabriqué un(e) {}.".format(recipe["name"])) print() def pecher(self): if 'canne à pêche' not in self.inventory: read("Vous n'avez pas de canne à pêche.") return read("Vous lancez votre ligne à l'eau...") time.sleep(2) key = random.choice(list(poissons.keys())) poisson = poissons[key] self.add_inventory(poisson,1) read("Vous avez attrapé un {}, il vaut {} pièces d'or.".format(poisson["name"],poisson["price"])) def aller_chez_le_marchand(self): self.show_inventory() marchand1.show_inventory() # demander à l'utilisateur ce qu'il veut acheter ou vendre print() action = input_read("Que voulez-vous faire ? acheter ou vendre?") print() if action == "acheter": marchand1.show_inventory() # demander à l'utilisateur ce qu'il veut acheter et combien print() item_choice = input_read("Que voulez-vous acheter ? ") print() if item_choice not in marchand1.inventory: print() read("Le marchand ne vend pas cet article.") print() return item_buy = marchand1.inventory[item_choice] price = item_buy["price"] print() quantity = input_read("Combien de {} voulez-vous acheter ? ".format(item_buy["name"])) quantity = int(quantity) if self.pieces_or < price * quantity: print() read("Vous n'avez pas assez d'argent.") print() return self.add_inventory(item_buy, quantity) self.pieces_or -= price * quantity marchand1.remove_inventory(item_buy, quantity) marchand1.pieces_or += price * quantity print() read("Vous avez acheté {} {} pour {} pièces d'or.".format(quantity, item_buy["name"],price * quantity)) print() self.show_inventory() marchand1.show_inventory() elif action == "vendre": # demander à l'utilisateur ce qu'il veut vendre et combien self.show_inventory() print() item_choice = input_read("Que voulez-vous vendre ? ") print() if item_choice not in self.inventory: print() read("Vous n'avez pas cet article.") print() return item_sell = self.inventory[item_choice] price = item_sell["price"] print() quantity = input_read("Combien de {} voulez-vous vendre ? ".format(item_sell["name"])) print() quantity = int(quantity) self.remove_inventory(item_sell, quantity) self.pieces_or += price * quantity marchand1.add_inventory(item_sell, quantity) marchand1.pieces_or -= price * quantity print() read("Vous avez vendu {} pour {} pièces d'or.".format(quantity, item_sell["name"], price * quantity)) print() self.show_inventory() marchand1.show_inventory() marchand1 = Marchand ("Pierre")#Crée un Marchand Pierre, défini son inventaire et ses body parts marchand1.body_parts = { "tête": { "points_de_vie": 10, "sous_partie": { "crâne" : {"points_de_vie" : 5, }, "joue gauche": {"points_de_vie" : 5, }, "joue droite": {"points_de_vie" : 5, }, "front": {"points_de_vie" : 5, }, "oeil gauche": {"points_de_vie" : 5, }, "oeil droit": {"points_de_vie" : 5, }, "nez": {"points_de_vie" : 5, }, "bouche": {"points_de_vie" : 5, }, "menton": {"points_de_vie" : 5, }, "oreille droite": {"points_de_vie" : 5, }, "oreille gauche": {"points_de_vie" : 5, } }, }, "cou": { "points_de_vie": 8, "sous_partie": {}, }, "bras gauche": { "points_de_vie": 8, "sous_partie": { "épaule gauche": {"points_de_vie": 5}, "biceps gauche": {"points_de_vie" : 5, }, "triceps gauche": {"points_de_vie" : 5, }, "avant-bras gauche": {"points_de_vie" : 5, }, "coude gauche": {"points_de_vie" : 5, }, "poignet gauche": {"points_de_vie" : 5, }, "main gauche": {"points_de_vie" : 5, } }, }, "bras droit": { "points_de_vie": 8, "sous_partie": { "épaule droite": {"points_de_vie": 5}, "biceps droit": {"points_de_vie" : 5, }, "triceps droit": {"points_de_vie" : 5, }, "avant-bras droit": {"points_de_vie" : 5, }, "coude droit": {"points_de_vie" : 5, }, "poignet droit": {"points_de_vie" : 5, }, "main droite": {"points_de_vie" : 5, } }, }, "dos": { "points_de_vie": 10, "sous_partie": { "trapèzes": {"points_de_vie" : 5, }, "rhomboïdes": {"points_de_vie" : 5, }, "lombaires": {"points_de_vie" : 5, } }, }, "abdomen": { "points_de_vie": 10, "sous_partie": { "abdominaux": {"points_de_vie" : 5, }, "pectoral gauche": {"points_de_vie" : 5, }, "pectoral droit": {"points_de_vie" : 5, }, "sternum": {"points_de_vie" : 5, } }, }, "hanche": { "points_de_vie": 7, "sous_partie": { "fessiers": {"points_de_vie" : 5, }, "hanches": {"points_de_vie" : 5, } }, }, "jambe gauche": { "points_de_vie": 8, "sous_partie": { "cuisse gauche": {"points_de_vie" : 5, }, "mollet gauche": {"points_de_vie" : 5, }, "genou gauche": {"points_de_vie" : 5, }, "cheville gauche": {"points_de_vie" : 5, }, "pied gauche": {"points_de_vie" : 5, }}, }, "jambe droite": { "points_de_vie": 8, "sous_partie": { "cuisse droite": {"points_de_vie" : 5, }, "mollet droit": {"points_de_vie" : 5, }, "genou droit": {"points_de_vie" : 5, }, "cheville droite": {"points_de_vie" : 5, }, "pied droit": {"points_de_vie" : 5, }}, }, } marchand1.inventory = { "branche taillée": { "name": "branche taillée", "quantity": 5, "price": 10, "description": "Restaure 50 points de vie." }, "silex": { "name": "silex", "quantity": 1, "price": 7, "description": "Une épée en fer standard." }, "ficelle": { "name": "ficelle", "quantity": 2, "price": 20, "description": "Un bouclier en bois standard." }, "eau": { "name": "eau", "quantity": 2, "price": 20, "description": "Un bouclier en bois standard." }, "tissus sale": { "name": "tissus sale", "quantity": 2, "price": 20, "description": "Un bouclier en bois standard." }, "canne à pêche": { "name": "canne à pêche", "quantity": 1, "price": 10, "description": "Restaure 50 points de vie." }, } marchand1.map = {"x": 45, "y": 20, "shape": "$"}#Défini où le personnage se trouve sur la carte read("Bienvenue dans cette aventure !")#Premier texte d'arrivé name = input_read("Entrez votre nom : ")#Initier les valeurs du joueur age = input_read("Entrez votre âge : ") sexe = input_read("Entrez votre sexe : ") personnage = Personnage(name, sexe, age) personnage.inventory = { "branche taillée": { "name": "branche taillée", "quantity": 5, "price": 10, "description": "Restaure 50 points de vie." }, "silex": { "name": "silex", "quantity": 1, "price": 50, "description": "Une épée en fer standard." }, "ficelle": { "name": "ficelle", "quantity": 2, "price": 20, "description": "Un bouclier en bois standard." }, "eau": { "name": "eau", "quantity": 2, "price": 20, "description": "Un bouclier en bois standard." }, "tissus sale": { "name": "tissus sale", "quantity": 2, "price": 20, "description": "Un bouclier en bois standard." } }#Défini l'inventaire, les body parts, et les stats du joueur personnage.body_parts = { "tête": { "points_de_vie": 10, "sous_partie": { "crâne" : {"points_de_vie" : 5, }, "joue gauche": {"points_de_vie" : 5, }, "joue droite": {"points_de_vie" : 5, }, "front": {"points_de_vie" : 5, }, "oeil gauche": {"points_de_vie" : 5, }, "oeil droit": {"points_de_vie" : 5, }, "nez": {"points_de_vie" : 5, }, "bouche": {"points_de_vie" : 5, }, "menton": {"points_de_vie" : 5, }, "oreille droite": {"points_de_vie" : 5, }, "oreille gauche": {"points_de_vie" : 5, } }, }, "cou": { "points_de_vie": 8, "sous_partie": {}, }, "bras gauche": { "points_de_vie": 8, "sous_partie": { "épaule gauche": {"points_de_vie": 5}, "biceps gauche": {"points_de_vie" : 5, }, "triceps gauche": {"points_de_vie" : 5, }, "avant-bras gauche": {"points_de_vie" : 5, }, "coude gauche": {"points_de_vie" : 5, }, "poignet gauche": {"points_de_vie" : 5, }, "main gauche": {"points_de_vie" : 5, } }, }, "bras droit": { "points_de_vie": 8, "sous_partie": { "épaule droite": {"points_de_vie": 5}, "biceps droit": {"points_de_vie" : 5, }, "triceps droit": {"points_de_vie" : 5, }, "avant-bras droit": {"points_de_vie" : 5, }, "coude droit": {"points_de_vie" : 5, }, "poignet droit": {"points_de_vie" : 5, }, "main droite": {"points_de_vie" : 5, } }, }, "dos": { "points_de_vie": 10, "sous_partie": { "trapèzes": {"points_de_vie" : 5, }, "rhomboïdes": {"points_de_vie" : 5, }, "lombaires": {"points_de_vie" : 5, } }, }, "abdomen": { "points_de_vie": 10, "sous_partie": { "abdominaux": {"points_de_vie" : 5, }, "pectoral gauche": {"points_de_vie" : 5, }, "pectoral droit": {"points_de_vie" : 5, }, "sternum": {"points_de_vie" : 5, } }, }, "hanche": { "points_de_vie": 7, "sous_partie": { "fessiers": {"points_de_vie" : 5, }, "hanches": {"points_de_vie" : 5, } }, }, "jambe gauche": { "points_de_vie": 8, "sous_partie": { "cuisse gauche": {"points_de_vie" : 5, }, "mollet gauche": {"points_de_vie" : 5, }, "genou gauche": {"points_de_vie" : 5, }, "cheville gauche": {"points_de_vie" : 5, }, "pied gauche": {"points_de_vie" : 5, }}, }, "jambe droite": { "points_de_vie": 8, "sous_partie": { "cuisse droite": {"points_de_vie" : 5, }, "mollet droit": {"points_de_vie" : 5, }, "genou droit": {"points_de_vie" : 5, }, "cheville droite": {"points_de_vie" : 5, }, "pied droit": {"points_de_vie" : 5, }}, }, } personnage.stats = { "Force" :{ "Niveau" : 0 }, "Agilité": { "Niveau" : 0 }, "Endurance": { "Niveau" : 0 }, "Perception": { "Niveau" : 0 }, "Intelligence": { "Niveau" : 0 }, "Charisme": { "Niveau" : 0 }, "Chance": { "Niveau" : 0 }, } personnage.map = {"x": 50, "y": 20,"shape": "@"}#Défini où le personnage se trouve sur la carte dimension = {"x": 150, "y": 30}#Crée la taille de la fenêtre de jeu, puis importe les localisation des personnage,... points = [{"x": 30, "y": 20,"shape": "&"}, marchand1.map, {"x": 90, "y": 10, "shape": "#"},personnage.map] logo = """ ___ ___ ______ __ _______ __ __ | | |.-----..--.--..----. | __ \.-----..----..-----..-----..-----..-----..---.-.| | | _ |.--| |.--.--..-----..-----.| |_ .--.--..----..-----. \ / | _ || | || _| | __/| -__|| _||__ --|| _ || || || _ || | | || _ || | || -__|| || _|| | || _|| -__| |___| |_____||_____||__| |___| |_____||__| |_____||_____||__|__||__|__||___._||__| |___|___||_____| \___/ |_____||__|__||____||_____||__| |_____| """ print(logo) read("Bienvenue dans notre aventure {} !".format(personnage.name)) read("Nous sommes le {}".format(game_date.strftime("%d/%m/%Y, %H:%M"))) #Boucle du jeu while True: #Affichage de la fenêtre de jeu for i in range(dimension["y"]): row = "" for j in range(dimension["x"]): has_b = False for point in points: if point["x"] - 1 == j and point["y"] == i: row += point["shape"] has_b = True break if not has_b: row += " " print(row) if math.sqrt((personnage.map["x"] - marchand1.map["x"])**2 + (personnage.map["y"] - marchand1.map["y"])**2) <= 1 : action = input("Appuie sur a pour parler à {}".format(marchand1.name)) if action == "a" : personnage.aller_chez_le_marchand() else : personnage.move() else : personnage.move()