Pygame: TypeError:源对象必须是曲面



这是我实现的两个版本

#version 1 this one works fine when I call it
class Asset(pygame.sprite.Sprite):
def __init__(self, x_pos, y_pos, path=None, paths=None):
super().__init__()
self.x_pos = x_pos
self.y_pos = y_pos
if path:
# if only one image
self.image = pygame.image.load(path).convert_alpha()
else:
self.images = paths
self.images_index = 0
self.image = self.images[self.images_index]

self.rect = self.image.get_rect(midbottom=(x_pos, y_pos))
class Player(Asset):
def __init__(self, x_pos, y_pos):
walking = [pygame.image.load('Images/graphics/Player/player_walk_1.png'),
pygame.image.load('Images/graphics/Player/player_walk_2.png')]
super().__init__(x_pos=x_pos, y_pos=y_pos, paths=walking)
self.vert_speed = 0
self.jumping_image = pygame.image.load(
'Images/graphics/Player/player_jump.png')
self.time_in_air = 0
self.ground = y_pos

然而这个不起作用(返回"TypeError:源对象必须是曲面(")当我把它叫做

*最近的调用是"pygamesprite.py',第546行,在drawsurface.blits (spr)。图像,sprr .rect)用于sprites中的SPR)">

#version 2
class Asset(pygame.sprite.Sprite):
def __init__(self, x_pos, y_pos, path=None, paths=None):
super().__init__()
self.x_pos = x_pos
self.y_pos = y_pos
if path:
# if only one image
self.image = pygame.image.load(path).convert_alpha()
else:
self.images = paths
self.images_index = 0
self.image = pygame.image.load(
self.images[self.images_index]).convert_alpha()
# The problem is right here
self.rect = self.image.get_rect(midbottom=(x_pos, y_pos))

class Player(Asset):
def __init__(self, x_pos, y_pos):
walking = ['Images/graphics/Player/player_walk_1.png',
'Images/graphics/Player/player_walk_2.png']
super().__init__(x_pos=x_pos, y_pos=y_pos, paths=walking)
self.vert_speed = 0
self.jumping_image = pygame.image.load(
'Images/graphics/Player/player_jump.png')
self.time_in_air = 0
self.ground = y_pos

我预计这两个版本会有相同的结果,但无法弄清楚为什么会出现错误。

谁能解释一下这两个版本有什么不同?我一直在寻找答案,但就是找不到。

您应该在pygameload图像,并且不能使用未加载的图像。
您甚至在Assset类的__init__中加载了图像,在这里:

[...]
if path:
# if only one image
self.image = pygame.image.load(path).convert_alpha() # <-----
[...]
所以你应该将图像加载到Player类的__init__中,在这里:
[...]
walking = [pygame.image.load('Images/graphics/Player/player_walk_1.png'),
pygame.image.load('Images/graphics/Player/player_walk_2.png')]
[...]

最新更新