【发布时间】:2018-09-14 16:47:02
【问题描述】:
如果我创建了自己的类,该类具有一些属性为 pygame.Surfaces,并且我想将此对象保存到文件中,我该如何执行此操作,因为尝试时会发生错误。
我创建的类是一个对象,它本质上是以下内容(包含 Item 类是因为玩家的项目具有 pygame.Surfaces 属性):
class Item:
def __init__(self,name,icon):
self.name = name
self.icon = pygame.image.load(icon)
class Player(pygame.Sprite):
def __init__(self,{some_attrs})
skins = [{a pygame.Surface from a load image},{another}]
self.money = 0
self.items = [{Item object}]
然后当我尝试保存时,我使用以下内容:
with open('save.dat','wb') as file:
pickle.dump(player , file , protocol = 4)
#I have also tried without the protocol argument
但我收到以下错误:
Traceback (most recent call last):
File "{file path}", line 883, in <module>
save_progress()
File "{file path}", line 107, in save_progress
pickle.dump(player,file,protocol=4)
TypeError: can't pickle pygame.Surface objects
仅供参考,我放在花括号({ 和 })中的所有内容只是我已缩写或省略的内容,因为不需要它
如果您需要更多详细信息,如果您回复您需要的内容,我可以轻松添加它
【问题讨论】:
-
是的,你
can't pickle pygame.Surface objects,就是这样。
标签: python python-3.x pygame pickle pygame-surface