Microsoft Small Basic

Program Listing: KNP762
' Sun Rise Animation 0.1
' Copyright (c) 2014 Nonki Takahashi. The MIT License.
'
gw = 598
gh = 428
GraphicsWindow.Width = gw
GraphicsWindow.Height = gh
GraphicsWindow.BackgroundColor = "#000040"
DrawStars()
AddSun()
SunRise()
Sub DrawStars
GraphicsWindow.BrushColor = GraphicsWindow.GetColorFromRGB(200, 200, 255)
For i = 1 To 300
x = Math.GetRandomNumber(gw)
y = Math.GetRandomNumber(gh)
GraphicsWindow.FillEllipse(x, y, 2, 2)
EndFor
EndSub
Sub AddSun
GraphicsWindow.PenWidth = 0
GraphicsWindow.BrushColor = "White"
sz = 50
sun = Shapes.AddEllipse(sz, sz)
sr = gw * 3 / 4
ox = gw
oy = gh + sz / 2
Shapes.Move(sun, sx - sz / 2, sy - sz / 2)
EndSub
Sub SunRise
h = 4.5
msec = 1000 / 24
xSpeed = 500
dt = xSpeed * msec / (60 * 60 * 1000) ' [hour]
tick = "False"
Timer.Interval = msec
Timer.Tick = OnTick
While "True"
If tick Then
h = h + dt
HourToTime()
GraphicsWindow.Title = time
a = h * Math.Pi / 12
sx = ox - sr * Math.Sin(a)
sy = oy + sr * Math.Cos(a)
Shapes.Move(sun, sx - sz / 2, sy - sz / 2)
UpdateSky()
If 13 < h Then
Timer.Pause()
EndIf
tick = "False"
Else
Program.Delay(msec)
EndIf
EndWhile
EndSub
Sub OnTick
tick = "True"
EndSub
Sub HourToTime
' param h - hour
' return time - text "hh:mm"
time = Math.Remainder(Math.Floor(h), 24)
m = Math.Floor((h - time) * 60)
If time < 10 Then
time = Text.Append(0, time)
EndIf
If m < 10 Then
m = Text.Append(0, m)
EndIf
time = time + ":" + m
EndSub
Sub UpdateSky
If h <= 5.5 Then
r1 = 0 ' dark blue
g1 = 0
b1 = 64
r2 = 128 ' medium blue
g2 = 128
b2 = 192
percent = (h - 4.5) * 100
GetGradientColor()
GraphicsWindow.BackgroundColor = GraphicsWindow.GetColorFromRGB(r, g, b)
ElseIf h <= 6.5 Then
r1 = 128 ' medium blue
g1 = 128
b1 = 192
r2 = 200 ' orange red
g2 = 100
b2 = 64
percent = (h - 5.5) * 100
GetGradientColor()
GraphicsWindow.BackgroundColor = GraphicsWindow.GetColorFromRGB(r, g, b)
ElseIf h <= 7 Then
r1 = 200 ' orange red
g1 = 100
b1 = 64
r2 = 200 ' light blue
g2 = 200
b2 = 255
percent = (h - 6.5) * 200
GetGradientColor()
GraphicsWindow.BackgroundColor = GraphicsWindow.GetColorFromRGB(r, g, b)
EndIf
EndSub
Sub GetGradientColor
' param r1, g1, b1 - color 1
' param r2, g2, b2 - color 2
' param percent - 0 .. 100 [%]
' return r, g, b - gradient color
r = Math.Floor(r1 * (1 - percent / 100) + r2 * (percent / 100))
g = Math.Floor(g1 * (1 - percent / 100) + g2 * (percent / 100))
b = Math.Floor(b1 * (1 - percent / 100) + b2 * (percent / 100))
EndSub