Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to make save / load game functions in pygame?

I need to make save / load game functions for my rpg. I can save the location of my player, but what I want is to freeze the entire screen at one point like it is done in emulators like vba and snes9x. Or maybe to make save locations where I can save the game and start again from. Can anyone tell me how you do these things? any code is welcomed even theorybased pseudocode.

like image 340
ApprenticeHacker Avatar asked Jun 21 '11 04:06

ApprenticeHacker


1 Answers

You can use pickle to serialize Python data. This has nothing to do with pygame.

So if your game state is completely stored in the object foo, to save to file "savegame" (import pickle first):

with open("savegame", "wb") as f:
    pickle.dump(foo, f)

To load:

with open("savegame", "rb") as f:
    foo = pickle.load(f)

The game state is all the necessary information you need to restore the game, that is, the game world state as well as any UI state, etc. If your game state is spread across multiple objects with no single object that composes them, you can simply pickle a list with all the needed objects.

like image 124
Antti Avatar answered Sep 27 '22 21:09

Antti