1

我正在尝试按播放器名称保存和加载文件,但是当我尝试为路径调用 PlayerIG.name 时出错。我已经宣布全球 PlayerIG 更高了。

基本上我使用 PlayerIG 加载然后覆盖值,因为名称仍然保持不变。玩家不必知道,我找不到更简单的方法来做到这一点

class Player:
def __init__(self, name):
    self.name = name
    self.maxhealth = 100
    self.health = self.maxhealth
    self.base_attack = 10
    self.gold = 10
    self.pots = 1
    self.weap = ["Rusty Sword"]
    self.curweap = self.weap

@property
def attack(self):
    attack = self.base_attack
    if self.curweap == "Rusty Sword":
        attack += 5
    if self.curweap == "Great Sword":
        attack += 12
    return attack

def attack_damage(self, attack):
    damage = random.randint(attack / 2, attack)
    return damage

def start():
    os.system('clear')
    print ("Hello, what is your name?")
    options = input("--> ")
    global PlayerIG
    PlayerIG = Player(options)
    name = "Your name is: " + PlayerIG.name
    send_status(name)                         
    main()

def main():
os.system('clear')
menutext = "Welcome to text RPG!\n 1.) Start\n 2.) Load\n 3.) Exit\n"
print (menutext)
options = input("-> ")
if options == "1":
    Menu()
elif options == "2":
    if os.path.exists(PlayerIG.name == True:
        os.system('clear')
        with open(PlayerIG.name, 'rb') as f:
            PlayerIG = pickle.load(f)
        loaded = "You loaded your save"
        print (loaded)
        send_status(loaded)
        time.sleep(2)
        Menu()
    else:
        noload = "You have no save file for this game."
        print (noload)
        time.sleep(2)
        main()

elif options == "3":
    sys.exit()
else:
    main()
4

0 回答 0