如何使用搁置模块保存 pygame 精灵组 [英] How to save a pygame sprite-group using the shelve module
问题描述
我正在制作一款想要保存游戏功能的游戏.我有一个精灵组,我想使用架子模块保存它,但是在尝试保存我的精灵组时出现无法腌制表面对象"错误.我想知道有没有什么方法可以使用shelf模块来保存精灵组,如果没有,我应该使用其他什么保存方法?
I'm making a game in which I want to have a save game feature. I have a sprite group which I want to save using the shelf module, but I get a 'can't pickle Surface objects' error when trying to save my sprite group. I was wondering if there is any way to use the shelf module to save a sprite group, and if there isnt, what other saving method should I use?
推荐答案
当用户想要保存游戏时,我会将精灵的相关值存储在 json 文件中.要加载游戏,请清空精灵组,使用加载的数据重建精灵并将它们添加到组中.例如.(点击精灵拖动它们并按s"保存和w"加载):
I'd store the relevant values of the sprites in a json file when the user wants to save the game. To load the game, empty the sprite group, reconstruct the sprites with the loaded data and add them to the group. E.g. (click on the sprites to drag them around and press "s" to save and "w" to load):
import json
import pygame as pg
class Actor(pg.sprite.Sprite):
def __init__(self, pos, color):
super().__init__()
self.color = color
self.image = pg.Surface((32, 52))
self.image.fill(self.color)
self.rect = self.image.get_rect(topleft=pos)
class Game:
def __init__(self):
self.fps = 30
self.done = False
self.bg_color = pg.Color('gray13')
self.clock = pg.time.Clock()
self.screen = pg.display.set_mode((640, 480))
actor1 = Actor((250, 120), pg.Color('steelblue1'))
actor2 = Actor((400, 260), pg.Color('sienna1'))
self.all_sprites = pg.sprite.Group(actor1, actor2)
self.selected = None
def run(self):
while not self.done:
self.handle_events()
self.run_logic()
self.draw()
self.clock.tick(self.fps)
def handle_events(self):
for event in pg.event.get():
if event.type == pg.QUIT:
self.done = True
elif event.type == pg.MOUSEBUTTONDOWN:
if self.selected:
self.selected = None
else:
for sprite in self.all_sprites:
if sprite.rect.collidepoint(event.pos):
self.selected = sprite
elif event.type == pg.MOUSEMOTION:
if self.selected:
self.selected.rect.x += event.rel[0]
self.selected.rect.y += event.rel[1]
elif event.type == pg.KEYDOWN:
if event.key == pg.K_s:
self.save()
elif event.key == pg.K_w:
self.load()
def run_logic(self):
self.all_sprites.update()
def draw(self):
self.screen.fill(self.bg_color)
self.all_sprites.draw(self.screen)
pg.display.flip()
def save(self):
with open('save_game.json', 'w') as file:
print('Saving')
data = [(sprite.rect.topleft, sprite.color[:4])
for sprite in self.all_sprites]
json.dump(data, file)
def load(self):
with open('save_game.json', 'r') as file:
print('Loading')
data = json.load(file)
self.selected = None
self.all_sprites.empty()
for pos, color in data:
self.all_sprites.add(Actor(pos, color))
if __name__ == '__main__':
pg.init()
Game().run()
pg.quit()
这篇关于如何使用搁置模块保存 pygame 精灵组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!