Friday, September 6, 2013

TurtleArt: Variable Stars

One way TurtleArt uses variables is through a Numbers block, the "random number" block.


You can connect the random number block to other blocks to produce output from the turtle. Some of the results might produce exciting and beautiful results.


The turtle travels forward a short distance during this run.


The turtle changed its pen color and went another short distance on this run.


The turtle turned a random number between 0 and 360 degrees on this run. 
It looks like the turtle has turned between 180° and 270°.


Let us consider how we can use the random number block variable to produce beautiful art. There are limitations to using this block when moving the turtle, as we will discover. This project will also help you understand how to use the "store in box" block, found under the My Blocks tab, to more accurately control your turtle when using variables.



This project iterates on a star design. When we look at the night sky and see the stars, we see points of light in the sky. We can create a star-like point of light by asking the turtle to go forward and back and turn from 0 to 360 degrees. In order to get this procedure to work the forward and back numbers need to match.



Forward and Back blocks match.


The turtle travels around a central point and makes a point of light with a defined diameter.

What if we want to create a night sky of stars in TurtleArt? When I look at the night sky the stars are not uniform in brightness, color, or size. We can use TurtleArt to simulate a night sky with the variables of brightness, color, and length of the lines in each point of light programmed and displayed.

Can we make the arms of the light different lengths by using the random number with the forward and back blocks, like we did in the example above?



Randomizing the turtle's forward and back movements is pretty but not the intended design.

In the example above the forward and back commands are not the same number, which makes the turtle's movement unpredictable. We need a way to randomize the length of the turtle's path and use that number for both the forward and back movement.


Think of the "store in box" block as a container that can hold numbers, colors, or more.

The "store in box1" block can hold the randomized number and the "box1" block can be used to recall that number for the forward and back movement of the turtle. This way both forward and back are the same number.


Make sure the store in box is included in the repeat procedure so the number changes on each repetition.

When we run this procedure we get the beginning of a solution for the challenge above: programming a night sky with the variables of brightness, color, and length of the lines in each point of light programmed and displayed.


Try using the "set shade" block and the "set color" blocks in your night sky program. Mine uses a procedure called "star" that is exactly like the example above, but my minimum number is 10. There is another procedure called "stars" that sets up the pen size, color, and shade and places the turtle in a random place on the screen. Finally, there is a procedure called "sky" that clears the screen, fills the screen with a color, and repeats the "stars" procedure thirty times.




One solution to the night sky challenge.

The procedures work nicely.


Night sky.

Every time the procedure runs the night sky looks different!

This project was inspired by an example on the use of variables in Michael Friendly's most excellent Advanced Logo: A Language for Learning.