赋值前引用的局部变量(不想使用全局变量)

2024-06-16 09:56:43 发布

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

我正在用“艰苦地学习Python”学习Python,目前正在制作一个简单的游戏

按照这本书的指导方针,每个“房间”都是它自己的函数,这就是我的问题所在,我有一些需要所有函数访问的变量。e、 g.当前的生命值,护甲,力量值

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

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

现在start()由于我的全局变量而起作用,但是由于wep\u dam引用的原因,当它尝试运行shadow\u 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()

Tags: theyouglobalpointsarmfigureprintstr
1条回答
网友
1楼 · 发布于 2024-06-16 09:56:43

您可以将全局变量包装在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

相关问题 更多 >