我正在做一个游戏,你可以在其中创建迷宫。将块放置在16x16网格上,同时从各种块中进行选择以使其水平。无论何时创建块,它都会添加此类:
class Block(object):
def __init__(self,x,y,spr):
self.x=x
self.y=y
self.sprite=spr
self.rect=self.sprite.get_rect(x=self.x,y=self.y)
一个名为instances
的列表。在
我尝试将其搁置到.bin文件中,但它在处理曲面时返回一些错误。如何保存和加载级别?在
感谢任何帮助!:)
以下是整个代码供参考:
^{pr2}$
不能序列化/pickle/shelve pygame的
Surface
对象(至少不需要大量的努力)。所以你的问题的答案是:不要尝试序列化你的表面(这只会浪费磁盘空间)。在例如,您可以创建一个简单的
dict
来存储曲面,并让您的类只存储键,例如:现在您可以节省地尝试pickle/sheldall
Block
-实例(我看到您已经问了一个相关的问题here)。在我自己找到了一个办法。我使用python内置的
open(fname,mode)
来创建一个级别文件。在无论何时创建块,它都会获取该块的精灵名称和坐标,并以.bin格式将其添加到保存文件中:
f.write('Block('+str(placepos[0])+','+str(placepos[1])+',b1spr).')
然后我创建了一个函数来读取这个,并相应地创建了级别:
而且效果很好!在
这是全部代码,谢谢你们的帮助。在
^{pr2}$相关问题 更多 >
编程相关推荐