Time for action - join two sentences

For every question the seeker asks, the teller has a response. Let's use the join block to add a little "thoughtfulness" to the response.

  1. The current teller script uses a say block to repeat the fortune in the if/else block. Replace the second instance of the your_fortune block with the join block from the Operators palette. See the following screen shot.
    Time for action - join two sentences
  2. Type a greeting into the first field of the join block. My example uses "That's an interesting question."
  3. Add the your_fortune block to the second field of the join block.
Time for action - join two sentences

Run the script several times to test the code to observe the teller's response. Remember, we previously built this script to provide a different answer on every fifth question, which means our new code will not always run.

What just happened?

Using the join block, we combined a constant phrase with the phrase contained in the your_fortune block, a variable. We could have achieved the same results with say blocks, but we would have needed to use two say blocks to accomplish the same thing. Also, the join block gave the script a different tempo than what we would have been able to achieve using multiple say blocks.

Nest join blocks

Like many other blocks in Scratch, we can nest multiple join blocks. This allows us to join multiple text phrases to form complex thoughts and paragraphs, as the following screenshot demonstrates.

Nest join blocks

Additional string operations

In addition to the join block, we can also manipulate text using the letter of and length of Operator blocks. The letter of block identifies the specified position in the supplied text phrase. The length of block, reports the number of characters in a phrase.

Consider the following screen shot, which is a help screen from Scratch.

Additional string operations

We can also use the comparison blocks, (>, <, and =) to evaluate one string against another. Consider the following screen shot.

Additional string operations

The code in the screen shot checks to see if the keyboard input in the answer block, matches any of the items in the questions list. Just like comparing two numbers, if a match is made, the statement evaluates to true; otherwise it's false.

Next steps

Our focus has been on using lists and variables to drive the action in our game. If you wanted to expand on this game, try adding graphical effects to the sprites and creating a design that begins to tell a story.

Using the concepts in this chapter, you could create other games of chance, such as blackjack, bingo, or a lottery number predictor. The text operators give you a lot of flexibility to create more complex and natural conversations.

..................Content has been hidden....................

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