Microsoft Small Basic

Program Listing: LKB431
' Aliens 0.1
' 2014-03-17 23:17:22 Shapes generated by Shapes 1.6b.
'
' initialize shapes
GraphicsWindow.BackgroundColor = "DimGray"
GraphicsWindow.Title = "Aliens 0.1"
SB_Workaround()
Shapes_Init()
' add shapes
scale = 0.8
angle = 0
iMin = 1
iMax = 34
Shapes_Add()
x = 80
y = 100
Shapes_Move()
For i = iMin To iMax
shape[i + 34] = shape[i]
shape[i + 68] = shape[i]
EndFor
eyes = "e1=23;e2=24;e3=25;i1=26;i2=27;i3=28;"
For i = 1 To 3
eyes["x" + i] = 80 + shape[eyes["e" + i]]["x"] * scale
eyes["y" + i] = 100 + shape[eyes["e" + i]]["y"] * scale
eyes["e" + (i + 3)] = eyes["e" + i] + 34
eyes["i" + (i + 3)] = eyes["i" + i] + 34
eyes["x" + (i + 3)] = 240 + shape[eyes["e" + i]]["x"] * scale
eyes["y" + (i + 3)] = 120 + shape[eyes["e" + i]]["y"] * scale
eyes["e" + (i + 6)] = eyes["e" + i] + 68
eyes["i" + (i + 6)] = eyes["i" + i] + 68
eyes["x" + (i + 6)] = 400 + shape[eyes["e" + i]]["x"] * scale
eyes["y" + (i + 6)] = 100 + shape[eyes["e" + i]]["y"] * scale
EndFor
iMin = 35
iMax = 68
Shapes_Add()
x = 240
y = 120
Shapes_Move()
iMin = 69
iMax = 102
Shapes_Add()
x = 400
y = 100
Shapes_Move()
moved = "False"
GraphicsWindow.MouseMove = OnMouseMove
While "True"
If moved Then
MoveEyes()
Else
Program.Delay(1000 / 24)
EndIf
EndWhile
Sub MoveEyes
For i = 1 To 9
ir = shape[eyes["i" + i]]["width"] / 2 * scale
er = shape[eyes["e" + i]]["width"] / 2 * scale
ex = eyes["x" + i] + er
ey = eyes["y" + i] + er
x = mx - ex
y = my - ey
Math_CartesianToPolar()
_a = Math.GetRadians(a)
r = (er - ir) * 0.7
ix = ex + r * Math.Cos(_a)
iy = ey + r * Math.Sin(_a)
Shapes.Move(shape[eyes["i" + i]]["obj"], ix - ir, iy - ir)
EndFor
EndSub
Sub OnMouseMove
mx = GraphicsWindow.MouseX
my = GraphicsWindow.MouseY
moved = "True"
EndSub
Sub Shapes_Init
' Shapes | Initialize shapes data
' return shX, shY - current position of shapes
' return shape - array of shapes
shX = 220 ' x offset
shY = 100 ' y offset
shape = ""
shape[1] = "func=ell;x=0;y=53;width=21;height=50;bc=#B2E34F;pc=#000000;pw=0;rem=ear;"
shape[2] = "func=ell;x=176;y=49;width=20;height=53;bc=#B2E34F;pc=#000000;pw=0;rem=ear;"
shape[3] = "func=ell;x=88;y=0;width=17;height=18;bc=#B2E34F;pc=#000000;pw=0;rem=horn;"
shape[4] = "func=rect;x=91;y=15;width=10;height=22;bc=#B2E34F;pc=#000000;pw=0;rem=horn;"
shape[5] = "func=ell;x=10;y=35;width=175;height=111;bc=#B2E34F;pc=#000000;pw=0;rem=face;"
shape[6] = "func=ell;x=45;y=66;width=102;height=51;bc=#B2E34F;pc=#333333;pw=2;rem=mouth;"
shape[7] = "func=rect;x=34;y=62;width=127;height=38;bc=#B2E34F;pw=0;rem=mouth;"
shape[8] = "func=ell;x=5;y=136;width=31;height=81;bc=#23ADD7;pc=#000000;pw=0;rem=arm;"
shape[9] = "func=ell;x=154;y=135;width=36;height=82;bc=#23ADD7;pc=#000000;pw=0;rem=arm;"
shape[10] = "func=ell;x=9;y=246;width=83;height=29;angle=350;bc=#154784;pc=#000000;pw=0;rem=foot;"
shape[11] = "func=ell;x=101;y=245;width=83;height=29;angle=7;bc=#154784;pc=#000000;pw=0;rem=foot;"
shape[12] = "func=rect;x=100;y=230;width=72;height=25;bc=#23ADD7;pc=#000000;pw=0;rem=leg;"
shape[13] = "func=rect;x=21;y=231;width=72;height=23;bc=#23ADD7;pc=#000000;pw=0;rem=leg;"
shape[14] = "func=rect;x=21;y=136;width=152;height=97;bc=#23ADD7;pc=#000000;pw=0;rem=body;"
shape[15] = "func=rect;x=20;y=197;width=157;height=15;bc=#154784;pc=#000000;pw=0;rem=belt;"
shape[16] = "func=rect;x=77;y=192;width=41;height=28;bc=#0B2646;pc=#000000;pw=0;rem=backle;"
shape[17] = "func=ell;x=30;y=180;width=37;height=18;bc=#B2E34F;pc=#000000;pw=0;rem=finger;"
shape[18] = "func=ell;x=30;y=196;width=36;height=15;bc=#B2E34F;pc=#000000;pw=0;rem=finger;"
shape[19] = "func=ell;x=29;y=207;width=36;height=17;bc=#B2E34F;pc=#000000;pw=0;rem=finger;"
shape[20] = "func=ell;x=135;y=182;width=34;height=17;bc=#B2E34F;pc=#000000;pw=0;rem=finger;"
shape[21] = "func=ell;x=133;y=195;width=38;height=14;bc=#B2E34F;pc=#000000;pw=0;rem=finger;"
shape[22] = "func=ell;x=134;y=207;width=35;height=16;angle=349;bc=#B2E34F;pc=#000000;pw=0;finger;"
shape[23] = "func=ell;x=30;y=51;width=44;height=44;bc=#FFFFFF;pc=#000000;pw=0;rem=eye;"
shape[24] = "func=ell;x=75;y=38;width=44;height=44;bc=#FFFFFF;pc=#000000;pw=0;rem=eye;"
shape[25] = "func=ell;x=121;y=51;width=44;height=44;bc=#FFFFFF;pc=#000000;pw=0;rem=eye;"
shape[26] = "func=ell;x=51;y=57;width=18;height=18;bc=#000000;pc=#000000;pw=2;rem=iris;"
shape[27] = "func=ell;x=96;y=44;width=18;height=18;bc=#000000;pc=#000000;pw=2;rem=iris;"
shape[28] = "func=ell;x=139;y=56;width=18;height=18;bc=#000000;pc=#000000;pw=2;rem=iris;"
shape[29] = "func=rect;x=24;y=125;width=146;height=13;bc=#B84AC9;pc=#000000;pw=0;rem=neck;"
shape[30] = "func=ell;x=13;y=186;width=30;height=33;bc=#B2E34F;pc=#000000;pw=0;rem=hand;"
shape[31] = "func=ell;x=151;y=186;width=30;height=33;bc=#B2E34F;pc=#000000;pw=0;rem=hand;"
shape[32] = "func=ell;x=118;y=159;width=40;height=11;angle=334;bc=#23ADD7;pc=#D8D822;pw=2;rem=mark;"
shape[33] = "func=ell;x=127;y=154;width=20;height=22;bc=#23ADD7;pc=#D86822;pw=2;rem=mark;"
shape[34] = "func=line;x=128;y=164;x1=0;y1=10;x2=21;y2=0;pc=#D8D822;pw=2;rem=mark;"
EndSub
Sub Math_CartesianToPolar
' Math | convert cartesian coodinate to polar coordinate
' param x, y - cartesian coordinate
' return r, a - polar coordinate
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
Else
a = Math.ArcTan(y / x) * 180 / Math.Pi
EndIf
If x < 0 Then
a = a + 180
ElseIf x > 0 And y < 0 Then
a = a + 360
EndIf
EndSub
Sub SB_RotateWorkaround
' Small Basic | Rotate workaround for Silverlight
' param x, y - original coordinate
' param alpha - angle [radian]
' returns x, y - workaround coordinate
If shape[i]["func"] = "tri" Then
x1 = -Math.Floor(shape[i]["x3"] / 2)
y1 = -Math.Floor(shape[i]["y3"] / 2)
ElseIf shape[i]["func"] = "line" Then
x1 = -Math.Floor(Math.Abs(shape[i]["x1"] - shape[i]["x2"]) / 2)
y1 = -Math.Floor(Math.Abs(shape[i]["y1"] - shape[i]["y2"]) / 2)
EndIf
ox = x - x1
oy = y - y1
x = x1 * Math.Cos(alpha) - y1 * Math.Sin(alpha) + ox
y = x1 * Math.Sin(alpha) + y1 * Math.Cos(alpha) + oy
EndSub
Sub SB_Workaround
' Small Basic | Workaround for Silverlight
' returns silverlight - "True" if in remote
color = GraphicsWindow.GetPixel(0, 0)
If Text.GetLength(color) > 7 Then
silverlight = "True"
msWait = 300
Else
silverlight = "False"
EndIf
EndSub
Sub Shapes_Add
' Shapes | add shapes as shapes data
' param iMin, iMax - shape indices to add
' param shape - array of shapes
' param scale - 1 if same scale
' return shWidth, shHeight - total size of shapes
' return shAngle - current angle of shapes
Stack.PushValue("local", i)
Stack.PushValue("local", x)
Stack.PushValue("local", y)
Shapes_CalcWidthAndHeight()
s = scale
For i = iMin To iMax
GraphicsWindow.PenWidth = shape[i]["pw"] * s
If shape[i]["pw"] > 0 Then
GraphicsWindow.PenColor = shape[i]["pc"]
EndIf
If Text.IsSubText("rect|ell|tri|text", shape[i]["func"]) Then
GraphicsWindow.BrushColor = shape[i]["bc"]
EndIf
If shape[i]["func"] = "rect" Then
shape[i]["obj"] = Shapes.AddRectangle(shape[i]["width"] * s, shape[i]["height"] * s)
ElseIf shape[i]["func"] = "ell" Then
shape[i]["obj"] = Shapes.AddEllipse(shape[i]["width"] * s, shape[i]["height"] * s)
ElseIf shape[i]["func"] = "tri" Then
shape[i]["obj"] = Shapes.AddTriangle(shape[i]["x1"] * s, shape[i]["y1"] * s, shape[i]["x2"] * s, shape[i]["y2"] * s, shape[i]["x3"] * s, shape[i]["y3"] * s)
ElseIf shape[i]["func"] = "line" Then
shape[i]["obj"] = Shapes.AddLine(shape[i]["x1"] * s, shape[i]["y1"] * s, shape[i]["x2"] * s, shape[i]["y2"] * s)
ElseIf shape[i]["func"] = "text" Then
If silverlight Then
fs = Math.Floor(shape[i]["fs"] * 0.9)
Else
fs = shape[i]["fs"]
EndIf
GraphicsWindow.FontSize = fs * s
GraphicsWindow.FontName = shape[i]["fn"]
shape[i]["obj"] = Shapes.AddText(shape[i]["text"])
EndIf
x = shape[i]["x"]
y = shape[i]["y"]
shape[i]["rx"] = x
shape[i]["ry"] = y
If silverlight And Text.IsSubText("tri|line", shape[i]["func"]) Then
alpha = Math.GetRadians(shape[i]["angle"])
SB_RotateWorkaround()
shape[i]["wx"] = x
shape[i]["wy"] = y
EndIf
Shapes.Move(shape[i]["obj"], shX + x * s, shY + y * s)
If Text.IsSubText("rect|ell|tri|text", shape[i]["func"]) And shape[i]["angle"] <> 0 Then
Shapes.Rotate(shape[i]["obj"], shape[i]["angle"])
EndIf
EndFor
shAngle = 0
y = Stack.PopValue("local")
x = Stack.PopValue("local")
i = Stack.PopValue("local")
EndSub
Sub Shapes_CalcRotatePos
' Shapes | Calculate position for rotated shape
' param["x"], param["y"] - position of a shape
' param["width"], param["height"] - size of a shape
' param ["cx"], param["cy"] - center of rotation
' param ["angle"] - rotate angle
' return x, y - rotated position of a shape
_cx = param["x"] + param["width"] / 2
_cy = param["y"] + param["height"] / 2
x = _cx - param["cx"]
y = _cy - param["cy"]
Math_CartesianToPolar()
a = a + param["angle"]
x = r * Math.Cos(a * Math.Pi / 180)
y = r * Math.Sin(a * Math.Pi / 180)
_cx = x + param["cx"]
_cy = y + param["cy"]
x = _cx - param["width"] / 2
y = _cy - param["height"] / 2
EndSub
Sub Shapes_CalcWidthAndHeight
' Shapes | Calculate total width and height of shapes
' param iMin, iMax - shape indices to add
' return shWidth, shHeight - total size of shapes
For i = iMin To iMax
If shape[i]["func"] = "tri" Or shape[i]["func"] = "line" Then
xmin = shape[i]["x1"]
xmax = shape[i]["x1"]
ymin = shape[i]["y1"]
ymax = shape[i]["y1"]
If shape[i]["x2"] < xmin Then
xmin = shape[i]["x2"]
EndIf
If xmax < shape[i]["x2"] Then
xmax = shape[i]["x2"]
EndIf
If shape[i]["y2"] < ymin Then
ymin = shape[i]["y2"]
EndIf
If ymax < shape[i]["y2"] Then
ymax = shape[i]["y2"]
EndIf
If shape[i]["func"] = "tri" Then
If shape[i]["x3"] < xmin Then
xmin = shape[i]["x3"]
EndIf
If xmax < shape[i]["x3"] Then
xmax = shape[i]["x3"]
EndIf
If shape[i]["y3"] < ymin Then
ymin = shape[i]["y3"]
EndIf
If ymax < shape[i]["y3"] Then
ymax = shape[i]["y3"]
EndIf
EndIf
shape[i]["width"] = xmax - xmin
shape[i]["height"] = ymax - ymin
EndIf
If i = 1 Then
shWidth = shape[i]["x"] + shape[i]["width"]
shHeight = shape[i]["y"] + shape[i]["height"]
Else
If shWidth < shape[i]["x"] + shape[i]["width"] Then
shWidth = shape[i]["x"] + shape[i]["width"]
EndIf
If shHeight < shape[i]["y"] + shape[i]["height"] Then
shHeight = shape[i]["y"] + shape[i]["height"]
EndIf
EndIf
EndFor
EndSub
Sub Shapes_Move
' Shapes | Move shapes
' param iMin, iMax - shape indices to add
' param shape - array of shapes
' param scale - to zoom
' param x, y - position to move
' return shX, shY - new position of shapes
Stack.PushValue("local", i)
s = scale
shX = x
shY = y
For i = iMin To iMax
If silverlight And Text.IsSubText("tri|line", shape[i]["func"]) Then
_x = shape[i]["wx"]
_y = shape[i]["wy"]
Else
_x = shape[i]["rx"]
_y = shape[i]["ry"]
EndIf
Shapes.Move(shape[i]["obj"], shX + _x * s, shY + _y * s)
EndFor
i = Stack.PopValue("local")
EndSub
Sub Shapes_Remove
' Shapes | Remove shapes
' param iMin, iMax - shapes indices to remove
' param shape - array of shapes
Stack.PushValue("local", i)
For i = iMin To iMax
Shapes.Remove(shape[i]["obj"])
EndFor
i = Stack.PopValue("local")
EndSub
Sub Shapes_Rotate
' Shapes | Rotate shapes
' param iMin, iMax - shapes indices to rotate
' param shape - array of shapes
' param scale - to zoom
' param angle - to rotate
Stack.PushValue("local", i)
Stack.PushValue("local", x)
Stack.PushValue("local", y)
s = scale
param["angle"] = angle
param["cx"] = shWidth / 2
param["cy"] = shHeight / 2
For i = iMin To iMax
param["x"] = shape[i]["x"]
param["y"] = shape[i]["y"]
param["width"] = shape[i]["width"]
param["height"] = shape[i]["height"]
Shapes_CalcRotatePos()
shape[i]["rx"] = x
shape[i]["ry"] = y
If silverlight And Text.IsSubText("tri|line", shape[i]["func"]) Then
alpha = Math.GetRadians(angle + shape[i]["angle"])
SB_RotateWorkAround()
shape[i]["wx"] = x
shape[i]["wy"] = y
EndIf
Shapes.Move(shape[i]["obj"], shX + x * s, shY + y * s)
Shapes.Rotate(shape[i]["obj"], angle + shape[i]["angle"])
EndFor
y = Stack.PopValue("local")
x = Stack.PopValue("local")
i = Stack.PopValue("local")
EndSub