全局变量不跨函数传递

2024-09-29 23:18:48 发布

您现在位置:Python中文网/ 问答频道 /正文

(对于上次我问这个问题时看到这个问题的人,我真诚地道歉,我用了“模块”这个词来表示“功能”,但非常感谢您的宝贵建议!当我开始向公式中添加其他文件时,一定要记住这一点。)

我正在尝试使用python制作一个基于文本的冒险游戏,结果它需要很多变量,而回溯是必须的,我需要使用全局变量作为基本变量。我在试图让其他函数读取这些文件时遇到了减速带。这是用于定义通用变量及其起始值的代码行

def reset():
    global gold, exp, etnl, maxHP, curHP, maxmana, curmana, attack, defence, helm, armtop, armbot, boots, gloves, weapons
    gold = 0
    exp = 0
    etnl = 100 #exp to next level
    maxHP = 50
    curHP = 50
    maxmana = 10
    curmana = 10
    attack = 5
    defence = 5
    helm = "none"
    armtop = "none"
    armbot = "none"
    boots = "none" 
    gloves = "none"
    weapon = "fists"

例如,当我试图显示一个全局变量时,它显示为未定义的变量,如下所示:

def gamestart():
    clear() #this command is fine, simply to make it look neater when it is run again
    print("you wake up in a clearing in the forest, you can't remember what happened.")
    print("you feel numb, you realize you're lying flat on your back.")
    print
    print("HP " + str(curHP) + "/" + str(maxHP))

有人能帮我解决这个问题吗? 有没有更简单的方法? 感谢您的帮助! (是的,我确保在newgame函数之前运行reset函数)

一个更简单的版本,至少在我看来是:

def variable():
    global foo
    foo = 7

def trigger():
    variable():
    output():

def output():
    print(foo)

Tags: 文件函数noneyoufoodefglobalreset
3条回答

您可以将这些东西存储到用作存储容器的类中。如果将它们声明为类变量和任何访问器@classmethods,则不需要实例。你知道吗

class GameState:
    gold = 0
    exp = 0
    etnl = 100 #exp to next level
    maxHP = 50
    curHP = 50
    maxmana = 10
    curmana = 10
    helm = "none"
    armtop = "none"
    armbot = "none"
    boots = "none"
    gloves = "none"
    weapon = "fists"

    weapons = {"fists":(5,5),"sword":(15,12),"mace":(30,3),"cushion":(2,20)}

    @classmethod
    def reset(cls):
        cls.gold = 0
        cls.exp = 0
        cls.etnl = 100 #exp to next level
        cls.maxHP = 50
        cls.curHP = 50
        cls.maxmana = 10
        cls.curmana = 10
        cls.helm = "none"
        cls.armtop = "none"
        cls.armbot = "none"
        cls.boots = "none"
        cls.gloves = "none"
        cls.weapon = "fists"

    @classmethod
    def attack(cls):
        return cls.weapons.get(cls.weapon,(0,0))[0]

    @classmethod
    def defense(cls):         
        return cls.weapons.get(cls.weapon,(0,0))[1]


for w in State.weapons:
    State.weapon = w
    print("{} has attack {} and defense {}.".format(w, State.attack(),State.defense()))

输出:

fists has attack 5 and defense 5.
sword has attack 15 and defense 12.
mace has attack 30 and defense 3.
cushion has attack 2 and defense 20.

你可能想把一些东西分开-f.e.一个额外的类用于武器/伤害/防御相关的东西。。。你知道吗

更多阅读:

这是一个简单的例子,我认为你正在努力实现。如果您使用的是全局变量,那么您需要确保不会无意中在函数中创建具有相同名称的局部变量(当您打算修改全局变量时)。你知道吗

你应该看看如何使用classes,我认为这会帮助你解决一些语义上的混乱。你知道吗

value = 10

def reset():
    global value
    value = 10

def start():
    print(f'initial value: {value}')
    global value
    value += 1
    print(f'updated value: {value}')
    reset()
    print(f'reset value: {value}')

start()

# OUTPUT
# initial value: 10
# updated value: 11
# reset value: 10

您是否考虑过将所有统计信息存储在类/结构中而不是全局变量?在游戏开始时创建类的实例,其默认值在构造函数中指定。你知道吗

G=开始类()

def gamestart():       
    print("you wake up in a clearing in the forest, you can't remember what happened.")
    print("you feel numb, you realize you're lying flat on your back.")
    print("HP " + str(G.curHP) + "/" + str(G.maxHP))

或者,全局声明G并将其传递到gamestart(G)和/或在reset()函数中重新实例化可能是可选的。你知道吗

相关问题 更多 >

    热门问题