Microsoft Small Basic

Program Listing:
Embed this in your website
' Turtle Graphics 0.5b
' Copyright © 2014-2015 Nonki Takahashi. The MIT Lisence.
'
' History:
' 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.5b"
Init()
Opening()
Main()
Sub Main
  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)
  clicked = "False"
  Controls.ButtonClicked = OnButtonClicked
  GraphicsWindow.PenColor = "DimGray"
  GraphicsWindow.PenWidth = 2
  Turtle.Show()
  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], "FORWARD") Then
    distance = Text.GetSubTextToEnd(line[i], 8)
    Turtle.Move(distance)
  ElseIf Text.StartsWith(line[i], "PENUP") Then
    Turtle.PenUp()
  ElseIf Text.StartsWith(line[i], "PENDOWN") Then
    Turtle.PenDown()
  ElseIf Text.StartsWith(line[i], "RIGHT") Then
    angle = Text.GetSubTextToEnd(line[i], 6)
    Turtle.Turn(angle)
  ElseIf Text.StartsWith(line[i], "LEFT") Then
    angle = -Text.GetSubTextToEnd(line[i], 5)
    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 = "FORWARD 100" + CR + "RIGHT 90" + CR + "FORWARD 200"
  SrcToLines()
EndSub
Sub Opening
  ms = "TITLE=2000;FORWARD=5;RIGHT=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["FORWARD"])
  EndFor
  GraphicsWindow.DrawText(10, 120, line[2])
  For a = 0 To 90
    Shapes.Rotate(obj, a)
    Program.Delay(ms["RIGHT"])
  EndFor
  GraphicsWindow.DrawText(10, 160, line[3])
  For x = x To 700
    Shapes.Move(obj, x, y)
    Program.Delay(ms["FORWARD"])
  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, "FORWARD distance")
  GraphicsWindow.DrawText(10, 110, "LEFT|RIGHT angle")
  GraphicsWindow.DrawText(10, 150, "PENDOWN|PENUP")
  GraphicsWindow.DrawText(10, 190, "REPEAT count [commands]")
  GraphicsWindow.BrushColor = "Green"
  GraphicsWindow.DrawText(10, 250, "HAVE FUN!")
  Program.Delay(ms["HELP"])
  GraphicsWindow.Clear()
EndSub
Sub OnKeyDown
  If GraphicsWindow.LastKey = "Space" Then
    ms = ""
  EndIf
EndSub
Copyright (c) Microsoft Corporation. All rights reserved.