I’m in the process of linking a game (made in pygame) together.
The directory has several files that contain classes for different components of the game, which all have corresponding demos (if not called from the main file)
Some of these components control the game (client socket, input handler) while others only use the game object for a config dict and are accessed from by the controllers, e.g. the client receiving a chat message will print the message to the game’s chat system like this:
if message.startswith("MSG"): self.game.chat.print(message(3:))
Where the object “game” contains the other components of the system and the gamestate (if the user is in the menus, etc).
Right now, in the class Game, these components are made by passing self into them, for example:
class Game: def __init__(self): self.my_object = Component(self) class Component: def __init__(self, game): self.game = game
What I’m wondering is whether I should import the game object in the files for the different components like this:
#in component file #import game object from game import game class Component: def __init__(self): pass #in main game file class Game: def __init__(self): #other parameters pass #create a Game object that can be imported game = Game()
or if I should continue using the previous method.
Any help appreciated :¬)