Pygame Sprite 在 Surface 上绘制问题
Pygame Sprite Drawing on Surface issue
我遇到的问题是我的源对象不是表面,问题不在我的代码中,而是在为 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')
Draw/Update:
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())
我遇到的问题是我的源对象不是表面,问题不在我的代码中,而是在为 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')
Draw/Update:
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())