Child Classes: Builder Drone/Remover(Eliminator) Drone
Apply Inheritance to Drone
Based on Drone
, we'll design two subclasses, Builder
and Eliminator
. They both inherit all the methods in Drone
, and also have their own functions: Builder
drone can put a block below it's location; while Eliminator
drone can remove the block under itself away.
A very basic Builder
drone has a simple method to put a block at location(x,y,z)
Note that Builder
drone will put block right under it's location
class Builder_basic(Drone):
def putBlock(self, m):
block(self.x, (self.y-1), self.z, m)
# test a Builder drone
d1 = Builder_basic(5,5,-20)
d1.show()
d1.putBlock('brick')
d1.goTo(10, 10, -20)
d1.show()
d1.putBlock('cobblestone')
d1
as a Builder_basic
instance object(we call it a drone), it can perform all the functions of a drone(from the parent class Drone
), it can also do a basic task: put a block right below it's location.
Just one thing bothers me, when I call d1.show()
and d1.putBlock()
, it puts a block right away before finish showing itself. I like these actions to be just like in real life, finishing the 'show' and then put a block.
So here is the upgraded version of Builder
class:
# upgraded Builder
class Builder(Drone):
def setBlock(self, m='brick'):
game.set_timer((self.n+1), block, self.x, (self.y-1),self.z, m)
# test the new Builder drone
my_builder = Builder(2,4,-20)
my_builder.show()
my_builder.putBlock('box_red')
my_builder.goTo(4,6,-15)
my_builder.show()
my_builder.putBlock('box_yellow')
Enjoy the show!
Eliminator
Similar to Builder
, we can define another child class of Drone
that can remove blocks away.
Use material 'air'
in Eliminator
, note that Eliminator
drone will remove block right under it's location
class Eliminator(Drone):
def show(self):
game.set_timer(self.n+0.2, block, self.x, self.y, self.z,'pumpkin_face_on')
game.set_timer(self.n+0.4, block, self.x, self.y, self.z, 'pumpkin_face_off')
game.set_timer(self.n+0.6, block, self.x, self.y, self.z, 'pumpkin_face_on')
game.set_timer(self.n+0.8, block, self.x, self.y, self.z, 'pumpkin_face_off')
game.set_timer(self.n+1, block, self.x, self.y, self.z, 'brick')
def removeBlock(self):
game.set_timer((self.n+1), block, self.x, (self.y-1),self.z, 'air')
# build a brick wall to test Eliminator drone
wall(0,1,-20, 20, 20, 'brick')
# a Eliminator drone (make sure it's on the wall)
e = Eliminator(0, 3, -20)
e.show()
e.removeBlock()
e.moveUp(2)
e.show()
e.removeBlock()
e.moveRight(10)
e.show()
e.removeBlock()
e
as a Eliminator
drone, it can move to different locations(even go through solid wall), show itself, and remove blocks.
A note, Eliminator
overrides the parent method show()
, (now it's object has a pumpkin face), can you figure out why we have to override this method? If it's difficult, first try Eliminator
that didn't override this method.