[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
Re: [pygame] Implementing save feature
Hi Tommy,
I have though a lot about state saving, so I thought I should
contribute to this discussion.
Here are my requirements for a good Python state saving system :
* it must be text, preferably human-readable
* it must need the smallest efforts possible to save and reload data
* if it can be matched to Python classes, why not !
Therefore, my current solution uses twisted.spread.jelly which offers
exactly that :
http://twistedmatrix.com/documents/8.1.0/api/twisted.spread.jelly.html
I just pretty-fy it a bit before saving it to a file, so that it is
even easier to edit for the users/developers.
Here is my serialize module, from the rats package, plus a simple test
as an example :
http://code.google.com/p/toonloop/source/browse/trunk/py/rats/serialize.py
http://code.google.com/p/toonloop/source/browse/trunk/py/test/test_serialize.py
Of course, I am a big twisted enthusiast. It will add a dependency to
your software, but I can say twisted totally rocks for anything
related to network protocols. I guess your game might need some kind
of network communication some day, no ? You can look in the rats
package for how to make a pygame game with twisted. More specifically
:
http://code.google.com/p/toonloop/source/browse/trunk/py/rats/render.py
I plan on releasing the rats package separate from the toonloop
software soon. I will announce it here when it will be done.
a
2009/5/23 Olaf Nowacki <ioa@xxxxxxx>:
> Hi Tommy,
>
> here is how I did it. (I'm making an point-and-click-adventure myself with
> py-thon & -game, too.)
>
> In my game there are just scenes and actors. Allmost all of the data is in
> the actors, even the cutscenes. So I (more or less) only save the actors
> with PICKLE.
>
> I'd like to rewrite it someday and try YAML, because a human readable
> savegame is much help during development.
>
> **Olaf
>
>> from inventory import inventory
>> import pickle
>>
>>
>>
>> class Game:
>>
>> def __init__(self):
>> self.actors = {}
>> self.scene_transition = False
>> self.hidden_inventory = False
>>
>> def setup(self, stage, cast):
>> self.inventory = inventory
>> self.stage = stage
>> self.cast = cast
>> self.player = self.cast[Player_ID]
>> self.load_dialogs(Language_ID)
>> set_scene(Start_Scene_ID)
>>
>>
>>
>> def save(self):
>> filter = [
>> '_Sprite__g',
>> 'rect',
>> 'act_pos',
>> 'behaviors',
>> 'image',
>> 'image_path',
>> 'images',
>> 'path',
>> 'sequences',
>> 'animation',
>> 'uid']
>> savedata = {}
>> savedata['current_scene'] = self.scene.uid
>> savedata['hidden_inventory'] = self.hidden_inventory
>> savedata['inventory'] = []
>> for item in self.inventory.items:
>> savedata['inventory'].append(item.uid)
>> savedata['actors'] = {}
>> for actor in (self.actors.values()):
>> savedata['actors'][actor.uid] = {}
>> for var in vars(actor):
>> if not var in filter:
>> savedata['actors'][actor.uid][var] =
>> actor.__dict__[var]
>> savedata['scene_walkmaps'] = {}
>> for scene in self.stage.values():
>> savedata['scene_walkmaps'][scene.uid] = scene.walkmap.name
>> savegame = open('savegame.txt', 'w')
>> translator = pickle.Pickler(savegame)
>> translator.dump(savedata)
>> savegame.close()
>>
>> def load(self):
>> savegame = open('savegame.txt', 'r')
>> translator = pickle.Unpickler(savegame)
>> savedata = translator.load()
>> savegame.close()
>> for actor in savedata['actors']:
>> if actor in self.actors:
>> for var in savedata['actors'][actor]:
>> if not var in filter:
>> self.actors[actor].__dict__[var] =
>> savedata['actors'][actor][var]
>>
>> self.actors[actor].set_image(savedata['actors'][actor]['image_id'])
>> self.inventory.clear()
>> for item in savedata['inventory']:
>> self.inventory.add(get_actor(item))
>> for scene_id in savedata['scene_walkmaps']:
>> set_walkmap(scene_id, savedata['scene_walkmaps'][scene_id])
>> self.hidden_inventory = savedata['hidden_inventory']
>> set_scene(savedata['current_scene'])
>
>
> On Fri, May 22, 2009 at 11:23 PM, Nevon <mgodnevon@xxxxxxxxx> wrote:
>>
>> I'm currently part of a group developing a FOSS point-and-click
>> adventure game using Python and Pygame. The game engine is actually a
>> fork of the engine used in the game Colonel Wiljafjord and the
>> Tarbukas Tyranny, and entry in Pyweek #3. Right now we're working on
>> improving the engine in whatever way we can. If you're interested, the
>> source is available here:
>> http://github.com/kallepersson/subterranean/tree/master.
>>
>> So now I'd like to implement a save/load feature, but I don't really
>> know where to start. I have no idea how game save systems are usually
>> constructed, so I'm kind of lost. If anyone has any ideas, links to
>> resources, or pointers on how to best do this, please share.
>>
>> Thank you!
>>
>> Tommy Brunn
>> http://www.blastfromthepast.se/blabbermouth
>
--
Alexandre Quessy
http://alexandre.quessy.net/