简体   繁体   English

存储基于文本的游戏的关卡和exp的最佳方法-Python 3.x

[英]Best way of storing level and exp for text based game - Python 3.x

So I'm currently trying to write a text-based game, in python. 因此,我目前正在尝试使用python编写基于文本的游戏。

I want to use an experience based system within it 我想在其中使用基于体验的系统

What is the best way to store the values 什么是存储值的最佳方法

eg. 例如。

Level 1 = 100
Level 2 = 200
Level 3 = 500
Level 4 = 800

I don't know if to use a list or dictionary, and can you also please show some examples of how to do it. 我不知道该使用清单还是字典,还可以显示一些示例。

Thanks! 谢谢!

eg: Edit: I have tried this 例如:编辑:我已经尝试过

levels = []
    #levels = {"Level":"0", "XPNeed":"0"}
    level = 1
    n = 1
    expneeded = 0
    expneeded = 100
    levels = {"Level":0, "XPNeed":0}
    print(levels)
    #print(levels)
    #print(len(levels))
    """print(levels.get("Level")[0])
    print(levels.get("XPNeed")[0])"""
    newXPNeed = levels["XPNeed"] + (level * 100)
    print(newXPNeed)
    #print(str(levels.get("XPNeed")[0] + (level * 100)))


    while len(levels) <= 31:
        newXPNeed = levels["XPNeed"][n] + (level * 100)
        experiencecalc = str(newXPNeed)
        print(experiencecalc)
        levelReq = {'Level':n, 'XPNeed':experiencecalc}
        print(levelReq)
        level += 1
        levels.append(levelReq)
        n += 1

I agree with the other comments that the easiest way to do this might be to have an external properties file that houses the leveling information. 我同意其他意见,认为最简单的方法可能是拥有一个包含平整信息的外部属性文件。 If you wanted to do this purely within the code itself, you could define a 'level object' to house the fields that matter for leveling, and then store a dictionary of level objects using the level number as the key: 如果您只想在代码本身中执行此操作,则可以定义一个“级别对象”以容纳与级别有关的字段,然后使用级别编号作为键来存储级别对象的字典:

class LevelObject(object):

    def __init__(self, level_number, minimum_exp_bound, maximum_exp_bound):
        self._level_number = level_number
        self._minimum_exp_bound = minimum_exp_bound
        self._maximum_exp_bound = maximum_exp_bound

    @property
    def level_number(self):
        return self._level_number

    @property
    def minimum_exp_bound(self):
        return self._minimum_exp_bound

    @property
    def maximum_exp_bound(self):
        return self._maximum_exp_bound


def main():
    level_dictionary = {}

    level_dictionary[0] = LevelObject(0, 0, 99)
    level_dictionary[1] = LevelObject(1, 100, 199)
    level_dictionary[2] = LevelObject(2, 200, 499)
    level_dictionary[3] = LevelObject(3, 500, 799)
    level_dictionary[4] = LevelObject(4, 800, 1499)
    # Etc...

    for key, value in level_dictionary.iteritems():
        print 'Level: %s  Min Exp: %s  Max Exp: %s' % (value.level_number, value.minimum_exp_bound, value.maximum_exp_bound)

if __name__ == "__main__":
    main()

>>>Level: 0  Min Exp: 0  Max Exp: 99
>>>Level: 1  Min Exp: 100  Max Exp: 199
>>>Level: 2  Min Exp: 200  Max Exp: 499
>>>Level: 3  Min Exp: 500  Max Exp: 799
>>>Level: 4  Min Exp: 800  Max Exp: 1499

What I've included above is just a mock-up of what the object/dictionary could look like, along with a print statement so that you can validate what the output from the dictionary would return. 我上面包含的内容只是对象/词典的外观模型,以及打印语句,因此您可以验证字典输出将返回什么。 Within your code, you would likely want to store the object code in its own file, and import it into the main portion of your code. 在您的代码中,您可能希望将目标代码存储在其自己的文件中,并将其导入到代码的主要部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM