2016-04-29 11 views
0

私はそれぞれの「部屋」、「Pythonのに苦労して学び」、現在は本のガイドライン次のような単純なゲームローカル変数(グローバル使用したくない)

を作っています使ってPythonを学んでいますそれは自分自身の機能であり、ここでは私の問題です。すべての機能によってアクセスする必要のある変数があります。例えばCurrent_health、アーマー、強さ。

私はここで(私が実際に理解した)前の質問を見てきましたが、唯一の解決策は関数内のグローバル変数として宣言するようですが、私は20以上の部屋)毎回それらを宣言するのは少しばかげているようです。

もう1つの方法は、関数を呼び出すときに変数を渡すことですが、毎回8つの変数を渡す必要がありますが、その都度も実用的ではないようです。どんな助けでも大歓迎です。私は、以下のコードを貼り付けておきます(ゲームが完成していないので、意味が分かりません)

今、グローバル変数のためにstart()は動作しますが、すぐにそれは(shadow_figureを実行しようとして)のでwep_dam参照

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

のあなたは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

+0

正確に私が必要としたもの、ありがとう!この変更を行った後、このインデントのエラーを処理するようになりました... – craigread7

関連する問題