Microsoft Small Basic

Program Listing: QDR256
' Stop Watch 0.1
GraphicsWindow.Title = "Stop Watch 0.1 - [Space] for start/stop, [Enter] for lap/reset"
gw = 640
gh = 480
GraphicsWindow.Width = gw
GraphicsWindow.Height = gh
start = "False"
lapped = "False"
delta = 0
nLap = 0
fs = 20
x1 = gw / 2 - fs * 6 / Math.SquareRoot(2) - fs
y1 = gh / 2 - fs * 6 / Math.SquareRoot(2) - fs
x2 = gw / 2 + fs * 6 / Math.SquareRoot(2) - fs
y2 = gh / 2 - fs * 6 / Math.SquareRoot(2) - fs
x3 = gw / 2 - fs * 6
y3 = gh / 2 - fs * 6
x4 = gw / 2 - fs * 5
y4 = gh / 2 - fs * 5
x = gw / 2 - fs * 4.6
y = gh / 2 - fs * 1.2
GraphicsWindow.BrushColor = "Black"
GraphicsWindow.FillEllipse(x1, y1, fs * 2, fs * 2)
GraphicsWindow.FillEllipse(x2, y2, fs * 2, fs * 2)
GraphicsWindow.BrushColor = "#333333"
GraphicsWindow.FillEllipse(x3, y3, fs * 12, fs * 12)
GraphicsWindow.BrushColor = "Black"
GraphicsWindow.FillEllipse(x4, y4, fs * 10, fs * 10)
GraphicsWindow.BrushColor = "DarkSeaGreen"
GraphicsWindow.FillRectangle(x, y, fs * 9.2, fs * 2.4)
ms = 0
MilliSecToTime()
GraphicsWindow.FontName = "Courier New"
GraphicsWindow.FontSize = fs
GraphicsWindow.BrushColor = "Black"
oNum = Shapes.AddText("[0]")
Shapes.Move(oNum, x, y)
oLap = Shapes.AddText(t)
Shapes.Move(oLap, x + fs * 2.4, y)
oTime = Shapes.AddText(t)
Shapes.Move(oTime, x + fs * 2.4, y + fs * 1.2)
GraphicsWindow.KeyDown = OnKeyDown
Timer.Interval = 10
Timer.Tick = OnTick
Timer.Pause()
Sub OnKeyDown
key = GraphicsWindow.LastKey
If key = "Space" Then ' start/stop
If start Then ' stop
ltime = Clock.ElapsedMilliseconds
delta = ltime - stime
start = "False"
Timer.Pause()
Else ' start
stime = Clock.ElapsedMilliseconds - delta
lltime = stime
start = "True"
Timer.Resume()
EndIf
ElseIf key = "Return" Then ' lap/reset
If start Then ' lap
ltime = Clock.ElapsedMilliseconds
nLap = nLap + 1
ms = ltime - lltime
MilliSecToTime()
Shapes.SetText(oNum, "[" + nLap + "]")
Shapes.SetText(oLap, t)
lltime = ltime
lapped = "True"
Else ' reset
If lapped Then
nLap = nLap + 1
ms = ltime - lltime
MilliSecToTime()
Shapes.SetText(oNum, "[" + nLap + "]")
Shapes.SetText(oLap, t)
lapped = "False"
Else
delta = 0
ms = 0
MilliSecToTime()
Shapes.SetText(oTime, t)
nLap = 0
Shapes.SetText(oNum, "[0]")
Shapes.SetText(oLap, t)
EndIf
EndIf
EndIf
'ShowLap()
'ShowTime()
EndSub
Sub OnTick
ctime = Clock.ElapsedMilliseconds
If start Then
ms = ctime - stime
MilliSecToTime()
Shapes.SetText(oTime, t)
EndIf
EndSub
Sub MilliSecToTime
' param ms - millisecond
' returns t - time
frac = Math.Remainder(Math.Floor(ms / 10), 100)
sec = Math.Remainder(Math.Floor(ms / 1000), 60)
If Text.GetLength(sec) = 1 Then
sec = Text.Append(0, sec + frac / 100)
Else
sec = sec + frac / 100
EndIf
If Text.GetLength(sec) <= 2 Then
sec = Text.Append(sec, ".00")
ElseIf Text.GetLength(sec) <= 4 Then
sec = Text.Append(sec, 0)
EndIf
min = Math.Remainder(Math.Floor(ms / 60000), 60)
If Text.GetLength(min) = 1 Then
min = Text.Append(0, min)
EndIf
hour = Math.Remainder(Math.Floor(ms / 3600000), 60)
If Text.GetLength(hour) = 1 Then
hour = Text.Append(0, hour)
EndIf
t = hour + ":" + min + ":" + sec
EndSub