如何制作变量信息表

问题描述 投票:0回答:1

我正在学习 Python 并制作一款基于《龙与地下城》的文本游戏。我很好奇是否有办法制作一个表格或其他东西,这样我就不必每次都为每个生物指定相同的变量(enemy_ac、enemy_hp 等)。

#Function to spawn an enemy
def encounter():
    global enemy
    global enemy_weapon
    global enemy_ac
    global enemy_hp
    global enemy_attack_mod
    global enemy_damage_mod
    global enemy_exp

    #print("Debug: Running Encounter Function")
    chance_encounter = randint(1,100)
    if chance_encounter in range(90,96):
        enemy = "Orc"
        enemy_weapon = "Greataxe"
        enemy_ac = 13
        enemy_hp = 15
        enemy_attack_mod = 5
        enemy_damage_mod = 3
        enemy_exp = 100
        print("An Orc howls in bloodlust as it brings its Greataxe to bare!!")
        print("")
    elif chance_encounter >= 97:
        enemy = "Worg"
        enemy_weapon = "Bite"
        enemy_ac = 13
        enemy_hp = 26
        enemy_attack_mod = 5
        enemy_damage_mod = 3
        enemy_exp = 100
        print(r"""
                          ,
               ,,/( ,,,,,,,,,,___,,
              )b     ,,,           "`,_,
             /(     /                   `,
            L/7_/\,,|            /        \
             ,`      `,  \     ,|          \
              ,      /  /``````||      |\,  \__,)))
                    /  / |      \\     \  \,,,,,,/
                   |  /  |       \\   )/
                   \ (|  )     ,,//   /
                    `_)_/     ((___/"'
            """)
        print("A Worg is released into the arena!!")
        print("")
    else:
        enemy = "Goblin"
        enemy_weapon = "Scimitar"
        enemy_ac = 15
        enemy_hp = 7
        enemy_attack_mod = 4
        enemy_damage_mod = 2
        enemy_exp = 50
        print(r"""
              /(.-""-.)\      
          |\  \/      \/  /|  
          | \ / =.  .= \ / |  
          \( \   o\/o   / )/  
           \_, '-/  \-' ,_/   
             /   \__/   \     
             \ \__/\__/ /     
           ___\ \|--|/ /___   
         /`    \      /    `\  
        /       '----'       \
        """)
        print ("A frenzied Goblin charges you with a Scimitar!!")

任何建议表示赞赏 - 谢谢!

不知道如何开始解决这个问题...谷歌搜索。

python python-3.x variables
1个回答
0
投票

游戏中不同对象“实例”(例如敌人或房间)具有“相同变量”的想法就是“面向对象”的想法。

在 Python 中,我们通常会使用

class
语句创建自定义对象类型 - 类“Enemy”可以根据需要保存
name
weapon
hp
attack_mod
属性。 (“属性”是附加到对象时变量的名称)。

尝试阅读“class”上的Python教程,看看你是否可以使用它们。为了最大限度地减少此代码中的样板,您可能需要使用“数据类”。 https://docs.python.org/3/tutorial/classes.html

© www.soinside.com 2019 - 2024. All rights reserved.