This is a schooling fish simulator. Please run in Turbo mode. (Hold down "Shift" when clicking the green flag.) Press "S" to make the fish "School". At first, the fish swim in a stationary way staying in their random starting position. There is also a Shark that you can control -- when you hold down the space bar, it follows the mouse. So you can see what happens when the shark affects the fish. Each fish is a clone. So, it has the same movement algorithm as all of the other fish. But together, the individual fish become a school with behavior that is a different than you might predict. Here is the way a fish thinks: a) get closer to my 3 nearest neighbors. b) if I'm too close to any fish, move a little bit away. c) if a shark is nearby, move above or below it. (And turn red <grin>!) A few secret keypresses: "z" Says the ID of each fish "p" Thinks the IDs of the 3 closest fish. "l" Thinks the current target position that the fish is swimming towards. "k" The distance^2 to the nearest fish, and the ID of that fish. "x" Clears all thoughts and words from the fish.
Thanks to @ExperienceSea, @-CoralReef-, and @Except for hints and helpful discussions. If you don't run in Turbo, it might be a bit glitchy. For example, all of the fish might sink. You can try adjusting the number of fish that are used by changing the "num_fish" value in the "Student" Sprite. Reynolds, CW (1987). "Flocks, herds and schools: A distributed behavioral model". Computer Graphics. 21 (4): 25–34.