Microsoft Small Basic

Program Listing: KDH145-1
' Turtle Brick
' Version 0.2
' Copyright © 2016 Nonki Takahshi. The MIT License.
' Program ID KDH145-1
'
GraphicsWindow.Title = "Turtle Brick"
Init()

' Go to top
cmd = "Right 5"
Do()
cmd = "Down"
Do()

' Draw hair
For row = 1 To 5
cmd = "Fill Black"
Do()
cmd = "Down"
Do()
cmd = "Left"
Do()
EndFor
cmd = "Right 6"
Do()
cmd = "Up 4"
Do()
For row = 1 To 4
cmd = "Fill Black"
Do()
cmd = "Down"
Do()
cmd = "Right"
Do()
EndFor

' Draw face
cmd = "Left 5"
Do()
cmd = "Up 4"
Do()
For row = 1 To 4
For col = 1 To row * 2 - 1
cmd = "Fill LightYellow"
Do()
cmd = "Right"
Do()
EndFor
cmd = "Down"
Do()
cmd = "Left " + (row * 2)
Do()
EndFor
cmd = "Right"
Do()
For row = 1 To 5
For col = 1 To 7
cmd = "Fill LightYellow"
Do()
cmd = "Right"
Do()
EndFor
cmd = "Down"
Do()
cmd = "Left 7"
Do()
EndFor
cmd = "Right"
Do()
For col = 1 To 5
cmd = "Fill LightYellow"
Do()
cmd = "Right"
Do()
EndFor

' Draw eyes
cmd = "Up 5"
Do()
cmd = "Left"
Do()
For row = 1 To 2
cmd = "Fill Black"
Do()
cmd = "Down"
Do()
EndFor
cmd = "Up 2"
Do()
cmd = "Left 4"
Do()
For row = 1 To 2
cmd = "Fill Black"
Do()
cmd = "Down"
Do()
EndFor

' Draw mouth
cmd = "Right"
Do()
cmd = "Down 2"
Do()
For row = 1 To 3
cmd = "Fill Red"
Do()
cmd = "Right"
Do()
EndFor

' Return to home
cmd = "Left 7"
Do()
cmd = "Up 10"
Do()

Sub Init
size = 40
gw = size * 11
gh = size * 13
Turtle.Speed = 10
GraphicsWindow.Width = gw
GraphicsWindow.Height = gh
GraphicsWindow.BackgroundColor = "SaddleBrown"
Turtle.X = size / 2
Turtle.Y = size / 2
Turtle.PenUp()
DrawGrid()
EndSub

Sub Do
x = Turtle.X
y = Turtle.Y
If Text.StartsWith(cmd, "Right") Then
GetOperand()
If operand = "" Then
operand = 1
EndIf
Turtle.MoveTo(x + operand * size, y)
ElseIf Text.StartsWith(cmd, "Left") Then
GetOperand()
If operand = "" Then
operand = 1
EndIf
Turtle.MoveTo(x - operand * size, y)
ElseIf Text.StartsWith(cmd, "Up") Then
GetOperand()
If operand = "" Then
operand = 1
EndIf
Turtle.MoveTo(x, y - operand * size)
ElseIf Text.StartsWith(cmd, "Down") Then
GetOperand()
If operand = "" Then
operand = 1
EndIf
Turtle.MoveTo(x, y + operand * size)
ElseIf Text.StartsWith(cmd, "Fill") Then
GetOperand()
If operand = "" Then
operand = "Black"
EndIf
FillBrick()
EndIf
EndSub

Sub DrawGrid
GraphicsWindow.PenWidth = 1
GraphicsWindow.PenColor = "Gray"
For x = 0 To gw Step size
GraphicsWindow.DrawLine(x, 0, x, gh)
EndFor
For y = 0 To gh Step size
GraphicsWindow.DrawLine(0, y, gw, y)
EndFor
EndSub

Sub FillBrick
' param operand - brick color
GraphicsWindow.BrushColor = operand
GraphicsWindow.FillRectangle(x - (size - 1) / 2, y - (size - 1) / 2, (size - 1), (size - 1))
EndSub

Sub GetOperand
' param cmd
sp = Text.GetIndexOf(cmd, " ")
If sp = 0 Then
operand = ""
Else
operand = Text.GetSubTextToEnd(cmd, sp + 1)
EndIf
EndSub