<< Chapter < Page
  Teaching beginners to code     Page 6 / 9
Chapter >> Page >

It may seem strange to say that a round basketball is facing in one direction or the other. However, every sprite has a front, back, top, and bottomeven in those cases where it is not visually obvious. (The orientation would be visually obvious if I were to use an animal for the sprite in place ofthe basketball. You can tell which direction the basketball is facing by observing the curved diagonal lines on the basketball.)

After you drag the blue block labeled point in direction into the center pane, you can click the arrow in the white box to expose the followingfour choices:

  • (90) right
  • (-90) left
  • (0) up
  • (180) down

As you can see, I selected the choice that causes the basketball to face to the right. As a result, when the user clicks the green flag, the basketballwill move to the origin and turn to face the right.

Handling mouse events on the basketball

Image 8 shows all of the code in the center pane that applies to the basketball.

Image 8. all of the code that applies to the basketball.

Missing image.
Image 8. All of the code that applies to the basketball.

The top script in Image 8 is another image of the script that was shown in Image 7 . At this point, we are interested in the behavior of the bottom script in Image 8 .

Behavior of the bottom script

The tan block labeled when Basketball clicked specifies that all of the actions produced by the blocks below that block will occur when the userclicks the basketball with the mouse. Furthermore, those actions will occur in top to bottom order.

The blue block labeled move 90 steps causes the basketball to move 90 steps forward (in the direction that it is facing) . Each step constitutes one unit in the Cartesian coordinate system. For example, if you were to change this value from 90 to 240, thatwould cause the basketball to move from the origin to the extreme right edge of the Stage.

The selection block

As I indicated earlier , the selection structure is sometimes referred to as a decision structure. In other words, this structure causes the program to select or make a decision between two alternatives. The pseudocode in Image 9 describes this process.

Image 9. pseudocode for a selection structure.

if a specified condition is true perform a specified actionotherwise (when the specified condition is not true) perform a different action
Image 9. Pseudocode for a selection structure.

Sometimes the second part of the selection process isn't needed. In other words, in some cases, if the specified condition is not true, there is norequirement to do anything at all. That is the case in this program.

Two independent decisions

This program makes two independent decisions shown by the pseudocode in Image 10 .

Image 10. two independent decisions.

if the basketball is touching the beach ball on the right turn around and face to the leftif the basketball is touching the beach ball on the left turn around and face to the right
Image 10. Two independent decisions.

These two decisions are implemented by the two tan blocks in Image 8 containing the word if .

The Control group Clicking the tan Control button shown at the upper left in Image 4 exposes a large number of programming blocks. Twoof them are used to create pure selection structures and one is used to create a combination of a loop structure and a selection structure.

Selection control structures available in Scratch

Clicking the tan Control button shown at the upper left in Image 4 exposes the blocks shown in Image 11 . The bottom two blocks can be used to create pure selection structures. The top block can be used to create acombination of a loop structure and a selection structure.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Teaching beginners to code. OpenStax CNX. May 27, 2013 Download for free at http://cnx.org/content/col11498/1.20
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Teaching beginners to code' conversation and receive update notifications?

Ask