Microsoft Small Basic

Program Listing: DTF312-1
' Lunar Module
' 2014-10-02 15:43:13 Shapes generated by Shapes 1.6b.
' Version 0.5b
' Last update 2014-10-31
' Program ID DTF312-1
'
gw = 598
gh = 428
GraphicsWindow.Width = gw
GraphicsWindow.Height = gh
title = "Lunar Module 0.5b"
GraphicsWindow.Title = title
GraphicsWindow.BackgroundColor = "Black"
GraphicsWindow.FontName = "Trebuchet MS"
SB_Workaround()
DrawStars()
AddTitle()
' initialize shapes for Lunar Module
Shapes_Init()
' add shapes
scale = 0.1
angle = 0
iMin = 1
iMax = 18
Shapes_Add()
x = (gw - 45) / 2
dy = 1
' side thruster off
For op = 100 To 0 Step -1
Shapes.SetOpacity(shape[2]["obj"], op)
Shapes.SetOpacity(shape[3]["obj"], op)
EndFor
ys = 125
ye = gh
For y = ys To ye Step dy
op = (ye - y) / (ye - ys) * 50
Shapes_Move()
Shapes.SetOpacity(shape[1]["obj"], op) ' main frame
Program.Delay(1000 / 24)
EndFor
RemoveTitle()
DrawMoon()
DrawVScale()
lmw = 9 ' Lunar Module width [m]
lmh = 6 ' Lunar Module height [m]
fw = 1 ' frame width [m]
fh = 4 ' frame height [m]
gap = 0.5 ' gap between LM and frame [m]
pxpm = 5 ' [px/m]
x = 175 ' [px]
y = y0 - 170 / 2 * pxpm ' [px]
MoveLM()
m = 15000 ' mass of LM [kg] = 15[t]
g = 9.8 / 6 ' acceleration due to gravity on the surface of the Moon [m/s^2]
f = 45040 ' descent engine thrust [N]
f2 = 455 ' side thruster thrust [N]
a = f / m ' acceleration by descent engine [m/s^2]
a2 = f2 / m ' acceleration by side thruster [m/s^2]
vx2 = 0 ' velocity x [m/s]
vy2 = 0 ' velocity y [m/s]
engineOn = "False"
rightOn = "False"
leftOn = "False"
GraphicsWindow.KeyDown = OnKeyDown
GraphicsWindow.KeyUp = OnKeyUp
t2 = Clock.ElapsedMilliseconds / 1000
While "True"
Program.Delay(1000 / 24)
t1 = t2
t2 = Clock.ElapsedMilliseconds / 1000
dt = t2 - t1 ' [s]
vx1 = vx2
vy1 = vy2
If engineOn Then
Shapes.SetOpacity(shape[1]["obj"], 50) ' main frame
vy2 = vy1 + (g - a) * dt ' [m/s]
Else
Shapes.SetOpacity(shape[1]["obj"], 0) ' main frame
vy2 = vy1 + g * dt ' [m/s]
EndIf
If rightOn Then
Shapes.SetOpacity(shape[2]["obj"], 50) ' right frame
vx2 = vx1 + a2 * dt ' [m/s]
Else
Shapes.SetOpacity(shape[2]["obj"], 0) ' right frame
EndIf
If leftOn Then
Shapes.SetOpacity(shape[3]["obj"], 50) ' left frame
vx2 = vx1 - a2 * dt ' [m/s]
Else
Shapes.SetOpacity(shape[3]["obj"], 0) ' left frame
EndIf
dx = (vx2 + vx1) * dt / 2 * pxpm ' [px]
x = x + dx ' [px]
dy = (vy2 + vy1) * dt / 2 * pxpm ' [px]
y = y + dy ' [px]
XtoY0()
If y0 - lmh / 2 * pxpm <= y Then
y = y0 - lmh / 2 * pxpm
vy2 = 0
vx2 = 0
EndIf
ShowV()
MoveLM()
EndWhile
Sub XtoY0
' param x - Lunar Module x position (center) [px]
' param lmw - Lunar Module width [m]
' param pxpm - scale [px/m]
' return y0
x0 = x - lmw * pxpm / 2 ' Luner Module x position (left) [px]
i = 1
GetTerrain()
x1 = x2
y1 = y2
n = Array.GetItemCount(terrain)
For i = 2 To n
GetTerrain()
If x0 < x2 Then
i0 = i
i = n + 1 ' break
Else
x1 = x2
y1 = y2
EndIf
EndFor
' for left bottom corner of Lunar Module
y0 = y1 + Math.Floor((x0 - x1) / (x2 - x1) * (y2 - y1))
x0 = x + lmw * pxpm / 2 ' Lunar Module x position (right) [px]
' for vertex between left and right of Luner Module
For i = i0 To n
If x2 < x0 And y2 < y0 Then
y0 = y2
EndIf
If x0 <= x2 Then
i = n + 1 ' break
Else
x1 = x2
y1 = y2
GetTerrain()
EndIf
EndFor
' for right bottom corner of Luner Module
_y = y1 + Math.Floor((x0 - x1) / (x2 - x1) * (y2 - y1))
If _y < y0 Then
y0 = _y
EndIf
EndSub
Sub AddTitle
' param title
GraphicsWindow.BrushColor = "Blue"
GraphicsWindow.FontSize = 50
objTitle = Shapes.AddText(title)
x = (gw - 438) / 2
y = (gh - 100) / 2
Shapes.Move(objTitle, x, y)
EndSub
Sub RemoveTitle
Shapes.Remove(objTitle)
EndSub
Sub DrawMoon
terrain = "1=0,31;2=20,36;3=71,377;4=99,314;5=175,314;"
terrain = terrain + "6=247,418;7=328,296;8=391,296;"
terrain = terrain + "9=393,194;10=429,163;11=472,418;"
terrain = terrain + "12=539,418;13=590,300;14=598,319;"
y0 = gh - 10
GraphicsWindow.PenWidth = 0
GraphicsWindow.BrushColor = "DimGray"
i = 1
GetTerrain()
x1 = x2
y1 = y2
For i = 2 To Array.GetItemCount(terrain)
GetTerrain()
ymax = Math.Max(y1, y2)
If y1 < y2 Then
moon = Shapes.AddTriangle(0, 0, x2 - x1, y2 - y1, 0, y2 - y1)
Shapes.Move(moon, x1, y1)
ElseIf y2 < y1 Then
moon = Shapes.AddTriangle(0, y1 - y2, x2 - x1, 0, x2 - x1, y1 - y2)
Shapes.Move(moon, x1, y2)
EndIf
moon = Shapes.AddRectangle(x2 - x1, gh - ymax)
Shapes.Move(moon, x1, ymax)
x1 = x2
y1 = y2
EndFor
EndSub
Sub GetTerrain
' param i - index to a point of terrain
' return x2, y2 - the point
c = Text.GetIndexOf(terrain[i], ",")
x2 = Text.GetSubText(terrain[i], 1, c - 1)
y2 = Text.GetSubTextToEnd(terrain[i], c + 1)
EndSub
Sub DrawStars
GraphicsWindow.PenWidth = 0
For i = 1 To 1000
l = Math.GetRandomNumber(256) - 1
GraphicsWindow.BrushColor = GraphicsWindow.GetColorFromRGB(l, l, l)
x = Math.GetRandomNumber(gw)
y = Math.GetRandomNumber(gh)
s = Math.GetRandomNumber(2)
GraphicsWindow.FillEllipse(x, y, s, s)
EndFor
EndSub
Sub DrawVScale
GraphicsWindow.BrushColor = "#99000000"
GraphicsWindow.FillRectangle(10, 10, 40, gh - 40)
GraphicsWindow.PenWidth = 2
scaleColor = "Lime"
GraphicsWindow.PenColor = scaleColor
GraphicsWindow.BrushColor = scaleColor
GraphicsWindow.FontSize = 12
u = Shapes.AddText("v[m/s]")
Shapes.Move(u, 10, 10)
For v = -20 To 20
y = 30 + (1 - v / 20) * ((gh - 60) / 2)
If Math.Remainder(v + 20, 5) = 0 Then
s = Shapes.AddLine(0, 0, 30, 0) ' step
Shapes.Move(s, 10, y)
n = Shapes.AddText(v) ' number
Shapes.Move(n, 10, y)
Else
s = Shapes.AddLine(0, 0, 10, 0) ' step
Shapes.Move(s, 30, y)
EndIf
EndFor
Program.Delay(300) ' for Silverlight
GraphicsWindow.PenWidth = 0
GraphicsWindow.BrushColor = scaleColor
vHand = Shapes.AddTriangle(0, 0, 10, -5, 10, 5)
Shapes.Move(vHand, 40, 10 + (gh - 60) / 2)
EndSub
Sub ShowV
yV = 30 + (1 + vy2 / 20) * ((gh - 60) / 2)
Shapes.Move(vHand, 40, yV)
EndSub
Sub MoveLM
Stack.PushValue("local", x)
Stack.PushValue("local", y)
x = x - lmw / 2 * pxpm
y = y - lmh / 2 * pxpm
Shapes_Move()
y = Stack.PopValue("local")
x = Stack.PopValue("local")
EndSub
Sub OnKeyDown
key = GraphicsWindow.LastKey
If key = "Up" Then
engineOn = "True"
ElseIf key = "Right" Then
rightOn = "True"
ElseIf key = "Left" Then
leftOn = "True"
EndIf
EndSub
Sub OnKeyUp
key = GraphicsWindow.LastKey
If key = "Up" Then
engineOn = "False"
ElseIf key = "Right" Then
rightOn = "False"
ElseIf key = "Left" Then
leftOn = "False"
EndIf
EndSub
Sub Shapes_Init
' Shapes | Initialize shapes data
' return shX, shY - current position of shapes
' return shape - array of shapes
shX = 276 ' 84 ' x offset
shY = 125 ' 114 ' y offset
shape = ""
shape[1] = "func=ell;x=196;y=260;width=46;height=145;bc=#FFA500;pw=0;"
shape[2] = "func=ell;x=40;y=60;width=58;height=25;bc=#FFA500;pw=0;"
shape[3] = "func=ell;x=332;y=60;width=58;height=25;bc=#FFA500;;pw=0;"
shape[4] = "func=tri;x=119;y=54;x1=12;y1=0;x2=0;y2=35;x3=25;y3=35;angle=90;bc=#222222;pc=#FFFFFF;pw=2;"
shape[5] = "func=tri;x=295;y=54;x1=12;y1=0;x2=0;y2=35;x3=25;y3=35;angle=270;bc=#222222;pc=#FFFFFF;pw=2;"
shape[6] = "func=ell;x=262;y=80;width=69;height=69;bc=#666666;pc=#FFFFFF;pw=2;"
shape[7] = "func=ell;x=134;y=0;width=171;height=165;bc=#777777;pc=#FFFFFF;pw=2;"
shape[8] = "func=tri;x=155;y=50;x1=30;y1=0;x2=0;y2=30;x3=60;y3=30;angle=45;bc=#000000;pc=#FFFFFF;pw=2;"
shape[9] = "func=tri;x=223;y=50;x1=30;y1=0;x2=0;y2=30;x3=60;y3=30;angle=315;bc=#000000;pc=#FFFFFF;pw=2;"
shape[10] = "func=tri;x=177;y=164;x1=40;y1=0;x2=0;y2=70;x3=81;y3=70;bc=#222222;pc=#FFFFFF;pw=2;"
shape[11] = "func=rect;x=196;y=0;width=46;height=145;bc=#888888;pc=#FFFFFF;pw=2;"
shape[12] = "func=rect;x=128;y=142;width=186;height=66;bc=#554400;pc=#FFFFFF;pw=2;"
shape[13] = "func=line;x=38;y=142;x1=90;y1=0;x2=0;y2=144;pc=#FFFFFF;pw=2;"
shape[14] = "func=line;x=0;y=286;x1=0;y1=0;x2=86;y2=0;pc=#FFFFFF;pw=2;"
shape[15] = "func=line;x=312;y=142;x1=0;y1=0;x2=92;y2=144;pc=#FFFFFF;pw=2;"
shape[16] = "func=line;x=355;y=286;x1=0;y1=0;x2=91;y2=0;pc=#FFFFFF;pw=2;"
shape[17] = "func=line;x=175;y=286;x1=0;y1=0;x2=91;y2=0;pc=#FFFFFF;pw=2"
shape[18] = "func=line;x=217;y=142;x1=0;y1=0;x2=2;y2=144;pc=#FFFFFF;pw=2;"
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