Microsoft Small Basic

Program Listing: FHV760-9
' Simon 0.61
' Simon clone for Small Basic written by Nonki Takahashi
'
' History:
' 0.61 2014-05-09 Added delay and set font name for Silverlight. (FHV760-9)
' 0.5 2013-12-06 Workaround for Silverlight. (FHV760-7)
' 0.2 2013-12-05 Completed as a game. (FHV760-0)
' 0.1a 2013-12-05 Graphics designed for mask bitmap. (FHV760)
'
SB_Workaround()
gw = 598
gh = 428
GraphicsWindow.Width = gw
GraphicsWindow.Height = gh
GraphicsWindow.Title = "Simon 0.61"
InitGame()
While "True"
PlayGame()
EndWhile
Sub PlayGame
inGame = "True"
seq = ""
nSeq = 0
While inGame
nSeq = nSeq + 1
delay = 1000 / (Math.Floor(nSeq / 5) + 1)
seq[nSeq] = Math.GetRandomNumber(4)
PlaySeq()
GetSeq()
Program.Delay(2000)
EndWhile
EndSub
Sub PlaySeq
For i = 1 To nSeq
s = seq[i]
Shapes.SetOpacity(mask[s], opacity["on"])
Sound.PlayMusic(note[s])
Program.Delay(delay)
Shapes.SetOpacity(mask[s], opacity["off"])
EndFor
EndSub
Sub GetSeq
For i = 1 To nSeq
invalidClick = "True"
While invalidClick
waitingClick = "True"
While waitingClick
Program.Delay(200)
EndWhile
CheckClick()
EndWhile
s = seq[i]
If clicked = s Then
Shapes.SetOpacity(mask[s], opacity["on"])
Sound.PlayMusic(note[s])
Shapes.SetOpacity(mask[s], opacity["off"])
Else
Sound.PlayChimeAndWait()
inGame = "False"
Goto break
EndIf
EndFor
break:
EndSub
Sub CheckClick
' param mx, my - coordinate where mouse clicked
' return clicked - 1..4 color of clicked button
' return invalidClick - True if clicked not on buttons
x = mx - cx
y = my - cy
dx = Math.Abs(x)
dy = Math.Abs(y)
d = Math.SquareRoot(Math.Power(x, 2) + Math.Power(y, 2))
If dx < 10 Or dy < 10 Or d < 100 Or 200 < d Then
invalidClick = "True"
Else
Math_CartesianToPolar()
clicked = Math.Floor(a / 90) + 1
invalidClick = "False"
EndIf
EndSub
Sub InitGame
cx = gw / 2
cy = gh / 2
note = "1=O5A16;2=O6C16;3=O6A16;4=O6E16;"
color = "1=Blue;2=Gold;3=Green;4=Red"
r2 = 200
bx = "1=" + cx + ";2=" + (cx - r2) + ";3=" + (cx - r2) + ";4=" + cx + ";"
by = "1=" + cy + ";2=" + cy + ";3=" + (cy - r2) + ";4=" + (cy - r2) + ";"
For i = 1 To 4
GraphicsWindow.BrushColor = color[i]
GraphicsWindow.FillRectangle(bx[i], by[i], r2, r2)
EndFor
r3 = 245
x3 = (gw - 2 * r3) / 2
y3 = (gh - 2 * r3) / 2
GraphicsWindow.PenColor = "White"
GraphicsWindow.PenWidth = 80
If silverlight Then
delta = 80
Else
delta = 0
EndIf
GraphicsWindow.DrawEllipse(x3 - delta / 2, y3 - delta / 2, 2 * r3 + delta, 2 * r3 + delta)
x2 = (gw - 2 * r2) / 2
y2 = (gh - 2 * r2) / 2
GraphicsWindow.PenColor = "Black"
If silverlight Then
Program.Delay(msWait)
EndIf
GraphicsWindow.PenWidth = 20
If silverlight Then
delta = 20
Else
delta = 0
EndIf
GraphicsWindow.DrawEllipse(x2 - delta / 2, y2 - delta / 2, 2 * r2 + delta, 2 * r2 + delta)
GraphicsWindow.DrawLine(x2, gh / 2, gw - x2, gh / 2)
GraphicsWindow.DrawLine(gw / 2, y2, gw / 2, gh - y2)
r1 = 100
x1 = (gw - 2 * r1) / 2
y1 = (gh - 2 * r1) / 2
GraphicsWindow.BrushColor = "Black"
GraphicsWindow.FillEllipse(x1, y1, 2 * r1, 2 * r1)
GraphicsWindow.BrushColor = "White"
GraphicsWindow.FontName = "Tahoma"
GraphicsWindow.FontSize = 50
xs = x1 + 22
ys = gh / 2 - 34
GraphicsWindow.DrawText(xs, ys, "Simon")
url = "http://www.nonkit.com/smallbasic.files/simonmask.png"
opacity = "on=0;off=50;"
For i = 1 To 4
mask[i] = Shapes.AddImage(url)
If silverlight Then
Program.Delay(msWait)
EndIf
Shapes.SetOpacity(mask[i], opacity["off"])
Shapes.Move(mask[i], bx[i], by[i])
Shapes.Rotate(mask[i], 90 * (i - 1))
EndFor
GraphicsWindow.MouseDown = OnMouseDown
EndSub
Sub OnMouseDown
mx = GraphicsWindow.MouseX
my = GraphicsWindow.MouseY
waitingClick = "False"
EndSub
Sub DrawMask
GraphicsWindow.BrushColor = "Black"
GraphicsWindow.FillEllipse(x2, y2, 2 * r2, 2 * r2)
GraphicsWindow.PenColor = "White"
GraphicsWindow.PenWidth = 90
GraphicsWindow.DrawEllipse(x3, y3, 2 * r3, 2 * r3)
r0 = 90
x0 = (gw - 2 * r0) / 2
y0 = (gh - 2 * r0) / 2
GraphicsWindow.BrushColor = "White"
GraphicsWindow.FillEllipse(x0, y0, 2 * r0, 2 * r0)
EndSub
Sub DrawGrid
GraphicsWindow.PenColor = "White"
GraphicsWindow.PenWidth = 2
For x = 120 To 620 Step 200
GraphicsWindow.DrawLine(x, 0, x, gh)
EndFor
For y = 40 To 440 Step 200
GraphicsWindow.DrawLine(0, y, gw, y)
EndFor
EndSub
Sub Math_CartesianToPolar
' Math | convert cartesian coodinate to polar coordinate
' param x, y - cartesian coordinate
' return r, a - polar coordinate
r = Math.SquareRoot(x * x + y * y)
If x = 0 And y > 0 Then
a = 90 ' [degree]
ElseIf x = 0 And y < 0 Then
a = -90
Else
a = Math.ArcTan(y / x) * 180 / Math.Pi
EndIf
If x < 0 Then
a = a + 180
ElseIf x > 0 And y < 0 Then
a = a + 360
EndIf
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