Apr-10-2024, 07:54 AM
Hi, I've been working on a game that requires classes and subclasses. In order to keep some sort of structure, I wanted to put them in different files. Of course with subclasses comes the super() keyword. For single inheritance this does not pose a problem. But when it comes down to multiple inheritance I can't wrap my head around it.
In BaseClasses.py are all the classes most other classes inherit from. The ones in my problem all inherit from Object:
The weird thing here is that line 11 in CollisionObjects.py is the super().__init__() line in the Collision_Object class. It doesn't inherit from Rectangle, yet it appears to try and initialize it. Writing this out, it makes me think there might be a problem with the MRO?
Anyways, any help is appreciated.
In BaseClasses.py are all the classes most other classes inherit from. The ones in my problem all inherit from Object:
class Object: x = 0 y = 0 def __init__(self, x, y) -> None: self.x = x self.y = yAll the visual classes are contained in Shapes.py. The important ones here are Rectangle and its base Shape
class Shape(BaseClasses.Object): def __init__(self, x, y) -> None: super().__init__(x, y) class Rectangle(Shape): size_x = 0 size_y = 0 color = (0, 0, 0) def __init__(self, x, y, size_x, size_y, color) -> None: super().__init__(x, y) self.size_x = size_x self.size_y = size_y self.color = colorLastly there's CollisionObjects.py, where the problem occurs.
class Collision_Object(BaseClasses.Object): collider_size_x = 0 collider_size_y = 0 def __init__(self, x, y, collider_size_x, collider_size_y): super().__init__(x, y) self.collider_size_x = collider_size_x self.collider_size_y = collider_size_y class Static_Object(Collision_Object, Shapes.Rectangle): Color = (255, 0, 0) def __init__(self, x, y, size_x, size_y): print(Static_Object.__mro__) super().__init__(x, y, size_x, size_x) super().__init__(x,y, size_x, size_y, self.Color)I call the Static_Object class from the main game code with the line
StaticObject = CollisionObjects.Static_Object(400, 300, 40, 40)I've tried a lot of different ways of using super() in the Static_Object class, but none of them seem to work. The way I've got it set up now gives me the following error:
Quote:Exception has occurred: TypeError
Rectangle.__init__() missing 3 required positional arguments: 'size_x', 'size_y', and 'color'
File "C:\Users\Manu Coppieters\Documents\Tomb of the mask\CollisionObjects.py", line 11, in __init__
super().__init__(x, y)
File "C:\Users\Manu Coppieters\Documents\Tomb of the mask\CollisionObjects.py", line 28, in __init__
super().__init__(x, y, size_x, size_x)
File "C:\Users\Manu Coppieters\Documents\Tomb of the mask\Game.py", line 18, in <module>
StaticObject = CollisionObjects.Static_Object(400, 300, 40, 40)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: Rectangle.__init__() missing 3 required positional arguments: 'size_x', 'size_y', and 'color'
The weird thing here is that line 11 in CollisionObjects.py is the super().__init__() line in the Collision_Object class. It doesn't inherit from Rectangle, yet it appears to try and initialize it. Writing this out, it makes me think there might be a problem with the MRO?
Anyways, any help is appreciated.