Page 225 - Learn To Program With Scratch
P. 225

In each iteration of the loop, the Driver sprite asks the player to guess
                          a letter and waits for input w. When the player enters a guess, the script
                          calls ProcessAnswer, which will update a flag (named gotLetter) to indicate
                          whether the letter was right or wrong.
                             When ProcessAnswer returns, the script checks the gotLetter flag x and
                          acts based on whether the player’s guess was correct or not. I’ll explain the
                          procedures called by NewGame next, starting with the scripts in Figure 8-26.


                                         Belongs to the                          Belongs to the
                                        Driver sprite                            Helper sprite











                                            Belongs to the
                                            Hangman sprite


                          Figure 8-26: Scripts triggered from the Initialize procedure

                             During initialization, the Driver sprite hides itself, initializes displayWord
                          to a string with six question marks, and sets remAttempts (how many guesses
                          the player has left) to 8. It then selects the secretWord from a predefined
                          list of six-letter words. Next the procedure broadcasts Update so the Helper
                          sprite will assign its variables (whose monitors are visible on the Stage) to
                          the correct values. The last instruction broadcasts the Reset message to the
                          Hangman sprite. When the Hangman sprite receives this message, it switches
                          to its start costume, which shows an empty gallows.
                             Now let’s consider a simple example to help us understand what the
                          ProcessAnswer procedure does (see Figure 8-27). Assume the secret
                          word is across and that this is the first round of the game (which means
                          that displayWord is “??????”). If the player’s first guess is r, ProcessAnswer
                          should set gotLetter to 1 to indicate a correct guess, set displayWord to “??r???”
                          to show the letter’s position, and set qmarkCount (the number of question
                          marks in the updated display string) to 5. When qmarkCount reaches 0, the
                          player has guessed all the letters in the secret word. ProcessAnswer belongs
                          to the Driver sprite, and you can see the full script in Figure 8-27 (left).
                             ProcessAnswer starts by resetting both the gotLetter flag and  qmarkCount
                          to 0. It will increase qmarkCount by 1 for every unknown letter in the secret
                          word. The temporary variable, temp, which is used to construct the display
                          string after every guess, is initialized to an empty string. The pos variable is
                          used as a loop counter.





                                                                               String Processing   203

                                           www.it-ebooks.info
   220   221   222   223   224   225   226   227   228   229   230