Scratch ChatBot

By Russell Barnes. Posted

Nano the cute robot loves to chat. He’ll respond to your answers, and he’ll even jump up and down if you ask him to...

For this project, you’ll be creating your own talking robot which responds to your text input. We’ll also alter his expression by switching between different costumes. We’ll be using ask commands, if…else blocks, and the join Operator. We’ll also create a variable to store the user’s name – variables are really handy for storing values to use elsewhere. That’s enough chitchat – let’s start up a new Scratch project…

You can find this and more articles in Learn to Code with Scratch!

STEP-01 Prepare your artwork

After deleting the Scratch cat by right-clicking on it and selecting Delete, it’s time to import a new stage background (space) and our character sprite (Nano). You can either obtain these from the Scratch 2.0 library or download them for Scratch 1.4. In the latter case, click Stage in the Sprite List, select the Backgrounds tab, then click Import and navigate to Space.png in the folder where you’ve stored the downloaded graphics for this project. Next, click the star/folder icon above the Sprite List, then navigate to the same folder and import Nano.sprite. If you click the Costumes tab, you’ll notice that Nano has four of them; we’ll switch between them to animate our little robot friend.

STEP-02 Ask for a name

First, we’ll get our robot to ask for the user’s name and then use it in a response. With the Nano sprite selected, click the Scripts tab (top middle) and add the code below (Listing 1). Note that instead of using a standard when green flag clicked block, we’re starting the program when the Nano sprite is clicked. He then asks for the user’s name, which is stored in a variable called name. First, we need to create the latter: select Variables from the top left, then click ‘Make a variable’, ‘For this sprite only’, and enter ‘name’ in the text field. Untick the name block to stop it showing on the stage. We can now set name to answer (the user’s text input) and then add it into Nano’s response by using the join Operator block. Make sure you put a space after ‘Hi’ to avoid it being joined together with the name.

 Listing 1

STEP-03 Add a question

Next, we’ll add some more blocks from Listing 2 (below) to the bottom of this script. After saying ‘hi’ to them, Nano asks the user if they’re OK. Again, we use the ask Sensing block for this, and the name variable to refer to them by name. We then use an if…else Control block to determine Nano’s response based on the user’s input. If it’s ‘yes’ (which we test for using the = Operator) we switch Nano’s costume to happy nano-c, using the drop-down box on his Looks block. We also get him to say ‘That’s great to hear!’

 Listing 2

STEP-04 Else this…

In the else part of the if…else block, we determine what happens if the user’s input isn’t ‘yes’. In this case, we’ll switch Nano’s costume to the frowning nano-d and get him to say ‘Oh no!’ Test out this code with different inputs to check that it’s working as expected. Note that while the user’s text input isn’t case sensitive, it has to be just ‘yes’, with nothing added, in order to be recognised as such.

STEP-05 Jump up and down

Finally, we’ll add another question with ask, using a standard if block to make Nano jump up and down or not. Add the blocks from Listing 3 (below) to the script. We use a repeat loop to make Nano move repeatedly up and down, to jump. To make sure he’s not frowning from the previous response while doing so, we switch his costume to nano-c before the repeat loop.

 Listing 3

STEP-06 Taking it further

You can alter the example questions or add any extra ones you want, even getting Nano to tell a joke. You could also add extra costumes by copying and editing them in the Paint Editor, or even a design a brand new sprite with various costumes.

Win! One of 10 Raspberry Pi 3 & Official Case signed by Eben Upton. Click here to enter

From The MagPi store

Subscribe

Subscribe to the newsletter

Get every issue delivered directly to your inbox and keep up to date with the latest news, offers, events, and more.