Daniel
Daniel

Reputation: 15

Pygame Sprite Drawing on Surface issue

The problem that I am encountering is that my source object is not a surface, and the problem is not in my code, but in the code that designed the sprites for pygame, and I don't know how to fix it. I'll put the code that I made in there anyway, in case it's the problem.

Error Message:

Traceback (most recent call last):
File "C:\Users\Daniel\Desktop\Thing.py", line 22, in <module>
level.run()
File "C:\Users\Daniel\Desktop\level2.py", line 63, in run
self.crate_sprites.draw(self.display_surface)
File "C:\Users\Daniel\AppData\Local\Programs\Python\Python310\lib\site- 
packages\pygame\sprite.py", line 551, in draw
self.spritedict.update(zip(sprites, surface.blits((spr.surface, spr.rect) 
for spr in sprites)))
TypeError: Source objects must be a surface

Code in sprites.py:

def draw(self, surface):
    """draw all sprites onto the surface

    Group.draw(surface): return Rect_list

    Draws all of the member sprites onto the given surface.

    """
    sprites = self.sprites()
    if hasattr(surface, "blits"):
        self.spritedict.update(zip(sprites, surface.blits((spr.surface, 
    spr.rect) for spr in sprites)))
    else:
        for spr in sprites:
            self.spritedict[spr] = surface.blit(spr.image, spr.rect)
            self.lostsprites = []
            dirty = self.lostsprites

Tiles:

class StaticTile(Tile):
def __init__(self, size, x, y, pos, surface):
    super().__init__(size, x, y, (x,y))
    self.image = surface
    self.surface = surface
    
class Crate(StaticTile):
    def __init__(self, size, x, y, pos, surface):
    super().__init__(size, x, y, 
    pygame.image.load('C:\\desktop\\game\\crate.png').convert_alpha(), (x, 
    y))

Layout:

crate_layout = import_csv_layout(level_data['crates'])
self.crate_sprites = self.create_tile_group(crate_layout, 'crates')

Draw/Update:

self.crate_sprites.update(self.world_shift)
self.crate_sprites.draw(self.display_surface)

Upvotes: 1

Views: 282

Answers (1)

Rabbid76
Rabbid76

Reputation: 210909

The signature of the constructor of StaticTile is:

def __init__(self, size, x, y, pos, surface):

So it needs to be called like this (in Crate. __init__)

super().__init__(size, x, y, pygame.image.load('C:\\desktop\\game\\crate.png').convert_alpha(), (x, y))

super().__init__(size, x, y, (x, y), 
    pygame.image.load('C:\\desktop\\game\\crate.png').convert_alpha())

Upvotes: 2

Related Questions