我目前正在制作一个基于文本的游戏,但是当我尝试挑选我的玩家类时,它保存了除了字典和列表之外的所有东西。以下是我保存游戏的代码:
# Saving
def save():
os.system('cls')
print("As", player.Name, "opens the book, series of names and stats are written throughout the pages...")
ask = input("Write your name and stats in the book?\n[" + Fore.GREEN + "Y" + Fore.RESET + "]Yes "
"\n[" + Fore.GREEN + "N" + Fore.RESET + "]No\n> ").upper()
if ask == "Y" or ask == "YES":
print("(Which Save File will you save into?)")
print("[" + Fore.GREEN + "1" + Fore.RESET + "]Save_File1")
print("[" + Fore.GREEN + "2" + Fore.RESET + "]Save_File2")
print("[" + Fore.GREEN + "3" + Fore.RESET + "]Save_File3")
print("[" + Fore.GREEN + "4" + Fore.RESET + "]Save_File4")
save_choice = input('> ')
print()
if save_choice == '1':
with open("Save_File1.pkl", "wb") as save_file:
pickle.dump(player, save_file) # Creates the file and puts the data into the file
elif save_choice == '2':
pickle.dump(player, open('Save_File2.pkl', 'wb')) # Creates the file and puts the data into the file
elif save_choice == '3':
pickle.dump(player, open('Save_File3.pkl', 'wb')) # Creates the file and puts the data into the file
elif save_choice == '4':
pickle.dump(player, open('Save_File4.pkl', 'wb'))
else:
print('Invalid Command')
input('> ')
save()
print("Your progress has been saved!")
input("> ")这是我加载游戏的代码:
# Loading
def load():
while True:
os.system('cls')
print("( Load a save file )")
print("[" + Fore.GREEN + "1" + Fore.RESET + "]Save_File1")
print("[" + Fore.GREEN + "2" + Fore.RESET + "]Save_File2")
print("[" + Fore.GREEN + "3" + Fore.RESET + "]Save_File3")
print("[" + Fore.GREEN + "4" + Fore.RESET + "]Save_File4")
load_choice = input('> ')
global player
if load_choice == '1':
with open("Save_File1.pkl", "rb") as save_file:
player = pickle.load(save_file)
elif load_choice == '2':
player = pickle.load(open('Save_File2.pkl', 'rb')) # Loads the file
elif load_choice == '3':
player = pickle.load(open('Save_File3.pkl', 'rb')) # Loads the file
elif load_choice == '4':
player = pickle.load(open('Save_File4.pkl', 'rb')) # Loads the File以下是球员的统计数据:
# Player Stats
class Player(object):
Name = ""
Class = ""
Race = ""
HP = 0
MaxHP = 0
SP = 0
MaxSP = 0
BP = 0
MaxBP = 100
Power = 0
Vitality = 0
Dexterity = 0
Intelligence = 0
Perception = 0
Persona = 0
XP = 0
MXP = 50
Gold = 50
Lv = 1
Offence = 0
Defence = 0
Base_Offence = 0
Base_Defence = 0
Offence_Mod = 0
Defence_Mod = 0
Bounty = 0
Reputation = {"Commoner": 0, "Noble": 0, "Peasant": 0, "Adventurer": 0}
events = []
Lang = ["Common"] # This one doesn't Save
Skills = {} # This one doesn't Save
Status_Effects = []
Key_Items = []
Health_Potion = 0
MaxHealth_Potion = 5
Ether_Potion = 0
MaxEther_Potion = 5
Antidote_Potion = 0
MaxAntidote_Potion = 5
location = ""
weapon = ""
armr = ""
player = Player()我在谷歌上找不到任何帮助,所以我希望我能在这里得到帮助。任何帮助都将不胜感激!
编辑:搞清楚了!我使用的是类变量而不是实例变量!我把玩家等级改为:
# Player Stats
class Player():
def __init__(self):
self.Name = ""
self.Class = ""
self.Race = ""
self.HP = 0
self.MaxHP = 0
self.SP = 0
self.MaxSP = 0
self.BP = 0
self.MaxBP = 100
self.Power = 0
self.Vitality = 0
self.Dexterity = 0
self.Intelligence = 0
self.Perception = 0
self.Persona = 0
self.XP = 0
self.MXP = 50
self.Gold = 50
self.Lv = 1
self.Offence = 0
self.Defence = 0
self.Base_Offence = 0
self.Base_Defence = 0
self.Offence_Mod = 0
self.Defence_Mod = 0
self.Bounty = 0
self.Reputation = {"Commoner": 0, "Noble": 0, "Peasant": 0, "Adventurer": 0}
self.events = []
self.Lang = ["Common"]
self.Skills = {}
self.Status_Effects = []
self.Key_Items = []
self.Health_Potion = 0
self.MaxHealth_Potion = 5
self.Ether_Potion = 0
self.MaxEther_Potion = 5
self.Antidote_Potion = 0
self.MaxAntidote_Potion = 5
self.location = ""
self.weapon = ""
self.armr = ""
player = Player()我刚开始编程,所以这对我有很大帮助.
发布于 2021-01-28 08:51:49
您创建了类变量,而不是实例变量。您要保存的数据没有一个是实例的一部分,因此不会保存。(确实保存的部分也不会被保存,只是您后来在没有意识到的情况下,在没有发布的代码中,不经意地用新的实例变量隐藏了类变量。)
使用实例变量。
https://stackoverflow.com/questions/65933735
复制相似问题