完全搞不懂如何使用全局变量和全局状态

2024-04-26 18:22:51 发布

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

以下代码集完全依赖于全局变量:

SwordImpact = 1
SwordCrit = 0
SwordSwingSpeed = 2
SDCost = 1.99
SCCost = 4.99
SSSCost = 2.99
Gold = 10.0
inventory = []
Location = 'Town Center'


def Shop():
    global Gold
    global inventory
    global SDCost
    global SCCost
    global SSSCost
    global SwordImpact
    global SwordCrit
    global SwordSwingSpeed
    if Location == 'Town Center':
        Buy = input('Catalog:\nA - Health potion (5.00)\nB - Apple (1.00)\nC - Upgrade Sword (Varies)\nD - Regen potion (7.00)\n---|')
        if Buy == 'A':
            if Gold > 4.99:
                Gold = Gold - 4.99
                inventory.append('Health potion')
                print('Item bought.')
            else:
                print('Not Enough Gold.')
        if Buy == 'B':
            if Gold > 0.99:
                Gold = Gold - 0.99
                inventory.append('Apple')
                print('Item bought.')
            else:
                print('Not Enough Gold.')
        if Buy == 'C':
            Upgrade = input('Select Upgrade:\nA - Sword Damage (2.00 - (increases by 2 each upgrade))\nB - SwordCrit (5.00 (increases by 3 each upgrade))\nC - Sword Swing Speed (3.00 (Increases by 3 each upgrade))\n---|')
            if Upgrade == 'A':
                verify = input('Are you sure you want to pay ' + str(SDCost) + ' for this upgrade?\nA - Yes\nAnything else - No\n---|')
                if verify == 'A':
                    if int(Gold) > int(SDCost):
                        Gold = int(Gold) - int(SDCost)
                        SDCost = int(SDCost) + 2
                        SwordImpact = SwordImpact + 1
                    else:
                        print('Not enough gold.')
            if Upgrade == 'B':
                verify = input('Are you sure you want to pay ' + str(SCCost) + ' for this upgrade?\nA - Yes\nAnything else - No\n---|')
                if verify == 'A':
                    if int(Gold) > int(SCCost):
                        Gold = int(Gold) - int(SCCost)
                        SCCost = int(SCCost) + 3
                        SwordCrit = SwordCrit + 1.5
                    else:
                        print('Not enough gold.')
            if Upgrade == 'C':
                verify = input('Are you sure you want to pay ' + str(SSSCost) + ' for this upgrade?\nA - Yes\nAnything else - No\n---|')
                if verify == 'A':
                    if int(Gold) > int(SSSCost):
                        Gold = int(Gold) - int(SSSCost)
                        SSSCost = int(SSSCost) + 3
                        SwordSwingSpeed = SwordSwingSpeed + 1.0
                    else:
                        print('Not enough gold.')
        if Buy == 'D':
            if Gold > 6.99:
                Gold = Gold - 6.99
                inventory.append('Regen potion')
                print('Item bought.')
            else:
                print('Not Enough Gold.')
    else:
        print('You need to be in Town Center.')

但我对这组代码有一个主要问题:当我去检查变量Gold和Inventory时,它们在经过并“购买”了一些东西之后没有改变。 我能找到一个更好的方法来做到这一点,或者更好地使用“全球”声明吗?你知道吗


Tags: youifnotglobalelseupgradeintprint
1条回答
网友
1楼 · 发布于 2024-04-26 18:22:51

在您的代码中,似乎没有使用全局变量的具体原因:因为只有一个函数访问它们,所以它们最好在函数中定义。你知道吗

不过,我怀疑还有其他代码我们不知道。你知道吗

当您说变量的值没有改变时,最好显示您用来验证它的代码。很明显,在每次运行开始时,Gold的值都是从10.0开始的,在购买东西时应该会改变。你知道吗

如果有许多函数需要访问相同的状态变量,您很可能会发现,将它们全部保留为某个状态对象的属性比较容易混淆,然后将这些属性传递给每个函数,以允许它访问和修改状态,这些状态现在在每个游戏函数中都显式可用。例如,调用状态“player”,因为如果有多个玩家,每个玩家都需要自己的,所以您可以这样写:

class Player:
    def __init__(self):
        self.SwordImpact = 1
        self.SwordCrit = 0
        self.SwordSwingSpeed = 2
        self.SDCost = 1.99
        self.SCCost = 4.99
        self.SSSCost = 2.99
        self.Gold = 10.0
        self.inventory = []
        self.Location = 'Town Center'

您的代码如下:

def Shop(player):
    if player.Location == 'Town Center':
        Buy = input('Catalog:\nA - Health potion (5.00)\nB - Apple (1.00)\nC - Upgrade Sword (Varies)\nD - Regen potion (7.00)\n -|')
        if Buy == 'A':
            if player.Gold > 4.99:
                player.Gold -=  4.99
                player.inventory.append('Health potion')
                print('Item bought.')
            else:
                print('Not Enough Gold.')

以此类推—请注意,player对象现在存储各种可以更改的内容。你知道吗

一般来说,不鼓励使用全局变量,因为它使代码非模块化:函数需要的值应该作为参数传递给方法调用,而不是保存在特定的全局变量中。你知道吗

相关问题 更多 >