Small Basic #15: The Turtle

Microsoft Small Basic provides a Turtle object that lets you draw on the graphics window with a little turtle. You can make the turtle crawl around, drawing wherever you lead it, as demonstrated in the following code that draws the word "Hi!" near the middle of the graphics window:

 ' Center of the graphics window.
w = GraphicsWindow.Width / 2
h = GraphicsWindow.Height / 2

' Move the turtle to the center of the graphics window.
Turtle.Show()
Turtle.X = w
Turtle.Y = h

' Make the turtle draw fast (but not too fast!).
Turtle.Speed = 7

' Draw the "H" in "Hi!"
Turtle.Turn(180) ' Turns the turtle at the specified speed.
Turtle.Move(40)
Turtle.MoveTo(w, h + 20) ' Same as Turtle.X with Turtle.Y, but moves at specified speed instead of instantly.
Turtle.TurnRight()
Turtle.Move(20)
Turtle.MoveTo(w + 20, h) 
Turtle.Angle = 180 ' Same as Turtle.Turn(180), but Turtle.Angle turns instantly.
Turtle.Move(40)

' Draw the "i" in "Hi!"
Turtle.X = w + 30 ' Same as first part of Turtle.MoveTo, but moves instantly and doesn't draw while moving.
Turtle.Y = h ' Same as second part of Turtle.MoveTo, but moves instantly and doesn't draw while moving.
Turtle.PenUp() ' Now, when the turtle moves, it won't also draw.
Turtle.Move(10)
Turtle.PenDown() ' Now the turtle draws as it moves.
Turtle.Move(5)
Turtle.PenUp() ' Stop drawing.
Turtle.Move(5)
Turtle.PenDown() ' Start drawing again.
Turtle.Move(20)

' Draw the "!" in "Hi!"
Turtle.X = w + 40
Turtle.Y = h
Turtle.Move(30)
Turtle.PenUp()
Turtle.Move(5)
Turtle.PenDown()
Turtle.Move(5)

Turtle.Hide()

Although not quite as fun, you can accomplish the same thing with the graphics window's DrawLine operation, as demonstrated in the following code:

 ' Center of the graphics window.
w = GraphicsWindow.Width / 2
h = GraphicsWindow.Height / 2

' Draw the "H" in "Hi!"
GraphicsWindow.DrawLine(w, h, w, h + 40)
GraphicsWindow.DrawLine(w, h + 20, w + 20, h + 20)
GraphicsWindow.DrawLine(w + 20, h, w + 20, h + 40)

' Draw the "i" in "Hi!"
GraphicsWindow.DrawLine(w + 30, h + 10, w + 30, h + 15)
GraphicsWindow.DrawLine(w + 30, h + 20, w + 30, h + 40)

' Draw the "!" in "Hi!"
GraphicsWindow.DrawLine(w + 40, h, w + 40, h + 30)
GraphicsWindow.DrawLine(w + 40, h + 35, w + 40, h + 40)