Skip to content Skip to sidebar Skip to footer

How To Save A Pygame Sprite-group Using The Shelve Module

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 w

Solution 1:

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()

Post a Comment for "How To Save A Pygame Sprite-group Using The Shelve Module"