Wednesday, September 30, 2015

The Repeat Block

The little icon next to this script means that you can click to open it in another window.
This script draws a square.
Take turns speaking Take turns reading.
The repeat block makes this script shorter and simpler to construct. Also, structure—the logic of the script—is clearer when we see repeat 4 (move 50, turn 90) than when we see move 50, turn 90, move 50, turn 90, move 50, turn 90, move 50, turn 90.
Making the logic clear is essential to good coding. For one thing, it makes it easier to catch bugs. In that long script (with no repeat), we'd have to count the parts to see if it's right, and it would be easy to make a mistake if we were drawing a 20-sided figure instead of a 4-sided one.
    Talk with Your Partner
  1. The three input numbers in the script—4, 50, and 90—determine the script's behavior. Experiment with each number, and discuss how each contributes to the square that is drawn.
  2. When you change the 4 to 40, what does change and what does not change about the picture and the way it is drawn?
  3. Look for patterns.
    A regular polygon is a shape in which all the sides are the same length and all the turning angles are the same. A square is a regular 4-sided polygon.
    Use empty repeat-n block to draw the following regular shapes using the smallest number of lines:  
    • Equilateral Triangle
    • Pentagon
    • Hexagon
    • Octagon
  4. What number can you put into the blank numeric input of the repeat block below to produce the picture shown below?
     

Take IT Further


  1. Figure out what turning angles to use in scripts that draw a 12-sided or 36-sided polygon and make small enough sides so that these can fit on the stage. With more and more sides, these get closer and closer to a circle.
  2. drawn star
  3. The code in problem 4 above created a regular 9-pointed star. Through how many degrees did the sprite turn altogether to draw that star?
  4. Adjust the numbers in that script to get a regular 5-pointed star.
  5. Through how many degrees did your sprite turn altogether when you drew the five-pointed star?
  6. Find a starting direction so that the star sits "straight."

No comments:

Post a Comment