So I have some Python code that’s structured something like this;
class GameObject(pygame.spriteDirtySprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = None
self.rect = None
self.state = None
class Bullet(gameobject.GameObject):
FRAME = pygame.Rect(23, 5, 5, 5)
STATES = config.Enum('IDLE', 'FIRED', 'MOVING', 'COLLIDE', 'RESET')
def __init__(self):
gameobject.GameObject.__init__(self)
self.image = config.SPRITES.subsurface(self.__class__.FRAME)
self.rect = self.__class__.START_POS.copy()
self.state = self.__class__.STATES.IDLE
class ShipBullet(bullet.Bullet):
START_POS = pygame.Rect(somewhere)
def __init__(self):
super(bullet.Bullet, self).__init__()
self.add(ingame.PLAYER)
class EnemyBullet(bullet.Bullet):
START_POS = pygame.Rect(somewhere else)
def __init__(self):
super(bullet.Bullet, self).__init__()
self.add(ingame.ENEMIES)
These are actually in different files, but this is an inheritance issue, not a dependency issue.
Note that ShipBullet and EnemyBullet have different START_POS static members, but Bullet doesn’t. Since Bullet will never actually be created (if this were C++ I’d make it an abstract class), that’s intentional. My reasoning is that when I call Bullet.__init__() from its subclasses, however, said subclasses will refer to their own START_POS in initializing their members. That’s not the case, however; ShipBullet.rect (likewise for EnemyBullet) is None. I believe image might be None too, but I haven’t tested for that yet. Anyone mind helping me figure out what I’m doing wrong?
Use
super(EnemyBullet, self).__init__()(and similar forShipBullet).superuses the class in the first argument to determine the next base in the MRO.