5. For this project, use the Scratch
Cat sprite that comes with the
program. You can modify or
replace it later. (You’ll learn
more about creating sprites and
backdrops in the next project.)
To make your sprite talk:
Go to Looks (purple), and drag the “say [Hello!] for [2] seconds
block into the workspace, below the first block. It should snap
into place
(Figure
D
)
. Click on the stack of blocks and watch what
happens!
Type whatever you want your sprite to say right in the white oval
(Figure
E
)
.
To make it easier for people to read, add a new “say Hello” block for
each line. Change the number of seconds if you want to give people
more time to read the speech bubble
(Figure
F
)
.
6. Now get the person talking:
Go to Sensing (light blue) and drag an “ask [Whats your name?] and
wait” block to the bottom of your stack
(Figure
G
)
.
Also drag the oval “answer” block, below the “ask” block, onto the
workspace, but don’t attach it to anything yet. Right click (or double-
click) on the “answer” oval, then click “Duplicate.” You will need a
bunch of “answer” blocks, so copy the original to save time.
D
E F
76
Making Simple Robots, 2nd Edition
Make_Simple_Robots_interior_FIN.indd 76Make_Simple_Robots_interior_FIN.indd 76 4/26/22 1:55 PM4/26/22 1:55 PM
7. Let the person know you heard
their answer by saying it back
to them. Here’s one way to set
it up:
Add a “say Hello” block and
change it to respond to their
answer. Keep it general, so
it applies to anything they
say! For example, imagine
you asked “What is your
favorite number?” and they
answered “99”. The sprite
could say something silly,
like, “Unbelievable! Know
how many donuts I had for
breakfast?”
Add another “say Hello”
block, and drag the
“answer” block over the
white space
(Figure
H
)
. It
will snap into place. The
sprite will now say the
answer given – 99!
NOTE: Every time a new “ask” block is used, the answer automatically
changes to the newest response.
8. Want more of a challenge? Keep the conversation going until the person
tells the chatbot they’re done. You’ll use an “if-then-else” block that
G
H
Chapter 3: Robots That Think 77
Make_Simple_Robots_interior_FIN.indd 77Make_Simple_Robots_interior_FIN.indd 77 4/26/22 1:55 PM4/26/22 1:55 PM
watches for the person to answer “No” when the chatbot asks if they
want to chat some more. Here’s how:
Go to Control (orange) and drag an “if-then-else” block into the
workspace
(Figure
I
)
. Don’t attach it to the main stack yet.
Go to Operators (green) to get a comparison block. These blocks
have pointy ends, and fit into the pointy-ended space in the “if-then-
else” block. You want a
block that will check for the
answer “No,” so you could
pick a “[ ] = [50]” block. But
if you also want it to work
with other answers that
are similar, like “Nope” and
“Not now, thank you,” a
better choice is the “[apple]
contains [a] ?” block. Find it
and drag it near the “if-then-
else” block
(Figure
J
)
.
I
J
78
Making Simple Robots, 2nd Edition
Make_Simple_Robots_interior_FIN.indd 78Make_Simple_Robots_interior_FIN.indd 78 4/26/22 1:55 PM4/26/22 1:55 PM
Make another copy of the
“answer” oval and snap it
into the first oval space on
the “contains” block. In the
second space, type the word
“no” over the letter “a.” Then
drag the comparison block
over the pointy-ended space
on the “if-then-else” block
and snap it in
(Figure
K
)
.
The “if-then-else” block
has two “mouths.” Drag
a “say Hello” block into
the top mouth. Change
the saying to a goodbye
message
(Figure
L
).
Go back to the Control
(orange) blocks and find the
stop [all]” block. Snap it in
the top mouth, right under
the goodbye message block.
The mouth will automatically
stretch to fit
(Figure
M
)
!
Now build a separate
stack of code that
will run if the person says
anything that doesn’t contain the letters “no.” This stack
goes in the bottom mouth, under the “else.” Just keep
creating questions, responses, and answers as before.
K
L
M
Chapter 3: Robots That Think 79
Make_Simple_Robots_interior_FIN.indd 79Make_Simple_Robots_interior_FIN.indd 79 4/26/22 1:55 PM4/26/22 1:55 PM
9. If you want to use the person’s answer in a sentence, try a “join [apple]
[banana]” block from the Operators (green) menu
(Figure
N
)
:
In one oval space of the “join” block, type part of the sentence.
In the other oval space, snap in an “answer” oval.
Drag the whole “join” block into a “say” or “ask” block.
End the “else” stack of code with an “ask” block to find out if the
person is ready to leave the conversation
(Figure
O
)
.
To make the “else” conversation repeat, go back to Control (orange)
and drag a “forever” block into the workspace. It goes under the first
part of the program.
N
80
Making Simple Robots, 2nd Edition
Make_Simple_Robots_interior_FIN.indd 80Make_Simple_Robots_interior_FIN.indd 80 4/26/22 1:55 PM4/26/22 1:55 PM
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset