0

In my game loop I am trying to draw numbers to represent the cost of a card. From my hand, I am pulling the name of the card, which is part of a class where .cost will call a number but when I throw it into the function below it says that str has no attribute cost:

class Text:
    def __init__(self, text, font, surface, x, y):
        self.text = text
        self.font = font
        self.surface = surface
        self.x = x
        self.y = y

    def draw_text(self):
        text_obj = textPromptFont.render(self.text, 1, (0,0,0))
        text_rect = text_obj.get_rect()
        text_rect.topleft = (self.x, self.y)
        self.surface.blit(text_obj, text_rect)

x= hand[currentHandImageIndex][:-4] #wont show code but printing results something like: Card_Name
text_card_stats_cost = Text(x.cost, textCardStatsFont, DISPLAYSURF, 1050, 200)

Text(x.cost, textCardStatsFont, DISPLAYSURF, 1050, 200).draw_text()

Here is the code for my card class if relevant:

class Card_Details_Players:
    def __init__(self, trait, pos, cost, attack, defense, ability, act_type, act_act, sig_type, sig_act, extra1, extra2):
        self.trait = trait
        self.pos = pos
        self.cost = cost
        self.attack = attack
        self.defense = defense
        self.ability = ability
        self.act_type = act_type
        self.act_act = act_act
        self.sig_type = sig_type
        self.sig_act = sig_act
        self.extra1 = extra1
        self.extra2 = extra2
immeeh
  • 59
  • 5
  • It looks like `x` is just a string (`str`) - you can check this using `print(type(x))` - not a `Card_Details_Players`, and hence won't have `cost`, which is only an attribute of `Card_Details_Players`. – Ollie Apr 05 '18 at 03:51
  • @Ollie is there a way around this? The problem for me is that the object I am trying to use is being pulled from a list, which is then used to call its attribute. – immeeh Apr 05 '18 at 06:07
  • It will work if you are getting an object of type `Card_Details_Players` from a list, rather than an object of type `str`. – Ollie Apr 05 '18 at 07:06
  • 1
    The problem is most likely in how you insert elements in the `hand` list, so you should probably post that part of the code, too. – ChatterOne Apr 05 '18 at 07:24

0 Answers0