6. Monster Expression
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 “when 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, click the More Blocks menu, 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 the Options dropdown, and click "add string input."
A string is a sequence of characters or numbers, including spaces and punctuation marks.
Name this string something like “monster statements.”
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 the function so far.
Replace the “say” blocks with “show feelings” blocks, and copy the statements over.
Click the flag to test.
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 feeling” 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 “when flag clicked” stacks, so delete them.
Test it out.
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 functions.
To make your monster have a neutral expression at the start of the program, Go to 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 other users 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.
Call your function using “when flag clicked” and the "show feelings" block.
Add more "show feelings" blocks to give your monster a personality.
Give your monster even more expressions by exploring the add-ons.