在外部文件中存储 unpicklabe pygame.Surface 对象

发布于 2024-10-19 11:49:26 字数 329 浏览 5 评论 0原文

所以我遇到了一个问题 - 我正在使用 Pygame 用 Python 编写游戏原型,并且我想保存我的游戏。所有与游戏相关的数据都位于某些类的三个实例中,我想将这三个实例保存到一个文件中。但是,我尝试过对这些实例进行酸洗,但它不起作用。相反,我得到“TypeError:无法腌制 Surface 对象”。这是一个问题,因为我想存储Surface对象。

我愿意接受任何可能存在的酸洗替代方案,使用任何其他类型的数据类型。重要的是这些实例被存储,并且稍后可以检索它们的数据。那么我能做些什么来克服这个问题呢?请记住,我不是一个非常有经验的程序员,一年前在业余时间学习了Python,尽管我正在慢慢学习C++,但我不会编写太多其他语言。

So I've got a problem - I'm writing a game prototype in Python, using Pygame, and I want to save my games. All of the game-related data is in three instances of certain classes, and I want to save these three instances to a file. However, I've tried pickling these instances, and it doesn't work. Instead, I get "TypeError: can't pickle Surface objects". This is a problem, because I want to store Surface objects.

I'm open to any alternatives to pickling that there may be, using any other kind of data type. The important thing is that these instances get stored, and that their data is then retrievable later on. So what can I do to overcome this issue? Please keep in mind, I'm not a very experienced programmer, having learned Python in my spare time a year ago, and I can't write much of any other language, though I'm slowly learning C++.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萤火眠眠 2024-10-26 11:49:26

酸洗的基本点是您应该能够以某种方式序列化对象。 SDL 表面是一个保存大量运行时状态的内存对象。尝试将其连载并不完全明智。

您应该做的是将游戏状态与渲染组件分离,以便您可以序列化这些组件(酸洗或其他)。

这就像尝试通过某种方式保存保存解码视频的内存缓冲区来保存视频的状态。这是行不通的。相反,保存方式是序列化视频文件的位置和时间偏移量。然后,您可以在下次恢复应用程序时在加载时继续播放。

The basic point of pickling is that you should be able to serialise the object somehow. An SDL surface is an in memory object holding a lot of run time state. Trying to serialise it is not totally sensible.

What you should do is to decouple the state of your game from the rendering components so that you can serialise just those (pickling or whatever).

It's like trying to save the state of a video by somehow saving the memory buffers holding the decoded video. This will not work. Instead, how you save it is to serialise the location of the video file and the time offset. Then you can continue playback upon load the next time you restore your application.

最初的梦 2024-10-26 11:49:26

阅读 http://docs.python.org/library/pickle.html#pickle-协议,您需要让 Surface 对象导出 reduce 方法,或者使用 copy_reg 模块告诉 pickle 如何处理该数据,如 http://docs.python.org/library/copy_reg.html#module-copy_reg

不管怎样,pickle 需要一个函数,将它无法处理的 blob 转换为 (some_class, [arguments here])。然后当你 unpickle 时,它​​将用这些参数构造该类的一个新事物。

Reading http://docs.python.org/library/pickle.html#pickle-protocol, you need to either have Surface objects export a reduce method or use the copy_reg module to tell pickle how to handle that data as documented in http://docs.python.org/library/copy_reg.html#module-copy_reg.

Either way what pickle needs is a function that will turn a blob it can't handle into (some_class, [arguments here]). And then when you unpickle it will construct a new thing of that class with those arguments.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文