how to make save / load game functions in pygame?

随声附和 提交于 2019-11-30 07:22:18

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!