0

I have looked through similar questions but I can't find the answer to my question

I'm making a game with Pygame and I am getting an error with one class that I am not getting with another. The idea is that this code returns a list of sprites in a group, called 'platform_group', that are touching the instance of the Goon class:

self.block_hit_list = pygame.sprite.spritecollide(self, self.level.platform_group, False)

And this line keeps returning this error:

line 251, in update
self.block_hit_list = pygame.sprite.spritecollide(self, self.level.platform_group, False)
AttributeError: 'int' object has no attribute 'platform_group'

The weird thing is that I am running into this problem with the Goon class, but not with the Player class. It uses the exact same line above, but it works exactly as it should for Player.

Code for Player and Goon classes:

class Player(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        self.image = pygame.Surface([40, 40])
        self.image.fill(RED)
        self.rect = self.image.get_rect()

        self.change_x = 0
        self.change_y = 0

        self.level = 0
        self.block_hit_list = []

    def update(self):
        self.calc_grav()

        self.rect.x += self.change_x
        self.block_hit_list = pygame.sprite.spritecollide(self, self.level.platform_group, False)

        for block in self.block_hit_list:
            if self.change_x > 0:
                self.rect.right = block.rect.left
            elif self.change_x < 0:
                self.rect.left = block.rect.right

class Goon(pygame.sprite.Sprite):
    def __init__(self, x, y):
        super().__init__()

        self.image = pygame.Surface([25, 25])
        self.image.fill(PURPLE)

        self.rect = self.image.get_rect()

        self.change_x = 0
        self.change_y = 0

        self.block_hit_list = []
        self.rect.x = x
        self.rect.y = y

        self.level = 0

def update(self):
    self.calc_grav

    self.rect.x += self.change_x
    self.block_hit_list = pygame.sprite.spritecollide(self, self.level.platform_group, False)
    for block in self.block_hit_list:
        if self.change_x > 0:
            self.rect.right = block.rect.left
            self.change_y = -6

This is some of the main code:

def main():
    pygame.init()
    size = [SCREEN_WIDTH, SCREEN_HEIGHT]
    screen = pygame.display.set_mode(size)
    global walls
    walls = []

    pygame.display.set_caption("Practice Game")

    player = Player()

    level_list = []
    level_list.append(Level_01(player))

    current_level_no = 0
    current_level = level_list[current_level_no]

    player.level = current_level
    current_level.enemy_group.level = current_level

    player.rect.x = player_spawn[0]
    player.rect.y = player_spawn[1]

    active_sprite_list.add(player)

    done = False
    clock = pygame.time.Clock()

And this is the class for Level_01:

class Level_01(Level):
def __init__(self):
    Level.__init__(self)
    self.level_limit = -1000

    x = y = 0

    level = ["WWWWWWWWWWWWWWWWWWWWWWWWWWWWWW",
             "W                            W",
             "W WWWW                       W",
             "W                            W",
             "WW                           W",
             "W                    WW      W",
             "W W           WWWWWWW     WWWW",
             "W                            W",
             "WWWWWWW               WWW    W",
             "W                    W       W",
             "W                           WW",
             "W             WWW        WWWWW",
             "W                            W",
             "W S      WWG                 W",
             "WWWWWWWWWWWWWWWWWWWWWWWWWWWWWW"
             ]


    for row in level:
        for col in row:
            if col == "W":
                block = Platform(x, y)
                self.platform_group.add(block)
            if col == "S":
                global player_spawn
                player_spawn = [x, y]
            if col == "G":
                goon = Goon(x, y)
                self.enemy_group.add(goon)
                self.enemy_list.append(goon)

            x += 40
        y += 40
        x = 0

Thanks!

L. Pye
  • 11
  • 1
  • 3

0 Answers0