Microsoft Small Basic

Program Listing: KXQ212-0
' Gravity 0.2
' Copyright (c) 2014 Nonki Takahashi. The MIT License.
' Program ID KXQ212-0
'
GraphicsWindow.Title = "Gravity 0.2"
GraphicsWindow.BackgroundColor = "Black"
gw = 598
gh = 428
GraphicsWindow.Width = gw
GraphicsWindow.Height = gh
DrawStars()
sz = 4 ' size [m]
fw = 1 ' frame width [m]
fh = 4 ' frame height [m]
gap = 0.5 ' gap between LM and frame [m]
scale = 10 ' [px/m]
y0 = gh - 10
GraphicsWindow.BrushColor = "Gray"
GraphicsWindow.FillRectangle(0, y0, gw, 10)
GraphicsWindow.PenWidth = 0
GraphicsWindow.BrushColor = "LightGray"
lm = Shapes.AddRectangle(sz * scale, sz * scale)
GraphicsWindow.BrushColor = "Orange"
fr = Shapes.AddEllipse(fw * scale, fh * scale)
x = gw / 2 ' [px]
y = y0 - sz * scale / 2 ' [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 trust [N]
a = f / m ' acceleration by descent engine [m/s^2]
vx2 = 0 ' velocity x [m/s]
vy2 = 0 ' velocity y [m/s]
engineOn = "False"
GraphicsWindow.KeyDown = OnKeyDown
GraphicsWindow.KeyUp = OnKeyUp
While "True"
t1 = t2
t2 = Clock.ElapsedMilliseconds / 1000
dt = t2 - t1 ' [s]
vy1 = vy2
If engineOn Then
Shapes.SetOpacity(fr, 50)
vy2 = vy1 + (g - a) * dt ' [m/s]
Else
Shapes.SetOpacity(fr, 0)
vy2 = vy1 + g * dt ' [m/s]
EndIf
dy = (vy2 + vy1) * dt / 2 * scale ' [px]
y = y + dy ' [px]
If y0 - sz * scale / 2 <= y Then
y = y0 - sz * scale / 2
vy2 = 0
EndIf
MoveLM()
Program.Delay(1000 / 24)
EndWhile
Sub OnKeyDown
key = GraphicsWindow.LastKey
If key = "Up" Then
engineOn = "True"
EndIf
EndSub
Sub OnKeyUp
If GraphicsWindow.LastKey = "Up" Then
engineOn = "False"
EndIf
EndSub
Sub DrawStars
GraphicsWindow.PenWidth = 0
GraphicsWindow.BrushColor = "White"
For i = 1 To 300
x = Math.GetRandomNumber(gw)
y = Math.GetRandomNumber(gh)
GraphicsWindow.FillEllipse(x, y, 2, 2)
EndFor
EndSub
Sub MoveLM
Shapes.Move(lm, x - sz / 2 * scale y - sz /2 * scale)
Shapes.Move(fr, x - fw / 2 * scale, y + (sz / 2 + gap) * scale)
EndSub