How to make save / load functions in pygame?

I need to make upload / download functions for my rpg. I can save the location of my player, but I want to freeze the entire screen at some point, as is done in emulators such as vba and snes9x. Or maybe save the save locations where I can save the game and start over again. Can someone tell me how you do this? any code is welcome even on the basis of pseudo-code theory.

+5
source share
1 answer

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

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

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

For loading:

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

The state of the game is all the necessary information necessary to restore the game, that is, the state of the game world, as well as any state of the user interface, etc. If your game state spreads over several objects without a single object that makes them up, you can simply sort the list with all the necessary objects.

+11
source

All Articles