0

I'm creating a Space Invaders with PyGame, and I would like to create a subclass of the class Alien, to simplify my code a bit. How would I do this? This is the code i have so far:

class Alien(pygame.sprite.Sprite):
    def __init__(self, width, height):
        super().__init__()
        self.image = pygame.Surface([width, height])
        self.image.fill(RED)
        self.image = pygame.image.load("alien1.png").convert_alpha()
        self.rect = self.image.get_rect()
Dan Hill
  • 43
  • 5
  • Possible duplicate of [Python: How do I make a subclass from a superclass?](https://stackoverflow.com/questions/1607612/python-how-do-i-make-a-subclass-from-a-superclass) – Micheal O'Dwyer Feb 18 '18 at 15:03

1 Answers1

3

In fact, you've just created a subclass of Sprite. Just do the same with Alien.

class Reptilian(Alien):
    def __init__(self, width, height, human_form):  # you can pass some other properties
        super().__init__(width, height)  # you must pass required args to Alien's __init__
        # your custom stuff:
        self.human_form = human_form
kszl
  • 1,203
  • 1
  • 11
  • 18