Microsoft Small Basic

Program Listing: JPQ837
' Thinking Turtle Trailer
' Copyright © 2015 Nonki Takahashi. The MIT License.
'
GraphicsWindow.Title = "Thinking Turtle Trailer"
GraphicsWindow.BackgroundColor = "LightGray"
CRLF = Text.GetCharacter(13) + Text.GetCharacter(10)
Turtle.Show()
Turtle.PenUp()
Balloon_Add()
Balloon_Hide()
While "True"
Program.Delay(1000)
mx = GraphicsWindow.MouseX
my = GraphicsWindow.MouseY
tx = Turtle.X
ty = Turtle.Y
dx = Math.Floor(mx - tx)
dy = Math.Floor(my - ty)
If dx <> 0 Or dy <> 0 Then
txt = "Well..."
Balloon_SetText()
Balloon_Move()
Balloon_Show()
Program.Delay(1000)
mx = GraphicsWindow.MouseX
my = GraphicsWindow.MouseY
dx = Math.Floor(mx - tx)
txt = ""
If dx < 0 Then
txt = "LT 90" + CRLF + "FD " + (-dx) + CRLF
ElseIf 0 < dx Then
txt = "RT 90" + CRLF + "FD " + dx + CRLF
EndIf
dy = Math.Floor(my - ty)
If dy < 0 Then
If dx < 0 Then
txt = txt + "RT 90" + CRLF
ElseIf 0 < dx Then
txt = txt + "LT 90" + CRLF
EndIf
txt = txt + "FD " + (-dy) + CRLF
ElseIf 0 < dy Then
If dx < 0 Then
txt = txt + "LT 90" + CRLF
ElseIf 0 < dx Then
txt = txt + "RT 90" + CRLF
EndIf
txt = txt + "FD " + dy + CRLF
txt = txt + "RT 180" + CRLF
Else
If dx < 0 Then
txt = txt + "RT 90" + CRLF
ElseIf 0 < dx Then
txt = txt + "LT 90" + CRLF
EndIf
EndIf
Balloon_SetText()
Program.Delay(5000)
Balloon_Hide()
Turtle_Move()
EndIf
EndWhile
Sub Balloon_Add
GraphicsWindow.PenColor = "Gray"
GraphicsWindow.BrushColor = "White"
b[1] = Shapes.AddEllipse(100, 120)
b[2] = Shapes.AddEllipse(20, 20)
b[3] = Shapes.AddEllipse(15, 15)
GraphicsWindow.BrushColor = "Gray"
b[4] = Shapes.AddText("")
EndSub
Sub Balloon_Hide
For i = 1 To 4
Shapes.HideShape(b[i])
EndFor
EndSub
Sub Balloon_Move
Shapes.Move(b[1], tx + 10, ty - 120)
Shapes.Move(b[2], tx + 20, ty - 20)
Shapes.Move(b[3], tx + 10, ty - 15)
Shapes.Move(b[4], tx + 40, ty - 98)
EndSub
Sub Balloon_Show
For i = 1 To 4
Shapes.ShowShape(b[i])
EndFor
EndSub
Sub Balloon_SetText
Shapes.SetText(b[4], txt)
EndSub
Sub Turtle_Move
p = 1
len = Text.GetLength(txt)
While p <= len
nl = Text.GetIndexOf(Text.GetSubTextToEnd(txt, p), CRLF)
cmd = Text.GetSubText(txt, p, nl - 1)
If Text.StartsWith(cmd, "FD") Then
Turtle.Move(Text.GetSubTextToEnd(cmd, 4))
ElseIf Text.StartsWith(cmd, "LT") Then
Turtle.Turn(-Text.GetSubTextToEnd(cmd, 4))
ElseIf Text.StartsWith(cmd, "RT") Then
Turtle.Turn(Text.GetSubTextToEnd(cmd, 4))
EndIf
p = p + nl + 1
EndWhile
EndSub