Microsoft Small Basic

Program Listing: BGH855-1
' Binary Quiz
' Copyright © 2016-2017 Nonki Takahashi. The MIT License.
' Last update 2017-05-12
' Challenge 2016-05
' Program ID BGH855-1

title = "Binary Quiz"
GraphicsWindow.Title = title
Form()
For dec = 255 To 0 Step -17
ShowBinary()
ShowHex()
ShowDec()
Program.Delay(1000)
EndFor
While "True"
dec = Math.GetRandomNumber(256) - 1
ShowBinary()
ShowHex()
ShowDec()
Shapes.SetOpacity(hex, 0)
Shapes.SetOpacity(dtext, 0)
Program.Delay(5000)
For op = 0 To 100
Shapes.SetOpacity(hex, op)
Shapes.SetOpacity(dtext, op)
Program.Delay(30)
EndFor
Program.Delay(1000)
EndWhile

Sub Form
GraphicsWindow.Width = 598
GraphicsWindow.Height = 428
size = 40
x0 = 70
y0 = 60
GraphicsWindow.BackgroundColor = "Black"
GraphicsWindow.BrushColor = "Lime"
GraphicsWindow.PenColor = "#333333"
For i = 1 To 8
x = x0 + (i - 1) * size * 1.4
GraphicsWindow.PenWidth = 6
GraphicsWindow.DrawEllipse(x - 3, y0 - 3, size + 6, size + 6)
GraphicsWindow.PenWidth = 0
bit[8 - i] = Shapes.AddEllipse(size, size)
Shapes.Move(bit[8 - i], x, y0)
Shapes.SetOpacity(bit[8 - i], 20)
EndFor
GraphicsWindow.FontName = "Trebuchet MS"
GraphicsWindow.FontSize = size * 5
GraphicsWindow.BrushColor = "White"
hex = Shapes.AddText("00")
Shapes.Move(hex, x0 + size * 2.5, y0 + size * 1.6)
GraphicsWindow.FontSize = size
dtext = Shapes.AddText(0)
Shapes.Move(dtext, x0 + size * 9.5, y0 + size * 6)
EndSub

Sub ShowBinary
' param dec - decimal number
n = 2
Math_Dec2BaseN()
len = Text.GetLength(num) ' binary
If len < 8 Then
num = Text.Append(Text.GetSubTextToEnd("0000000", len), num)
len = 8
EndIf
GraphicsWindow.Title = title + " - " + num
For i = 1 To len
If Text.GetSubText(num, i, 1) = "1" Then
Shapes.SetOpacity(bit[len - i], 100)
Else
Shapes.SetOpacity(bit[len - i], 20)
EndIf
EndFor
EndSub

Sub ShowDec
' param dec - decimal number
Shapes.SetText(dtext, dec)
EndSub

Sub ShowHex
' param dec - decimal number
n = 16
Math_Dec2BaseN()
If Text.GetLength(num) = 1 Then
num = Text.Append(0, num)
EndIf
Shapes.SetText(hex, num)
EndSub

Sub Math_Dec2BaseN
' Convert decimal number to notation system of base n
' param dec - decimal number
' param n - base n
' return num - number in notation system of base n
Stack.PushValue("local", dec)
If dec = 0 Then
num = 0
Else
num = ""
While 0 < dec
num = Text.Append(Text.GetSubText("0123456789ABCDEF", Math.Remainder(dec, n) + 1, 1), num)
dec = Math.Floor(dec / n)
EndWhile
EndIf
dec = Stack.PopValue("local")
EndSub