Microsoft Small Basic

Program Listing: QHH936-9
' Bats Testing (n = 9, fps = 10, w/o show/hide, tuned)
' Copyright (c) 2014 Nonki Takahashi. The MIT License.
' Program ID QHH936-9
'
SB_Workaround()
gw = 598
gh = 428
GraphicsWindow.Width = gw
GraphicsWindow.Height = gh
GraphicsWindow.Title = "Bats"
GraphicsWindow.BackgroundColor = "#333333"
GraphicsWindow.BrushColor = "White"
GraphicsWindow.FontName = "Trebuchet MS"
ms = Shapes.AddText("")
Shapes.Move(ms, 10, 10)
n = 9
fps = 10
ShowBats()
Sub ShowBats
url = "http://gallery.technet.microsoft.com/site/view/file/127750/1/OpenBat.png"
oImg = ImageList.LoadImage(url)
If silverlight Then
ow = 63
oh = 30
Else
ow = ImageList.GetWidthOfImage(oImg)
oh = ImageList.GetHeightOfImage(oImg)
EndIf
url = "http://gallery.technet.microsoft.com/site/view/file/127751/1/CloseBat.png"
cImg = ImageList.LoadImage(url)
If silverlight Then
cw = 26
ch = 26
Else
cw = ImageList.GetWidthOfImage(cImg)
ch = ImageList.GetHeightOfImage(cImg)
EndIf
For i = 1 To n
bat[i]["open"] = Shapes.AddImage(oImg)
bat[i]["close"] = Shapes.AddImage(cImg)
bat[i]["cycle"] = Math.GetRandomNumber(4)
bat[i]["x"] = Math.GetRandomNumber(gw - ow) + ow / 2
bat[i]["y"] = Math.GetRandomNumber(gh - 110 - oh) + oh / 2
bat[i]["angle"] = Math.GetRandomNumber(21) - 11
EndFor
tick = "False"
Timer.Interval = 1000 / fps
Timer.Tick = OnTick
While "True"
If tick Then
UpdateBats()
tick = "False"
Else
Program.Delay(1000 / fps)
EndIf
EndWhile
EndSub
Sub OnTick
tick = "True"
EndSub
Sub UpdateBats
t1 = Clock.ElapsedMilliseconds
For i = 1 To n
bat[i]["cycle"] = bat[i]["cycle"] + 1
If 4 < bat[i]["cycle"] Then
bat[i]["cycle"] = 1
EndIf
bat[i]["x"] = bat[i]["x"] + Math.GetRandomNumber(7) - 4
If bat[i]["x"] < ow / 2 Then
bat[i]["x"] = ow / 2
ElseIf (gw - ow / 2) < bat[i]["x"] Then
bat[i]["x"] = gw - ow / 2
EndIf
bat[i]["y"] = bat[i]["y"] + Math.GetRandomNumber(17) - 9
If bat[i]["y"] < oh / 2 Then
bat[i]["y"] = oh / 2
ElseIf (gh - 110 - oh / 2) < bat[i]["y"] Then
bat[i]["y"] = gh - 110 - oh / 2
EndIf
bat[i]["angle"] = bat[i]["angle"] + Math.GetRandomNumber(7) - 4
If bat[i]["angle"] < -10 Then
bat[i]["angle"] = -10
ElseIf 10 < bat[i]["angle"] Then
bat[i]["angle"] = 10
EndIf
If bat[i]["cycle"] = 1 Then
Shapes.SetOpacity(bat[i]["close"], 0)
Shapes.Move(bat[i]["open"], bat[i]["x"] - ow / 2, bat[i]["y"] - oh / 2)
Shapes.Rotate(bat[i]["open"], bat[i]["angle"])
Shapes.SetOpacity(bat[i]["open"], 100)
ElseIf bat[i]["cycle"] = 4 Then
Shapes.SetOpacity(bat[i]["open"], 0)
Shapes.Move(bat[i]["close"], bat[i]["x"] - cw / 2, bat[i]["y"] - ch / 2)
Shapes.Rotate(bat[i]["close"], bat[i]["angle"])
Shapes.SetOpacity(bat[i]["close"], 100)
Else
Shapes.Move(bat[i]["open"], bat[i]["x"] - ow / 2, bat[i]["y"] - oh / 2)
Shapes.Rotate(bat[i]["open"], bat[i]["angle"])
EndIf
EndFor
t2 = Clock.ElapsedMilliseconds
Shapes.SetText(ms, Math.Floor(t2 - t1))
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