To get the application started, we need a method that will be triggered by the user hitting the mouse button. The Apple library uses a function called mouseButtonDown for this purpose. When the mouse goes down, we give the cue ball some energy. Here the cue ball is being held in a global variable. We also set the direction of the cue ball as away from the mouse position. Then we loop over every ball, updating each in turn. As long as some ball is moving, the loop continues.
The important thing to note about this program is that the overall control is rather simple, and that the effect is achived by putting more responsibility into the individual objects themselves. Each ball is responsible for its own behavior, the controlling program knows very little about what is happening to any individual ball.