1

I'm using Pygame to make a platformer game, and I'm trying to use a Class to draw the platforms in each level.

Here is the class for Level01:

class Level01(Level):
def __init__(self):
    Level.__init__(self)

    walls = [[300, 600, 210, 70],
             [500, 500, 210, 70]
             ]

    for item in walls:
        wall = Wall(item[0], item[1], item[2], item[3])
        self.wall_list.add(wall)

The final line in this code (self.wall_list.add(wall) seems to be where I'm having the error. It returns 'AttributeError: 'Wall' object has no attribute 'add_internal'.

And here is the class for Wall:

class Wall(object):
def __init__(self, x, y, width, height):
    super().__init__()
    self.image = pygame.Surface([width, height])
    self.image.fill(green)

    self.rect = self.image.get_rect()
    self.rect.x = x
    self.rect.y = y

Can someone help me out?

EDIT: Here's the code for Level class:

class Level(object):
wall_list = None
enemy_sprites = None

def __init__(self):
    self.wall_list = pygame.sprite.Group()
    self.enemy_sprites = pygame.sprite.Group()
5
  • maybe super(Wall, self).__init__() ?
    – wolendranh
    Sep 9, 2015 at 8:34
  • @wolendranh: what would that do to help avoid the error with the Wall instance?
    – Martijn Pieters
    Sep 9, 2015 at 8:43
  • 2
    So what kind of object is wall_list here?
    – Martijn Pieters
    Sep 9, 2015 at 8:44
  • Yes, can you show us wall_list in class Level? Maybe use append instead of add?
    – Berci
    Sep 9, 2015 at 8:54
  • Tried using append instead, did not work. Also added all the Level class code.
    – L. Pye
    Sep 9, 2015 at 10:18

2 Answers 2

3

It seems to me wall_list is a Group (or any subclass of it), and you try to add a non-Sprite object to it (hint: Group calls add_internal on all sprites that get added).

If you want to use a Group with your Wall class, let Wall inherit from Sprite.

0

add_internal() is a pygame.sprite.Sprite() function. In order to fix this error, copy this Wall class:

class Wall(pygame.sprite.Sprite):
    def __init__(self, x, y, width, height):
        super().__init__()
        self.image = pygame.Surface([width, height])
        self.image.fill(green)

        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.