-1

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:

  1. 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()
    
  2. 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()
    
  3. 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()
    
  4. (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?

Vadim Kotov
  • 7,103
  • 8
  • 44
  • 57
Anton
  • 1
  • 1
  • 1
  • 1
    Possible duplicate of [Creating a singleton in Python](http://stackoverflow.com/questions/6760685/creating-a-singleton-in-python) – AChampion Apr 05 '17 at 03:12
  • I helped someone out asking questions pertaining to a singleton like code he was working on a while ago. His question (and the working code he was referencing) is here and could prove useful to your cause: http://stackoverflow.com/questions/42226099/why-does-a-scene-take-a-scene-manager-as-a-parameter/ – oxrock Apr 05 '17 at 08:29
  • In that case, he had two main classes that controlled the game (the Director and Scene classes). I'm aiming for just having one, as that seems more efficient and logical than having two separate classes that can be combined. – Anton Apr 05 '17 at 13:46
  • There is nothing wrong with #3. Using global *constants* (immutable) is perfectly acceptable. You run into trouble when you start using global variables (mutable). – Remolten Apr 06 '17 at 02:11

1 Answers1

0

Here is how i would do it:

class Game(object):
    def __new__(cls):  
        if not hasattr(cls, 'instance'):
            cls.instance = super(Game, cls).__new__(cls)
        return cls.instance

    screen_size = 10
    game_speed = 30

Then you can just instanciate that in your main().

Leonardo Chirivì
  • 1,746
  • 2
  • 18
  • 36