腌制变量和类,用于文本冒险中的保存功能 [英] Pickle variables and classes for save function in text adventure

查看:75
本文介绍了腌制变量和类,用于文本冒险中的保存功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在文本冒险中创建保存功能. 该功能必须为播放器保存一些变量和参数.

I am trying to make a save function in a text adventure. The function must save some variables and parameters for the player.

我已经尝试过使用Google并在论坛上提问,但我无法使其正常工作.

I have tried to Google it and asking on forums, but I can't make it work.

整个代码在这里: http://pastebin.com/8Y3PNnRx

保存功能应将player = player('Niels', 'Knight, 0, 0, 0, 0, 0, 0, 1)(第417行)以及变量player.goldplayer.potion(第420行)保存在位置'C:\\Users\\XXXXX\\Desktop\\SmallDungeons\\save.dat'

The saving function should save the player = player('Niels', 'Knight, 0, 0, 0, 0, 0, 0, 1) (line 417) and variables like player.gold and player.potion (line 420) at location 'C:\\Users\\XXXXX\\Desktop\\SmallDungeons\\save.dat'

谢谢您的时间!

推荐答案

举一个最小的例子,我们在foo.py中构建一个类(这与您的类player相似).

For a minimal example, we build a class in foo.py (this is similar to your class player).

# foo.py
class Foo(object):
  def __init__(self, x):
    self.x = x
  def bar(self):
    return self.x**2

现在,我们导入它,并添加一些属性(这与添加player.gold相似),然后对类实例进行腌制.完成后,我们将终止python解释器会话.

Now we import it, and add some attributes (this is similar to you adding player.gold), and then pickle the class instance. When we're done, we terminate the python interpreter session.

Python 2.7.9 (default, Dec 11 2014, 01:21:43) 
[GCC 4.2.1 Compatible Apple Clang 4.1 ((tags/Apple/clang-421.11.66))] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from foo import Foo
>>> f = Foo(3)
>>> f.y = 10
>>> 
>>> import pickle
>>> with open('foobar.pkl', 'w') as pik:
...   pickle.dump(f, pik)
... 
>>> 

然后,我们重新启动会话,并加载泡菜.

Then, we restart the session, and load the pickle.

Python 2.7.9 (default, Dec 11 2014, 01:21:43) 
[GCC 4.2.1 Compatible Apple Clang 4.1 ((tags/Apple/clang-421.11.66))] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pickle
>>> with open('foobar.pkl', 'r') as pik:
...   f = pickle.load(pik)
... 
>>> f.x
3
>>> f.bar()
9
>>> f.y
10
>>> 

您将看到,仅通过保存类实例,就可以保留添加的属性(如f.y).这是因为pickle保留对文件的引用(从中导入文件)以及所有添加的属性.您可以在泡菜串本身中看到它.

You'll see that just by saving the class instance, even the added attributes (like f.y) are preserved. This is because pickle preserves a reference to your file (where to import it from) as well as any added attributes. You can see this in the pickle string itself.

Python 2.7.9 (default, Dec 11 2014, 01:21:43) 
[GCC 4.2.1 Compatible Apple Clang 4.1 ((tags/Apple/clang-421.11.66))] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from foo import Foo
>>> f = Foo(3)
>>> 
>>> import pickle
>>> pickle.dumps(f)
"ccopy_reg\n_reconstructor\np0\n(cfoo\nFoo\np1\nc__builtin__\nobject\np2\nNtp3\nRp4\n(dp5\nS'x'\np6\nI3\nsb."
>>> 
>>> f.y = 10
>>> pickle.dumps(f)
"ccopy_reg\n_reconstructor\np0\n(cfoo\nFoo\np1\nc__builtin__\nobject\np2\nNtp3\nRp4\n(dp5\nS'y'\np6\nI10\nsS'x'\np7\nI3\nsb."

我的建议是使用pickle协议-1,因为它会使泡菜变小.您将此标志添加到使用dumpdumps的任何位置.

My suggestion is to use pickle protocol -1, as it makes the pickles smaller. You add this flag to any of the places you use dump or dumps.

>>> pickle.dumps(f, protocol=-1)
'\x80\x02cfoo\nFoo\nq\x00)\x81q\x01}q\x02U\x01xq\x03K\x03sb.'

这篇关于腌制变量和类,用于文本冒险中的保存功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆