在分配之前引用的局部变量(不想使用全局)

时间:2016-04-29 23:00:19

标签: python python-2.7

我正在学习使用" Python以艰难的方式学习Python"我正在制作一个简单的游戏

按照本书的指导原则,每个"房间"是它自己的功能,这就是我的问题,我有一些需要被所有功能访问的变量。例如Current_health,护甲,力量属性。

我已经看过这里的前面的问题(我实际上已经理解了),唯一的解决方案似乎是将它声明为函数中的全局变量,但是我将拥有20多个房间(函数)每次都声明它们似乎有点傻了

另一种选择是在调用函数时传递变量,但是因为我每次需要传递8个变量,这似乎也是不切实际的。任何帮助将不胜感激。我会粘贴下面的代码以防它有用(游戏远未完成,所以有些可能没有意义)

现在start()因我的全局变量而起作用,但是一旦因为wep_dam引用而尝试运行shadow_figure(),我就会遇到错误

from sys import exit

str = 0
wep_dam = 0
dam = str + wep_dam
cha = 0
sne = 0
arm = 0
max_life = 10
points_remaining = 0

def shadow_figure():
    print "\n\nYou approach the figure, who remains silent."
    print "As you get closer you realise he has bag at his feet."
    print "Mysterious figure: \"You may choose only one.\""
    print "You look into the bag, and see a shiny sword on top of a large steel shield."
    ans = raw_input("Do you: \n1. Take the sword \n2. Take the shield \n3. Take the whole bag and run \n4. Walk away without taking anything\n> ")
    if ans == "1":
        print "The sword gives you an extra 3 damage"
        wep_dam += 3
        exit_beach()
    elif ans == "2":
        print "The shield gives you 3 armor, but it's so heavy it reduces your sneak by 1"
        arm += 3
        sne -= 1


def beach():
    print "\n\nYou wake up on a beach with no idea how you got there. \nYou see a shadowy figure close to the water."
    ans = raw_input("Do you: \n1. Approach him \n2. Go the other way\n> ")
    if ans == "1":
        shadow_figure()
    elif ans == "2":
        exit_beach()
    else:
        print "Please enter either 1 or 2"

def reset_stats():
    str = 0
    wep_dam = 0
    dam = str + wep_dam
    cha = 0
    sne = 0
    arm = 0

    max_life = 10

    points_remaining = 10
    print "\n\n\n\nGame Reset\n\n\n\n"


def start():
    global str
    global wep_dam
    global dam
    global cha
    global sne
    global arm
    str = 0
    wep_dam = 0
    dam = str + wep_dam
    cha = 0
    sne = 0
    arm = 0

    max_life = 10
    points_remaining = 0

    print "You are an adventurer, your stats are currently:"
    print "Strength:  %d \nCharisma:  %d \n  Sneak:   %d" % ( str,  cha,  sne)
    print "Strength determines your damage, charisma determines your chance of pursuasion, \nand sneak determines whether or not you can go get past enemies without being detected"
    print "you have 10 points available to spend, to spend a point, simply type the number which corresponds\nwith the skill and hit enter"
    print "\n\n1. Strength \t2. Charisma \t3. Sneak\n"
    points_remaining = 10
    while points_remaining > 0:
        ans = raw_input("Choose a skill: ")
        if ans == "1":
            str += 1
            points_remaining -= 1
            print "Strength is now  %d" % ( str)
            print "%d  points remaining\n" % ( points_remaining)

        elif ans == "2":
            cha += 1
            points_remaining -= 1
            print "Charisma is now %d" % ( cha)
            print "%d points remaining\n" % ( points_remaining)

        elif ans == "3":
            sne += 1
            points_remaining -= 1
            print "Sneak is now %d" % ( sne)
            print "%d points remaining\n" % (points_remaining)
        else:
            print "Error, please enter a number from 1 to 3\n"

    print "Your stats are now: "
    print "Strength:  %d \nCharisma:  %d \n   Sneak:  %d\n\n" % ( str,  cha,  sne)
    print "Is this OK? Or would you like to restart?\n"
    ans = raw_input("1. Continue \n2. Restart\n> ")
    if ans == "1":
        print "Game will now begin...."
        beach()
    elif ans == "2":
        ans = raw_input("Are you sure? Yes/No\n> ")
        ans = ans.lower()
        if ans == "yes":
            reset_stats()
            start()
        else:
            beach()
    else:
        print "Error, please enter 1 or 2"


start()

1 个答案:

答案 0 :(得分:1)

您可以将全局变量包装在class

class Player:
    str = 0
    wep_dam = 0
    dam = str + wep_dam
    cha = 0
    sne = 0
    arm = 0
    max_life = 10
    points_remaining = 0

并访问其中的字段,如下所示:Player.arm += 3