Microsoft Small Basic

Program Listing: RZQ456
'==================================================
'Setup window and events
'==================================================

nball = 10
radius = 20
gw = (nball+1)*4*radius
gh = 500
GraphicsWindow.BackgroundColor = "LightBlue"
GraphicsWindow.Width = gw
GraphicsWindow.Height = gh
startButton = Controls.AddButton("START",10,10)
pauseButton = Controls.AddButton("PAUSE/RESUME",80,10)
colourButton = Controls.AddButton("CHANGE COLOUR",210,10)
Controls.ButtonClicked = OnButtonClicked

'==================================================
'Main game loop
'==================================================

While ("True")
If (nactive = 0) Then
EndIf
If (startFlag = 1) Then
initialise()
EndIf
If (colourFlag = 1) Then
changeColour()
EndIf
detectHit()
If (runFlag = 1) Then
For i = 1 To nball
xVel[i] = xVel[i]+xAcc[i]
yVel[i] = yVel[i]+yAcc[i]
xPos[i] = xPos[i]+xVel[i]
yPos[i] = yPos[i]+yVel[i]
'Bounce on bottom
If (yPos[i] > gh-radius) Then
yPos[i] = 2*(gh-radius)-yPos[i]
yVel[i] = -yVel[i]
EndIf
'Bounce on sides
If (xPos[i] < radius) Then
xPos[i] = 2*(radius)-xPos[i]
xVel[i] = -xVel[i]
EndIf
If (xPos[i] > gw-radius) Then
xPos[i] = 2*(gw-radius)-xPos[i]
xVel[i] = -xVel[i]
EndIf
Shapes.Move(ball[i],xPos[i]-radius,yPos[i]-radius)
EndFor
updateTime()
EndIf
Program.Delay(20)
EndWhile

'==================================================
'Subroutines
'==================================================

Sub initialise
startFlag = 0
runFlag = 1
colourFlag = 0
nactive = nball
GraphicsWindow.BrushColor = GraphicsWindow.GetRandomColor()
For i = 1 To nball
xPos[i] = i*4*radius
yPos[i] = 80
xVel[i] = (Math.GetRandomNumber(201)-101)/40
yVel[i] = 0
xAcc[i] = 0
yAcc[i] = 1
active[i] = 1
Shapes.Remove(ball[i])
ball[i] = Shapes.AddEllipse(2*radius,2*radius)
Shapes.Move(ball[i],xPos[i]-radius,yPos[i]-radius)
EndFor
startTime = Clock.ElapsedMilliseconds
EndSub

Sub changeColour
GraphicsWindow.BrushColor = GraphicsWindow.GetRandomColor()
For i = 1 To nball
If (active[i] = 1) Then
Shapes.Remove(ball[i])
ball[i] = Shapes.AddEllipse(2*radius,2*radius)
Shapes.Move(ball[i],xPos[i]-radius,yPos[i]-radius)
EndIf
EndFor
colourFlag = 0
EndSub

Sub detectHit
xM = GraphicsWindow.MouseX
yM = GraphicsWindow.MouseY
For i = 1 To nball
If (active[i] = 1) Then
dist = (xM-xPos[i])*(xM-xPos[i]) + (yM-yPos[i])*(yM-yPos[i])
If (dist < radius*radius) Then
Shapes.HideShape(ball[i])
active[i] = 0
nactive = nactive-1
Sound.PlayClick()
EndIf
EndIf
EndFor
EndSub

Sub updateTime
elapsed = 0.1*Math.Floor(0.5+(Clock.ElapsedMilliseconds-startTime)/100)
GraphicsWindow.BrushColor = GraphicsWindow.BackgroundColor
GraphicsWindow.FillRectangle(gw-80,0,100,30)
GraphicsWindow.BrushColor = "Black"
GraphicsWindow.FontSize = 20
GraphicsWindow.DrawText(gw-80,10,elapsed)
EndSub

'==================================================
'Event Subroutines
'==================================================

Sub OnButtonClicked
If (Controls.LastClickedButton = startButton) Then
startFlag = 1
ElseIf (Controls.LastClickedButton = pauseButton) Then
runFlag = 1-runFlag
If (runFlag = 0) Then
pauseTime = Clock.ElapsedMilliseconds-startTime
Else
startTime = Clock.ElapsedMilliseconds-pauseTime
EndIf
ElseIf (Controls.LastClickedButton = colourButton) Then
colourFlag = 1
EndIf
EndSub