在外部文件中存储取消拾取的pygame.Surface对象 [英] Storing unpicklabe pygame.Surface objects in external files

查看:165
本文介绍了在外部文件中存储取消拾取的pygame.Surface对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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++.

推荐答案

酸洗的基本要点是您应该能够以某种方式序列化对象. 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.

这篇关于在外部文件中存储取消拾取的pygame.Surface对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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