1

So i'm trying to shoot a bullet. the idea is to create a list with the variables needed to create and bullet instance and add that list to a list. then i create a bullet instance from the last item in the list that contains lists. sadly i got the "TypeError: 'tuple' object is not callable" for the line where i try to create a new bullet instance. i hope you can help me, the variable names should be self explaining.

def shoot(self):
        if self.bullets_isshot == True:
            self.m_x, self.m_y = pygame.mouse.get_pos()
            self.bullets_new_bullet = (self.m_x, self.m_y, self.cords_x, self.cords_y)
            self.bullets_list.append(self.bullets_new_bullet)
            for bullet in self.bullets_list:
                if bullet == self.bullets_list[-1]:
                    new_bullet = bullet(bullet[0], bullet[1], bullet[2], bullet[3])
            self.bullets_isshot = False 

and in case you need it the bullet init method:

def __init__(self, target_x, target_y, player_x, player_y):
        #var img, cords
        self.img = fast_load_img('/data/img/bullets/fireball.png')
        self.width = 128
        self.height = self.width
        self.x = player_x
        self.y = player_y
        self.target_x = target_x
        self.target_y = target_y
        self.d_x = self.target_x - self.x
        self.d_y = self.target_y - self.y

        #var vel, angle
        self.vel = 5
        self.angle = math.atan2(self.d_x, self.d_y)
        self.x_change = self.vel * math.cos(self.angle)
        self.y_change = self.vel * math.sin(self.angle)
2
  • 1
    Look at your data types. You have a list of tuple, but you're trying to invoke/call the tuple which is an invalid action. You're not actually instantiating the class Commented Oct 7, 2020 at 15:42
  • @MaximilianBurszley i just realized it to and changed the tuple to a list but is didnt make any difference except it says "list" object not callable now Commented Oct 7, 2020 at 15:47

1 Answer 1

2

It looks like you're creating a list of a single item, then iterating over it. This is unnecessary.

You don't really need the list or the tuple at all. Just create a new bullet with the class constructor. You don't seem to store the new bullet anywhere, so I've just returned it from the function. Probably you should store it in a list or suchlike (assuming more than a single bullet is on-screen at a time).

def shoot( self ):
    """ If the player has fired a shot, create a new bullet.
        Returns the new bullet or None """
    new_bullet = None
    if self.bullets_isshot == True:
        self.m_x, self.m_y = pygame.mouse.get_pos()
        new_bullet = bullet( self.m_x, self.m_y, self.cords_x, self.cords_y )
        self.bullets_isshot = False 
    return new_bullet    
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.