0

I am making a platformer game where there are moving platforms. The way I have it is where you can enter a few parameters to customize the platform. However, I keep getting this error that I can't fix. The full error is at the bottom. I also think the issue may be somewhere at the bottom of the update section where 'world_shift' is used. If anyone could fix this, I would greatly appreciate it!

class Platform(pygame.sprite.Sprite):

    def __init__(self, sprite_sheet_data):


        super().__init__()


        sprite_sheet = SpriteSheet('tiles_spritesheet.png')


        self.image = sprite_sheet.get_image(sprite_sheet_data[0],
                                            sprite_sheet_data[1],
                                            sprite_sheet_data[2],
                                            sprite_sheet_data[3])

        self.rect = self.image.get_rect()


class MovingPlatform(Platform):

    def __init__(self, sprite_sheet_data):

        super().__init__(sprite_sheet_data)

        self.change_x = 0
        self.change_y = 0

        self.boundary_top = 0
        self.boundary_bottom = 0
        self.boundary_left = 0
        self.boundary_right = 0

        self.player = player

        self.level = level

   def update(self):

        self.rect.x += self.change_x

        self.rect.y += self.change_y

        hit = pygame.sprite.collide_rect(self, self.player)
        if hit:
            if self.change_x < 0:
                self.player.rect.right = self.rect.left
            else:             
                self.player.rect.left = self.rect.right

        hit = pygame.sprite.collide_rect(self, self.player)
        if hit:           
            if self.change_y < 0:
                self.player.rect.bottom = self.rect.top
            else:  
                self.player.rect.top = self.rect.bottom

        if self.rect.bottom > self.boundary_bottom or self.rect.top 
         < self.boundary_top:
            self.change_y *= -1

        cur_pos = self.rect.x - self.level.world_shift  
        if cur_pos < self.boundary_left or cur_pos > 
        self.boundary_right:
            self.change_x *= -1


class Level(object):


    def __init__(self, player):
        self.platform_list = None

        self.platform_list = pygame.sprite.Group()

    def update(self):
        self.platform_list.update()

    def draw(self, screen):

        self.platform_list.draw(screen)


class Level01(Level):

    def __init__(self, player):


        Level.__init__(self, player)

            for block in level_blocks:
            platform = Platform(block[0])
            platform.rect.x = block[1]
            platform.rect.y = block[2]
            platform.player = self.player
            self.platform_list.add(platform)

        block = MovingPlatform(STONE_PLATFORM_MIDDLE)
        block.rect.x = 1350
        block.rect.y = 280
        block.boundary_left = 1350
        block.boundary_right = 1600
        block.change_x = 1
        block.player = self.player
        block.level = self
        self.platform_list.add(block)


def main():
    """ Main Program Loop """

    # Loop until the user clicks the close button.
    done = False

    # Create all the levels
    level_list = [Level01(player)]

    # Sets the current level
    current_level_no = 0
    current_level = level_list[current_level_no]

    # -- Main Loop -- #
    while not done:

        # Updates items in the level
        current_level.update()

        # Goes to the next level if the player completes the current 
          one 
        current_position = player.rect.x + current_level.world_shift
        if current_position < current_level.level_limit:
            player.rect.x = 120
            if current_level_no < len(level_list) - 1:
                current_level_no += 1
                current_level = level_list[current_level_no]
                player.level = current_level

        current_level.draw(screen)

Traceback (most recent call last):
  File "/Users/qingduliu/PycharmProjects/Platformer/Video Game.py", 
line 665, in <module>
main()
  File "/Users/qingduliu/PycharmProjects/Platformer/Video Game.py", 
line 629, in main
current_level.update()
  File "/Users/qingduliu/PycharmProjects/Platformer/Video Game.py", 
line 274, in update
self.platform_list.update()
  File "/Users/qingduliu/Platformer/lib/python3.7/site- 
packages/pygame/sprite.py", line 463, in update
s.update(*args)
  File "/Users/qingduliu/PycharmProjects/Platformer/Video Game.py", 
line 172, in update
cur_pos = self.rect.x - self.level.world_shift
AttributeError: 'int' object has no attribute 'world_shift'
Blazian444
  • 131
  • 9
  • Add the arguments to the Platform `__init__` method. This is not really related to pygame, but to classes in general – Valentino Aug 08 '19 at 21:21
  • Now there is an error saying: "AttributeError: 'NoneType' object has no attribute 'world_shift'" – Blazian444 Aug 08 '19 at 21:27
  • It is in the last few lines of the update section – Blazian444 Aug 08 '19 at 21:28
  • It doesn't look like you're setting self.level to anything. It's being set to None in the `__init__` function of MovingPlatform, and then you're using it in update. That's why it's being called a NoneType; it's not set to anything. Maybe assign it by passing in the level object as a parameter of MovingPlatform's `__init__` function. – SnootierBaBoon Aug 08 '19 at 21:36
  • Now there is an error: "AttributeError: 'NoneType' object has no attribute 'world_shift'" – Blazian444 Aug 08 '19 at 21:46
  • It has to do with the last lines in the update function. – Blazian444 Aug 08 '19 at 21:47
  • II you've edited your code and got new errors, you should update your question with both the code and the full traceback of the errors. – Valentino Aug 08 '19 at 22:02
  • I've edited the question – Blazian444 Aug 08 '19 at 22:15
  • Are you setting self.level to an int? These attribute errors happen when you're getting a variable from a class (like `level.world_shift`) from something that doesn't have that variable (like an int). You can't expect an integer to have a `world_shift` element. I can't tell what exactly is happening from the limited code. It would help if you add more code. – SnootierBaBoon Aug 08 '19 at 22:35
  • Also, if the integer refers to a level number (like 1 for level01, 2 for level02), what you could do is create an instance of of those level classes and put it in a global list called levels (do `levels = [Level01(), Level02()]`. Then you can do `levels[self.level].world_shift` to get the `world_shift` value of the actual level object that corresponds with the level integer in self.level. Alternatively, you can set self.level to levels[integer] instead of setting it to the integer, where integer is whatever you originally set it to. – SnootierBaBoon Aug 08 '19 at 22:39
  • I set self.level to level and in the actual platform I put 1 as in level one – Blazian444 Aug 08 '19 at 22:40
  • Can you update the code in the question? I still can't tell because the code still has `self.level = None`. – SnootierBaBoon Aug 08 '19 at 22:42
  • I made 'self.level = level' and I also added the main loop in case that helps – Blazian444 Aug 08 '19 at 22:44
  • Double check that self.level is not being set to an integer. This error should only appear if self.level is being set to an integer in a MovingPlatform, rather than an instance of the Level() or Level01() class. – SnootierBaBoon Aug 08 '19 at 22:50
  • How do I set it to an instance of the Level class? – Blazian444 Aug 08 '19 at 22:51

0 Answers0