title: How to record N-body simulation as a video?
body: <p>I have implemented an N-body simulation using the Barnes-Hut optimisation in Python which runs at a not-unacceptable speed for N=10,000 bodies, but it's still too slow to watch real time. </p>
  
  <p>A new frame is generated each time-step, and to display the frame we must first calculate the new positions of the bodies, and then draw them all. For N=10,000, it takes about 5 seconds to generate one frame (this is waaay too high as Barnes-Hut should be giving better results). The display is done through the pygame module.</p>
  
  <p>I would thus like to record my simulation and replay it once after it's done at a higher speed.</p>
  
  <p><strong>How can I accomplish this without slowing down the program or exceeding memory limitations?</strong></p>
  
  <p>One potential solution is simply to save the pygame screen each timestep, but this is apparently very slow. </p>
  
  <p>I thought also about storing the list of positions of the bodies generated each time step, and then redrawing all the frames once the situation finishes. Drawing a frame still takes some time, but not as much time as it takes to calculate the new positions.</p>