In this video you will program your monster
to say a statement that goes along with an expression.
To start, drag out a say block
and add a one flag clicked block to it.
Since the current emotion is sad,
write a sad sentence or message in the say block.
This example writes I ran out of ice cream.
When you test this the creature shows a sad expression
and says the sad line.
Next, write a happy phrase.
This example says, so, I went to the store to get more.
To set the expression add a set feeling block
above the happy statement and type in happy.
This works, but you have to keep adding say
and set feeling blocks to create more animation,
which feels repetitive.
As you gain more experience as a computer scientist,
you'll notice when you're creating similar code
and figure out ways to make the code reusable.
This will save you time when building projects.
A function allows you to reuse code
with different inputs, or parameters,
so that the functionality is different each time.
Create a function that tells the monster
what to say and which feeling it will display.
To start make a new block and name it something
that makes sense for the project.
This example names the block show feelings.
To tell the function what the monster should say,
add a parameter or input.
For this project, the monster should say a statement
and express an emotion.
To say a statement click add input.
Name this string something like monster statements.
A string is a sequence of characters or numbers,
including spaces and punctuation marks.
Next, place a say block under the define block
and add the monster statements block inside it.
Any blocks placed under the show feelings block
will run whenever the monster's feelings change
in your animation.
You've defined the show feelings block
to say the monster statements.
Test this function so far.
Replace the say blocks with show feelings blocks
and copy the statements over.
Click the flag to test.
Cool, the monster says the message.
Next, animate the monster's expression at the same time.
Right click the define block, click edit,
then add another string input.
Name it something like monster feelings.
Next, add a set feelings block above the say block
and place the monster feelings argument inside it.
When the show feelings block is used,
the monster feelings value in the block
will be set in the variable
and will change the monster animation.
A new blank appears in the show feelings blocks.
Type in the emotion associated with each statement.
You no longer need the set feeling blocks
under both one flag clicked stacks, so delete them.
Test it out.
Great, the show feelings block uses the first input
to make the monster talk and the second input
to animate the monster's expression.
The code is much shorter and more manageable,
because of the functions.
To make your monster have a neutral expression
at the start of the program,
go to the set feeling block and type calm.
Add more show feelings blocks
to give your monster personality.
Check out the costumes tab
to see the different feelings costumes
in the starter project.
When you finish click share,
so that others can see your project.
Also, go to the project page and give the project
a creative title and description.
Now it's your turn.
Program a function to make your monster say a statement
and change its expression at the same time.
Code your function using one flag clicked
and the show feelings block.
Give your monster even more expressions
by exploring the add ons.
Sign in to save your project and track your progress.