I'm confused as to how to correctly implement a game manager-type class (that controls most aspects of the program, such as the main loop) with the variables and constants of that program. From what I understand, the variables and constants should be defined in the main function, and then somehow passed onto the game manager. However, that presents some obstacles. So far, I've come up with 3 options:
Defining variables in the Main function and passing them on to the class (most standard)
class Game: def __init__(self, size, speed): self.size = size self.speed = speed def main(): SCREEN_SIZE = 10 GAME_SPEED = 30 game = Game(SCREEN_SIZE, GAME_SPEED) game.mainloop() if __name__ == "__main__": main()
Defining the variables in the class itself
class Game: def __init__(self): self.screen_size = 10 self.game_speed = 30 def main(): game = Game() game.mainloop() if __name__ == "__main__": main()
Making the variables global
SCREEN_SIZE = 10 GAME_SPEED = 30 class Game: def __init__(self): pass def main(): game = Game() game.mainloop() if __name__ == "__main__": main()
- (possible option) Not having a game manager class at all.
Number 1 would become annoying with larger programs that have many more variables, and, consequently, much more information to pass on to the class. Number 2 goes against the point of a class. Number 3 just seems like bad practice. Is there an alternative that I'm missing?