Page 171 - Learn To Program With Scratch
P. 171

When the game starts, this sprite sets its pen’s size and color and gets
                          ready to draw. When it receives the Redraw message, it moves to the Point1
                          sprite, clears the Stage, and then moves to the Point2 sprite. The result is a
                          straight line that connects Point1 and Point2.
                             The Tutor sprite also executes a script when it receives the Redraw mes-
                          sage, as shown in Figure 6-28.



                                                               




                                                               



                                                               




                                                               





                          Figure 6-28: Redraw message handler for the Tutor sprite


                             The script performs the following checks:
                          •   If the coordinates of Point1 and Point2 are the same, there is no line to
                             process. The script simply says “Same point.”
                          •   If the two points are different but their x-coordinates are the same,
                             then we have a vertical line. The script displays an equation of the form
                             x = constant.
                          •   If the two points are different but their y-coordinates are the same,
                             then we have a horizontal line. The script displays an equation of the
                             form y = constant.
                          •   Otherwise, the two points form a straight line whose equation has the
                             form y = mx + b. The script first calls the Compute procedure to find the
                             slope and the y-intercept of the line. Then it calls ShowEquation to put
                             the equation in a proper format and show it to the user.
                             The Compute procedure is shown in Figure 6-29. It computes the slope
                          (m) and the y-intercept (b) and then rounds these values to the nearest
                          hundredth.






                                                                              Making Decisions   149

                                           www.it-ebooks.info
   166   167   168   169   170   171   172   173   174   175   176