Fluid pendulum explains reversals of the large-scale circulation in thermal convection