Page 219 - Learn To Program With Scratch
P. 219

Used to hold the part of answer before the comma.

                                                        Used to hold the part of answer after the comma.
                                                      Flag to mark when we find the comma while parsing answer.
                                                      Loop counter to examine the characters of answer.
                                                      Check the characters of answer, one by one.

                                                      When we find the comma, change the flag to 1.


                                                      If we did not reach the comma yet,
                                                      append the current character to angle.


                                                      Otherwise, append the current character to distance.








                 Figure 8-16: The GetAnswers script

                             Finally, the Player sprite executes
                          the CheckAnswers procedure to see
                          whether it is close enough to the tar-
                          get. The game ends only if the Player
                          sprite comes within a very close dis-
                          tance of the target. Figure 8-17 shows
                          how the Player sprite checks its dis-
                          tance from the target.
                             The Player sprite uses the distance
                          to block to check how close it is to the
                          Target sprite. If the distance is less than
                          20 steps, the game considers this a hit
                          and says, “You won!” Otherwise, the   Figure 8-17: The CheckAnswers proce-
                          shooting trial is considered a miss,   dure of the Player sprite .
                          the forever loop starts again, and the
                          player gets another chance.



                                                   try it out 8-4
                            Modify the Shoot game to keep track of the number of times it takes the player to
                            hit the target and assign the player a score accordingly .




                                                                               String Processing   197

                                           www.it-ebooks.info
   214   215   216   217   218   219   220   221   222   223   224