Microsoft Small Basic

Program Listing: LNM164-4
' Small Roulette 0.4b
' Copyright © 2013-2015 Nonki Takahashi. The MIT License.
'
' History:
' 0.4a 2015-09-22 Added table layout. (LNM164-4)
' 0.3b 2015-04-08 Added ball animation. (LNM164-3)
' 0.22b 2013-06-18 Changed for Silverlight. (LNM164-2)
' 0.2b 2013-06-18 Rotated wheel. (LNM164-0)
' 0.11a 2013-06-18 Background color changed.
' 0.1a 2013-06-12 Created just for picture. (LNM164)
'
GraphicsWindow.Title = "Roulette 0.4a"
gw = 598
gh = 428
GraphicsWindow.Width = gw
GraphicsWindow.Height = gh
csv = "1,5,8,4,9,2,10,6,3,7"
color = "1=Black;2=DarkRed;"
CSVtoArray()
scale = 0.5
x0 = 135
y0 = gh / 2
DrawRoulette()
DrawLayout()
GraphicsWindow.PenWidth = 0
GraphicsWindow.BrushColor = "White"
ball = Shapes.AddEllipse(rb * 2, rb * 2)
angle = 0
Math_PolarToCartesian()
Shapes.Animate(ball, x - rb, y - rb, 1000)
GraphicsWindow.BrushColor = "Black"
Controls.AddButton("Start", 10, 10)
Controls.ButtonClicked = OnButtonClicked
Timer.Interval = 1000/24
Sub CSVtoArray
n = 0
p = 1
l = Text.GetLength(csv)
While p <= l
_p = Text.GetIndexOf(Text.GetSubTextToEnd(csv, p), ",")
If 0 < _p Then
n = n + 1
arry[n] = Text.GetSubText(csv, p, _p - 1)
p = p + _p
Else
n = n + 1
arry[n] = Text.GetSubTextToEnd(csv, p)
p = l + 1
EndIf
EndWhile
EndSub
Sub DrawLayout
GraphicsWindow.PenColor = "White"
GraphicsWindow.PenWidth = 4
y1 = 130
Stack.PushValue("local", y1)
x1 = 270
x2 = 470
x3 = 550
dy = 10
For i = 1 To 10
row = Math.Remainder(i - 1, 2) + 1
col = Math.Floor((i - 1) / 2) + 1
x = x1 + (col - 1) * 40
y = y1 + (row - 1) * 40 + 40
value = i
GetIndexOf()
iColor = Math.Remainder(index - 1, 2) + 1
GraphicsWindow.BrushColor = color[iColor]
GraphicsWindow.FillRectangle(x, y, 40, 40)
If i < 10 Then
dx = 15
Else
dx = 12
EndIf
x = x1 + (col - 1) * 40 + dx
y = y1 + (row - 1) * 40 + 40 + dy
GraphicsWindow.BrushColor = "White"
GraphicsWindow.DrawText(x, y, i)
EndFor
dx = 35
x = x1 + dx
y = y1 + dy
GraphicsWindow.DrawText(x, y, "1-5")
x = x + 100
GraphicsWindow.DrawText(x, y, "6-10")
dx = 24
x = x1 + 200 + dx
y = y1 + 40 + dy
GraphicsWindow.DrawText(x, y, "ODD")
y = y + 40
GraphicsWindow.DrawText(x, y, "EVEN")
For i = 1 To 5
y = y1 + (i - 1) * 40
If i = 1 Or i = 5 Then
x = x2
Else
x = x3
EndIf
GraphicsWindow.DrawLine(x1, y, x, y)
EndFor
x = x1
y2 = y1 + 40 * 4
GraphicsWindow.DrawLine(x, y1, x, y2)
x = x1 + 40 * 5
y2 = y1 + 40 * 4
GraphicsWindow.DrawLine(x, y1, x, y2)
y1 = y1 + 40
y2 = y1 + 40 * 2
For i = 2 To 8
If i <> 6 And i <> 7 Then
x = x1 + (i - 1) * 40
GraphicsWindow.DrawLine(x, y1, x, y2)
EndIf
EndFor
x = x1 + 100
y1 = Stack.PopValue("local")
y2 = y1 + 40
GraphicsWindow.DrawLine(x, y1, x, y2)
y1 = y1 + 120
y2 = y1 + 40
GraphicsWindow.DrawLine(x, y1, x, y2)
x = x1 + 30
y = y1 + 10
width = 40
height = 20
GraphicsWindow.BrushColor = "Black"
FillRhombus()
x = x + 100
GraphicsWindow.BrushColor = "DarkRed"
FillRhombus()
EndSub
Sub DrawRoulette
r4 = 441 / 2 * 0.9 * scale
r3 = r4 * 0.8
r2 = r3 * 0.8
r1 = r3 * 0.5
r0 = r3 * 0.1
rb = r1 * 0.15
r = r1
a = -92
fs = (r3 - r2) * 0.8
GraphicsWindow.BackgroundColor = "Green"
GraphicsWindow.BrushColor = "#331800"
GraphicsWindow.FillEllipse(x0 - r4, y0 - r4, r4 * 2, r4 * 2)
url = "http://www.nonkit.com/smallbasic.files/Roulette10.png"
wheel = Shapes.AddImage(url)
Shapes.Move(wheel, x0 - r3 / scale, y0 - r3 / scale)
Shapes.Zoom(wheel, scale, scale)
EndSub
Sub FillRhombus
' param x, y - left top position
' param width, height - size of the rhombus
Stack.PushValue("local", x)
Stack.PushValue("local", y)
x[0] = x
y[0] = y
x[1] = x[0] + width / 2
y[1] = y[0]
x[2] = x[1]
y[2] = y[0] + height
x[3] = x[0]
y[3] = y[0] + height / 2
GraphicsWindow.FillTriangle(x[1], y[1], x[2], y[2], x[3], y[3])
x[3] = x[0] + width
GraphicsWindow.FillTriangle(x[1], y[1], x[2], y[2], x[3], y[3])
y = Stack.PopValue("local")
x = Stack.PopValue("local")
EndSub
Sub GetIndexOf
' param value - to search
' return index
max = Array.GetItemCount(arry)
For index = 1 To max
If arry[index] = value Then
Goto break
EndIf
EndFor
break:
EndSub
Sub OnButtonClicked
da = 0.1
da0 = Math.GetRandomNumber(10) + 10
t0 = Clock.ElapsedMilliseconds
tl = Clock.ElapsedMilliseconds
Timer.Tick = OnTick
EndSub
Sub OnTick
t1 = Clock.ElapsedMilliseconds
dt = (t1 - tl) / 1000
tl = t1
If r1 - r < 0 Then
sign = -1
ElseIf r1 - r = 0 Then
sign = 0
Else
sign = 1
EndIf
ar = 30
vr = vr + sign * ar * dt
vr = vr * 0.99 * Math.Abs(r - r1) / (Math.Abs(r - r1) + 1)
dr = vr * dt
r = r + dr
If 0 < da Then
da = da0 - (t1 - t0) / 1000
ElseIf Math.Abs(r - r1) < 1 Then
Timer.Tick = DoNothing
EndIf
va = (va + Math.GetRadians(da) * 50) * 0.99
a = a + va * dt
_a = Math.GetRadians(va * dt)
r = Math.Abs(r / Math.Cos(_a))
If r2 < r Then
r = r - (r - r2)
EndIf
Math_PolarToCartesian()
Shapes.Move(ball, x - rb, y - rb)
If 0 < da Then
angle = angle + da
If 360 <= angle Then
angle = angle - 360
EndIf
Shapes.Rotate(wheel, angle)
EndIf
EndSub
Sub DoNothing
EndSub
Sub Math_PolarToCartesian
' param r
' param a
' param x0, y0
' return x, y
_a = Math.GetRadians(a)
x = x0 + r * Math.Cos(_a)
y = y0 + r * Math.Sin(_a)
EndSub