wrote algorithm for game loop