Home > Back-end >  Class attribute error while creating python object
Class attribute error while creating python object

Time:11-13

I am writing a small game. I want to create two objects from the same class and combine them into a group. But when creating the second object, an error occurs "AttributeError: 'Enemy' object has no attribute 'get_rect'"

Here is the code related to the problem

class Enemy(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = enemy
        self.rect = self.image.get_rect()
        self.rect.center = (200, 200)
        self.speedx = random.randint(1, 10)
        self.speedy = random.randint(1, 10)
        self.collis = 0
        self.enemy_health = 5
        self.enemy_rect = pygame.rect.Rect((self.rect.x   35, self.rect.y   20), (35, 15))
        self.skin = 0
        self.skin_index = 0
        self.skins = [enemy, enemy_1, enemy_2]
        self.left_skins = [pygame.transform.flip(self.skins[0], True, False),
                           pygame.transform.flip(self.skins[1], True, False),
                           pygame.transform.flip(self.skins[2], True, False)]
        self.move_enemy = 1

    def update(self, collis):

        self.enemy_rect = pygame.rect.Rect((self.rect.x   35, self.rect.y   20), (35, 15))
        if self.move_enemy == 1:
            if self.collis < 1:
                self.speedx = random.randint(1, 2)
                self.speedy = random.randint(1, 2)
                if player.player_x_pos > self.rect.x:
                    self.rect.x  = self.speedx
                    self.skin  = 1
                    if self.skin % 10 == 0:
                        self.skin_index  = 1
                        if self.skin_index > 2:
                            self.skin_index = 0
                    self.image = self.skins[self.skin_index]

                if player.player_y_pos   50 > self.rect.y:
                    self.rect.y  = self.speedy
                    self.skin  = 1
                    if self.skin % 10 == 0:
                        self.skin_index  = 1
                        if self.skin_index > 2:
                            self.skin_index = 0
                    self.image = self.skins[self.skin_index]

                if player.player_x_pos < self.rect.x:
                    self.rect.x -= self.speedx
                    self.skin  = 1
                    if self.skin % 10 == 0:
                        self.skin_index  = 1
                        if self.skin_index > 2:
                            self.skin_index = 0
                    self.image = self.left_skins[self.skin_index]

                if player.player_y_pos   50 < self.rect.y:
                    self.rect.y -= self.speedy
                    self.skin  = 1
                    if self.skin % 10 == 0:
                        self.skin_index  = 1
                        if self.skin_index > 2:
                            self.skin_index = 0
                    if player.player_x_pos < self.rect.x:
                        self.image = self.left_skins[self.skin_index]
                    else:
                        self.image = self.skins[self.skin_index]

            else:
                self.collis = 0
            # if self.enemy_health <= 0:
            #     del enemy
            print('Health:', self.enemy_health)
        if self.move_enemy == 2:
            if self.rect.y <= 550:
                self.rect.y  = 2
            else:
                self.rect.y -= 2

enemy = Enemy()
enemy2 = Enemy()
enemy2.rect.center = (100, 100)
scene_1_sprites.add(enemy2)
scene_1_sprites.add(enemy)

But the class has the necessary attribute

I tried to create the second object like this: enemy2 = enemy

But in the end there was only one object

CodePudding user response:

Yes, of course an Enemy does not have a get_rect method. The error message is very clear. The problem here is:

class Enemy(pygame.sprite.Sprite):
   def __init__(self):
       pygame.sprite.Sprite.__init__(self)
       self.image = enemy                      #<---
       self.rect = self.image.get_rect()        

This is because the enemy is itself an Enemy object:

enemy = Enemy()

Most likely you have used the same name (enemy) for the object instance of the pygame.Surface (image) and the object instance of the Enemy. You cannot use the same name for 2 different things (variables). So creating the first enemy" will work, but all subsequent ones will fail.

Change the name of the image:

enemy = pygame.image.load(....)

enemy_image = pygame.image.load(....)
class Enemy(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = enemy_image              #<---
  • Related