Microsoft Small Basic

Program Listing: HMX882
' Chirstmas Tree 0.1
' Copyright (c) 2012 Nonki Takahashi. All rights reserved.
'
' History:
' 0.1 2012/12/11 Created.
' 0.0 2012/12/11 15:54:28 Generated by Shapes 1.5
'
GraphicsWindow.Title = "Merry Christmas and A Happy New Year 2013"
GraphicsWindow.BackgroundColor = "#111111"

' initialize shapes
Tree_Init()
' add shapes
scale = 1
Shapes_Add()
angle = 0
' add flash lights as shapes
AddFlashLights()
' initialize shapes
scale = 0.7
Robot_Init()
Shapes_Add()

Timer.Interval = 3000
Timer.Tick = BlinkFlashLights

Sub AddFlashLights
fbegin = 21
fend = 46
GraphicsWindow.PenWidth = 0
GraphicsWindow.BrushColor = "Black"
For f = fbegin To fend
flash[f] = Shapes.AddRectangle(shape[f]["width"], shape[f]["height"])
Shapes.Move(flash[f], shape[f]["x"] + shX, shape[f]["y"] + shY)
Shapes.SetOpacity(flash[f], 70) ' flash lights off
EndFor
EndSub

Sub BlinkFlashLights
times = times + 1
If Math.Remainder(times, 10) = 0 Then
y = 230
For x = -200 To 700 Step 5
Shapes_Move()
Program.Delay(30)
EndFor
EndIf
pattern = Math.GetRandomNumber(3)
If pattern = 1 Then
For f = fbegin To fend
Shapes.SetOpacity(flash[f], 0) ' flash lights on
Program.Delay(50)
EndFor
ElseIf pattern = 2 Then
For f = fend To fbegin Step -1
Shapes.SetOpacity(flash[f], 0) ' flash lights on
Program.Delay(50)
EndFor
ElseIf pattern = 3 Then
For f = 1 To (fend - fbegin + 1) * 4
r = Math.GetRandomNumber(fend - fbegin + 1)
Shapes.SetOpacity(flash[r + fbegin - 1], 0) ' flash lights on
Program.Delay(10)
EndFor
EndIf
Program.Delay(200)
For f = fbegin To fend
Shapes.SetOpacity(flash[f], 70) ' flash lights off
EndFor
EndSub

Sub Tree_Init
' Shapes | Initialize shapes data
' return shX, shY - current position of shapes
' return shape - array of shapes
shX = 221 ' x offset
shY = 93 ' y offset
shape = ""
shape[1] = "func=rect;x=44;y=247;width=84;height=22;bc=#670904;pw=0;"
shape[2] = "func=tri;x=50;y=273;x1=8;y1=0;x2=0;y2=56;x3=17;y3=56;angle=180;bc=#670904;pw=0;"
shape[3] = "func=tri;x=104;y=273;x1=8;y1=0;x2=0;y2=56;x3=17;y3=56;angle=180;bc=#670904;pw=0;"
shape[4] = "func=rect;x=59;y=273;width=54;height=53;bc=#670904;pw=0;"
shape[5] = "func=rect;x=74;y=205;width=22;height=38;bc=#3B1E0C;pw=0;"
shape[6] = "func=tri;x=30;y=19;x1=54;y1=0;x2=0;y2=82;x3=109;y3=82;bc=#17330B;pw=0;"
shape[7] = "func=tri;x=16;y=49;x1=69;y1=0;x2=0;y2=98;x3=138;y3=98;bc=#17330B;pw=0;"
shape[8] = "func=tri;x=0;y=89;x1=84;y1=0;x2=0;y2=113;x3=168;y3=113;bc=#17330B;pw=0;"
shape[9] = "func=tri;x=61;y=0;x1=24;y1=0;x2=0;y2=14;x3=49;y3=14;angle=181;bc=#B5925E;pw=0;"
shape[10] = "func=tri;x=60;y=0;x1=24;y1=0;x2=0;y2=14;x3=49;y3=14;angle=252;bc=#B5925E;pw=0;"
shape[11] = "func=tri;x=59;y=0;x1=24;y1=0;x2=0;y2=14;x3=49;y3=14;angle=331;bc=#B5925E;pw=0;"
shape[12] = "func=rect;x=157;y=253;width=77;height=74;bc=#EFD671;pw=0;"
shape[13] = "func=ell;x=170;y=287;width=26;height=28;bc=#B5925E;pw=0;"
shape[14] = "func=ell;x=171;y=263;width=17;height=18;bc=#B5925E;pw=0;"
shape[15] = "func=ell;x=204;y=284;width=21;height=22;bc=#B5925E;pw=0;"
shape[16] = "func=rect;x=187;y=236;width=16;height=91;bc=#B61007;pw=0;"
shape[17] = "func=tri;x=164;y=227;x1=13;y1=0;x2=0;y2=20;x3=27;y3=20;bc=#111111;pc=#B61007;pw=8;"
shape[18] = "func=tri;x=200;y=227;x1=13;y1=0;x2=0;y2=20;x3=27;y3=20;bc=#111111;pc=#B61007;pw=8;"
shape[19] = "func=line;x=166;y=245;x1=26;y1=0;x2=0;y2=21;pc=#B61007;pw=8;"
shape[20] = "func=line;x=197;y=245;x1=0;y1=0;x2=23;y2=23;pc=#B61007;pw=8;"
' flashing lights start
shape[21] = "func=rect;x=100;y=57;width=8;height=8;bc=#0029CC;pw=0;"
shape[22] = "func=rect;x=91;y=68;width=9;height=9;bc=#0029CC;pw=0;"
shape[23] = "func=rect;x=78;y=75;width=10;height=8;bc=#0029CC;pw=0;"
shape[24] = "func=rect;x=67;y=82;width=10;height=9;bc=#0029CC;pw=0;"
shape[25] = "func=rect;x=52;y=86;width=10;height=10;bc=#0029CC;pw=0;"
shape[26] = "func=rect;x=39;y=86;width=10;height=8;bc=#0029CC;pw=0;"
shape[27] = "func=rect;x=121;y=83;width=10;height=10;bc=#0029CC;pw=0;"
shape[28] = "func=rect;x=114;y=95;width=9;height=9;bc=#0029CC;pw=0;"
shape[29] = "func=rect;x=102;y=106;width=8;height=9;bc=#0029CC;pw=0;"
shape[30] = "func=rect;x=92;y=117;width=10;height=8;bc=#0029CC;pw=0;"
shape[31] = "func=rect;x=82;y=125;width=8;height=10;bc=#0029CC;pw=0;"
shape[32] = "func=rect;x=71;y=133;width=8;height=10;bc=#0029CC;pw=0;"
shape[33] = "func=rect;x=57;y=138;width=10;height=10;bc=#0029CC;pw=0;"
shape[34] = "func=rect;x=43;y=138;width=10;height=10;bc=#0029CC;pw=0;"
shape[35] = "func=rect;x=31;y=137;width=8;height=9;bc=#0029CC;pw=0;"
shape[36] = "func=rect;x=26;y=127;width=10;height=8;bc=#0029CC;pw=0;"
shape[37] = "func=rect;x=134;y=126;width=8;height=10;bc=#0029CC;pw=0;"
shape[38] = "func=rect;x=125;y=135;width=8;height=10;bc=#0029CC;pw=0;"
shape[39] = "func=rect;x=115;y=145;width=8;height=10;bc=#0029CC;pw=0;"
shape[40] = "func=rect;x=105;y=154;width=9;height=10;bc=#0029CC;pw=0;"
shape[41] = "func=rect;x=93;y=163;width=10;height=10;bc=#0029CC;pw=0;"
shape[42] = "func=rect;x=80;y=170;width=10;height=10;bc=#0029CC;pw=0;"
shape[43] = "func=rect;x=66;y=174;width=10;height=9;bc=#0029CC;pw=0;"
shape[44] = "func=rect;x=51;y=176;width=9;height=9;bc=#0029CC;pw=0;"
shape[45] = "func=rect;x=37;y=175;width=8;height=9;bc=#0029CC;pw=0;"
shape[46] = "func=rect;x=22;y=173;width=10;height=10;bc=#0029CC;pw=0;"
' flashing lights end
shape[47] = "func=ell;x=79;y=185;width=23;height=24;bc=#969696;pw=0;"
shape[48] = "func=ell;x=93;y=126;width=23;height=24;bc=#969696;pw=0;"
shape[49] = "func=ell;x=41;y=149;width=23;height=24;bc=#969696;pw=0;"
shape[50] = "func=ell;x=41;y=101;width=23;height=24;bc=#969696;pw=0;"
shape[51] = "func=ell;x=138;y=138;width=23;height=24;bc=#969696;pw=0;"
shape[52] = "func=ell;x=57;y=41;width=23;height=24;bc=#B5925E;pw=0;"
shape[53] = "func=ell;x=80;y=88;width=23;height=24;bc=#B5925E;pw=0;"
shape[54] = "func=ell;x=20;y=191;width=23;height=24;bc=#B5925E;pw=0;"
shape[55] = "func=ell;x=124;y=176;width=23;height=24;bc=#B5925E;pw=0;"
EndSub

Sub Robot_Init
' Shapes | Initialize shapes data
' return shX, shY - current position of shapes
' return shape - array of shapes
shX = -200 ' x offset
shY = 230 ' y offset
shape = ""
shape[1] = "func=ell;x=4;y=10;width=129;height=129;bc=#939393;pc=#000000;pw=2;"
shape[2] = "func=rect;x=30;y=70;width=131;height=158;angle=340;bc=#DEDEDE;pc=#000000;pw=2;"
shape[3] = "func=tri;x=0;y=238;x1=28;y1=0;x2=0;y2=49;x3=57;y3=49;bc=#DEDEDE;pc=#000000;pw=2;"
shape[4] = "func=tri;x=101;y=237;x1=28;y1=0;x2=0;y2=49;x3=57;y3=49;bc=#DEDEDE;pc=#000000;pw=2;"
shape[5] = "func=ell;x=9;y=225;width=37;height=37;bc=#DEDEDE;pc=#000000;pw=2;"
shape[6] = "func=ell;x=110;y=220;width=37;height=37;bc=#DEDEDE;pc=#000000;pw=2;"
shape[7] = "func=rect;x=31;y=133;width=30;height=102;angle=17;bc=#DEDEDE;pc=#000000;pw=2;"
shape[8] = "func=ell;x=49;y=77;width=46;height=47;bc=#DEDEDE;pc=#000000;pw=2;"
shape[9] = "func=rect;x=41;y=99;width=46;height=60;angle=16;bc=#DEDEDE;pw=0;"
shape[10] = "func=line;x=33;y=94;x1=18;y1=0;x2=0;y2=56;pc=#000000;pw=2;"
shape[11] = "func=line;x=77;y=105;x1=17;y1=0;x2=0;y2=59;pc=#000000;pw=2;"
shape[12] = "func=line;x=34;y=151;x1=0;y1=0;x2=43;y2=15;pc=#000000;pw=2;"
shape[13] = "func=tri;x=67;y=236;x1=16;y1=0;x2=0;y2=24;x3=33;y3=24;angle=159;bc=#DEDEDE;pc=#000000;pw=2;"
shape[14] = "func=tri;x=154;y=205;x1=16;y1=0;x2=0;y2=24;x3=33;y3=24;angle=159;bc=#DEDEDE;pc=#000000;pw=2;"
shape[15] = "func=rect;x=83;y=221;width=94;height=23;angle=340;bc=#DEDEDE;pw=0;"
shape[16] = "func=rect;x=110;y=85;width=33;height=18;angle=340;bc=#1C235E;pw=0;"
shape[17] = "func=rect;x=119;y=110;width=33;height=18;angle=340;bc=#1C235E;pw=0;"
shape[18] = "func=rect;x=3;y=64;width=126;height=9;angle=340;bc=#1C235E;pw=0;"
shape[19] = "func=rect;x=28;y=43;width=62;height=18;angle=340;bc=#1C235E;pw=0;"
shape[20] = "func=line;x=89;y=229;x1=0;y1=30;x2=90;y2=0;pc=#000000;pw=2;"
shape[21] = "func=ell;x=59;y=95;width=22;height=22;bc=#000000;pc=#000000;pw=2;"
shape[22] = "func=rect;x=47;y=107;width=11;height=114;angle=17;bc=#000000;pc=#000000;pw=2;"
shape[23] = "func=rect;x=113;y=173;width=33;height=39;angle=340;bc=#DEDEDE;pc=#000000;pw=2;"
shape[24] = "func=line;x=152;y=212;x1=0;y1=0;x2=3;y2=25;pc=#000000;pw=2;"
shape[25] = "func=line;x=95;y=232;x1=0;y1=0;x2=11;y2=20;pc=#000000;pw=2;"
shape[26] = "func=line;x=170;y=205;x1=1;y1=0;x2=0;y2=27;pc=#000000;pw=2;"
shape[27] = "func=line;x=78;y=239;x1=0;y1=0;x2=17;y2=16;pc=#000000;pw=2;"
shape[28] = "func=rect;x=119;y=22;width=17;height=22;angle=326;bc=#000000;pc=#000000;pw=2;"
shape[29] = "func=rect;x=97;y=0;width=12;height=31;angle=301;bc=#000000;pc=#000000;pw=2;"
shape[30] = "func=rect;x=11;y=57;width=13;height=20;angle=351;bc=#1C235E;pw=0;"
shape[31] = "func=rect;x=94;y=27;width=13;height=20;angle=323;bc=#1C235E;pw=0;"
EndSub

Sub Shapes_Add
' Shapes | Add shapes as shapes data
' param shape - array of shapes
' param scale - to zoom
' return nShapes - number of shapes
' return shAngle - current angle of shapes
Stack.PushValue("local", i)
nShapes = Array.GetItemCount(shape)
s = scale
For i = 1 To nShapes
GraphicsWindow.PenWidth = shape[i]["pw"] * s
If shape[i]["pw"] > 0 Then
GraphicsWindow.PenColor = shape[i]["pc"]
EndIf
If shape[i]["func"] = "rect" Then
GraphicsWindow.BrushColor = shape[i]["bc"]
shape[i]["obj"] = Shapes.AddRectangle(shape[i]["width"]* s, shape[i]["height"] * s)
ElseIf shape[i]["func"] = "ell" Then
GraphicsWindow.BrushColor = shape[i]["bc"]
shape[i]["obj"] = Shapes.AddEllipse(shape[i]["width"]* s, shape[i]["height"] * s)
ElseIf shape[i]["func"] = "tri" Then
GraphicsWindow.BrushColor = shape[i]["bc"]
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)
EndIf
Shapes.Move(shape[i]["obj"], shX + shape[i]["x"] * s, shY + shape[i]["y"] * s)
If Text.IsSubText("rect|ell|tri", shape[i]["func"]) And shape[i]["angle"] <> 0 Then
Shapes.Rotate(shape[i]["obj"], shape[i]["angle"])
EndIf
shape[i]["rx"] = shape[i]["x"]
shape[i]["ry"] = shape[i]["y"]
EndFor
shAngle = 0
i = Stack.PopValue("local")
EndSub

Sub Shapes_CalcWidthAndHeight
' Shapes | Calculate total width and height of shapes
' return nShapes - number of shapes
' return shWidth, shHeight - total size of shapes
nShapes = Array.GetItemCount(shape)
For i = 1 To nShapes
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_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 shapes
' 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_Move
' Shapes | Move shapes
' 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)
Stack.PushValue("local", x)
Stack.PushValue("local", y)
dx = x - shX
dy = y - shY
shX = x
shY = y
For i = 1 To nShapes
_x = shape[i]["rx"] + dx
_y = shape[i]["ry"] + dy
Shapes.Move(shape[i]["obj"], shX + _x * s, shY + _y * s)
EndFor
y = Stack.PopValue("local")
x = Stack.PopValue("local")
i = Stack.PopValue("local")
EndSub

Sub Shapes_Remove
' Shapes | Remove shapes
' param shape - array of shapes
Stack.PushValue("local", i)
For i = 1 To nShapes
Shapes.Remove(shape[i]["obj"])
EndFor
i = Stack.PopValue("local")
EndSub

Sub Shapes_Rotate
' Shapes | Rotate shapes
' 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 = 1 To nShapes
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
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

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