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