Microsoft Small Basic

Program Listing: GWD549-1
' Toy Poodle
' Version 0.3
' Copyright © 2017 Nonki Takahashi. The MIT License.
' Program ID GWD549-1

GraphicsWindow.Title = "Toy Poodle"
Not = "False=True;True=False;"
debug = "False"
GraphicsWindow.BackgroundColor = "SkyBlue"
colors="1=Black;2=White;3=Mocha;"
n = 0
Controls.ButtonClicked = OnButtonClicked
GraphicsWindow.MouseMove = OnMouseMove
While "True"
GraphicsWindow.BrushColor = "Gray"
GraphicsWindow.FillRectangle(0, 400, 640, 80)
n = n + 1
If 3 < n Then
n = 1
EndIf
color = colors[n]
param = "x=200;y=200;width=250;height=100;"
FillCurlyRectangle()
param = "x=450;y=200;width=60;height=15;"
FillCurlyRectangle()
param = "x=200;y=300;width=15;height=100;"
FillCurlyRectangle()
param = "x=240;y=300;width=15;height=100;"
FillCurlyRectangle()
param = "x=390;y=300;width=15;height=100;"
FillCurlyRectangle()
param = "x=430;y=300;width=15;height=100;"
FillCurlyRectangle()
param = "x=150;y=100;width=100;height=120;"
FillCurlyEllipse()
param = "x=130;y=110;width=50;height=60;"
FillCurlyRectangle()
param = "x=220;y=110;width=50;height=60;"
FillCurlyRectangle()
GraphicsWindow.PenWidth = 2
GraphicsWindow.PenColor = "#000000"
GraphicsWindow.BrushColor = "#FFFFFF"
GraphicsWindow.FillEllipse(160, 150, 18, 18)
GraphicsWindow.DrawEllipse(160, 150, 18, 18)
GraphicsWindow.FillEllipse(212, 150, 18, 18)
GraphicsWindow.DrawEllipse(212, 150, 18, 18)
GraphicsWindow.BrushColor = "#000000"
GraphicsWindow.PenWidth = 0
eyeX[1] = 169
eyeY[1] = 159
eyeWidth[1] = 16
eyeHeight[1] = 16
eye[1] = Shapes.AddEllipse(eyeWidth[1], eyeHeight[1])
eyeX[2] = 221
eyeY[2] = 159
eyeWidth[2] = 16
eyeHeight[2] = 16
eye[2] = Shapes.AddEllipse(eyeWidth[2], eyeHeight[2])
eyeAngle[1] = 0
eyeAngle[2] = 0
rMove = 1
MoveEyes()
GraphicsWindow.PenWidth = 1
GraphicsWindow.PenColor = "#333333"
GraphicsWindow.FillEllipse(182, 180, 20, 15)
GraphicsWindow.DrawEllipse(182, 180, 20, 15)
GraphicsWindow.FillEllipse(184, 187, 7, 7)
GraphicsWindow.DrawEllipse(184, 187, 7, 7)
GraphicsWindow.FillEllipse(193, 187, 7, 7)
GraphicsWindow.DrawEllipse(193, 187, 7, 7)
Controls.AddButton("Next", 570, 408)
buttonClicked = "False"
mouseMove = "False"
While Not[buttonClicked]
If mouseMove Then
CalcAngles()
MoveEyes()
mouseMove = "False"
Else
Program.Delay(200)
EndIf
EndWhile
GraphicsWindow.Clear()
EndWhile

Sub CalcAngles
For i = 1 To 2
x = mx - eyeX[i]
y = my - eyeY[i]
Math_CartesianToPolar()
eyeAngle[i] = a
EndFor
EndSub

Sub DrawCurly
r = 5
dr = 5 / (360 / 10)
a0 = Math.GetRandomNumber(360)
For a = 0 To 360 Step 10
_a = Math.GetRadians(a + a0)
cx = cx + r * a / 360 * Math.Cos(_a)
cy = cy + r * a / 360 * Math.Sin(_a)
_x = cx - r / 2
_y = cy - r / 2
l = (360 - a) / 3
GetColors()
GraphicsWindow.BrushColor = root
GraphicsWindow.FillEllipse(_x, _y, 2*r, 2*r)
GraphicsWindow.BrushColor = tip
GraphicsWindow.FillEllipse(_x + 1, _y + 1, 2*r, 2*r)
r = r - dr
EndFor
EndSub

Sub FillCurlyEllipse
' param["x"], param["y"] - left top corner
' param["width"], param["height"] - size
GetParam()
If color = "Black" Then
GraphicsWindow.BrushColor = "#111111"
ElseIf color = "White" Then
GraphicsWindow.BrushColor = "#DDDDDD"
ElseIf color = "Mocha" Then
GraphicsWindow.BrushColor = "#663300"
EndIf
GraphicsWindow.FillEllipse(x, y, width, height)
If debug Then
Goto fce_exit
EndIf
For i = 1 To (width * height) / (5 * 5)
rx = Math.GetRandomNumber(width / 2)
ry = Math.GetRandomNumber(height / 2)
a = Math.GetRandomNumber(360)
_a = Math.GetRadians(a)
cx = x + width / 2 + rx * Math.Cos(_a)
cy = y + height / 2 + ry * Math.Sin(_a)
DrawCurly()
EndFor
fce_exit:
EndSub

Sub FillCurlyRectangle
' param["x"], param["y"] - left top corner
' param["width"], param["height"] - size
GetParam()
If color = "Black" Then
GraphicsWindow.BrushColor = "#111111"
ElseIf color = "White" Then
GraphicsWindow.BrushColor = "#DDDDDD"
ElseIf color = "Mocha" Then
GraphicsWindow.BrushColor = "#663300"
EndIf
GraphicsWindow.FillRectangle(x, y, width, height)
If debug Then
Goto fcr_exit
EndIf
For i = 1 To (width * height) / (5 * 5)
cx = x + Math.GetRandomNumber(width)
cy = y + Math.GetRandomNumber(height)
DrawCurly()
EndFor
fcr_exit:
EndSub

Sub GetColors
α = (120 - l) * 255 / 120
dec = α
Math_Dec2Hex()
If color = "Black" Then
tip = "#" + hex + "000000"
root = "#" + hex + "222222"
ElseIf color = "White" Then
tip = "#" + hex + "FFFFFF"
root = "#" + hex + "DDDDDD"
ElseIf color = "Mocha" Then
tip = "#" + hex + "663300"
root = "#" + hex + "442200"
EndIf
EndSub

Sub GetParam
x = param["x"]
y = param["y"]
width = param["width"]
height = param["height"]
EndSub

Sub MoveEyes
_a = Math.GetRadians(eyeAngle[1])
x = eyeX[1] + rMove * Math.Cos(_a)
y = eyeY[1] + rMove * Math.Sin(_a)
Shapes.Move(eye[1], x - eyeWidth[1] / 2, y - eyeHeight[1] / 2)
_a = Math.GetRadians(eyeAngle[2])
x = eyeX[2] + rMove * Math.Cos(_a)
y = eyeY[2] + rMove * Math.Sin(_a)
Shapes.Move(eye[2], x - eyeWidth[2] / 2, y - eyeHeight[2] / 2)
EndSub

Sub OnButtonClicked
buttonClicked = "True"
EndSub

Sub OnMouseMove
mouseMove = "True"
mx = GraphicsWindow.MouseX
my = GraphicsWindow.MouseY
EndSub

Sub Math_CartesianToPolar
' Math | convert Cartesian coodinate to polar coordinate
' param x, y - Cartesian coordinate
' return r, a - polar Coordinate (0<=a<360)
r = Math.SquareRoot(x * x + y * y)
If x = 0 And y > 0 Then
a = 90 ' [degree]
ElseIf x = 0 And y < 0 Then
a = -90
ElseIf x = 0 And y = 0 Then
a = 0
Else
a = Math.ArcTan(y / x) * 180 / Math.Pi
EndIf
' at this point -90<=a<=90
If x < 0 Then
a = a + 180
ElseIf x >= 0 And y < 0 Then
a = a + 360
EndIf
' at this point 0<=a<360
EndSub

Sub Math_Dec2Hex
' Math | Convert decimal to hexadecimal
' param dec
' param hex
Stack.PushValue("math", dec)
hex = ""
While dec > 0
mod = Math.Remainder(dec, 16)
hex = Text.Append(Text.GetSubText("0123456789ABCDEF", mod + 1, 1), hex)
dec = Math.Floor(dec / 16)
EndWhile
If Text.GetLength(hex) = 0 Then
hex = "00"
ElseIf Text.GetLength(hex) = 1 Then
hex = Text.Append("0", hex)
EndIf
dec = Stack.PopValue("math")
EndSub