如何在pygame中保存/加载游戏功能? [英] how to make save / load game functions in pygame?

查看:737
本文介绍了如何在pygame中保存/加载游戏功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为rpg制作保存/加载游戏功能.我可以保存播放器的位置,但是我想像在vba和snes9x这样的模拟器中一样,冻结整个屏幕到一个点.或者在我可以保存游戏并重新开始的位置上进行保存.谁能告诉我你怎么做这些事?任何代码都可以使用,即使是基于理论的伪代码也是如此.

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.

推荐答案

您可以使用 pickle 序列化Python数据.这与pygame无关.

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

因此,如果您的游戏状态完全存储在对象foo中,则要保存到文件"savegame"(首先是import pickle):

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)

要加载:

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

游戏状态是恢复游戏所需的所有必要信息,即游戏世界状态以及任何UI状态等.如果您的游戏状态分布在多个对象上,而没有单个对象组成它们,您只需为所有需要的对象腌制一个列表即可.

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.

这篇关于如何在pygame中保存/加载游戏功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆