Microsoft Small Basic

Program Listing: GDD325-11
' Halloween
' Copyright (c) 2014 Nonki Takahashi. The MIT License.
' 2014-10-27 15:47:30 Shapes generated by Shapes 1.7b.
' Version 1.1
' Last update 2014-10-30
' Program ID GDD325-11
'
Not = "True=False;False=True;"
SB_Workaround()
gw = 598
gh = 428
GraphicsWindow.Width = gw
GraphicsWindow.Height = gh
GraphicsWindow.Title = "Halloween"
GraphicsWindow.BackgroundColor = "#333333"
GraphicsWindow.FontName = "Trebuchet MS"
GraphicsWindow.FontSize = 40
GraphicsWindow.BrushColor = "Black"
trick = Controls.AddButton("TRICK", 10, 10)
GraphicsWindow.BrushColor = "LightGray"
orText = Shapes.AddText("OR")
Shapes.Move(orText, 280, 16)
GraphicsWindow.BrushColor = "Black"
treat = Controls.AddButton("TREAT", 455, 10)
' initialize shapes
Shapes_Init_Jack()
' add shapes
scale = 1
angle = 0
iMin = 1
iMax = 22
Shapes_Add()
clicked = "False"
Controls.ButtonClicked = OnButtonClicked
While Not[clicked]
Program.Delay(500)
EndWhile
Shapes_Remove()
Controls.Remove(trick)
Shapes.Remove(orText)
Controls.Remove(treat)
If Controls.LastClickedButton = trick Then
GraphicsWindow.BackgroundColor = "Purple"
' draw moon
GraphicsWindow.BrushColor = "White"
size = 100
x = gw - size - 10
y = 10
GraphicsWindow.FillEllipse(x, y, size, size)
' draw ground
GraphicsWindow.BrushColor = "Black"
GraphicsWindow.FillRectangle(0, gh - 110, gw, 110)
' draw tree
DrawTree()
' draw Jack-o'-lanterns
jx = "1=16;2=116;3=216;4=316;5=416;6=516;"
jy = "1=300;2=300;3=300;4=300;5=300;6=300;"
url = "http://gallery.technet.microsoft.com/site/view/file/127749/1/LightJack.png"
img = ImageList.LoadImage(url)
jw = ImageList.GetWidthOfImage(img)
jh = ImageList.GetHeightOfImage(img)
For i = 1 To 6
jack[i] = Shapes.AddImage(img)
s = 1
Shapes.Zoom(jack[i], s, s)
Shapes.Move(jack[i], jx[i] + (s - 1) * jw, jy[i] + (s - 1) * jh)
EndFor
' draw caption
GraphicsWindow.BrushColor = "Orange"
halloween = Shapes.AddText("Happy Halloween")
x = 10
y = gh - 60
Shapes.Move(halloween, x, y)
' draw bats
n = 9
fps = 10
ShowBats()
Else
GraphicsWindow.BackgroundColor = "Orange"
url = "http://gallery.technet.microsoft.com/site/view/file/127746/1/Candy1.png"
candy1 = Shapes.AddImage(url)
If silverlight Then
Program.Delay(msWait)
EndIf
x = 10
y = 40
Shapes.Move(candy1, x, y)
Shapes.Rotate(candy1, -10)
url = "http://gallery.technet.microsoft.com/site/view/file/127747/1/Candy2.png"
candy2 = Shapes.AddImage(url)
x = 210
Shapes.Move(candy2, x, y)
x = 410
url = "http://gallery.technet.microsoft.com/site/view/file/127748/1/Candy3.png"
candy3 = Shapes.AddImage(url)
If silverlight Then
Program.Delay(msWait)
EndIf
Shapes.Move(candy3, x, y)
Shapes.Rotate(candy3, 10)
GraphicsWindow.BrushColor = "Black"
halloween = Shapes.AddText("Happy Halloween")
x = 10
y = gh - 60
Shapes.Move(halloween, x, y)
EndIf
Sub DrawTree
pw = 20 ' pen width
dl = 10 ' delta length
branch[1]["x1"] = 100
branch[1]["y1"] = gh - 110
branch[1]["angle"] = -90
GraphicsWindow.PenColor = "Black"
For j = 1 To 20
If silverlight Then ' for known issue 21649
Program.Delay(msWait)
EndIf
GraphicsWindow.PenWidth = pw
n = Array.GetItemCount(branch)
m = 0
For i = 1 To n
If Math.GetRandomNumber(100) < 15 Then
_a1 = Math.GetRadians(branch[i]["angle"] - 20)
branch[i]["x2"] = branch[i]["x1"] + dl * Math.Cos(_a1)
branch[i]["y2"] = branch[i]["y1"] + dl * Math.Sin(_a1)
branch[i]["angle"] = branch[i]["angle"] - 20 + Math.GetRandomNumber(11) - 6
GraphicsWindow.DrawLine(branch[i]["x1"], branch[i]["y1"], branch[i]["x2"], branch[i]["y2"])
branch[i]["x1"] = branch[i]["x2"]
branch[i]["y1"] = branch[i]["y2"]
_a2 = Math.GetRadians(branch[i]["angle"] + 20)
m = m + 1
branch[n + m]["x2"] = branch[i]["x1"] + dl * Math.Cos(_a2)
branch[n + m]["y2"] = branch[i]["y1"] + dl * Math.Sin(_a2)
branch[n + m]["angle"] = branch[i]["angle"] + 20 + Math.GetRandomNumber(11) - 6
GraphicsWindow.DrawLine(branch[i]["x1"], branch[i]["y1"], branch[n + m]["x2"], branch[n + m]["y2"])
branch[n + m]["x1"] = branch[n + m]["x2"]
branch[n + m]["y1"] = branch[n + m]["y2"]
Else
_a = Math.GetRadians(branch[i]["angle"])
branch[i]["x2"] = branch[i]["x1"] + dl * Math.Cos(_a)
branch[i]["y2"] = branch[i]["y1"] + dl * Math.Sin(_a)
branch[i]["angle"] = branch[i]["angle"] + Math.GetRandomNumber(11) - 6
GraphicsWindow.DrawLine(branch[i]["x1"], branch[i]["y1"], branch[i]["x2"], branch[i]["y2"])
branch[i]["x1"] = branch[i]["x2"]
branch[i]["y1"] = branch[i]["y2"]
EndIf
EndFor
pw = pw - 1
EndFor
EndSub
Sub ShowBats
url = "http://gallery.technet.microsoft.com/site/view/file/127750/1/OpenBat.png"
oImg = ImageList.LoadImage(url)
If silverlight Then
ow = 63
oh = 30
Else
ow = ImageList.GetWidthOfImage(oImg)
oh = ImageList.GetHeightOfImage(oImg)
EndIf
url = "http://gallery.technet.microsoft.com/site/view/file/127751/1/CloseBat.png"
cImg = ImageList.LoadImage(url)
If silverlight Then
cw = 26
ch = 26
Else
cw = ImageList.GetWidthOfImage(cImg)
ch = ImageList.GetHeightOfImage(cImg)
EndIf
For i = 1 To n
bat[i]["open"] = Shapes.AddImage(oImg)
bat[i]["close"] = Shapes.AddImage(cImg)
bat[i]["cycle"] = Math.GetRandomNumber(4)
bat[i]["x"] = Math.GetRandomNumber(gw - ow) + ow / 2
bat[i]["y"] = Math.GetRandomNumber(gh - 110 - oh) + oh / 2
bat[i]["angle"] = Math.GetRandomNumber(21) - 11
EndFor
tick = "False"
Timer.Interval = 1000 / fps
Timer.Tick = OnTick
While "True"
If tick Then
UpdateBats()
tick = "False"
Else
Program.Delay(1000 / fps)
EndIf
EndWhile
EndSub
Sub UpdateBats
For i = 1 To n
bat[i]["cycle"] = bat[i]["cycle"] + 1
If 4 < bat[i]["cycle"] Then
bat[i]["cycle"] = 1
EndIf
bat[i]["x"] = bat[i]["x"] + Math.GetRandomNumber(7) - 4
If bat[i]["x"] < ow / 2 Then
bat[i]["x"] = ow / 2
ElseIf (gw - ow / 2) < bat[i]["x"] Then
bat[i]["x"] = gw - ow / 2
EndIf
bat[i]["y"] = bat[i]["y"] + Math.GetRandomNumber(17) - 9
If bat[i]["y"] < oh / 2 Then
bat[i]["y"] = oh / 2
ElseIf (gh - 110 - oh / 2) < bat[i]["y"] Then
bat[i]["y"] = gh - 110 - oh / 2
EndIf
bat[i]["angle"] = bat[i]["angle"] + Math.GetRandomNumber(7) - 4
If bat[i]["angle"] < -10 Then
bat[i]["angle"] = -10
ElseIf 10 < bat[i]["angle"] Then
bat[i]["angle"] = 10
EndIf
If bat[i]["cycle"] = 1 Then
Shapes.SetOpacity(bat[i]["close"], 0)
Shapes.Move(bat[i]["open"], bat[i]["x"] - ow / 2, bat[i]["y"] - oh / 2)
Shapes.Rotate(bat[i]["open"], bat[i]["angle"])
Shapes.SetOpacity(bat[i]["open"], 100)
ElseIf bat[i]["cycle"] = 4 Then
Shapes.SetOpacity(bat[i]["open"], 0)
Shapes.Move(bat[i]["close"], bat[i]["x"] - cw / 2, bat[i]["y"] - ch / 2)
Shapes.Rotate(bat[i]["close"], bat[i]["angle"])
Shapes.SetOpacity(bat[i]["close"], 100)
Else
Shapes.Move(bat[i]["open"], bat[i]["x"] - ow / 2, bat[i]["y"] - oh / 2)
Shapes.Rotate(bat[i]["open"], bat[i]["angle"])
EndIf
EndFor
EndSub
Sub OnButtonClicked
clicked = "True"
EndSub
Sub OnTick
tick = "True"
EndSub
Sub Shapes_Init_Jack
' Shapes | Initialize Jack shapes data
' return shX, shY - current position of shapes
' return shape - array of shapes
shX = 146 ' x offset
shY = 102 ' y offset
shape = ""
shape[1] = "func=ell;x=14;y=258;width=287;height=31;bc=#000000;pw=0;"
shape[2] = "func=ell;x=0;y=35;width=79;height=224;bc=#D76E18;pw=0;"
shape[3] = "func=ell;x=43;y=20;width=99;height=255;bc=#D76E18;pw=0;"
shape[4] = "func=ell;x=231;y=37;width=84;height=215;bc=#D76E18;pw=0;"
shape[5] = "func=ell;x=181;y=33;width=99;height=239;bc=#D76E18;pw=0;"
shape[6] = "func=ell;x=100;y=21;width=127;height=255;bc=#D76E18;pw=0;"
shape[7] = "func=rect;x=159;y=0;width=16;height=40;angle=7;bc=#2D5C32;pw=0;"
shape[8] = "func=tri;x=142;y=16;x1=9;y1=0;x2=0;y2=36;x3=19;y3=36;angle=271;bc=#2D5C32;pw=0;"
shape[9] = "func=tri;x=145;y=30;x1=8;y1=0;x2=0;y2=30;x3=16;y3=30;angle=234;bc=#2D5C32;pw=0;"
shape[10] = "func=tri;x=171;y=18;x1=8;y1=0;x2=0;y2=37;x3=16;y3=37;angle=95;bc=#2D5C32;pw=0;"
shape[11] = "func=tri;x=174;y=31;x1=6;y1=0;x2=0;y2=31;x3=12;y3=31;angle=116;bc=#2D5C32;pw=0;"
shape[12] = "func=ell;x=64;y=102;width=63;height=63;bc=#000000;pw=0;"
shape[13] = "func=ell;x=192;y=102;width=60;height=61;bc=#000000;pw=0;"
shape[14] = "func=ell;x=230;y=134;width=7;height=0;bc=#000000;pw=0;"
shape[15] = "func=tri;x=35;y=165;x1=22;y1=0;x2=0;y2=65;x3=44;y3=65;angle=298;bc=#000000;pw=0;"
shape[16] = "func=tri;x=229;y=170;x1=22;y1=0;x2=0;y2=57;x3=44;y3=57;angle=59;bc=#000000;pw=0;"
shape[17] = "func=rect;x=74;y=190;width=166;height=46;bc=#000000;pw=0;"
shape[18] = "func=rect;x=92;y=185;width=34;height=30;bc=#D76E18;pw=0;"
shape[19] = "func=rect;x=182;y=183;width=26;height=31;bc=#D76E18;pw=0;"
shape[20] = "func=rect;x=64;y=208;width=18;height=36;bc=#D76E18;pw=0;"
shape[21] = "func=rect;x=138;y=215;width=32;height=34;bc=#D76E18;pw=0;"
shape[22] = "func=rect;x=216;y=213;width=26;height=27;bc=#D76E18;pw=0;"
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