So I'm writing a game in python with pygame, and I have separate classes (of the type pygame.sprite.Sprite) for my different sprites - however they all share a lot of common physics code. How can I extend the basic sprite class, so that common physics stuff is written once, and I just add class specific stuff to each sprite class needed?
e.g. Change from this:
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
Into this
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)to make your custom one inherit from the base class? - SuperBiasedMan