源对象必须是表面
Source object must be a surface
我遇到了一个问题,它说源对象必须是一个表面。我以前问过这个问题,答案有效,但现在情况不同了。我看了我之前的问题,我想不出怎么解决。
错误信息:
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 131, in run
self.clouds.draw(self.display_surface, self.world_shift)
File "C:\Users\Daniel\Desktop\decoration.py", line 67, in draw
self.cloud_sprites.draw(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.image, spr.rect) for
spr in sprites)))
TypeError: Source objects must be a surface
基本上我是想把东西放到一个表面上,最让我困惑的是当我试图把水放到屏幕上时它起作用了,它有完全相同的代码但不是 'cloud' 它有 'water',但是水的闪烁有效而云没有。
水Class:
class Water:
def __init__(self, top, level_width):
water_start = -width
water_tile_width = 192
tile_x_amount = int((level_width + width) / water_tile_width)
self.water_sprites = pygame.sprite.Group()
for tile in range(tile_x_amount):
x = tile * water_tile_width + water_start
y = top
sprite = AnimatedTile(192, x, y, (x, y),
'C:\Desktop\Game\decoration\water')
self.water_sprites.add(sprite)
def draw(self, surface, shift):
self.water_sprites.update(shift)
self.water_sprites.draw(surface)
云Class:
class Clouds:
def __init__(self, horizon, level_width, cloud_number):
min_x = -width
max_x = level_width + width
min_y = 0
max_y = horizon
cloud_surface_list = 'C:\Desktop\Game\decoration\clouds'
self.cloud_sprites = pygame.sprite.Group()
for cloud in range(cloud_number):
cloud = choice(cloud_surface_list)
x = randint(min_x, max_x)
y = randint(min_y, max_y)
sprite = StaticTile(0, x, y, (x, y),
'C:\Desktop\Game\decoration\clouds')
self.cloud_sprites.add(sprite)
def draw(self, surface, shift):
self.cloud_sprites.update(shift)
self.cloud_sprites.draw(surface)
如您所见,draw 方法几乎是完全相同的代码。我可以发现可能导致问题的唯一区别是继承。 Water
继承自名为 AnimatedTile
的 class,而 Clouds
继承自 StaticTile
。我将把这两个的代码放在这里:
动画图块:
class AnimatedTile(Tile):
def __init__(self, size, x, y, pos, path):
super().__init__(size, x, y, (x, y))
self.frames = import_folder(path)
self.frame_index = 0
self.image = self.frames[int(self.frame_index)]
def animate(self):
self.frame_index += 0.15
if self.frame_index >= 4:
self.frame_index = 0
self.image = self.frames[int(self.frame_index)]
def update(self, shift):
self.animate()
self.rect.x += shift
静态平铺:
class StaticTile(Tile):
def __init__(self, size, x, y, pos, surface):
super().__init__(size, x, y, (x,y))
self.image = surface
self.surface = surface
绘图和更新:
self.clouds.draw(self.display_surface,
self.world_shift)
*我知道这些是表面,因为我用完全相同的方式绘制它们,将水滴到屏幕上(self.water.draw(self.display_surface, self.world_shift)
它起作用了。
其他代码:
class Level:
def __init__(self, level_data, surface):
self.display_surface = surface
self.world_shift = -8
平铺:
class Tile(pygame.sprite.Sprite):
def __init__(self, size, x, y, pos):
super().__init__()
self.image = pygame.Surface((size, size))
self.rect = self.image.get_rect(topleft = pos)
def update(self, shift):
self.rect.x += shift
world_shift
基本上是水平移动的速度(负数表示向右,正数表示向左)
如果这听起来很愚蠢而且答案很明显,我很抱歉。
您没有正确分配给 Clouds
中 StaticTile
精灵的 image
属性。我不确定是调用者 (Clouds
) 还是 tile class 弄错了,你必须决定 API 应该是什么。
不匹配是你的调用,这里:
sprite = StaticTile(0, x, y, (x, y),
'C:\Desktop\Game\decoration\clouds')
和StaticTile.__init__
方法,声明如下:
def __init__(self, size, x, y, pos, surface):
super().__init__(size, x, y, (x,y))
self.image = surface
self.surface = surface
请注意,调用代码将字符串作为 surface
参数传递。这类似于 AnimatedTile
的初始化方式(使用路径),但是 StaticTile
class 期望表面对象已经加载。
我遇到了一个问题,它说源对象必须是一个表面。我以前问过这个问题,答案有效,但现在情况不同了。我看了我之前的问题,我想不出怎么解决。
错误信息:
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 131, in run
self.clouds.draw(self.display_surface, self.world_shift)
File "C:\Users\Daniel\Desktop\decoration.py", line 67, in draw
self.cloud_sprites.draw(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.image, spr.rect) for
spr in sprites)))
TypeError: Source objects must be a surface
基本上我是想把东西放到一个表面上,最让我困惑的是当我试图把水放到屏幕上时它起作用了,它有完全相同的代码但不是 'cloud' 它有 'water',但是水的闪烁有效而云没有。
水Class:
class Water:
def __init__(self, top, level_width):
water_start = -width
water_tile_width = 192
tile_x_amount = int((level_width + width) / water_tile_width)
self.water_sprites = pygame.sprite.Group()
for tile in range(tile_x_amount):
x = tile * water_tile_width + water_start
y = top
sprite = AnimatedTile(192, x, y, (x, y),
'C:\Desktop\Game\decoration\water')
self.water_sprites.add(sprite)
def draw(self, surface, shift):
self.water_sprites.update(shift)
self.water_sprites.draw(surface)
云Class:
class Clouds:
def __init__(self, horizon, level_width, cloud_number):
min_x = -width
max_x = level_width + width
min_y = 0
max_y = horizon
cloud_surface_list = 'C:\Desktop\Game\decoration\clouds'
self.cloud_sprites = pygame.sprite.Group()
for cloud in range(cloud_number):
cloud = choice(cloud_surface_list)
x = randint(min_x, max_x)
y = randint(min_y, max_y)
sprite = StaticTile(0, x, y, (x, y),
'C:\Desktop\Game\decoration\clouds')
self.cloud_sprites.add(sprite)
def draw(self, surface, shift):
self.cloud_sprites.update(shift)
self.cloud_sprites.draw(surface)
如您所见,draw 方法几乎是完全相同的代码。我可以发现可能导致问题的唯一区别是继承。 Water
继承自名为 AnimatedTile
的 class,而 Clouds
继承自 StaticTile
。我将把这两个的代码放在这里:
动画图块:
class AnimatedTile(Tile):
def __init__(self, size, x, y, pos, path):
super().__init__(size, x, y, (x, y))
self.frames = import_folder(path)
self.frame_index = 0
self.image = self.frames[int(self.frame_index)]
def animate(self):
self.frame_index += 0.15
if self.frame_index >= 4:
self.frame_index = 0
self.image = self.frames[int(self.frame_index)]
def update(self, shift):
self.animate()
self.rect.x += shift
静态平铺:
class StaticTile(Tile):
def __init__(self, size, x, y, pos, surface):
super().__init__(size, x, y, (x,y))
self.image = surface
self.surface = surface
绘图和更新:
self.clouds.draw(self.display_surface,
self.world_shift)
*我知道这些是表面,因为我用完全相同的方式绘制它们,将水滴到屏幕上(self.water.draw(self.display_surface, self.world_shift)
它起作用了。
其他代码:
class Level:
def __init__(self, level_data, surface):
self.display_surface = surface
self.world_shift = -8
平铺:
class Tile(pygame.sprite.Sprite):
def __init__(self, size, x, y, pos):
super().__init__()
self.image = pygame.Surface((size, size))
self.rect = self.image.get_rect(topleft = pos)
def update(self, shift):
self.rect.x += shift
world_shift
基本上是水平移动的速度(负数表示向右,正数表示向左)
如果这听起来很愚蠢而且答案很明显,我很抱歉。
您没有正确分配给 Clouds
中 StaticTile
精灵的 image
属性。我不确定是调用者 (Clouds
) 还是 tile class 弄错了,你必须决定 API 应该是什么。
不匹配是你的调用,这里:
sprite = StaticTile(0, x, y, (x, y),
'C:\Desktop\Game\decoration\clouds')
和StaticTile.__init__
方法,声明如下:
def __init__(self, size, x, y, pos, surface):
super().__init__(size, x, y, (x,y))
self.image = surface
self.surface = surface
请注意,调用代码将字符串作为 surface
参数传递。这类似于 AnimatedTile
的初始化方式(使用路径),但是 StaticTile
class 期望表面对象已经加载。