如何解决:“ AttributeError:'int'对象没有属性'world_shift'”

时间:2019-08-08 21:14:39

标签: pygame python-3.7.4

我正在制作一个平台游戏,其中有移动平台。我拥有的方式是可以输入一些参数来自定义平台。但是,我不断收到无法修复的错误。完整的错误在底部。我还认为问题可能在使用“ world_shift”的更新部分的底部。如果有人可以解决此问题,我将不胜感激!

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'

0 个答案:

没有答案