Page 226 - Learn To Program With Scratch
P. 226

secretWord=across
                                                                 displayWord=??????
                                                                     answer=r


                                                                                     
                                                                   Iteration  char  char  temp
                                                                     1       a     ?   ?
                                                                     2       c     ?   ??
                                                                    3       r     r   ??r
                                                                    4       o     ?   ??r?
                                                                     5       s     ?   ??r??
                                                                     6       s     ?   ??r???





                                                     










                         Figure 8-27: The ProcessAnswer procedure

                             The loop examines each letter of secretWord, using pos as an index.
                         If the examined letter (saved in char) equals the guessed letter (saved in
                         Scratch’s built-in answer variable), the gotLetter flag is set to 1. Otherwise,
                         the char variable is set to the letter at the corresponding position in the
                         displayWord variable. Either way, the script adds char to the end of temp, as
                         illustrated in Figure 8-27 (right).
                             When the loop terminates, the displayWord variable will contain the
                         six letters to be displayed on the Stage, taking the user’s most recent guess
                         into account. The loop also tracks the number of question marks in the
                         display string. If there are none, then the user has successfully guessed
                         the secret word.
                             When ProcessAnswer returns, the NewGame message handler checks
                         gotLetter to see whether the player guessed correctly. If not, it will call
                         ProcessWrongGuess, shown in Figure 8-28.
                             This procedure broadcasts WrongGuess to notify the Hangman sprite to
                         show its next costume, and then it decrements the number of remaining
                         guesses by 1. If the user is out of guesses, the script reveals the secret word
                         and ends the game. Otherwise, it broadcasts an Update message to show how
                         many trials the player has left.






           204   Chapter 8
                                           www.it-ebooks.info
   221   222   223   224   225   226   227   228   229   230   231