Page 245 - Learn To Program With Scratch
P. 245

which is exactly what we want. When numList contains data from the user,
                          calling ShowValues displays the data in those same monitors.
                             Now let’s explore the Draw procedure, which is executed when
                          the Painter sprite receives the Draw message. You can see this script in
                          Figure 9-15.





                                                             







                                                                             224  pixels


                                                                  −164  −82  0  82  164



                          Figure 9-15: The Draw script of the Painter sprite

                             The sprite first sets the pen color. Then it sets the pen’s size to a large
                          value to draw the thick bars. To prepare for drawing the five vertical bars,
                          the sprite points in the up direction u.
                             The script starts a repeat loop to draw the five bars v. We knew the
                          x-position of each bar in advance, so we created a list named xPos to store
                          those values (also shown in the figure). During each iteration of the loop,
                          the Painter sprite moves to the x-position for the current bar, puts its pen
                          down, and then moves up to draw a vertical line.
                             The height of each line is
                            proportional to the correspond-
                          ing value in numList. Our chart
                          area on the Stage is 224 pixels tall,
                          and since 40 is the highest value,
                          an input of 40 should have a bar
                          as tall as the chart. To find the
                          height (in pixels) for any number
                          in numList, we need to multiply that
                          number by 5.6 (that is, 224/40).
                          Figure 9-16 shows the output of
                          the application after getting some
                          data from the user. Note that the   Figure 9-16: A sample output of the Bar
                          Frame sprite covers the rounded   Chart application
                          tip of the wide drawing pen so
                          the bars look flat at the bottom.



                                                                                      Lists   223

                                           www.it-ebooks.info
   240   241   242   243   244   245   246   247   248   249   250