Microsoft Small Basic

Program Listing: NML535-2
' Pirate Ship
' Version 0.3
' Copyright © 2017 Nonki Takahashi. The MIT License.
' Program ID NML535-2
' Last update 2017-10-25 - workaround for known issue 43905
' 2017-05-15 20:30:21 Shapes generated by Shapes 2.21b.
'
GraphicsWindow.Title = "Pirate Ship 0.3"
GraphicsWindow.BackgroundColor = "SkyBlue"
SB_Workaround()
gw = 598
gh = 428
GraphicsWindow.Width = gw
GraphicsWindow.Height = gh
' initialize shapes
Shapes_Init()
' add shapes
scale = 1
angle = 0
iMin = 1
iMax = 93
Shapes_Add()
cx = 260
cy = -20
h = 350
a = 20
_a = Math.GetRadians(a)
w2 = h * 2 * Math.Sin(_a)
w1 = 30 * Math.Sin(_a)
GraphicsWindow.PenWidth = 8
GraphicsWindow.PenColor = "#000000"
GraphicsWindow.DrawEllipse(shX + cx - 15, shY + cy - 15, 30, 30)
GraphicsWindow.PenWidth = 16
For i = 1 To 2
pole[i] = Shapes.AddLine((i - 1.5) * w1, 0, (i - 1.5) * w2, h - 15)
Shapes.Move(pole[i], shX + cx, shY + cy + 15)
EndFor
GraphicsWindow.PenWidth = 0
GraphicsWindow.BrushColor = "#999999"
y = shY + cy + h - 5
ground = Shapes.AddRectangle(gw, gh - y)
Shapes.Move(ground, 0, y)
While "True"
angle = 10 * Math.Sin(_a)
Shapes_Rotate()
_a = _a + 0.1
Program.Delay(100)
EndWhile


Sub Shapes_Init
' Shapes | Initialize shapes data
' return shX, shY - current position of shapes
' return shape - array of shapes
shX = 52 ' x offset
shY = 78 ' y offset
shape = ""
shape[1] = "func=ell;x=119;y=243;width=95;height=58;bc=#5F300C;pw=0;"
shape[2] = "func=rect;x=163;y=244;width=245;height=57;bc=#5F300C;pw=0;"
shape[3] = "func=tri;x=100;y=239;x1=21;y1=0;x2=0;y2=43;x3=43;y3=43;angle=180;bc=#5F300C;pw=0;"
shape[4] = "func=tri;x=78;y=222;x1=22;y1=0;x2=0;y2=18;x3=45;y3=18;angle=180;bc=#5F300C;pw=0;"
shape[5] = "func=tri;x=161;y=222;x1=13;y1=0;x2=0;y2=22;x3=26;y3=22;bc=#5F300C;pw=0;"
shape[6] = "func=tri;x=99;y=222;x1=22;y1=0;x2=0;y2=18;x3=45;y3=18;bc=#5F300C;pw=0;"
shape[7] = "func=rect;x=120;y=222;width=54;height=30;bc=#5F300C;pw=0;"
shape[8] = "func=tri;x=326;y=207;x1=14;y1=0;x2=0;y2=38;x3=28;y3=38;bc=#5F300C;pw=0;"
shape[9] = "func=tri;x=399;y=208;x1=14;y1=0;x2=0;y2=38;x3=28;y3=38;angle=180;bc=#5F300C;pw=0;"
shape[10] = "func=rect;x=341;y=208;width=72;height=37;bc=#5F300C;pw=0;"
shape[11] = "func=rect;x=359;y=172;width=72;height=37;bc=#5F300C;pw=0;"
shape[12] = "func=tri;x=345;y=173;x1=14;y1=0;x2=0;y2=38;x3=28;y3=38;bc=#5F300C;pw=0;"
shape[13] = "func=tri;x=417;y=172;x1=14;y1=0;x2=0;y2=38;x3=28;y3=38;angle=180;bc=#5F300C;pw=0;"
shape[14] = "func=line;x=0;y=188;x1=93;y1=39;x2=0;y2=0;pc=#5F300C;pw=4;"
shape[15] = "func=line;x=255;y=5;x1=0;y1=0;x2=3;y2=240;pc=#3B1E07;pw=4;"
shape[16] = "func=line;x=140;y=45;x1=0;y1=0;x2=0;y2=177;pc=#3B1E07;pw=4;"
shape[17] = "func=rect;x=169;y=258;width=17;height=17;bc=#000000;pc=#CEA75F;pw=2;"
shape[18] = "func=rect;x=209;y=258;width=17;height=17;bc=#000000;pc=#CEA75F;pw=2;"
shape[19] = "func=rect;x=251;y=258;width=17;height=17;bc=#000000;pc=#CEA75F;pw=2;"
shape[20] = "func=rect;x=293;y=258;width=17;height=17;bc=#000000;pc=#CEA75F;pw=2;"
shape[21] = "func=rect;x=335;y=258;width=17;height=17;bc=#000000;pc=#CEA75F;pw=2;"
shape[22] = "func=ell;x=171;y=260;width=12;height=12;bc=#000000;pc=#939393;pw=2;"
shape[23] = "func=ell;x=211;y=259;width=12;height=12;bc=#000000;pc=#939393;pw=2;"
shape[24] = "func=ell;x=253;y=260;width=12;height=12;bc=#000000;pc=#939393;pw=2;"
shape[25] = "func=ell;x=295;y=259;width=12;height=12;bc=#000000;pc=#939393;pw=2;"
shape[26] = "func=ell;x=337;y=259;width=12;height=12;bc=#000000;pc=#939393;pw=2;"
shape[27] = "func=line;x=102;y=175;x1=0;y1=0;x2=71;y2=0;pc=#3B1E07;pw=4;"
shape[28] = "func=line;x=103;y=97;x1=0;y1=0;x2=71;y2=0;pc=#3B1E07;pw=4;"
shape[29] = "func=line;x=212;y=221;x1=0;y1=0;x2=71;y2=0;pc=#3B1E07;pw=4;"
shape[30] = "func=line;x=210;y=132;x1=0;y1=0;x2=71;y2=0;pc=#3B1E07;pw=4;"
shape[31] = "func=line;x=219;y=84;x1=0;y1=0;x2=71;y2=0;pc=#3B1E07;pw=4;"
shape[32] = "func=line;x=220;y=32;x1=0;y1=0;x2=71;y2=0;pc=#3B1E07;pw=4;"
shape[33] = "func=line;x=335;y=71;x1=0;y1=0;x2=71;y2=0;pc=#3B1E07;pw=4;"
shape[34] = "func=tri;x=54;y=112;x1=24;y1=0;x2=0;y2=89;x3=48;y3=89;angle=33;bc=#E4E4A9;pw=0;"
shape[35] = "func=tri;x=341;y=100;x1=28;y1=0;x2=0;y2=105;x3=56;y3=105;angle=244;bc=#E4E4A9;pw=0;"
shape[36] = "func=line;x=336;y=152;x1=0;y1=0;x2=71;y2=0;pc=#3B1E07;pw=4;"
shape[37] = "func=line;x=372;y=35;x1=0;y1=0;x2=1;y2=137;pc=#3B1E07;pw=4;"
shape[38] = "func=rect;x=239;y=105;width=36;height=18;bc=#5F300C;pw=0;"
shape[39] = "func=tri;x=229;y=105;x1=8;y1=0;x2=0;y2=18;x3=17;y3=18;angle=179;bc=#5F300C;pw=0;"
shape[40] = "func=tri;x=266;y=105;x1=8;y1=0;x2=0;y2=18;x3=17;y3=18;angle=179;bc=#5F300C;pw=0;"
shape[41] = "func=tri;x=95;y=101;x1=7;y1=0;x2=0;y2=22;x3=15;y3=22;bc=#EEEEC8;pw=0;"
shape[42] = "func=tri;x=155;y=103;x1=7;y1=0;x2=0;y2=22;x3=15;y3=22;angle=180;bc=#EEEEC8;pw=0;"
shape[43] = "func=rect;x=101;y=103;width=61;height=19;bc=#EEEEC8;pw=0;"
shape[44] = "func=rect;x=95;y=122;width=68;height=28;bc=#EEEEC8;pw=0;"
shape[45] = "func=tri;x=156;y=148;x1=7;y1=0;x2=0;y2=21;x3=15;y3=21;bc=#EEEEC8;pw=0;"
shape[46] = "func=tri;x=95;y=150;x1=7;y1=0;x2=0;y2=21;x3=15;y3=21;angle=179;bc=#EEEEC8;pw=0;"
shape[47] = "func=rect;x=102;y=150;width=62;height=19;bc=#EEEEC8;pw=0;"
shape[48] = "func=tri;x=201;y=137;x1=8;y1=0;x2=0;y2=31;x3=17;y3=31;bc=#EEEEC8;pw=0;"
shape[49] = "func=tri;x=262;y=186;x1=8;y1=0;x2=0;y2=31;x3=17;y3=31;bc=#EEEEC8;pw=0;"
shape[50] = "func=tri;x=261;y=137;x1=8;y1=0;x2=0;y2=31;x3=17;y3=31;angle=180;bc=#EEEEC8;pw=0;"
shape[51] = "func=tri;x=202;y=187;x1=8;y1=0;x2=0;y2=31;x3=17;y3=31;angle=180;bc=#EEEEC8;pw=0;"
shape[52] = "func=rect;x=210;y=137;width=60;height=31;bc=#EEEEC8;pw=0;"
shape[53] = "func=rect;x=210;y=186;width=60;height=31;bc=#EEEEC8;pw=0;"
shape[54] = "func=rect;x=202;y=163;width=67;height=27;bc=#EEEEC8;pw=0;"
shape[55] = "func=tri;x=214;y=38;x1=7;y1=0;x2=0;y2=11;x3=14;y3=11;bc=#EEEEC8;pw=0;"
shape[56] = "func=tri;x=273;y=69;x1=7;y1=0;x2=0;y2=11;x3=14;y3=11;bc=#EEEEC8;pw=0;"
shape[57] = "func=tri;x=274;y=38;x1=7;y1=0;x2=0;y2=11;x3=14;y3=11;angle=180;bc=#EEEEC8;pw=0;"
shape[58] = "func=tri;x=213;y=70;x1=7;y1=0;x2=0;y2=11;x3=14;y3=11;angle=180;bc=#EEEEC8;pw=0;"
shape[59] = "func=rect;x=222;y=38;width=60;height=10;bc=#EEEEC8;pw=0;"
shape[60] = "func=rect;x=221;y=70;width=60;height=10;bc=#EEEEC8;pw=0;"
shape[61] = "func=rect;x=214;y=48;width=67;height=25;bc=#EEEEC8;pw=0;"
shape[62] = "func=tri;x=229;y=0;x1=8;y1=0;x2=0;y2=30;x3=17;y3=30;angle=269;bc=#000000;pw=0;"
shape[63] = "func=tri;x=114;y=41;x1=8;y1=0;x2=0;y2=30;x3=17;y3=30;angle=269;bc=#000000;pw=0;"
shape[64] = "func=tri;x=346;y=33;x1=8;y1=0;x2=0;y2=30;x3=17;y3=30;angle=269;bc=#000000;pw=0;"
shape[65] = "func=rect;x=350;y=216;width=10;height=24;bc=#000000;pc=#CEA75F;pw=2;"
shape[66] = "func=rect;x=371;y=216;width=10;height=24;bc=#000000;pc=#CEA75F;pw=2;"
shape[67] = "func=rect;x=392;y=216;width=10;height=24;bc=#000000;pc=#CEA75F;pw=2;"
shape[68] = "func=rect;x=366;y=182;width=10;height=24;bc=#000000;pc=#CEA75F;pw=2;"
shape[69] = "func=rect;x=389;y=182;width=10;height=24;bc=#000000;pc=#CEA75F;pw=2;"
shape[70] = "func=rect;x=411;y=182;width=10;height=24;bc=#000000;pc=#CEA75F;pw=2;"
shape[71] = "func=tri;x=329;y=77;x1=6;y1=0;x2=0;y2=22;x3=13;y3=22;bc=#EEEEC8;pw=0;"
shape[72] = "func=tri;x=390;y=124;x1=6;y1=0;x2=0;y2=22;x3=13;y3=22;bc=#EEEEC8;pw=0;"
shape[73] = "func=tri;x=328;y=127;x1=6;y1=0;x2=0;y2=22;x3=13;y3=22;angle=181;bc=#EEEEC8;pw=0;"
shape[74] = "func=tri;x=389;y=78;x1=6;y1=0;x2=0;y2=22;x3=13;y3=22;angle=181;bc=#EEEEC8;pw=0;"
shape[75] = "func=rect;x=336;y=78;width=61;height=21;bc=#EEEEC8;pw=0;"
shape[76] = "func=rect;x=334;y=126;width=61;height=21;bc=#EEEEC8;pw=0;"
shape[77] = "func=rect;x=328;y=99;width=67;height=28;bc=#EEEEC8;pw=0;"
shape[78] = "func=ell;x=222;y=151;width=29;height=22;bc=#000000;pw=0;"
shape[79] = "func=line;x=217;y=173;x1=0;y1=0;x2=38;y2=27;pc=#000000;pw=4;"
shape[80] = "func=line;x=220;y=174;x1=35;y1=0;x2=0;y2=26;pc=#000000;pw=4;"
shape[81] = "func=rect;x=226;y=169;width=5;height=8;bc=#000000;pc=#000000;pw=4;"
shape[82] = "func=rect;x=233;y=171;width=5;height=8;bc=#000000;pc=#000000;pw=4;"
shape[83] = "func=rect;x=240;y=170;width=5;height=8;bc=#000000;pc=#000000;pw=4;"
shape[84] = "func=ell;x=227;y=155;width=7;height=12;bc=#EEEEC8;pw=0;"
shape[85] = "func=ell;x=238;y=155;width=7;height=12;bc=#EEEEC8;pw=0;"
shape[86] = "func=ell;x=209;y=171;width=9;height=10;bc=#000000;pw=0;"
shape[87] = "func=ell;x=214;y=165;width=9;height=10;bc=#000000;pw=0;"
shape[88] = "func=ell;x=250;y=166;width=9;height=10;bc=#000000;pw=0;"
shape[89] = "func=ell;x=254;y=169;width=9;height=10;bc=#000000;pw=0;"
shape[90] = "func=ell;x=254;y=192;width=9;height=10;bc=#000000;pw=0;"
shape[91] = "func=ell;x=249;y=198;width=9;height=10;bc=#000000;pw=0;"
shape[92] = "func=ell;x=213;y=193;width=9;height=10;bc=#000000;pw=0;"
shape[93] = "func=ell;x=218;y=198;width=9;height=10;bc=#000000;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
ElseIf x = 0 Then
a = 0
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 shp - current shape
' param x, y - original coordinate
' param alpha - angle [radian]
' returns x, y - workaround coordinate
If shp["func"] = "tri" Then
x1 = -Math.Floor(shp["x3"] / 2)
y1 = -Math.Floor(shp["y3"] / 2)
ElseIf shp["func"] = "line" Then
x1 = -Math.Floor(Math.Abs(shp["x1"] - shp["x2"]) / 2)
y1 = -Math.Floor(Math.Abs(shp["y1"] - shp["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
shp = shape[i]
GraphicsWindow.PenWidth = shp["pw"] * s
If shp["pw"] > 0 Then
GraphicsWindow.PenColor = shp["pc"]
EndIf
If Text.IsSubText("rect|ell|tri|text", shp["func"]) Then
GraphicsWindow.BrushColor = shp["bc"]
EndIf
If shp["func"] = "rect" Then
shp["obj"] = Shapes.AddRectangle(shp["width"] * s, shp["height"] * s)
ElseIf shp["func"] = "ell" Then
shp["obj"] = Shapes.AddEllipse(shp["width"] * s, shp["height"] * s)
ElseIf shp["func"] = "tri" Then
shp["obj"] = Shapes.AddTriangle(shp["x1"] * s, shp["y1"] * s, shp["x2"] * s, shp["y2"] * s, shp["x3"] * s, shp["y3"] * s)
ElseIf shp["func"] = "line" Then
shp["obj"] = Shapes.AddLine(shp["x1"] * s, shp["y1"] * s, shp["x2"] * s, shp["y2"] * s)
ElseIf shp["func"] = "text" Then
If silverlight Then
fs = Math.Floor(shp["fs"] * 0.9)
Else
fs = shp["fs"]
EndIf
GraphicsWindow.FontSize = fs * s
GraphicsWindow.FontName = shp["fn"]
shp["obj"] = Shapes.AddText(shp["text"])
EndIf
x = shp["x"]
y = shp["y"]
shp["rx"] = x
shp["ry"] = y
If silverlight And Text.IsSubText("tri|line", shp["func"]) Then
alpha = Math.GetRadians(shp["angle"])
SB_RotateWorkaround()
shp["wx"] = x
shp["wy"] = y
EndIf
Shapes.Move(shp["obj"], shX + x * s, shY + y * s)
If Text.IsSubText("rect|ell|tri|text", shp["func"]) And (shp["angle"] <> 0) And (shp["angle"] <> "") Then
Shapes.Rotate(shp["obj"], shp["angle"])
EndIf
shape[i] = shp
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
shp = shape[i]
If shp["func"] = "tri" Or shp["func"] = "line" Then
xmin = shp["x1"]
xmax = shp["x1"]
ymin = shp["y1"]
ymax = shp["y1"]
If shp["x2"] < xmin Then
xmin = shp["x2"]
EndIf
If xmax < shp["x2"] Then
xmax = shp["x2"]
EndIf
If shp["y2"] < ymin Then
ymin = shp["y2"]
EndIf
If ymax < shp["y2"] Then
ymax = shp["y2"]
EndIf
If shp["func"] = "tri" Then
If shp["x3"] < xmin Then
xmin = shp["x3"]
EndIf
If xmax < shp["x3"] Then
xmax = shp["x3"]
EndIf
If shp["y3"] < ymin Then
ymin = shp["y3"]
EndIf
If ymax < shp["y3"] Then
ymax = shp["y3"]
EndIf
EndIf
shp["width"] = xmax - xmin
shp["height"] = ymax - ymin
EndIf
If i = 1 Then
shWidth = shp["x"] + shp["width"]
shHeight = shp["y"] + shp["height"]
Else
If shWidth < shp["x"] + shp["width"] Then
shWidth = shp["x"] + shp["width"]
EndIf
If shHeight < shp["y"] + shp["height"] Then
shHeight = shp["y"] + shp["height"]
EndIf
EndIf
shape[i] = shp
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
shp = shape[i]
If silverlight And Text.IsSubText("tri|line", shp["func"]) Then
_x = shp["wx"]
_y = shp["wy"]
Else
_x = shp["rx"]
_y = shp["ry"]
EndIf
Shapes.Move(shp["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
shp = shape[i]
Shapes.Remove(shp["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 cx, cy - rotation center
' 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
If cx <> "" Then
param["cx"] = cx
Else
cx = "" ' to avoid syntax error
param["cx"] = shWidth / 2
EndIf
If cy <> "" Then
param["cy"] = cy
Else
cy = "" ' to avoid syntax error
param["cy"] = shHeight / 2
EndIf
For i = iMin To iMax
shp = shape[i]
param["x"] = shp["x"]
param["y"] = shp["y"]
param["width"] = shp["width"]
param["height"] = shp["height"]
Shapes_CalcRotatePos()
shp["rx"] = x
shp["ry"] = y
If silverlight And Text.IsSubText("tri|line", shp["func"]) Then
If shp["angle"] = "" Then
a = angle
Else
a = angle + shp["angle"]
EndIf
alpha = Math.GetRadians(a)
SB_RotateWorkAround()
shp["wx"] = x
shp["wy"] = y
EndIf
Shapes.Move(shp["obj"], shX + x * s, shY + y * s)
If shp["angle"] = "" Then
a = angle
Else
a = angle + shp["angle"]
EndIf
Shapes.Rotate(shp["obj"], a)
shape[i] = shp
EndFor
y = Stack.PopValue("local")
x = Stack.PopValue("local")
i = Stack.PopValue("local")
EndSub