python - How to save a pygame sprite-group using the shelve module -


i'm making game in want have save game feature. have sprite group want save using shelf module, 'can't pickle surface objects' error when trying save sprite group. wondering if there way use shelf module save sprite group, , if there isnt, other saving method should use?

i'd store relevant values of sprites in json file when user wants save game. load game, empty sprite group, reconstruct sprites loaded data , add them group. e.g. (click on sprites drag them around , press "s" save , "w" load):

import sys import json  import pygame 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):         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:                     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):         open('save_game.json', 'w') file:             print('saving')             data = [(sprite.rect.topleft, sprite.color[:4])                     sprite in self.all_sprites]             json.dump(data, file)      def load(self):         open('save_game.json', 'r') file:             print('loading')             data = json.load(file)             self.selected = none             self.all_sprites.empty()             pos, color in data:                 self.all_sprites.add(actor(pos, color))   if __name__ == '__main__':     pg.init()     game().run()     pg.quit()     sys.exit() 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -