我正在使用PyGame创建一个Space Invaders,我想创建一个Alien类的子类,以简化我的代码。我该怎么做?这是我到目前为止的代码:
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()
答案 0 :(得分:3)
事实上,您刚刚创建了Sprite的子类。和外星人一样。
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