3
所以我正在用pygame編寫一個python遊戲,並且我爲不同的sprites分開了一些類(類型爲pygame.sprite.Sprite) - 但它們都共享很多常見的物理代碼。我如何擴展基本精靈類,以便共同的物理內容寫入一次,並且我只是向每個需要的精靈類添加類特定的東西?擴展pygame sprite類
例如從這一變化:
class ShipSprite(pygame.sprite.Sprite):
def __init__(self, start_position=(500,500)):
# Call the sprite initialiser
pygame.sprite.Sprite.__init__(self)
init_stuff()
def common_physics_stuff()
pass
def ship_specific_stuff()
pass
class AsteroidSprite(pygame.sprite.Sprite):
def __init__(self, start_position=(500,500)):
# Call the sprite initialiser
pygame.sprite.Sprite.__init__(self)
init_stuff()
def common_physics_stuff()
pass
def asteroid_specific_stuff()
pass
進入這個
class my_custom_class()
def common_physics_stuff()
pass
class ShipSprite(my_custom_class):
def __init__(self, start_position=(500,500)):
# Call the sprite initialiser
pygame.sprite.Sprite.__init__(self)
init_stuff()
def ship_specific_stuff()
pass
class AsteroidSprite(my_custom_class):
def __init__(self, start_position=(500,500)):
# Call the sprite initialiser
pygame.sprite.Sprite.__init__(self)
init_stuff()
def asteroid_specific_stuff()
pass
你就不能使用'my_custom_class(pygame.sprite.Sprite)'做你的自定義的繼承自基類? – SuperBiasedMan