Blazian444
Blazian444

Reputation: 129

How do you fix: "TypeError: argument 1 must be pygame.Surface, not SpriteSheet"?

I am making a platformer game. I have a Level class as a blueprint, and I pass it through to Levels 1 and 2. However, I keep getting an error saying: "TypeError: argument 1 must be pygame.Surface, not SpriteSheet" (Full error at bottom) Does anyone know how to fix this? NOTE: The error is at the bottom.

class Level(object):

        def __init__(self, player):
    """ Constructor. Pass in a handle to player. Needed for when moving platforms
        collide with the player. """

    # Lists of sprites used in all levels. Add or remove
    # lists as needed for your game.
    self.platform_list = None
    self.enemy_list = None

    # Background image
    self.background = None

    # How far this world has been scrolled left/right
    self.world_shift = 0
    self.level_limit = -1000
    self.platform_list = pygame.sprite.Group()
    self.enemy_list = pygame.sprite.Group()
    self.player = player

    # Update everything on this level
    def update(self):
        """ Update everything in this level."""
        self.platform_list.update()
        self.enemy_list.update()

    def draw(self, screen):
        """ Draw everything on this level. """

        # Draw the background
        # We don't shift the background as much as the 
    sprites are shifted
        # to give a feeling of depth.
        screen.blit(self.background, (self.world_shift 
        // 3, 0))

        # Draw all the sprite lists that we have
        self.platform_list.draw(screen)
        self.enemy_list.draw(screen)


# Create platforms for the level
class Level01(Level):
    """ Definition for level 1. """

    def __init__(self, player):
        """ Create level 1. """

        # Call the parent constructor
        Level.__init__(self, player)

        self.background = 
        SpriteSheet('background_01.png')

        self.background.sprite_sheet.set_colorkey(constants.WHITE)
        self.level_limit = -2500


# Create platforms for the level
class Level02(Level):
    """ Definition for level 2. """

    def __init__(self, player):
        """ Create level 1. """

        # Call the parent constructor
        Level.__init__(self, player)

        self.background = SpriteSheet('background_02.png')

        self.background.sprite_sheet.set_colorkey(constants.WHITE)
        self.level_limit = -1000


Traceback (most recent call last):
  File 
"/Users/qingduliu/PycharmProjects/Platformer/platform_scroller.py", 
line 107, in <module>
    main()
  File 
"/Users/qingduliu/PycharmProjects/Platformer/platform_scroller.py", 
line 90, in main
    current_level.draw(screen)
  File "/Users/qingduliu/PycharmProjects/Platformer/levels.py", 
line 43, in draw
    screen.blit(self.background, (self.world_shift // 3, 0))
TypeError: argument 1 must be pygame.Surface, not SpriteSheet

Upvotes: 0

Views: 88

Answers (1)

LeopardShark
LeopardShark

Reputation: 4416

You can only blit surfaces to other surfaces; background is not a surface. Your code implies you can access its associated surface with self.background.sprite_sheet, so try changing this line:

screen.blit(self.background, (self.world_shift // 3, 0))

to this:

screen.blit(self.background.sprite_sheet, (self.world_shift // 3, 0))

Upvotes: 1

Related Questions