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.

468 lines
31 KiB

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