This doesn’t directly answer your question but I think it will give you some ideas for how to implement the game loop.