Проблема, с которой я сталкиваюсь, заключается в том, что мой исходный объект не является поверхностью, и проблема не в моем коде, а в коде, который проектировал спрайты для pygame, и я не знаю, как это исправить. Я все равно положу код, который я сделал, на случай, если проблема в нем.
Сообщение об ошибке:
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
Код в 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
Плитки:
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))
Макет:
crate_layout = import_csv_layout(level_data['crates'])
self.crate_sprites = self.create_tile_group(crate_layout, 'crates')
Нарисовать/обновить:
self.crate_sprites.update(self.world_shift)
self.crate_sprites.draw(self.display_surface)
Подпись конструктора StaticTile
:
def __init__(self, size, x, y, pos, surface):
Поэтому его нужно называть так (в 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())