1. <tfoot id='fbwNj'></tfoot>

      <small id='fbwNj'></small><noframes id='fbwNj'>

    1. <i id='fbwNj'><tr id='fbwNj'><dt id='fbwNj'><q id='fbwNj'><span id='fbwNj'><b id='fbwNj'><form id='fbwNj'><ins id='fbwNj'></ins><ul id='fbwNj'></ul><sub id='fbwNj'></sub></form><legend id='fbwNj'></legend><bdo id='fbwNj'><pre id='fbwNj'><center id='fbwNj'></center></pre></bdo></b><th id='fbwNj'></th></span></q></dt></tr></i><div id='fbwNj'><tfoot id='fbwNj'></tfoot><dl id='fbwNj'><fieldset id='fbwNj'></fieldset></dl></div>
        <bdo id='fbwNj'></bdo><ul id='fbwNj'></ul>
    2. <legend id='fbwNj'><style id='fbwNj'><dir id='fbwNj'><q id='fbwNj'></q></dir></style></legend>

    3. Python文字游戏:如何制作保存功能?

      Python text game: how to make a save feature?(Python文字游戏:如何制作保存功能?)

    4. <i id='WfbFt'><tr id='WfbFt'><dt id='WfbFt'><q id='WfbFt'><span id='WfbFt'><b id='WfbFt'><form id='WfbFt'><ins id='WfbFt'></ins><ul id='WfbFt'></ul><sub id='WfbFt'></sub></form><legend id='WfbFt'></legend><bdo id='WfbFt'><pre id='WfbFt'><center id='WfbFt'></center></pre></bdo></b><th id='WfbFt'></th></span></q></dt></tr></i><div id='WfbFt'><tfoot id='WfbFt'></tfoot><dl id='WfbFt'><fieldset id='WfbFt'></fieldset></dl></div>
        <legend id='WfbFt'><style id='WfbFt'><dir id='WfbFt'><q id='WfbFt'></q></dir></style></legend>

        <small id='WfbFt'></small><noframes id='WfbFt'>

            <tbody id='WfbFt'></tbody>

          • <bdo id='WfbFt'></bdo><ul id='WfbFt'></ul>
            • <tfoot id='WfbFt'></tfoot>

                本文介绍了Python文字游戏:如何制作保存功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在使用 Python 制作基于文本的游戏,我已经大致了解了这一点.但我要让游戏深入到这样的程度,完成它需要的时间比坐下来要长.所以我希望能够让游戏在退出时将变量列表(玩家健康、金币、房间位置等)保存到文件中.然后如果玩家想要加载文件,他们就去加载菜单,它会加载文件.

                I am in the process of making a text based game with Python, and I have the general idea down. But I am going to make the game in depth to the point where, it will take longer than one sitting to finish it. So I want to be able to make the game to where, on exit, it will save a list of variables (player health, gold, room place, etc) to a file. Then if the player wants to load the file, they go to the load menu, and it will load the file.

                我目前使用的是 2.7.5 版的 Python,并且在 Windows 上.

                I am currently using version 2.7.5 of Python, and am on Windows.

                推荐答案

                如果我正确理解了这个问题,那么您是在询问一种序列化对象的方法.最简单的方法是使用标准模块 pickle:

                If I understand the question correctly, you are asking about a way to serialize objects. The easiest way is to use the standard module pickle:

                import pickle
                
                player = Player(...)
                level_state = Level(...)
                
                # saving
                with open('savefile.dat', 'wb') as f:
                    pickle.dump([player, level_state], f, protocol=2)
                
                # loading
                with open('savefile.dat', 'rb') as f:
                    player, level_state = pickle.load(f)
                

                可以通过这种方式存储标准 Python 对象和具有任何嵌套级别的简单类.如果您的类有一些重要的构造函数,则可能需要使用相应的 pickle 实际需要保存的内容.html#the-pickle-protocol" rel="noreferrer">协议.

                Standard Python objects and simple classes with any level of nesting can be stored this way. If your classes have some nontrivial constructors it may be necessary to hint pickle at what actually needs saving by using the corresponding protocol.

                这篇关于Python文字游戏:如何制作保存功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Running .jl file from R or Python(从 R 或 Python 运行 .jl 文件)
                Running Julia .jl file in python(在 python 中运行 Julia .jl 文件)
                Using PIP in a Azure WebApp(在 Azure WebApp 中使用 PIP)
                How to run python3.7 based flask web api on azure(如何在 azure 上运行基于 python3.7 的烧瓶 web api)
                Azure Python Web App Internal Server Error(Azure Python Web 应用程序内部服务器错误)
                Run python dlib library on azure app service(在 azure app 服务上运行 python dlib 库)

                    <tbody id='ZvcDI'></tbody>
                • <legend id='ZvcDI'><style id='ZvcDI'><dir id='ZvcDI'><q id='ZvcDI'></q></dir></style></legend>
                  <tfoot id='ZvcDI'></tfoot>
                  1. <small id='ZvcDI'></small><noframes id='ZvcDI'>

                          <bdo id='ZvcDI'></bdo><ul id='ZvcDI'></ul>
                          <i id='ZvcDI'><tr id='ZvcDI'><dt id='ZvcDI'><q id='ZvcDI'><span id='ZvcDI'><b id='ZvcDI'><form id='ZvcDI'><ins id='ZvcDI'></ins><ul id='ZvcDI'></ul><sub id='ZvcDI'></sub></form><legend id='ZvcDI'></legend><bdo id='ZvcDI'><pre id='ZvcDI'><center id='ZvcDI'></center></pre></bdo></b><th id='ZvcDI'></th></span></q></dt></tr></i><div id='ZvcDI'><tfoot id='ZvcDI'></tfoot><dl id='ZvcDI'><fieldset id='ZvcDI'></fieldset></dl></div>