Pygame: TypeError: Source objects must be a surface

Pygame: TypeError: Source objects must be a surface

这是我实现的两个版本

#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


而这个不起作用(returns“TypeError:源对象必须是表面”) 当我调用它时

*最近一次调用是“\pygame\sprite.py”,第 546 行,在 draw 中 surface.blits((spr.image, spr.rect) 精灵中的 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

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

有人可以解释一下这两个版本之间有什么区别吗? 我一直在寻找答案,但真的找不到

您应该 load 中的图像 pygame 并且在不加载图像的情况下不得使用这些图像。
您甚至在 class Assset__init__ 中加载了图像,此处:

[...]
if path:
         # if only one image
         self.image = pygame.image.load(path).convert_alpha() # <-----
[...]

因此您应该在 class Player__init__ 中加载图像,此处:

[...]
walking = [pygame.image.load('Images/graphics/Player/player_walk_1.png'),
                   pygame.image.load('Images/graphics/Player/player_walk_2.png')]
[...]