在基于Python文本的RPG中保存/加载系统

时间:2015-08-14 19:36:27

标签: python rpg

我试图在我的Python RPG中获得一个保存/加载系统,但只能部分地弄清楚如何完成这项工作。我已经看到了关于这个主题的其他问题,但它们都不适合我正在做的事情。

class Player:
    def __init__(self, name):
        self.name = name
        self.base_defe = 0
        self.weap = "Rusty Sword"
        self.wbow = "None"
        self.armH = "Worn Helmet"
        self.armB = "Worn Shirt"
        self.armA = "Worn Gloves"
        self.armL = "Worn Pants"
        self.armF = "Worn Boots"
        self.gold = 1000
        self.orichalcum = 0 
        self.base_stre = 1
        self.base_magi = 1
        self.base_rang = 1
        self.maxmana = 100
        self.mana = self.maxmana
        self.level = 1
        self.exp = 0
        self.maxexp = 100
        self.expdiff = 50
        self.pots = 0
        self.arrows = 0
        self.q1c = False
        self.q2c = False
        self.q3c = False
        self.q1 = False
        self.q2 = False
        self.q3 = False
        self.QuestActive = False
        self.qtitle = "None"
        self.maxhealth = 100 + self.defe
        self.health = self.maxhealth
        self.inventory = ["Rusty Sword", 
                          "Worn Shirt", 
                          "Worn Pants", 
                          "Worn Boots",
                          "Worn Helmet",
                          "Worn Gloves"]
        self.spells = []

我还有一个名为PlayerIG的变量代表播放器,但它是在一个函数中设置的,所以当我尝试加载游戏时,我不断收到像PlayerIG这样的消息。我试图使用pickle创建一个保存/加载系统,并将所有内容放入字典中。保存它工作正常,但加载它肯定不会,因为每当我尝试加载它时,变量都会被重置。我无法弄清楚如何使其正常工作,但任何帮助将不胜感激。

给我最多问题的部分是:

def name():
    os.system('clear')
    PlayerIG = Player(raw_input("What is your name?"))
    print ("Prints story")

原因是PlayerIG意味着代表播放器,但每当我尝试使用以下内容加载保存文件时

def load():
    os.system('clear')
    with open('savefile', 'rb') as f:
        data = pickle.load(f)
    print "Loaded last save state..."
    option = raw_input(" ")
    #data is equal to PlayerIG

它给我带来了诸如PlayerIG不存在的错误,或者每当我将PlayerIG设置在函数之外时它将PlayerIG设置为默认统计数据并且它将所有玩家统计数据的游戏设置加载到默认值而不是加载他们保存的统计数据

1 个答案:

答案 0 :(得分:1)

我只需对您的班级定义进行一次修正:self.maxhealth = 100 + self.defe应为self.maxhealth = 100 + self.base_defe(我认为这就是您的意思)。

这是一个运行的测试,似乎在OS X上的python 3.4.2中运行良好:

import pickle

class Player:
    def __init__(self, name):
        self.name = name
        self.base_defe = 0
        self.weap = "Rusty Sword"
        self.wbow = "None"
        self.armH = "Worn Helmet"
        self.armB = "Worn Shirt"
        self.armA = "Worn Gloves"
        self.armL = "Worn Pants"
        self.armF = "Worn Boots"
        self.gold = 1000
        self.orichalcum = 0 
        self.base_stre = 1
        self.base_magi = 1
        self.base_rang = 1
        self.maxmana = 100
        self.mana = self.maxmana
        self.level = 1
        self.exp = 0
        self.maxexp = 100
        self.expdiff = 50
        self.pots = 0
        self.arrows = 0
        self.q1c = False
        self.q2c = False
        self.q3c = False
        self.q1 = False
        self.q2 = False
        self.q3 = False
        self.QuestActive = False
        self.qtitle = "None"
        self.maxhealth = 100 + self.base_defe
        self.health = self.maxhealth
        self.inventory = ["Rusty Sword", 
                          "Worn Shirt", 
                          "Worn Pants", 
                          "Worn Boots",
                          "Worn Helmet",
                          "Worn Gloves"]
        self.spells = []

p = Player('Fred')
p.exp = 100
p.gold = 5000

with open('fred.player', 'wb') as f:
    pickle.dump(p, f)

with open('fred.player', 'rb') as f:
    p2 = pickle.load(f)

print('Loaded player "%s" with %d exp and %d gold.' % (p2.name, p2.exp, p2.gold))