Page 125 - Learn To Program With Scratch
P. 125

After executing any of the scripts in Figure 5-12, sum will contain
                          the number 5, making these scripts functionally equivalent. Note that the
                          method used in the second script is a common programming practice, and
                          I recommend that you study it for a moment to become comfortable with it.
                          Now let’s see the change command in action.

                          Spider Web

            SpiderWeb .sb2  We can create a spider web by drawing several hexagons of increasing size,
                          as shown in Figure 5-13. The Triangle procedure draws an equilateral
                          tri angle with a variable side length, while the Hexagon procedure calls
                          Triangle six times with a 60° (that is, 360° / 6) right turn after each call.
                          The figure clearly shows how the hexagon is made up of the six triangles.


                                                   sideLength



















                          Figure 5-13: Creating a spider web by drawing several hexagons of increasing size


                             The SpiderWeb procedure simply calls Hexagon repeatedly with a dif-
                          ferent value of the sideLength variable each time. This results in the concen-
                          tric (that is, having the same center) hexagons you see in the figure. Note
                          how the change command is used to set the value of sideLength inside the
                          repeat loop. Reproduce the SpiderWeb procedure, run it, and see how it
                          works.

                          Pinwheel
              Pinwheel .sb2  This example is similar to the previous one except that this time, we’ll use
                          a variable to control the number of triangular repetitions. The resulting
                          procedure (called Pins) is shown in Figure 5-14. The Pinwheel procedure
                          in the same figure works like the SpiderWeb procedure above, but we also
                          change the pen’s color each time through the loop for a fun rainbow effect.
                          Some outputs of the Pinwheel procedure for different pin counts are shown
                          in the figure. Experiment with this procedure to see what else you can
                          create.


                                                                                   Variables   103

                                           www.it-ebooks.info
   120   121   122   123   124   125   126   127   128   129   130