init
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
from base_mob import BaseMob
|
||||
import json
|
||||
import os
|
||||
|
||||
|
||||
class CharacterBuilder:
|
||||
@staticmethod
|
||||
def build_character():
|
||||
"""Demande à l'utilisateur de saisir les attributs du personnage."""
|
||||
print("Création d'un nouveau personnage :")
|
||||
name = input("Nom du personnage : ")
|
||||
max_pv = int(input("Points de vie maximum : "))
|
||||
strength = int(input("Force : "))
|
||||
protection = int(input("Protection : "))
|
||||
speed = int(input("Vitesse : "))
|
||||
mob_type = 0
|
||||
return BaseMob(name, max_pv, strength, protection, speed, mob_type)
|
||||
|
||||
@staticmethod
|
||||
def save_character(character):
|
||||
"""Enregistre un personnage dans un fichier JSON."""
|
||||
character_data = {
|
||||
"name": character.name,
|
||||
"max_pv": character.max_pv,
|
||||
"current_pv": character.current_pv,
|
||||
"strength": character.strength,
|
||||
"protection": character.protection,
|
||||
"speed": character.speed,
|
||||
"mob_type": character.mob_type
|
||||
}
|
||||
filename = character.name + ".json"
|
||||
with open(filename, "w",encoding='UTF-8') as file:
|
||||
json.dump(character_data, file, indent=4)
|
||||
print(f"Personnage enregistré dans {filename}.")
|
||||
|
||||
@staticmethod
|
||||
def load_character(name):
|
||||
"""Charge un personnage à partir d'un fichier JSON."""
|
||||
filename = name + ".json"
|
||||
if not os.path.exists(filename):
|
||||
print(f"Erreur : Le fichier {filename} n'existe pas.")
|
||||
return None
|
||||
|
||||
try:
|
||||
with open(filename, "r",encoding='UTF-8') as file:
|
||||
character_data = json.load(file)
|
||||
|
||||
# Vérification des clés essentielles dans le JSON pour éviter les erreurs
|
||||
required_keys = ["name", "max_pv", "current_pv",
|
||||
"strength", "protection", "speed", "mob_type"]
|
||||
if not all(key in character_data for key in required_keys):
|
||||
print(f"Erreur : Le fichier {filename} ne contient pas toutes les clés nécessaires.")
|
||||
return None
|
||||
|
||||
# Construction du personnage
|
||||
return BaseMob(
|
||||
name=character_data["name"],
|
||||
max_pv=character_data["max_pv"],
|
||||
strength=character_data["strength"],
|
||||
protection=character_data["protection"],
|
||||
speed=character_data["speed"],
|
||||
mob_type=character_data["mob_type"]
|
||||
)
|
||||
except json.JSONDecodeError:
|
||||
print(f"Erreur : Le fichier {filename} n'est pas un JSON valide.")
|
||||
return None
|
||||
Reference in New Issue
Block a user