Wednesday, September 30, 2015

The For Block

Repeating parts of your program is a very common practice in programming. There many ways to do it. You have already used two of them:
  • The block is appropriate when you want to repeat the same behavior forever.
  • The block is appropriate when you want to repeat the same behavior a specific number of times.
  • The for block (see Import Tools at right) is appropriate when you want to repeat almost the same behavior with a change in each step.
Import Tools
In the current version of Snap!, the for block isn't installed automatically. Click on the File File menu icon menu at the top of the Snap! window and select "Import Tools." You'll need to import tools once into each new Snap! project where you need for or other advanced blocks.
The numbers in the for block work as you might expect: the for block counts by 1 from the first number to the last number, running the script inside the C-shape with the new number each time. As the for block counts, it keeps track of the changing number with a variable, the orange oval set to i by default, which you can drag into the blocks inside the for loop.
With for, long scripts like:

can be simplfied to:


  1. The example above shows a script that counts from 1 to 10. Build it and modify it so that it counts 0, 2, 4, 6, 8... up to 30.Talk with Another Pair
  2. To change the name of the variable from i to length, click on the orange oval without dragging it. After you've changed the name, drag it where you want to use it.
  3. Build this script and try it out:
    squiral script
    This shape is called a "squiral" — a square spiral.
  4. The steps moved varies between repetitions.
  5. Talk with Your Partner Do you see why it spirals outward?
  6. Try switching the order of the 100 and the 1 in the for block in the squiral script. What is the result?
  7. Experiment! Try changing the turning angle in the squiral script to other numbers such as 92, 126, etc.
  8. Change the turn angle and the move steps to get as close as you can get to a smooth spiral:
    spiral
  9. "U1L2-Squiral"Save your work as U1L2-Squiral
  10. Click the script below to lanch it in Snap!:
    when (up arrow) key pressed (for (i = 60 to 84) (say (i), play note (i) for (0.2) beats))
  11. Create a second script that says and plays the notes down from 84 to 60 when you press the down arrow.
  12. You can additional build scripts by dragging more blocks out from the palettes on the left and snapping them together or by duplicating an existing script (by two-finger-clicking or left-clicking it).
  13. Code a script that says and plays the notes from 60 to 83 and then from 84 to 61 a total of 3 times quickly.

 Take IT Further

 Build a script that counts backwards from 100 to 0 by 10s (that is, 100, 90, 80, etc.).
  1. Build a script that nests squares. Draw 10 squares with each square larger than the previous:
    nested squares
  2. Buid a script that draws 12 regular polygons, each with one more side than the previous.
  3. Build a script that counts from 3.14 to 0.14 and then back again.

The Forever Block

As the name "forever" implies, the forever block will run the blocks inside it again and again and again and again... forever. You can stop one script by clicking on it again. You can stop all scripts by clicking on the stop sign at the top right corner of the Snap! window. This is equivalent to executing the stop all block in the "Control" palette.
  1. Talk with Your Partner What do you think this script will do?
    Chase That Mouse Script
  2. Build that script and click it to experiment with the sprite movement when you move the mouse pointer over the stage.
  3. Mouse X Reporter and Mouse Y Reporter are reporters in the "Sensing" palette. They report the mouse's location on the screen.
  4. What do you think this script will do?
    Follow That Mouse Script
  5. Build that script and move your mouse around the stage while the program is running. Does it follow your expectations?
  6. Change the go to block to look like this:
    You can change a script even while it is running.
    Go To Y+30 Script
    How does this change the program's behavior?
  7. What will this script do?
    Talk with Your Partner say (they kept) for (1) secs, forever [say (talking) for (1) secs, say (and) for (1) secs]

Nesting Repeats

It is also possible to nest one repeat block inside another. Since Snap! processes scripts from top to bottom, the first repeat will begin first, and the second repeat will have to complete its entire process before the process of the first repeat is called again.
  1. Predict what this script will do without building:
    1. How many times in total will the script say "Hip"?
    2. How many times in total will the script say "Hooray"?
  2. You'll want to use the pen down command tells the sprite to set its penpoint down to draw.
  3. Here is another script that has nested repeat blocks.
    squares in nested repeats
    1. Sketch on paper what you expect it to do.
    2. Build it and try it out.
    3. Embed one more repeat (move, turn) loop between move 50 steps and turn right 90 degrees.

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."

Getting to Know the Snap! Interface

Getting to Know the Snap! Interface

Instructions for opening files are in yellow at the bottom of Snap! Accounts and Files.
  1. Open the "U1L1-Interface" project file that you created on the previous page.
  2. Find pen down, and click it so you can draw.
The pen down command tells the sprite (located at the tip of the arrow, sprite) to set its penpoint down to draw.
You can click any script (or block) to run it.
  1. While a script is running, its border is highlighted. Some scripts run so quickly that you may not notice the brief border flash. This script takes longer to run, so it's easier to notice the highlighted border. Script Running
    Before building this script, predict what it will do. Talk with Your Partner Then try it out.
    You just made a script!

  2. Motion palette categories: Motion, Looks, Sound, Pen, Control, Sensing, Operators, and Variables
  3. Experiment! Look through the tabs in the color-coded palette menu (shown right) to find each of the blocks below. Click each block at least once, and figure out what all they do. Try changing their inputs (number, text, color, etc.) also.
    go to x 0 y 0 point in direction 90 set pen color to magenta set pen size to 1 say Hello! for 2 secs think Hmm... for 2 secs play note 60 for 0.5 beats sqrt of 10 pick random 1 to 10
  4. Blocks do different kinds of things. Some blocks create changes on the stage. Other blocks report information right where they are: Sqrt of 10 reporting 3.162... 7 < 3 reporting false x position reporting -79 pick random 1 to 10 reporting 2
    In general, you need to press pen down before you can draw. Here, you already pressed it above.
  5. Create this two-block script in the scripting area. Click on it a several times to see what it does.
    move 10 steps, turn right 15 degrees
  6. You may want to clear the stage periodically by finding and clicking the clear block.
  7. Modify the numbers in the two-block script above and discuss how each change affects the sprite's behavior. Try changing the numbers so that the sprite follows a square path if your script is clicked enough times. Talk with Your Partner