2015-10-20 40 views
0

所以我有兩個腳本,如下所示。DirectEntry和一個基本的Python聊天AI:Panda3D

腳本1:

while True: 
userInput = raw_input(">>> ") 
if userInput.lower() in ["yo",'hi', 'hello', 'hi there', 'hey there']: 
    print "Hi, I'm Jane." 
elif userInput.lower() in ["sup", "what's up", "how are you", "how are u", "sup?", "what's up?", "how are you?", "how are u?"]: 
    whassup = ['Not much, you?','The usual!', 'Working on paperwork.', 'Helping out, haha.', 'Annoying my sisters.'] 
    print(random.choice(whassup))   
elif userInput.lower() in ["cool", "awesome", "sounds cool", "rad"]: 
    print "Aww, thanks!" 
else: 
    print "Sorry, I can only use SpeedChat." 

然後,這裏是腳本2,這是示例代碼從here稍微編輯版本:

#add some text 
bk_text = "This is my Demo" 
userin = OnscreenText(text = bk_text, pos = (0, 0.7), 
scale = 0.07,fg=(0,0,0,1),align=TextNode.ACenter,mayChange=1) 
userin.setFont(font) 

#callback function to set text 
def setText(textEntered): 
    userin.setText(textEntered) 

#clear the text 
def clearText(): 
    b.enterText('') 

#add button 
b = DirectEntry(text = "" ,scale=.05, command=setText, 
initialText="Type Something", numLines = 2,focus=1,focusInCommand=clearText) 

所以喜歡。 我想要的是使用類型輸入到DirectEntry框中,然後Panda3D面板(有一個動畫角色但不相關)會打印出程序的響應。

即用戶輸入「你好!」那麼程序會繼續吐出「嗨,我是簡。」屏幕上。

我對編碼超新穎,從字面上看,我最終所做的一切都非常複雜;解釋一點點將是巨大的幫助!太感謝了!

回答

0

我實際上正在修補一個我試過的解決方案,並意識到它實際上會起作用!我只鍵入它都錯了,因爲我不能鍵入:P

這裏就是整個腳本,如果任何人的好奇:

#add some text 
bk_text = "Hi, I'm Jane." 
userin = OnscreenText(text = bk_text, pos = (0, 0.7), 
scale = 0.07,fg=(0,0,0,1),align=TextNode.ACenter,mayChange=1) 
userin.setFont(font) 

#callback function to set text 
def setText(textEntered): 
     if textEntered.lower() in ["yo",'hi', 'hello', 'hi there', 'hey there']: 
       txt = "Hello!" 
       grunt.play() 
     elif textEntered.lower() in ["sup", "what's up", "how are you", "how are u", "sup?", "what's up?", "how are you?", "how are u?"]: 
       whassup = ['Not much, you?',"The sky's up.", 'Working on paperwork.', 'Researching cookie recipes.', 'Being a giant robot bird, as usual.'] 
       txt = (random.choice(whassup)) 
       statement.play() 
     elif textEntered.lower() in ["cool", "awesome", "sounds cool", "rad"]: 
       txt = "Haha, thanks." 
       statement.play() 
     elif textEntered.lower() in ["when i was a young boy"]: 
       txt = "MY FATHER, TOOK ME INTO THE CITY, TO SEE A MARCHING BAND!" 
       grunt.play() 
     elif textEntered.lower() in ["jane", "hey jane", "jane?", "hey jane?"]: 
       txt = "Yes?" 
       murmur.play() 
     elif textEntered.lower() in ["who is it", "who is it?", "it is", "is it", "john cena"]: 
       txt = "JOHN CENA" 
       JC.play() 
     else: 
       txt = "I don't speak Toon?" 
       question.play() 

    userin.setText(txt) 

#clear the text 
def clearText(): 
    b.enterText('') 

#add button 
b = DirectEntry(text = "" ,scale=.05, command=setText, 
initialText="Type Something", numLines = 2,focus=1,focusInCommand=clearText) 
b.setPos(-0.27, 0, -0.6) 

所以!如果任何人都好奇,就直接前進並使用它。我可能會用某種PyAIML替換if/elif/else部分,但對於一個簡單的遊戲,它肯定會很棒! :)