Page 59 - Learn To Program With Scratch
P. 59

                                
                                                      
                                                      
                                                         
                                 Note:
                           Drag the when space
                           key pressed block from
                           the Events palette and
                           then select the desired
                           key from the drop-down
                           menu.

                          Figure 2-17: The scripts for the Player sprite

                             When the player clicks the green flag, this sprite moves to (–30,–30) u
                          and points to the right v. The other four scripts respond to the arrow keys.
                          When an arrow key is pressed, the corresponding script changes the sprite’s
                          direction w, plays a short sound (using the play sound block x from the
                          Sound palette), and moves the sprite 60 steps y. The sprite bounces off the
                          Stage’s edge z if needed. Because 60 steps correspond to 1 square on the
                          grid of Figure 2-16, each time you press an arrow key, the Player sprite moves
                          1 square.

                 n o t e    Have you noticed that the four arrow-handling scripts in Figure 2-17 are almost iden-
                          tical? In Chapter 4, you’ll learn how to avoid duplicating code this way.

                             Go ahead and test this part of the game. You should be able to move the
                          Player sprite around the Stage using the arrow keys on your keyboard. Once
                          you have that working, we’ll move on to the Gold sprite, whose script is shown
                          in Figure 2-18.



                                                                      
                                                                      

                                                                      


                                                                      


                                                                      






                          Figure 2-18: The script for the Gold sprite



                                                                              Motion and Drawing   37

                                           www.it-ebooks.info
   54   55   56   57   58   59   60   61   62   63   64