DirectEntry和一个基本的Python聊天AI:Panda3D
问题描述:
所以我有两个脚本,如下所示。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面板(有一个动画角色但不相关)会打印出程序的响应。
即用户输入“你好!”那么程序会继续吐出“嗨,我是简。”屏幕上。
我对编码超新颖,从字面上看,我最终所做的一切都非常复杂;解释一点点将是巨大的帮助!太感谢了!
答
我实际上正在修补一个我试过的解决方案,并意识到它实际上会起作用!我只键入它都错了,因为我不能键入: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部分,但对于一个简单的游戏,它肯定会很棒! :)