Time for action - force a positive fortune

Before we begin this exercise, re-order your answers list so that all the positive responses are at the end. Edit the list items in your text editor and then re-import the list. Make a note of where the first positive response begins:

  1. Our first task is to set up a variable to count how many questions the seeker asks so that we can calculate whether or not it's time to answer positively. Select the stage from the sprites list and create a new variable named question_number.

    Note

    We add the variable to stage because both sprites will use the variable, and when we create a variable on the stage it's always a global variable.

  2. Add the when flag clicked block to the scripts area.
  3. From the Variables palette, add the set question_number to 0 block to the when flag clicked block. Now, we have a way to reset our calculation.
    Time for action - force a positive fortune
  4. Next, we need to assign the question_number variable a value when the seeker asks a question. Click on the seeker sprite to display the scripts area.
  5. From the Variables palette, snap the change question_number by 1 block in place between the when space key pressed and the say blocks.
    Time for action - force a positive fortune
  6. Next, make the teller sprite give a specific range of answers based on the question. Select the teller sprite to display the scripts area.
  7. From the Control palette, add the if/else block to the when I receive block. Snap the current say block in place after the if block.
  8. Change the first input value on the pick random block to reflect the item number that begins your positive responses. In my example, that value is 11.
  9. Now, we need to supply a condition to the if statement to test whether or not we should issue a positive response. From the Operators palette, add the = block to the if block.
  10. Drag the mod block into the value to the left of the = sign. Change the value to the right of the equals sign to 0.
  11. We're going to use the mod block to divide question_number by 5 so that we can calculate the remainder. Add the question_number block to the first value of the mod block.
  12. Change the second value of the mod block to 5 so that the block reads question_number mod 5.
  13. Test your script by pressing the Space bar. With our current setup, the teller responds only on the fifth question, and it's always a positive response.
Time for action - force a positive fortune

Tip

If you double-click on the variable monitor block on the stage, a slider will display. Use the slider to assign a number to the variable before you run the script as a way to test.

Time for action - force a positive fortune

What just happened?

We asked our teller to issue only a positive response every fifth question, but we needed a way to let our teller sprite determine when the fifth question was asked. We set up the question_number variable as a way to count the question, and the seeker script updated the value of the question_number variable each time we pressed the space key. We call that a counter variable.

The mod block gave us the logic we needed to let the teller calculate whether or not to issue a positive response. The mod block divided the first number (question_number) by the second (5) and returned the remainder.

The teller sprite used the if block to compare the remainder to zero. We chose zero because when question_number is a multiple of 5, the remainder was zero. When the remainder was zero, we executed the code in the if block; otherwise, the code in the else block ran.

Let's evaluate some mod calculations using a divisor of 5:

  • 25 mod 5 is 0
  • 32 mod 5 is 2
  • 67 mod 5 is 3

In our script, question 25 guarantees a positive response, while questions 32 and 67 do not.

Counters

When we need to know how many items we've processed like we did in our "force a positive fortune" exercise, we use a counter variable. A counter variable is just an arbitrary name I chose so that we can easily associate that we are using a variable to count the steps in some process.

For example, iterating through each item in a list is a common example of using a variable to count the current list item's position. Consider the block of code in the following screenshot:

Counters

Imagine if we had used a list to store our jokes in Chapter 5. Our scripts would have been much simpler to construct. The sample code in the screenshot uses the counter variable in several ways. It sets the value to 1 prior to checking the condition in the forever if block. It uses the number assigned to counter to determine if we've processed all the items in the list. If counter is less than the number of items in the list, the block runs. At the end of the block, we increment counter's value by 1, and the forever if block checks the new value of counter.

Keep track of intervals with mod

If we identify an interval, then we can create a pattern of events based on the interval. We already saw an example where we look for the fifth occurrence of an event, but what if we wanted to make our sprite dance after 100 seconds elapse? A mod calculation helps us identify the interval. Assuming our timer starts at zero, the expression "current_time mod 100 is 0" becomes a check to identify every 100th second.

In our project example, we used the mod block to select certain items from our list, but we could program any number of events based on our interval, such as select items from a totally different list, change backgrounds or costumes, or we could use the mod calculation to do nothing at all.

Have a go hero

Give mod a try. Make the seeker sprite do something on every fifth response. Examples of things you might try include issuing a response to the teller, applying a graphical transformation, or jumping for joy.

If/else

In earlier chapters, we became familiar with forever, forever if, and if concepts. Each of these concepts checks a condition and then runs if the condition is met. We don't define what happens when the condition is not met.

In contrast, the if/else control block evaluates a condition, and if the evaluation is true, the code in the if block executes. If the if condition evaluates to false, then the code in the else block executes.

Think of the ultimatums you give your children, or your parents gave you. If you clean your room, you get ice cream. Or else, you go to bed without a snack.

Pop quiz

  1. The mod block:
    • Modifies a number in the list
    • Creates a variable that tracks an interval
    • Transforms the sprite into a leprechaun
    • Divides two numbers and returns the remainder
  2. We use a counter variable to:
    • Track how many times an event occurs
    • Identify how many sprites we have in the project
    • Select a random item from a list
    • Add a new item to a specific position in the list

Repeat the fortune

Up to this point, we've only stored numeric values in our variables, but variables can store text too. Let's add a script to our teller sprite to repeat the question.

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

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