0

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?

S.B
  • 13,077
  • 10
  • 22
  • 49
Aldi Sula
  • 11
  • 3
  • 3
    That sounds about right. What happened when you tried it out? – quamrana Mar 19 '22 at 20:19
  • @quamrana I actually wasn't able to make it work because it gave errors and the image didn't display. So I'm not exactly sure how I can move the draw method from Bullet to FlyingObject. I've started changing some things above as the commented lines show. Does it look ok or there's another approach I should follow? – Aldi Sula Mar 19 '22 at 20:26
  • 4
    Please share the error – Pitto Mar 19 '22 at 20:26
  • 1
    We can only help you with what we see here on stackoverflow. Please update your code with the code you tried, plus the error traceback. We will be able to figure out what's happening then. – quamrana Mar 19 '22 at 20:29

1 Answers1

1

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"
Ziur Olpa
  • 1,839
  • 1
  • 12
  • 27