Microsoft Small Basic

Program Listing: LWQ467-0
' Challenge of the month March 2013 Metronome 2013/03/04 By NaochanON
' LWQ467-0


GUI()
Controls.ButtonClicked=Onclicked


While "true"
while rotate="true"
N=N+PM
Shapes.SetText(msg,"Pitch="+WY) ' show pitch
'-------------------------------------------------------------------------
'TL=BH*0.7
NewTX=TL/2*math.Sin(Math.GetRadians(N))+tx+TW/2
NewTY=TL/2*(1- math.cos(Math.GetRadians(N)))+ty
Shapes.Move(tact[1],NewTX,NewTY) ' move tact
Shapes.Rotate(tact[1],N)
'-------------------------------------------------------------------------
WL=TL-WY+TL2/2
NewWX=WL*math.Sin(Math.GetRadians(N))+tx-TW2/2+TW
NewWY=ty+TL-WL*math.cos(Math.GetRadians(N))-TL2/2
Shapes.Move(tact[2],NewWX,NewWY) ' move weight
Shapes.Rotate(tact[2],N)
'-------------------------------------------------------------------------
dt=(200-wy)*0.2
Program.Delay(dt) ' timing delay
'-------------------------------------------------------------------------
If Math.Abs(N)>50 Then ' turning if N=+50 or -50
PM=-PM
If PM=-1 then
Sound.PlayClick()
EndIf
EndIf
endwhile
Program.Delay(100)
endwhile

Sub onclicked
cap=Controls.GetButtonCaption(Controls.LastClickedButton)
If cap="Go!" Then
rotate="true"
Controls.SetButtonCaption(btn,"Stop")
ElseIf cap="Stop" then
Controls.SetButtonCaption(btn,"Go!")
rotate="false"
N=0
TactPosition()
elseif cap="+" then
WY=WY+5
If WY>200 then
WY=200
EndIf
TactPosition()
elseif cap="-" then
WY=WY-5
If WY<60 then
WY=60
EndIf
TactPosition()
EndIf
EndSub

Sub GUI
GraphicsWindow.Title=" Small Metronome !! "
x0=150 ' position X
y0=100 ' position Y
BW=100 ' Width
BH=400 ' Height
WY=170 ' weight position
TW=6 ' tact width
TL=BH*0.7 ' tact height
TW2=40 ' weight Width
TL2=20 ' weight height
'-------------------------------------------------------------------------
GraphicsWindow.Width=BW*5
GraphicsWindow.Height=Y0+BH+20
GraphicsWindow.BackgroundColor="lightcyan"
GraphicsWindow.FontSize=20
btn= Controls.AddButton("Go!",x0+1.5*BW,BH-50) ' Go! or Stop button
PBTN=Controls.AddButton("+",x0+1.5*BW,BH+50) ' Plus pitch button
Controls.SetSize(PBTN,50,40)
MBTN=Controls.AddButton("-",x0+1.5*BW,BH) ' Minus Pitch button
Controls.SetSize(MBTN,50,40)
'-------------------------------------------------------------------------
msg= Shapes.AddText("Pitch= ") ' message
Shapes.Move(msg,x0,20)
'-------------------------------------------------------------------------
GraphicsWindow.BrushColor="Darkgreen"
GraphicsWindow.PenColor="Lime"
body[1]=Shapes.AddEllipse(BW,BH) ' body
Shapes.Move(body[1],x0,y0)
GraphicsWindow.BrushColor="Azure"
body[2]=Shapes.AddEllipse(BW*0.9,BH*0.9)
Shapes.Move(body[2],Shapes.GetLeft(body[1])+5,Shapes.GetTop(body[1])+10)
'-------------------------------------------------------------------------
GraphicsWindow.PenColor="Red"
tact[1]=Shapes.AddRectangle(TW,TL) ' tact w=6
GraphicsWindow.BrushColor="Darkgray"
GraphicsWindow.PenColor="gray"
tact[2]=Shapes.AddRectangle(TW2,TL2)
TactPosition()
'-------------------------------------------------------------------------
GraphicsWindow.BrushColor="darkgreen"
body[3]=Shapes.AddRectangle(BW,BH/3) ' base
Shapes.Move(body[3],Shapes.GetLeft(body[1]),Shapes.GetTop(body[1])+BH/3*2)
Shapes.SetOpacity(body[3],90)
'-------------------------------------------------------------------------
tx= Shapes.GetLeft(tact[1]) ' tact position X
ty= Shapes.GetTop(tact[1]) ' tact position Y
rotate="false"
PM=1 ' Plus Minus
EndSub

Sub TactPosition
Shapes.Move(tact[1],Shapes.GetLeft(body[1])+BW/2-tw/2,Shapes.GetTop(body[1])+20)
Shapes.Rotate(tact[1],N)
Shapes.Move(tact[2],Shapes.GetLeft(tact[1])-Tw2/2+TW/2,Shapes.GetTop(tact[1])+WY-TL2/2)
Shapes.Rotate(tact[2],N)
endsub