Microsoft Small Basic

Program Listing: XVK119-5
' Turtle Graphics 0.6b
' Copyright © 2014-2015 Nonki Takahashi. The MIT Lisence.
'
' History:
' 0.6 2015-10-03 Supported CS command. (XVK119-4)
' 0.5 2015-04-13 Supported skip opening and nested REPEAT. (XVK119-3)
' 0.4 2015-04-09 Added REPEAT command. (XVK119-2)
' 0.3 2015-04-06 Changed for Challenge of the Month - April 2014. (XVK119-1)
' 0.2 2014-03-21 Adopted to remote. (XVK119-0)
' 0.1 2014-03-20 Created. (XVK119)
'
GraphicsWindow.Title = "Turtle Graphics 0.6b"
Init()
Opening()
Main()
Sub Form
GraphicsWindow.BrushColor = "DimGray"
GraphicsWindow.FontSize = 20
pgm = Controls.AddMultiLineTextBox(10, 10)
Controls.SetSize(pgm, 180, gh - 60)
Controls.SetTextBoxText(pgm, src)
GraphicsWindow.BrushColor = "Black"
Controls.AddButton("RUN", 10, gh - 46)
GraphicsWindow.PenColor = "DimGray"
GraphicsWindow.PenWidth = 2
Turtle.Show()
EndSub
Sub Main
Form()
Controls.ButtonClicked = OnButtonClicked
clicked = "False"
While "True"
If clicked Then
src = Controls.GetTextBoxText(pgm)
Run()
clicked = "False"
Else
Program.Delay(500)
EndIf
EndWhile
EndSub
Sub OnButtonClicked
clicked = "True"
EndSub
Sub Run
SrcToLines()
l = 1 ' level
For i = 1 To nLines
DoLine()
EndFor
EndSub
Sub SrcToLines
p = 1
nLines = 0
pNewLine = Text.GetIndexOf(src, CR)
While 0 < pNewLine
nLines = nLines + 1
line[nLines] = Text.ConvertToUpperCase(Text.GetSubText(src, p, pNewLine - 1))
p = p + pNewLine
If Text.GetSubText(src, p, 1) = LF Then
p = p + 1
EndIf
pNewLine = Text.GetIndexOf(Text.GetSubTextToEnd(src, p), CR)
EndWhile
If p <= Text.GetLength(src) Then
nLines = nLines + 1
line[nLines] = Text.ConvertToUpperCase(Text.GetSubTextToEnd(src, p))
EndIf
EndSub
Sub DoLine
' param line - array of command lines
' param i - index to do for the line
If Text.StartsWith(line[i], "FD") Then
distance = Text.GetSubTextToEnd(line[i], 3)
Turtle.Move(distance)
ElseIf Text.StartsWith(line[i], "CS") Then
GraphicsWindow.Clear()
Form()
ElseIf Text.StartsWith(line[i], "PU") Then
Turtle.PenUp()
ElseIf Text.StartsWith(line[i], "PD") Then
Turtle.PenDown()
ElseIf Text.StartsWith(line[i], "RT") Then
angle = Text.GetSubTextToEnd(line[i], 3)
Turtle.Turn(angle)
ElseIf Text.StartsWith(line[i], "LT") Then
angle = -Text.GetSubTextToEnd(line[i], 3)
Turtle.Turn(angle)
ElseIf Text.StartsWith(line[i], "REPEAT") Then
b = Text.GetIndexOf(line[i], "[")
count[l] = Text.GetSubText(line[i], 7, b - 7)
iStart[l] = i + 1
iEnd[l] = nLines
nest = 0
For k = iStart[l] To nLines
If Text.StartsWith(line[k], "REPEAT") Then
nest = nest + 1
ElseIf Text.StartsWith(line[k], "]") Then
If nest = 0 Then
iEnd[l] = k - 1
k = nLines
Else
nest = nest - 1
EndIf
EndIf
EndFor
l = l + 1
For j = 1 To count[l - 1]
Stack.PushValue("local", j)
For i = iStart[l - 1] To iEnd[l - 1]
DoLine()
EndFor
j = Stack.PopValue("local")
EndFor
l = l - 1
i = iEnd[l] + 1
EndIf
EndSub
Sub Init
gw = 598
gh = 428
GraphicsWindow.Width = gw
GraphicsWindow.Height = gh
CR = Text.GetCharacter(13)
LF = Text.GetCharacter(10)
src = "FD 100" + CR + "RT 90" + CR + "FD 200"
SrcToLines()
EndSub
Sub Opening
ms = "TITLE=2000;FD=5;RT=20;CLEAR=400;HELP=7000;"
GraphicsWindow.KeyDown = OnkeyDown
url = "http://www.nonkit.com/smallbasic.files/"
obj = Shapes.AddImage(url + "Turtle.png")
GraphicsWindow.BrushColor = "Green"
GraphicsWindow.FontSize = 60
GraphicsWindow.FontName = "Tahoma"
GraphicsWindow.DrawText(10, 0, "TURTLE GRAPHICS")
x = 100
y = 440
Shapes.Move(obj, 10, 440)
Program.Delay(ms["TITLE"])
GraphicsWindow.FontSize = 40
GraphicsWindow.BrushColor = "DimGray"
GraphicsWindow.DrawText(10, 80, line[1])
For y = y To 100 Step -1
Shapes.Move(obj, x, y)
Program.Delay(ms["FD"])
EndFor
GraphicsWindow.DrawText(10, 120, line[2])
For a = 0 To 90
Shapes.Rotate(obj, a)
Program.Delay(ms["RT"])
EndFor
GraphicsWindow.DrawText(10, 160, line[3])
For x = x To 700
Shapes.Move(obj, x, y)
Program.Delay(ms["FD"])
EndFor
GraphicsWindow.BrushColor = "#99FFFFFF"
For i = 1 To 5
GraphicsWindow.FillRectangle(0, 0, gw, gh / 2)
Program.Delay(ms["CLEAR"])
EndFor
GraphicsWindow.BrushColor = "DimGray"
GraphicsWindow.DrawText(10, 10, "COMMANDS:")
GraphicsWindow.DrawText(10, 70, "FD distance")
GraphicsWindow.DrawText(10, 110, "LT|RT angle")
GraphicsWindow.DrawText(10, 150, "PD|PU")
GraphicsWindow.DrawText(10, 190, "REPEAT count [commands]")
GraphicsWindow.DrawText(10, 230, "CS")
GraphicsWindow.BrushColor = "Green"
GraphicsWindow.DrawText(10, 300, "HAVE FUN!")
Program.Delay(ms["HELP"])
GraphicsWindow.Clear()
EndSub
Sub OnKeyDown
If GraphicsWindow.LastKey = "Space" Then
ms = ""
EndIf
EndSub