Page 262 - Learn To Program With Scratch
P. 262

second list (named ans) contains the user’s input, and the third list (named
                         cellYCenter) contains the 11 vertical positions used by the Letter and the YesNo
                         sprites (so they know where they should stamp their costumes). When the
                         user clicks the mouse over any of the answer boxes, the Stage sprite detects
                         the mouse click and asks for an answer. The Stage sprite updates the cor-
                         responding element of the ans list to match what the user entered and
                         stamps that letter over the answer box. Open FlowerAnatomy.sb2 to read the
                         scripts that read and display the user’s answers.
                             When the user clicks the Check button, the YesNo sprite, which has the
                         costumes for the check mark and X images, executes the script shown in
                         Figure 9-37.





                                                               Start a loop to compare all
                                                               answers.

                                                               Get letter of the correct answer.
                                                               Get letter of the user’s answer.


                                                               If the answer is correct, switch
                                                               to the check mark costume.
                                                               Otherwise, switch to the X-mark
                                                               costume.
                                                               Move to the appropriate position
                                                               on the Stage, and stamp the
                                                               costume.
                                                               Point to the next answer.




                         Figure 9-37: The Check procedure of the YesNo sprite

                             The script compares the elements of the correctAns and the ans lists one
                         by one. If the two values are equal, it stamps a check mark to say the user
                         was correct. Otherwise, it stamps the red X where the user’s answers were
                         wrong. Either way, Check consults the cellYCenter list to get the correct posi-
                         tion for stamping the image. See Try It Out 9-11 on the opposite page.

                         Other Applications
                 SayThat   The extra resources you downloaded from the book’s website (http://
              Number .sb2  nostarch.com/learnscratch/) contain three more applications that you can
                         explore on your own, with full explanations. The first application is a two-
                         player game about sorting fractions and decimals. Each player gets 5 ran-
                         dom cards from a deck of 31 cards. Each player is then dealt one card from
                         the remaining set. You can either discard the new card or drag it over one
                         of your current five, replacing the old one. Whoever arranges five cards in
                         ascending order first wins the game.


           240   Chapter 9
                                           www.it-ebooks.info
   257   258   259   260   261   262   263   264   265   266   267