not really known
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

685 lines
30 KiB

  1. Guide to Programming with Turtle Art
  2. ====================================
  3. Turtle Blocks expands upon what children can do with Logo and how it
  4. can be used as the underlying motivator for “improving” programming
  5. languages and programmable devices.
  6. In this guide, we illustrate this point by both walking the reader
  7. through numerous examples, but also by discussing some of our favorite
  8. explorations of Turtle Blocks, including multi-media, the Internet
  9. (both as a forum for collaboration and data collection), and a broad
  10. collection of sensors.
  11. Getting Started
  12. ---------------
  13. Turtle Blocks Javascript is designed to run in a browser. Most of the
  14. development has been done in Chrome, but it should also work in
  15. Firefox. You can run it directly from index.html, from a [server
  16. maintained by Sugar Labs](http://turtle.sugarlabs.org), from the
  17. [github
  18. repo](http://walterbender.github.io/turtleblocksjs),
  19. or by setting up a [local
  20. server](https://github.com/walterbender/turtleblocksjs/blob/master/server.md).
  21. Once you've launched it in your browser, start by clicking on (or
  22. dragging) blocks from the *Turtle* palette. Use multiple blocks to
  23. create drawings; as the turtle moves under your control, colorful
  24. lines are drawn.
  25. You add blocks to your program by clicking on or dragging them from
  26. the palette to the main area. You can delete a block by dragging it
  27. back onto the palette. Click anywhere on a "stack" of blocks to start
  28. executing that stack or by clicking in the *Rabbit* (fast) or *Turtle*
  29. (slow) on the Main Toolbar. The *Snail* will step through your
  30. program, one block per click.
  31. For more details on how to use Turtle Blocks JS, see [Using Turtle
  32. Blocks
  33. JS](http://github.com/walterbender/turtleblocksjs/tree/master/documentation)
  34. for more details.
  35. ABOUT THIS GUIDE
  36. ----------------
  37. Many of the examples given in the guide have links to code you can
  38. run. Look for RUN LIVE links that will take you to
  39. http://turtle.sugarlabs.org.
  40. TO SQUARE
  41. ---------
  42. The traditional introduction to Logo has been to draw a square. Often
  43. times when running a workshop, I have the learners form a circle
  44. around one volunteer, the "turtle", and invite them to instruct the
  45. turtle to draw a square. (I coach the volunteer beforehand to take
  46. every command literally, as does our graphical turtle.) Eventually the
  47. group converges on "go forward some number of steps", "turn right (or
  48. left) 90 degrees", "go forward some number of steps", "turn right (or
  49. left) 90 degrees", "go forward some number of steps", "turn right (or
  50. left) 90 degrees", "go forward some number of steps". It is only on
  51. rare occasions that the group includes a final "turn right (or left)
  52. 90 degrees" in order to return the turtle to its original
  53. orientation. At this point I introduce the concept of "repeat" and
  54. then we start in with programming with Turtle Blocks.
  55. 1. Turtle Basics
  56. ----------------
  57. <img src='https://rawgithub.com/walterbender/turtleblocksjs/master/guide/basics1.svg' />
  58. A single line of length 100 [RUN LIVE](https://turtle.sugarlabs.org/?file=Forward_100.tb&run=True)
  59. <img src='https://rawgithub.com/walterbender/turtleblocksjs/master/guide/basics2.svg' />
  60. Changing the line length to 200 [RUN LIVE](https://turtle.sugarlabs.org/?file=Basic_2&run=True)
  61. <img src='https://rawgithub.com/walterbender/turtleblocksjs/master/guide/basics3.svg' />
  62. Adding a right turn of 90 degrees. Running this stack four times
  63. produces a square. [RUN LIVE](https://turtle.sugarlabs.org/?file=Basic_3&run=True)
  64. <img src='https://rawgithub.com/walterbender/turtleblocksjs/master/guide/basics4.svg' />
  65. Forward, right, forward, right, ... [RUN LIVE](https://turtle.sugarlabs.org/?file=Basic_4&run=True)
  66. <img src='https://rawgithub.com/walterbender/turtleblocksjs/master/guide/basics5.svg' />
  67. Using the *Repeat* block from the *Flow* palette [RUN LIVE](https://turtle.sugarlabs.org/?file=Basic_5&run=True)
  68. <img src='https://rawgithub.com/walterbender/turtleblocksjs/master/guide/basics6.svg' />
  69. Using the *Arc* block to make rounded corners [RUN LIVE](https://turtle.sugarlabs.org/?file=Basic_6&run=True)
  70. <img src='https://rawgithub.com/walterbender/turtleblocksjs/master/guide/basics7.svg' />
  71. Using the *Fill* blocks from the Pen palette to make a solid square
  72. (what ever is drawn inside the *Fill* clamp will be filled upon
  73. exiting the clamp.) [RUN LIVE](https://turtle.sugarlabs.org/?file=Basic_7&run=True)
  74. <img src='https://rawgithub.com/walterbender/turtleblocksjs/master/guide/basics8.svg' />
  75. Changing the color to 70 (blue) using the *Set Color* block from the
  76. *Pen* palette [RUN LIVE](https://turtle.sugarlabs.org/?file=Basic_8&run=True)
  77. <img src='https://rawgithub.com/walterbender/turtleblocksjs/master/guide/basics9.svg' />
  78. Using the *Random* block from the *Numbers* palette to select a random
  79. color (0 to 100) [RUN LIVE](https://turtle.sugarlabs.org/?file=Basic_9&run=True)
  80. A SHOEBOX
  81. ---------
  82. When explaining boxes in workshops, I often use a shoebox. I have
  83. someone write a number on a piece of paper and put it in the
  84. shoebox. I then ask repeatedly, "What is the number in the box?" Once
  85. it is clear that we can reference the number in the shoebox, I have
  86. someone put a different number in the shoebox. Again I ask, "What is
  87. the number in the box?" The power of the box is that you can refer to
  88. it multiple times from multiple places in your program.
  89. 2. Boxes
  90. --------
  91. Boxes let you store an object, e.g., a number, and then refer to the
  92. object by using the name of the box. (Whenever you name a box, a new
  93. block is created on the Boxes palette that lets you access the content
  94. of the box.) This is used in a trivial way in the first example below:
  95. putting 100 in the box and then referencing the box from the Forward
  96. block. In the second example, we increase the value of the number
  97. stored in the box so each time the box is referenced by the Forward
  98. block, the value is larger.
  99. <img src='https://rawgithub.com/walterbender/turtleblocksjs/master/guide/boxes1.svg' />
  100. Putting a value in a *Box* and then referring to the value in *Box* [RUN LIVE](https://turtle.sugarlabs.org/?file=Boxes_1&run=True)
  101. <img src='https://rawgithub.com/walterbender/turtleblocksjs/master/guide/boxes2.svg' />
  102. We can change the value in a *Box* as the program runs. Here we add 10
  103. to the value in the *Box* with each iteration. The result in this case
  104. is a spiral, since the turtle goes forward further with each step. [RUN LIVE](https://turtle.sugarlabs.org/?file=Boxes_2&run=True)
  105. <img src='https://rawgithub.com/walterbender/turtleblocksjs/master/guide/boxes3.svg' />
  106. If we want to make a more complex change, we can store in the *Box* some
  107. computed value based on the current content of the *Box*. Here we
  108. multiply the content of the box by 1.2 and store the result in the
  109. *Box*. The result in this case is also a spiral, but one that grows
  110. geometrically instead of arithmetically.
  111. In practice, the use of boxes is not unlike the use of keyword-value
  112. pairs in text-based programming languages. The keyword is the name of
  113. the *Box* and the value associated with the keyword is the value stored
  114. in the *Box*. You can have as many boxes as you'd like (until you run
  115. out of memory) and treat the boxes as if they were a dictionary. Note
  116. that the boxes are global, meaning all turtles and all action stacks
  117. share the same collection of boxes. [RUN LIVE](https://turtle.sugarlabs.org/?file=Boxes_3&run=True)
  118. 3. Action Stacks
  119. ----------------
  120. With Turtle Blocks there is an opportunity for the learner to expand
  121. upon the language, taking the conversation in directions unanticipated
  122. by the Turtle Block developers.
  123. *Action* stacks let you extend the Turtle Blocks language by defining
  124. new blocks. For example, if you draw lots of squares, you may want a
  125. block to draw squares. In the examples below, we define an action
  126. that draws a square (repeat 4 forward 100 right 90), which in turn
  127. results in a new block on the *Actions* palette that we can use whenever
  128. we want to draw a square. Every new *Action* stack results in a new
  129. block.
  130. <img src='https://rawgithub.com/walterbender/turtleblocksjs/master/guide/actions1.svg' />
  131. Defining an action to create a new block, "*Square*" [RUN LIVE](https://turtle.sugarlabs.org/?file=Actions_1&run=True)
  132. <img src='https://rawgithub.com/walterbender/turtleblocksjs/master/guide/actions2.svg' />
  133. Using the "*Square*" block [RUN LIVE](https://turtle.sugarlabs.org/?file=Actions_2&run=True)
  134. <img src='https://rawgithub.com/walterbender/turtleblocksjs/master/guide/actions3.svg' />
  135. The *Do* block lets you specify an action by name. In this example, we
  136. choose "one of" two names, "*Square*" and "*Triangle*" to determine which
  137. action to take. [RUN LIVE](https://turtle.sugarlabs.org/?file=Actions_3&run=True)
  138. 4. Parameters
  139. -------------
  140. Parameter blocks hold a value that represents the state of some turtle
  141. attribute, e.g., the x or y position of the turtle, the heading of the
  142. turtle, the color of the pen, the size of the pen, etc. You can use
  143. parameter blocks interchangeably with number blocks. You can change
  144. their values with the *Add* block or with the corresponding set blocks.
  145. <img src='https://rawgithub.com/walterbender/turtleblocksjs/master/guide/parameters1.svg' />
  146. Using the *Heading* parameter, which changes each time the turtle
  147. changes direction, to change the color of a spiral [RUN LIVE](https://turtle.sugarlabs.org/?file=Parameters_1&run=True)
  148. <img src='https://rawgithub.com/walterbender/turtleblocksjs/master/guide/parameters2.svg' />
  149. "Squiral" by Brian Silverman uses the *Heading* and *X* parameter
  150. blocks. [RUN
  151. LIVE](https://turtle.sugarlabs.org/?file=Card-36.tb&run=True)
  152. <img src='https://rawgithub.com/walterbender/turtleblocksjs/master/guide/parameters3.svg' />
  153. Often you want to just increment a parameter by 1. For this, use the
  154. *Add-1-to* block. [RUN LIVE](https://turtle.sugarlabs.org/?file=Parameters_3&run=True)
  155. <img src='https://rawgithub.com/walterbender/turtleblocksjs/master/guide/parameters4.svg' />
  156. To increment (or decrement) a parameter by an arbitrary value, use the
  157. *Add-to* block. [RUN LIVE](https://turtle.sugarlabs.org/?file=parameters_4&run=True)
  158. <img src='https://rawgithub.com/walterbender/turtleblocksjs/master/guide/parameters5.svg' />
  159. To make other changes to a parameter based on the current value, use
  160. the parameter's *Set* block. In this example, the pen size is doubled
  161. with each step in the iteration. [RUN LIVE](https://turtle.sugarlabs.org/?file=Parameters_5&run=True)
  162. 5. Conditionals
  163. ---------------
  164. Conditionals are a powerful tool in computing. They let your program
  165. behave differently under differing circumstances. The basic idea is
  166. that if a condition is true, then take some action. Variants include
  167. if-then-else, while, until, and forever. Turtle Blocks provides
  168. logical constructs such as equal, greater than, less than, and, or,
  169. and not.
  170. <img src='https://rawgithub.com/walterbender/turtleblocksjs/master/guide/conditionals1.svg' />
  171. Using a conditonal to select a color: Once the heading > 179, the
  172. color changes. [RUN
  173. LIVE](http://turtle.sugarlabs.org/?file=Conditionals-1.tb&run=true)
  174. <img src='https://rawgithub.com/walterbender/turtleblocksjs/master/guide/conditionals2.svg' />
  175. Conditionals along with the *Random* block can be used to simulate a
  176. coin toss. [RUN
  177. LIVE](http://turtle.sugarlabs.org/?file=Conditionals_2&run=true)
  178. <img src='https://rawgithub.com/walterbender/turtleblocksjs/master/guide/conditionals3.svg' />
  179. A coin toss is such a common operation that we added the *One-of* block
  180. as a convenience. [RUN
  181. LIVE](http://turtle.sugarlabs.org/?file=Conditionals_3&run=true)
  182. 6. Multimedia
  183. -------------
  184. Turtle Blocks provides rich-media tools that enable the incorporation
  185. of sound, typography, images, and video.
  186. At the heart of the multimedia extensions is the *Show* block. It can be
  187. used to show text, image data from the web or the local file system,
  188. or a web camera. Other extensions include blocks for synthetic speech,
  189. tone generation, and video playback.
  190. <img src='https://rawgithub.com/walterbender/turtleblocksjs/master/guide/media1.svg' />
  191. Using the *Show* block to display text; the orientation of the text
  192. matches the orientation of the turtle. [RUN
  193. LIVE](https://turtle.sugarlabs.org/?file=Media-1.tb&run=True)
  194. <img src='https://rawgithub.com/walterbender/turtleblocksjs/master/guide/media2.svg' />
  195. You can also use the *Show* block to show images. Clicking on the Image
  196. block (left) will open a file browser. After selecting an image file
  197. (PNG, JPG, SVG, etc.) a thumbnail will appear on the *Image* block
  198. (right).
  199. <img src='https://rawgithub.com/walterbender/turtleblocksjs/master/guide/media3.svg' />
  200. The *Show* block in combination with the *Camera* block will capture and
  201. display an image from a webcam. [RUN
  202. LIVE](http://turtle.sugarlabs.org/?file=Media_3&run=true)
  203. <img src='https://rawgithub.com/walterbender/turtleblocksjs/master/guide/media4.svg' />
  204. The *Show* block can also be used in conjunction with a URL that
  205. points to media. [RUN
  206. LIVE](http://turtle.sugarlabs.org/?file=Media_4&run=true)
  207. 7. Sensors
  208. ----------
  209. Seymour Papert’s idea of learning through making is well supported in
  210. Turtle Blocks. According to Papert, “learning happens especially
  211. felicitously in a context where the learner is consciously engaged in
  212. constructing a public entity, whether it’s a sand castle on the beach
  213. or a theory of the universe”. Research and development that supports
  214. and demonstrates the children’s learning benefits as they interact
  215. with the physical world continues to grow. In similar ways, children
  216. can communicate with the physical world using a variety of sensors in
  217. Turtle Blocks. Sensor blocks include keyboard input, sound, time,
  218. camera, mouse location, color that the turtle sees. For example,
  219. children may want to build a burglar alarm and save photos of the
  220. thief to a file. Turtle Blocks also makes it possible to save and
  221. restore sensor data from a file. Children may use a “URL” block to
  222. import data from a web page.
  223. <img src='https://rawgithub.com/walterbender/turtleblocksjs/master/guide/sensors1.svg' />
  224. Using sensors. The *Loudness* block is used to determine if there is an
  225. intruder. A loud sound triggers the alarm action: the turtle shouts
  226. “intruder” and takes a picture of the intruder.
  227. Teachers from the Sugar community have developed extensive collection
  228. of examples using Turtle Block sensors. Guzmán Trinidad, a physics
  229. teacher from Uruguay, wrote a book, *Physics of the XO*, which includes
  230. a wide variety of sensors and experiments. Tony Forster, an engineer
  231. from Australia, has also made remarkable contributions to the
  232. community by documenting examples using Turtle Blocks. In one example,
  233. Tony uses the series of switches to measure gravitational
  234. acceleration; a ball rolling down a ramp trips the switches in
  235. sequence. Examining the time between switch events can be used to
  236. determine the gravitational constant.
  237. One of the typical challenges of using sensors is calibration. This is
  238. true as well in Turtle Blocks. The typical project life-cycle
  239. includes: (1) reading values; (2) plotting values as they change over
  240. time; (3) finding minimum and maximum values; and finally (4)
  241. incorporating the sensor block in a Turtle program.
  242. Example: Paint
  243. --------------
  244. As described in the Sensors section, Turtle Blocks enables the
  245. programmer/artist to incorporate sensors into their work. Among the
  246. sensors available are the mouse button and mouse x and y
  247. position. These can be used to create a simple paint program, as
  248. illustrated below. Writing your own paint program is empowering: it
  249. demystifies a commonly used tool. At the same time, it places the
  250. burden of responsibility on the programmer: once we write it, it
  251. belongs to us, and we are responsible for making it cool. Some
  252. variations of paint are also shown below, including using microphone
  253. levels to vary the pen size as ambient sound-levels change. Once
  254. learners realize that they can make changes to the behavior of their
  255. paint program, they become deeply engaged. How will you modify paint?
  256. <img src='https://rawgithub.com/walterbender/turtleblocksjs/master/guide/paint1.svg' />
  257. In its simplest form, paint is just a matter of moving the turtle to
  258. whereever the mouse is positioned. [RUN
  259. LIVE](http://turtle.sugarlabs.org/?file=Paint_1&run=true)
  260. <img src='https://rawgithub.com/walterbender/turtleblocksjs/master/guide/paint2.svg' />
  261. Adding a test for the mouse button lets us move the turtle without
  262. leaving a trail of ink. [RUN
  263. LIVE](http://turtle.sugarlabs.org/?file=Paint_2&run=true)
  264. <img src='https://rawgithub.com/walterbender/turtleblocksjs/master/guide/paint3.svg' />
  265. In this example, we change the pen size based on the volume of
  266. microphone input. [RUN
  267. LIVE](http://turtle.sugarlabs.org/?file=Paint_3&run=true)
  268. <img src='https://rawgithub.com/walterbender/turtleblocksjs/master/guide/paint4.svg' />
  269. In another example, inspired by a student in a workshop in Colombia,
  270. we use time to change both the pen color and the pen size. [RUN
  271. LIVE](http://turtle.sugarlabs.org/?file=Paint-4.tb&run=true)
  272. Example: Slide Show
  273. -------------------
  274. <img src='https://rawgithub.com/walterbender/turtleblocksjs/master/guide/slideshow1.svg' />
  275. Why use Powerpoint when you can write Powerpoint? In this example, an
  276. Action stack is used to detect keyboard input: if the keyboard value
  277. is zero, then no key has been pressed, so we call the action again. If
  278. a key is pressed, the keyboard value is greater than zero, so we
  279. return from the action and show the next image.
  280. Example: Keyboard
  281. -----------------
  282. <img src='https://rawgithub.com/walterbender/turtleblocksjs/master/guide/keyboard.svg' />
  283. In order to grab keycodes from the keyboard, you need to use a *While*
  284. block. In the above example, we store the keyboard value in a box,
  285. test it, and if it is > 0, return the value. [RUN
  286. LIVE](http://turtle.sugarlabs.org/?file=Keyboard_1&run=true)
  287. <img src='https://rawgithub.com/walterbender/turtleblocksjs/master/guide/keyboard2.svg' />
  288. If you want to convert the keycode to a alphanumeric character, you
  289. need to use the *To ASCII* block. E.g., *toASCII(65) = A* [RUN
  290. LIVE](http://turtle.sugarlabs.org/?file=Keyboard_2&run=true)
  291. 8. Turtles, Sprites, Buttons, and Events
  292. ----------------------------------------
  293. <img src='https://rawgithub.com/walterbender/turtleblocksjs/master/guide/turtles1.svg' />
  294. A separate turtle is created for each *Start* block. The turtles run
  295. their code in parallel with each other whenever the *Run* button is
  296. clicked. Each turtle maintains its own set of parameters for position,
  297. color, pen size, pen state, etc. In this example, three different
  298. turtles draw three different shapes. [RUN
  299. LIVE](http://turtle.sugarlabs.org/?file=Turtles_1&run=true)
  300. <img src='https://rawgithub.com/walterbender/turtleblocksjs/master/guide/turtles2.svg' />
  301. Custom graphics can be applied to the turtles, using the *Shell*block
  302. on the *Media* palette. Thus you can treat turtles as sprites that can
  303. be moved around the screen. In this example, the sprite changes back
  304. and forth between two states as it moves across the screen.
  305. <img src='https://rawgithub.com/walterbender/turtleblocksjs/master/guide/turtles3.svg' />
  306. Turtles can be programmed to respond to a "click" event, so they can
  307. be used as buttons. In this example, each time the turtle is clicked,
  308. the action is run, which move the turtle to a random location on the
  309. screen. [RUN
  310. LIVE](http://turtle.sugarlabs.org/?file=Turtles_3&run=true)
  311. <img src='https://rawgithub.com/walterbender/turtleblocksjs/master/guide/turtles4.svg' />
  312. Events can be broadcast as well. In this example, another variant on
  313. Paint, turtle "buttons", which listen for "click" events, are used to
  314. broadcast change-color events. The turtle used as the paintbrush is
  315. listening for these events.
  316. 9. Advanced Actions
  317. --------------------
  318. Sometime you might want an action to not just run a stack of blocks
  319. but also to return a value. This is the role of the return block. If
  320. you put a return block into a stack, then the action stack becomes a
  321. calculate stack.
  322. <img src='https://rawgithub.com/walterbender/turtleblocksjs/master/guide/actions4.svg' />
  323. In this example, a *Calculate* stack is used to return the current
  324. distance of the turtle from the center of the screen. Renaming an
  325. action stack that has a *Return* block will cause the creation of a new
  326. block in the *Actions* palette that can be used to reference the return
  327. value: in this case, a *Distance* block is created. [RUN
  328. LIVE](http://turtle.sugarlabs.org/?file=Actions_4&run=true)
  329. <img src='https://rawgithub.com/walterbender/turtleblocksjs/master/guide/actions6.svg' />
  330. You can also pass arguments to an *Action* stack. In this example, we
  331. calculate the distance between the turtle and an arbitrary point on
  332. the screen by passing x and y coordinates in the *Calculate*
  333. block. You add additional arguments by dragging them into the "clamp".
  334. Note that args are local to *Action* stacks, but boxes are not. If you
  335. planned to use an action in a recursive function, you had best avoid
  336. boxes. [RUN
  337. LIVE](http://turtle.sugarlabs.org/?file=Actions_6&run=true)
  338. Example: Fibonacci
  339. ------------------
  340. Calculating the Fibonacci sequence is often done using a resursive
  341. method. In the example below, we pass an argument to the *Fib* action,
  342. which returns a value if the argument is &lt; 2; otherwise it returns
  343. the sum of the result of calling the *Fib* action with argument - 1 and
  344. argument - 2.
  345. <img src='https://rawgithub.com/walterbender/turtleblocksjs/master/guide/fibonacci1.svg' />
  346. Calculating Fibonacci [RUN
  347. LIVE](http://turtle.sugarlabs.org/?file=Fibonacci-1.tb&run=true)
  348. <img src='https://rawgithub.com/walterbender/turtleblocksjs/master/guide/fibonacci2.svg' />
  349. In the second example, we use a *Repeat* loop to generate the first six
  350. Fibonacci numbers and use them to draw a nautilus.
  351. Draw a nautilus [RUN
  352. LIVE](http://turtle.sugarlabs.org/?file=nautilus.tb&run=true)
  353. Example: Reflection Paint
  354. -------------------------
  355. By combining multiple turtles and passing arguments to actions, we can
  356. have some more fun with paint. In the example below, the *Paint Action*
  357. uses *Arg 1* and *Arg 2* to reflect the mouse coordinates about the y and
  358. x axes. The result is that the painting is reflected into all four
  359. quadrants.
  360. <img src='https://rawgithub.com/walterbender/turtleblocksjs/master/guide/turtles5.svg' />
  361. Reflection Paint [RUN LIVE](http://turtle.sugarlabs.org/?file=Reflection-Paint.tb&run=true)
  362. 10. Advanced Boxes
  363. ------------------
  364. Sometimes it is more convenient to compute the name of a *Box* than to
  365. specify it explicitly. (Note that the *Do* block affords a similar
  366. mechanism for computing the names of actions.)
  367. In the following examples, we use this to accumulate the results of
  368. toss a pair of dice 100 times (example inspired by Tony Forster).
  369. <img src='https://rawgithub.com/walterbender/turtleblocksjs/master/guide/boxes4.svg' />
  370. Rather than specifying a box to hold each possible result (2 through
  371. 12), we use a *Box* as a counter (index) and create a box with the name
  372. of the current value in the counter and store in that box a value of
  373. 0.
  374. <img src='https://rawgithub.com/walterbender/turtleblocksjs/master/guide/boxes5.svg' />
  375. Next we add an *Action* to toss the dice 100 times. To simulate tossing
  376. a pair of dice, we sum two random numbers between 1 and 6. We use the
  377. result as the name of the box we want to increment. So for example, if
  378. we throw a 7, we add one to the *Box* named 7. In this way we increment
  379. the value in the appropriate *Box*.
  380. <img src='https://rawgithub.com/walterbender/turtleblocksjs/master/guide/boxes6.svg' />
  381. Finally, we plot the results. Again, we use a *Box* as a counter ("index")
  382. and call the *Plot Action* in a loop. In the *Bar Action*, we draw a
  383. rectangle of length value stored in the *Box* with the name of the
  384. current value of the index. E.g., when the value in the index *Box* equals
  385. 2, the turtle goes forward by the value in *Box 2*, which is the
  386. accumulated number of times that the dice toss resulted in a 2; when
  387. the value in the *Index Box* is 3, the turtle goes forward by the value
  388. in *Box 3*, which is the accumulated number of times that the dice toss
  389. resulted in a 3; etc. [RUN
  390. LIVE](http://turtle.sugarlabs.org/?file=Advanced_Boxes&run=true)
  391. 11. The Heap
  392. ------------
  393. Sometimes you need a place to temporarily store data. One way to do it
  394. is with boxes (as mentioned at the end of the Boxes section of this
  395. guide, they can be used as a dictionary or individual keyword-value
  396. pairs). However, sometimes it is nice to simply use a heap.
  397. A heap is a essential a pile. The first thing you put on the heap is
  398. on the bottom. The last thing you put on the heap is on the top. You
  399. put things onto the heap using the *Push* block. You take things off of
  400. the heap using the *Pop* block. In Turtle Blocks, the heap is first-in
  401. last-out (FILO), so you pop things off of the heap in the reverse
  402. order in which you put them onto the heap.
  403. There is also an *Index* block that lets you refer to an item in the
  404. heap by an index. This essentially lets you treat the heap as an
  405. array. Some other useful blocks include a block to empty the heap, a
  406. block that returns the length of the heap, a block that saves the heap
  407. to a file, and a block that loads the heap from a file.
  408. In the examples below we use the heap to store a drawing made with a
  409. paint program similar to the previous examples and then to playback
  410. the drawing by popping points off of the heap.
  411. <img src='https://rawgithub.com/walterbender/turtleblocksjs/master/guide/heap1.svg' />
  412. In the first example, we simply push the turtle position whenever we
  413. draw, along with the pen state. Note since we pop in the reverse order
  414. that we push, we push y, then x, then the mouse state. [RUN
  415. LIVE](http://turtle.sugarlabs.org/?file=Heaps_1&run=true)
  416. <img src='https://rawgithub.com/walterbender/turtleblocksjs/master/guide/heap2.svg' />
  417. In the second example, we pop pen state, x, and y off of the heap and
  418. playback our drawing. [RUN
  419. LIVE](http://turtle.sugarlabs.org/?file=Heaps_2&run=true)
  420. <img src='https://rawgithub.com/walterbender/turtleblocksjs/master/guide/heap3.svg' />
  421. Use the *Save Heap* block to save the state of the heap to a file. In
  422. this example, we save our drawing to a file for playback later. [RUN
  423. LIVE](http://turtle.sugarlabs.org/?file=Heaps_3&run=true)
  424. <img src='https://rawgithub.com/walterbender/turtleblocksjs/master/guide/heap4.svg' />
  425. Use the *Load Heap* block to load the heap from data saved in a file. In
  426. this example, we playback the drawing from data stored in a file.
  427. 12. Extras
  428. ----------
  429. The *Extras* palette is full of utilities that help you use your
  430. project's output in different ways.
  431. <img src='https://rawgithub.com/walterbender/turtleblocksjs/master/guide/extras1.svg' />
  432. The *Save as SVG* block will save your drawing as simple vector graphics
  433. (SVG), a format compatible with HTML5 and many image manipulation
  434. programs, e.g., Inkscape. In the example above, we use it to save a
  435. design in a from that can be converted to STL, a common file format
  436. used by 3D printers. A few things to take note of: (1) the *No
  437. Background* block is used to suppress the inclusion of the background
  438. fill in the SVG output; (2) *Hollow lines* are used to make graphic have
  439. dimension; and (3) the *Save as SVG* block writes to the Downloads
  440. directory on your computer. (Josh Burker introduced me to Tinkercad, a
  441. website that can be used to convert SVG to STL.) [RUN
  442. LIVE](http://turtle.sugarlabs.org/?file=Extras_1&run=true)
  443. 13. Debugging Aids
  444. ------------------
  445. Probably the most oft-used debugging aid in any language is the print
  446. statement. In Turtle Blocks, it is also quite useful. You can use it
  447. to examine the value of parameters and variables (boxes) and to
  448. monitor progress through a program.
  449. <img src='https://rawgithub.com/walterbender/turtleblocksjs/master/guide/debugging1.svg' />
  450. In this example, we use the addition operator to concatinate strings
  451. in a print statement. The mouse x + ", " + mouse y are printed in the
  452. inner loop. [RUN
  453. LIVE](http://turtle.sugarlabs.org/?file=Debug_1&run=true)
  454. <img src='https://rawgithub.com/walterbender/turtleblocksjs/master/guide/status1.svg' />
  455. <img src='https://rawgithub.com/walterbender/turtleblocksjs/master/guide/status2.svg' />
  456. There is also a *Status* widget that can be programmed to show various
  457. paramters as per the figures above. [RUN
  458. LIVE](http://turtle.sugarlabs.org/?file=Status_1&run=true)
  459. Parameter blocks, boxes, arithmetic and boolean operators, and many
  460. sensor blocks will print their current value as the program runs when
  461. running in "slow" or "step-by-step" mode, obviating the need to use
  462. the Print block in many situations.
  463. The *Wait* block will pause program execution for some number (or
  464. fractions) of seconds.
  465. The *Hide* and *Show* blocks can be used to set "break points". When a
  466. *Hide* block is encountered, the blocks are hidden and the program
  467. proceeds at full speed. When a *Show* block is encountered, the program
  468. proceeds at a slower pace an the block values are shown.
  469. <img src='https://rawgithub.com/walterbender/turtleblocksjs/master/guide/debugging2.svg' />
  470. A *Show* block is used to slow down execution of the code in an *Action*
  471. stack in order to facilitate debugging. In this case, we slow down
  472. during playback in order to watch the values popped off the heap. [RUN
  473. LIVE](http://turtle.sugarlabs.org/?file=Debug_2&run=true)
  474. 14. Advanced Color
  475. ------------------
  476. The internal representation of color in Turtle Blocks is based on the
  477. [Munsell color
  478. system](http://en.wikipedia.org/wiki/Munsell_color_system). It is a
  479. three-dimensional system: (1) hue (red, yellow, green, blue, and
  480. purple), (2) value (black to white), and (3) chroma (gray to vivid).
  481. There are parameters for each color dimension and corresponding
  482. "setters". All three dimensions have been normalized to run from 0 to
  483. 100. For Hue, 0 maps to Munsell 0R. For Value, 0 maps to Munsell value
  484. 0 (black) and 100 maps to Munsell value 10 (white). For chroma, 0 maps
  485. to Munsell chroma 0 (gray) and 100 maps to Munsell chroma 26 (spectral
  486. color).
  487. A note about Chroma: In the Munsell system, the maximum chroma of
  488. each hue varies with value. To simplify the model, if the chroma
  489. specified is greated than the maximum chroma available for a hue/value
  490. pair, the maximum chroma available is used.
  491. The *Set Color* block maps the three dimensions of the Munsell color
  492. space into one dimension. It always returns the maximum value/chroma
  493. pair for a given hue, ensuring vivid colors. If you want to more
  494. subtle colors, be sure to use the *Set Hue* block rather than the *Set
  495. Color* block.
  496. <img src='https://rawgithub.com/walterbender/turtleblocksjs/master/guide/color1.svg' />
  497. Color vs. hue example [RUN LIVE](https://turtle.sugarlabs.org/?file=Color-vs-Hue.tb&run=true)
  498. To set the background color, use the *Background* block. It will set the
  499. background to the current hue/value/chroma triplet.
  500. 15. Plugins
  501. -----------
  502. There are a growing number of extensions to Turtle Blocks in the from
  503. of plugins. See
  504. [Plugins](http://github.com/walterbender/turtleblocksjs/tree/master/plugins)
  505. for more details.