Home > Software engineering >  How to use inheritance for this function
How to use inheritance for this function

Time:03-20

Hello I'm new in Python and this is my first post here! I am creating a small game using arcade library. One of the methods called draw is used to display the game images on the screen and it works fine. However, I'm reusing it in different files and repeating the same code so I'm trying to find a way to use inheritance. This is my parent class FlyingObject:

class FlyingObject(ABC):
    def __init__(self):
        # object location is Point()
        self.center = Point()
        self.velocity = Velocity()
        self.alive = True
        # self.img = img
        # self.texture = arcade.load_texture(self.img)
        # self.width = self.texture.width
        # self.height = self.texture.height
        self.radius = SHIP_RADIUS
        self.angle = 0
        self.speed = 0
        self.direction = 0

    def draw(self):
        pass

    def is_alive(self):
        return self.alive

    def advance(self):
        self.wrap()
        self.center.y  = self.velocity.dy
        self.center.x  = self.velocity.dx

    def wrap(self):
        # wraps the objects on the screen
        if self.center.x > SCREEN_WIDTH:
            self.center.x -= SCREEN_WIDTH
        if self.center.x < 0:
            self.center.x  = SCREEN_WIDTH
        if self.center.y > SCREEN_HEIGHT:
            self.center.y -= SCREEN_HEIGHT
        if self.center.y < 0:
            self.center.y  = SCREEN_HEIGHT

And one of the child classes named Bullet:

class Bullet(FlyingObject):
    def __init__(self, ship_angle, ship_x, ship_y, img):
        super().__init__()
        self.radius = BULLET_RADIUS
        self.life = BULLET_LIFE
        self.speed = BULLET_SPEED
        self.angle = ship_angle - 90
        self.center.x = ship_x
        self.center.y = ship_y
    
        bulletShot = Bullet("")
        bulletShot.draw(self)

    def fire(self):
        self.velocity.dx -= math.sin(
            math.radians(self.angle   90)) * BULLET_SPEED
        self.velocity.dy  = math.cos(
            math.radians(self.angle   90)) * BULLET_SPEED

    def draw(self):
        img = "images/laser.png"
        texture = arcade.load_texture(img)

        width = texture.width
        height = texture.height
        alpha = 255  # For transparency, 1 means transparent

        x = self.center.x
        y = self.center.y
        angle = self.angle

        arcade.draw_texture_rectangle(x, y, width, height, texture, angle,
                                      alpha)

    def advance(self):
        super().advance()
        self.life -= 1
        if self.life <= 0:
            self.alive = False

So the draw() method is used in the same way different modules just with different images. I'm assuming I should move the draw() method in FlyingObject() so that I can inherit it. How could I make that work?

CodePudding user response:

Exactly, you move (overwrite) the draw method into the FlyingObject and you pass the argument img:

def draw(self, img):

Or another option, you move the draw method, and then you put img as a parameter for the class in the init method:

def draw(self): # in FlyingObject
    img = self.img

And:

def __init__(self, ship_angle, ship_x, ship_y, img):
    super().__init__()
    self.img = "images/laser.png"
  • Related