Turtle Graphics - visible turtle version

Wanna see the turtle crawl around?    Evaluate the "Definitions" first.

Definitions

Off[General::spell1] ;

\(\(\( (*\ \ \(--\(--\(--\(--\(-\ 
                  show\)\)\)\)\)\ drawings\ in\ another\ \(window\ --\) \(--\(--\(--\(--\(--\(--\(--\(--\(---\)\)\)\)\)\)\)\)\)\ *) \)\(
\)\(\(If[\(! ValueQ[
            theImageWindow]\), 
theImageWindow = 
          NotebookCreate[
WindowTitle -> "Turtle Graphics Drawing Window", 
WindowSize -> {400, 
                400}, 
 (*\ 
              WindowMargins\ gives\ distance\ from\ screen\ edges\ *) 
WindowMargins -> {{Automatic, 20}, {Automatic, 
                  20}}\ \ \ \ \ 
]];\)
 (*\ \ \(\(--\(--\(--\(--\(-\ 
                    Turtle\)\)\)\)\)\ user\ \(\(\(\(\(\(\(\(\(\(commands\ --\)--\)--\)--\)--\)--\)--\)--\)--\)--\)\)\(-\)\ *) 

    \(draw[] := 
        Module[{theGraphics}, 
theGraphics = 
            Graphics[
stuffToDraw, 
PlotRange -> {{\(-100. \), 100. }, {\(-100. \), 
                    100. }}, 
Frame -> 
                True, 
FrameTicks -> 
                None, 
AspectRatio -> 
                1, 
ImageSize -> 
                360\ ]; 
SelectionMove[theImageWindow, All, 
            Notebook]; 
NotebookWrite[
theImageWindow, 
Cell[
GraphicsData["PostScript", 
                DisplayString[
                  theGraphics]], 
CellFrameMargins -> 
                2, 
ShowCellBracket -> 
                False, 
ShowSelection -> 
                False], 
All]
];\)

    \(turtleDisk[offset_, r_] := 
        Disk[{offset\ \ Cos[thetaTurtle] + xTurtle, 
            offset\ Sin[thetaTurtle] + yTurtle}, r];\)

    \(turtle[] := {turtleDisk[3. , 3. ], 
          turtleDisk[7. , 2. ]};\)

    \(reset[] := 
        Module[{}, \ \ \ \ \  (*\ \(\(==\)\(\ \)\(reset\ turtle\ globals\)\); \ clear\ image\ window\ *) 
xTurtle = 
            0.0; \ \ \ \ \ \ \ \ \ \ \ \ \ \ \  (*\ in\ PlotRange\ coords; \ 
            see\ above\ *) 
yTurtle = 
            0.0; 
thetaTurtle = 0.0; \ \ \ \  (*\ 
            in\ radians\ *) 
penIsDown = 
            True; 
currentColor = 
            RGBColor[0, 0, 
              0]; 
stuffToDraw = {currentColor, 
              turtle[]}; \ \ \ 
drawingIsOn = 
            True; \ \  (*\ 
            True\ implies\ draw[]\ called\ after\ every\ move[]\ *) 
draw[];
];\)

    \(move[distance_] := 
        Module[{\ x, y}, 
x\  = \ 
            xTurtle\  + \ 
              distance*Cos[thetaTurtle]; 
y\  = \ 
            yTurtle\  + \ 
              distance*Sin[thetaTurtle]; 
stuffToDraw = 
            Drop[stuffToDraw, \(-1\)]; \ \  (*\ 
            remove\ turtle\ *) 
If[
            penIsDown, \ 
AppendTo[stuffToDraw, 
              Line[{{xTurtle, yTurtle}, {x, 
                    y}}]]]; 
xTurtle = 
            x; 
yTurtle = y; 
AppendTo[
            stuffToDraw, turtle[]]; 
If[\ 
            drawingIsOn, 
draw[]];
];\)

    \(turn[angleInDegrees_] := 
        Module[{}, 
stuffToDraw = 
            Drop[stuffToDraw, \(-1\)]; 
thetaTurtle\  += \ 
            angleInDegrees*Pi/180. ; 
AppendTo[stuffToDraw, 
            turtle[]]; 
If[\ 
            drawingIsOn, 
draw[]];
];\)

    \(penDown[] := \ Module[{}, \(penIsDown = True;\)];\)

    \(penUp[] := Module[{}, \(penIsDown = False;\)];\)

    \(drawOn\ [] := \ 
        Module[{}, \(drawingIsOn = True;\)];\)

    \(drawOff[] := Module[{}, \(drawingIsOn = False;\)];\)

    \(color[r_, g_, b_] := 
        Module[{}, 
currentColor = 
            RGBColor[r, g, b]; 
stuffToDraw = 
            Drop[stuffToDraw, \(-1\)]; 
AppendTo[
            stuffToDraw, currentColor]; 
AppendTo[
            stuffToDraw, turtle[]]; 
If[
            drawingIsOn, 
draw[]];
];\)

    \(randomColor[] := 
        color[
Random[Real, {0.0, 0.8}, 
            3], 
Random[Real, {0.0, 0.8}, 
            3], 
Random[Real, {0.0, 0.8}, 
            3]];\)

    \(reset[];\)


    \(move::usage = "move[d] moves the turtle a distance d.  The screen is -100 to +100";\)

    \(turn::usage = "turn[angle] turns the turtle; angles are in degrees.";\)

    \(color::usage = "color[r,g,b] sets the drawing color.  Values are 0 to 1.";\)

    \(randomColor::usage = "randomColor[] picks a random drawing color. (Go figure.)";\)

    \(penUp::usage = "penUp[] lets you move the turtle without drawing lines.";\)

    \(penDown::usage = "penDown[] tells the turtle to draw as it moves.";\)

    \(reset::usage = "reset[] clears the screen and puts the turtle in the middle.";\)

    \(drawOff::usage = "drawOff[] lets you do lots of turtle stuff without seeing the intermediate steps.";\)

    \(drawOn::usage = "drawOn[] sets the window to redraw for each turtle move.";\)

    \(draw::usage = "draw[] draws the picture in the window now.";\)\)\)\)

So move the turtle around yourself already.

The commands you can use are :
  move[distance]                           		Screen edges are at -100 and +100;
  turn[angle]                                  		Angles are in degrees, with 0.0 heading eastward.  
  color[red,green,blue]                         		 Intensities are between 0 and 1.
  randomColor[ ]
  penUp[ ]
  penDown[ ]
  drawOn[ ]
  drawOff[ ]					If drawing is off, then moving with the pen down is hidden until a draw[ ] command.
  draw[ ]
  reset[ ]					Clear the screen and put the turtle at the center, heading eastwards.

Exercises

1

Write a recipe in turtlish that draws a simple stick house something like this.

2

Turn your house recipe into a program which will draw a house of a given size, 
which can be called as "drawHouse[size]".  Test your house program by having it 
draw several houses in the same window, in different locations and with different sizes.
Did your original version cause any side effects, or have any other bugs?

3

After playing around with the turtle user commands, are there other commands that you wished you had?  
What operations would make things easier?  Can you see from looking at the turtle code in the Definitions section how 
to implement any of these additional goodies?

Scratch Pad