首页 > 解决方案 > 位置论据据说是缺失的,但给出了,有什么问题?

问题描述

我正在使用pygame并创建了一个名为Entity和 Subclass 的类Player。Entity 类需要一个参数“pos”,它始终只是“pos”,因为它的值是在 init 中创建的。

class Entity(pygame.sprite.Sprite):
    def __init__(self, color, stype, pos, *groups):
        super().__init__(*groups)
        self.image = stype
        try:
            self.image.fill(color)
        except:
            pass
        self.image.convert()
        self.rect = self.image.get_rect(topleft=pos)

    def update(self, dt, events):
        pass

class Player(Entity):
    def __init__(self, platforms, pos, *groups):
        super().__init__((pygame.image.load("lemon.png")), pos)
        self.vel = pygame.Vector2((0, 0))
        self.onGround = False
        self.platforms = platforms
        self.speed = 8
        self.jump_strength = 10

但是,当我尝试运行代码时,它给了我这个错误:

TypeError:init()缺少1个必需的位置参数:'pos'

标签: pythonpygame

解决方案


为什么这里只有两个参数:

super().__init__((pygame.image.load("lemon.png")), pos)

但构造函数Entity至少需要3个:

def __init__(self, color, stype, pos, *groups)

我不太了解您的问题,但这可能是一个解决方案:

super().__init__(color=None, stype=(pygame.image.load("lemon.png")), pos=pos)

推荐阅读