Page 223 - Learn To Program With Scratch
P. 223

try it out 8-6
                            Make the Driver sprite validate the number entered by the user before broadcast-
                            ing the BinaryToDecimal message to the Bit sprite . You should verify that (1) the
                            number entered by the user is a binary number (that is, it only contains ones and
                            zeros) and (2) the length of the input is at most eight digits .



                          Hangman
             Hangman .sb2  In this section, we’ll write a classic Hangman game. Figure 8-23 shows the
                          game in action.



                                                                      Helper sprite
                                                                      (hidden)
                                                                      Hangman sprite

                                                                      New sprite

                                                                      Variable rem

                                                                      Variables c1 to c6
                                                                      Driver sprite
                                                                      (hidden)


                          Figure 8-23: The user interface for the Hangman game
                             The program randomly selects a secret six-letter word and shows one
                          question mark for each letter. The player has eight chances to guess the let-
                          ters in the word. If the player guesses a letter correctly, the program shows
                          all occurrences of that letter in the secret word. Otherwise, the program
                          shows a new part of a hanging figure (the head, body, left arm, and so on).
                          After eight wrong guesses, the program finishes the figure, and the player
                          loses the game. If the player guesses the secret word in eight attempts or
                          fewer, the result is victory. This application has the following four sprites:
                             Driver  This sprite hides itself when the game starts, prompts the
                             player to enter guesses, and processes the player’s answers. When the
                             game ends, the sprite shows one of the following two costumes:






                             Hangman  This sprite displays the evolving image of the hangman. It
                             has a total of nine costumes, each one showing an additional part of
                             the hangman’s body, as depicted in Figure 8-24.


                                                                               String Processing   201

                                           www.it-ebooks.info
   218   219   220   221   222   223   224   225   226   227   228