Search code examples
pythonartificial-intelligencechatbotpanda3d

DirectEntry and a basic Python chat AI: Panda3D


So I have two scripts which are as follow.

script 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."

Then, here is script 2, which is a slightly edited version of the example code from 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)

So like. What I'm trying to to is to have the use type their inputs into the DirectEntry box so then the Panda3D panel (there's an animated character but that's not relevant) would print the program's response out.

i.e. the user types in "Hello!" so then the program would proceed to spit out "Hi, I'm Jane." on the screen.

I'm super-new to coding and literally everything I end up doing is ultra complicated; explaining a little bit would be mega helpful!! Thanks a million!


Solution

  • I was actually tinkering around with one solution I'd tried and realized it actually would work! I'd only typed it all wrong because I can't type :P

    Here's the entire script if anyone's curious:

    #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)
    

    So! If anyone's curious, go right ahead and use it. I'm probably going to replace the if/elif/else part with PyAIML of some sort, but for a simple game it'd definitely be great! :)