In order to have a successful fortune-telling, we need two things: a question and an answer. Let's start by defining some questions and answers:
I'm certain you could come up with a hundred different questions to ask a fortune-teller. Don't worry, you'll get your chance to ask more questions later.
Did you notice that the new list we created behaved a lot like a variable? We were able to make the questions list private; we don't want our teller to peek at our questions, after all. Also, the list became visible on the screen allowing us to edit the contents.
The most notable difference is that we added more than one item, and each item corresponds to a number. We essentially created a numbered list.
Because the seeker's questions were contained in a list, we used the item block to provide special instructions to the say block in order to ask the question. The first value of the item block was position, which defaulted to one. The second value was the name of the list, which defaulted to questions.
In contrast, if we used a variable to store a question, we would only need to supply the name of the variable to the say block. We saw those examples in Chapter 6.
Create an answers list for the teller sprite, and add several items to the list. Remember, there are no wrong answers in this exercise.
We can use lists to group related items, but accessing the items in the list requires an extra level of specificity. We need to know the name of the list and the position of the item within the list before we can do anything with the values.
The following table shows the available ways to access a specific item in a list.
Position |
Description |
Uses |
---|---|---|
First |
Identifies the first item in the list. |
Insert, delete, replace, or retrieve the first item in the list. |
Any |
Selects a random item in the list. |
Insert, replace, or retrieve a random item in the list. |
Last |
Selects the last item in the list. |
Insert, delete, replace, or retrieve the last item in the list. |
Variable |
Specifies a variable that contains a number instead of the default first, any, or last values. |
Use the variable to store the position of an item in the list, then insert, delete, replace, retrieve the item that corresponds to the value. |
Manual input |
Enters a specific item number. |
Insert, delete, replace, or retrieve a constant item number, such as item 5. |
Entering one item at a time via the Scratch interface is functional, but the small size of the list box can be difficult to use when we need to add a large number of items. Fortunately, we can create a text file outside of Scratch and then import it into our list.
Before you continue with the exercise, build your own text file with the fortunes you want to use in response to the seeker's questions. Enter one fortune per line in a text editor, such as Notepad.
You can use any fortunes you want, but I'm going to use a list of common Magic 8 ball responses that I found on Wikipedia at http://en.wikipedia.org/wiki/Magic_8_ball. My list will contain 19 items to start.