2017-02-09 69 views
-2

我剛開始學習python幾個星期前,我試圖做一個簡單的RPG遊戲。然而,在用戶之後,他想與村民交談,沒有發生任何事情。有人可以幫忙嗎?謝謝 !函數不能在python遊戲中工作

from random import randint 

import random 
class Character: 
    def __init__(self,name,hp,thaco,ac,inventory,exp): 
     self.name=name 
     self.hp=hp 
     self.thaco=thaco 
     self.ac=ac 
     self.inventory=inventory 
     self.exp=exp 



class Fighter(Character): 
    def __init__(self): 
     super().__init__(name=input("What is your characters name?"),thaco=20,ac=10, 
         hp=10,inventory={"sword","shield"},exp=10) 
    prof = "fighter" 
    maxhp=10 
    level=1 
    hd=10 
    level2=20 

class warior(Character): 
    def __init__(self): 
     super().__init__(name=input("What is your characters name?"),thaco=20,ac=10, 
         hp=8,inventory={""},exp=8) 
    prof= "cleric" 
    maxhp=8 
    level=1 
    hd=8 
    level2=15 
class Mage(Character): 
    def __init__(self): 
     super().__init__(name=input("What is your characters name?"),thaco=20,ac=10, 
         hp=4,inventory={},exp=4) 
    prof= "mage" 
    mana=1 
    maxmana=1 
    maxhp=4 
    level=1 
    hd=4 
    level2=10 
class Goblin(Character): 
    def __init__(self): 
     super().__init__(name="goblin", 
         hp=7,thaco=20, 
         ac=6,inventory={}, 
         exp=7) 


class Orc(Character): 
    def __init__(self): 
     super().__init__(name="orc", 
         hp=8,thaco=18, 
         ac=6,inventory={}, 
         exp=8) 

def profession(): 
    print("What is your class?",'\n', 
      " press f for Fighter",'\n', 
      " press w for warior",'\n', 
      " press m for Mage") 
    pclass=input(">>>") 
    if pclass =="f": 
     Prof = Fighter() 
    elif pclass=="w": 
     Prof = warior() 
    elif pclass == "m": 
     Prof = Mage() 
    else: 
     Prof=Fighter() 
     #profession() 
    return Prof 
hero=profession() 
print("name hp thaco ac inventory xp",'\n', 
     hero.name,hero.hp,hero.thaco,hero.ac,hero.inventory,hero.exp) 




def villager(): 

global npcname 

global response 



response=["hi", " I need your help to fight this monster" ,"i m looking for treasure",] 

print(random.choice(response)) 




print (" a villager is walking by") 
print ("would you like to speak to him? yes or no") 
answer=input(">>>") 

if answer=="yes": 
    villager() 
+0

如果您有任何錯誤,請將其發佈。 – kenorb

+0

好吧,發生了一些事情,但'村民()'沒有做任何有用的事情。縮進遍佈在那裏,但你在'職業()'中是正確的。 – roganjosh

+0

提示:你似乎正在得到一些意想不到的輸出。爲什麼不「打印」你輸入的內容(或者,如果你想看看,用一個調試器來檢查它)? – 2017-02-09 17:57:48

回答

0

所有你villager函數做,因爲它目前爲是定義一個全局變量

def villager(): 

    global npcname 

你應該補充一點,你想這個方法來定義全局變量npcname我想以後做什麼都邏輯您希望該功能看起來更像這樣:

def villager(): 
    response=["hi", " I need your help to fight this monster" ,"i m looking for treasure",] 

    print(random.choice(response))