2016-05-15 13 views
0

コードに定義されたものの:グローバルvarialbeが、私は、変数の定義/エラーから持っ以前

def preFight(): 
    global enemy 
    enemynum=random.randint(1,3) 
    if enemynum==1: 
     enemy=goblinIG 
    elif enemynum==2: 
     enemy=zombieIG 
    elif enemynum==3: 
     enemy==skeletonIG 
    fight() 

def fight(): 
    print "%s vs %s" % (playerIG.name, enemy.name) 
    print "%s Health: %d/%d  &s's Health: %i/%i" % (playerIG.name,playerIG.health, playerIG.maxhealth, enemy.name, enemy.health, enemy.maxhealth) 
    print "Potions: %s" % (playerIG.pots) 
    print "1.) Attack" 
    print "2.) Drink Potion" 
    print "3.) Run" 
    option=raw_input("-->") 
    if option=="1": 
     attack() 
    elif option=="2": 
     drinkpot() 
    elif option=="3": 
     run() 
    else: 
     fight() 

def attack(): 
    os.system('clear') 
    pAttack=random.randint(playerIG.attack/2, playerIG.attack) 
    eAttack=random.randint(enemy.attack/2, enemy.attack) 
    if pAttack()==playerIG.attack/2: 
     print "You miss!" 
    else: 
     enemy.health-=pAttack 
     print "You deal %s damage" % pAttack 
    option=raw_input("") 
    if enemy.health<=0: 
     win() 
    os.system('clear') 
    if eAttack()==enemy.attack/2: 
     print "The enemy missed!" 
    else: 
     playerIG.health-=eAttack 
     print "The enemy deals %s damage" % eAttack 
    option=raw_input("") 
    if playerIG.health<=0: 
     die() 
    else: 
     fight() 

def drinkpot(): 
    os.system('clear') 
    if playerIG.pots==0: 
     print "You don't have any potions!" 
     option=raw_input("") 
     fight() 
    else: 
     playerIG.health+=50 
     if playerIG.health>playerIG.maxhealth: 
      playerIG.health=playerIG.maxhealth 
     print "You drank a potion" 
    option=raw_input("") 
    fight() 

def run(): 
    pAttack=random.randint(playerIG.attack/2, playerIG.attack) 
    eAttack=random.randint(enemy.attack/2, enemy.attack) 
    os.system('clear') 
    runnum=random.randint(1,3) 
    if runnum==1: 
     print "You successfully ran away!" 
     option=raw_input("") 
     start1() 
    else: 
     print "You failed to get away!" 
     option=raw_input("") 
     os.system('clear') 
     if eAttack()==enemy.attack/2: 
      print "The enemy missed!" 
     else: 
      playerIG.health-=eAttack 
      print "The enemy deals %s damage" % eAttack 
    option=raw_input("") 
    if playerIG.health<=0: 
     die() 
    else: 
     fight() 

def win(): 
    print "You have successfully killed the %s!" % enemy.name 
    print "You have gained &s gold!" % enemy.goldgain 

def die(): 
    print "You have died whilst trying to defeat %s" % enemy.name 
    print "1.) Try again" 
    print "2.) Quit" 
    option=raw_input("-->") 
    if option=="1": 
     start() 
    elif option=="2": 
     sys.exit() 
    else: 
     die() 

#def store() 


main() 

戦いが開始したとき、私は取得していますエラーは次のとおりです。

Traceback (most recent call last): 
    File "/home/ubuntu/workspace/rpg_game.py", line 194, in <module> 
    main() 
    File "/home/ubuntu/workspace/rpg_game.py", line 46, in main 
    start() 
    File "/home/ubuntu/workspace/rpg_game.py", line 60, in start 
    start1() 
    File "/home/ubuntu/workspace/rpg_game.py", line 75, in start1 
    preFight() 
    File "/home/ubuntu/workspace/rpg_game.py", line 93, in preFight 
    enemy==skeletonIG 
NameError: global name 'enemy' is not defined 

これは、私が敵を定義し、グローバルとして設定するので、私を混乱させます。誰かが私が問題の原因を見つけるのを助けてくれるでしょうか?

+1

最初はどこに定義しますか? – Schore

+0

def preFight()の後:その次の行は敵の定義です。 – Nicolas

+1

「グローバル敵」は、敵がグローバルであることをPythonに伝えます。それは何も定義しない。あなたはどこかに "敵= ..."と書く必要があります。 – Schore

答えて

0

あなたはこの機能の代入でエラーがあります。=に対し、平等のための==チェックが代入演算子なので

def preFight(): 
    global enemy 
    enemynum=random.randint(1,3) 
    if enemynum==1: 
     enemy=goblinIG 
    elif enemynum==2: 
     enemy=zombieIG 
    elif enemynum==3: 
     enemy==skeletonIG 
    fight() 

enemy==skeletonIGは、enemy=skeletonIGでなければなりません。

関連する問題