'==================================================
'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
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