我对python还比较陌生,正在读一本关于用pygame创建游戏的书。我在书中偶然发现了这段代码。我不明白为什么Sprite.__init__(self)
是必要的。我已经运行了这个注释掉的代码,它以同样的方式运行。你知道吗
class Creep(Sprite):
def __init__(
self, screen, img_filename, init_position,
init_direction, speed)
Sprite.__init__(self)
self.screen = screen
self.speed = speed
self.base_image = pygame.image.load(img_filename).convert_alpha()
self.image = self.base_image
有人知道为什么会有这个,为什么人们叫它init吗?你知道吗
这是调用超类初始化方法。大概,
Sprite
会进行一些初始化,您也需要在子类中进行初始化,而在Python中,您需要显式地调用它。你知道吗但是,通常您可以通过调用
super
:相关问题 更多 >
编程相关推荐