move main loop into a function of Game