This is a very sweet game. I'm very tempted to make a couple more levels just so I can play it some more.
As far as calculating standard deviation:
+ variance = 0
+ for each:
- variance_x = variance_x + (mean_x - current_object.x)^2
+ std_dev_x = sqrt(variance_x)
That will give you a true standard deviation. But it also requires going through the loop a second time. I think it would be faster to just calculate an approximate standard deviation:
+ Always
-> Set x_sum to 0
-> Set y_sum to 0
-> old_mean_x = mean_x
+ For each swarm_object
-> Add swarm_object.x to x_sum
-> Add swarm_object.y to y_sum
-> variance_x = variance_x + (old_mean_x - current_object.x)^2
+ Always
-> Set mean_x to x_sum / swarm_object.count
-> Set mean_y to y_sum / swarm_object.count
-> std_dev_x = sqrt(variance_x)
(Sorry for mixing syntaxes but I just had time for a quick post.)
If you want a better approximation for the standard deviation maybe adjust old_mean_x by taking into consideration which direction the swarm is traveling and shifting old_mean_x in that direction. (or maybe just shifting it towards the current mouse position) but I think the approximation I showed would be good enough.