Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #mix-in demo
- class BaseObject(object):
- def __init__(self, *args, **kwargs):
- pass
- class SolidObject(BaseObject):
- def __init__(self, *args, **kwargs):
- super(SolidObject, self).__init__(*args, **kwargs)
- # field declarations
- self.x = kwargs.get('x')
- self.y = kwargs.get('y')
- self.visibility = True
- #mix-in
- class DestroyableObject(BaseObject):
- def __init__(self, *args, **kwargs):
- super(DestroyableObject, self).__init__(*args, **kwargs)
- self.obj_life = 100
- def hit(self):
- if self.obj_life > 0:
- self.obj_life -= 20
- print 'hit! life left: ' + str(self.obj_life)
- else:
- self.visibility = False
- print 'broken'
- class Crate(SolidObject, DestroyableObject):
- def __init__(self, *args, **kwargs):
- super(Crate, self).__init__(*args, **kwargs)
- self.material = kwargs.get('material')
- def get_material(self):
- print self.material
- wooden_crate = Crate(None, material='wood', x=0.1, y=-0.2)
- cardboard_crate = Crate(None, material='cardboard', x=0.3, y=0.5)
- for _ in range(6):
- wooden_crate.hit()
- print wooden_crate.visibility
- print cardboard_crate.visibility
Advertisement
Add Comment
Please, Sign In to add comment