如何在 pygame 中使用精灵矩形的边?

How to use sides of a sprite rect in pygame?

我试图在同一精灵 class 的两个不同实例之间画一条线,精灵 class 被编码为此处所示(这只是 __init__ 函数):

class Atom(pygame.sprite.Sprite):
"""Creates an atom of an element.
 The element can be specified using the element parameter.
 This will take a dict argument, contained in a constants file of some sort."""
def __init__(self, element, centre_loc):
    pygame.sprite.Sprite.__init__(self)

    self.image = pygame.Surface((30, 30), pygame.SRCALPHA)
    pygame.gfxdraw.aacircle(self.image, 15, 15, 14, (0, 255, 0))
    pygame.gfxdraw.filled_circle(self.image, 15, 15, 14, (0, 255, 0))


    self.rect = self.image.get_rect()
    self.rect.center = centre_loc
    self.element = element
    self.show_text(element)
    self.print_properties(self.element)

我已尝试使用此处的代码执行此操作:

pygame.draw.line(screen, color, sprite.rect.right. sprite.rect.left)

我简化了上面的代码,以突出我实际所做的事情。 上面的实际代码已找到 here,它在第 25 行被注释掉。其余代码也可以在该存储库中找到以供参考。

我曾希望这会奏效,但我却收到了这个错误:

TypeError: Invalid start point argument

那么我做错了什么?提前致谢。

sprite.rect.leftsprite.rect.right 不是有效点,因为它们都是一个整数。您可能想要的是 sprite.rect.topleftsprite.rect.topright,或者底部或中间的等效项。