【问题标题】:How to use sides of a sprite rect in pygame?如何在pygame中使用精灵矩形的侧面?
【发布时间】:2017-09-02 18:12:00
【问题描述】:

我试图在同一个精灵类的两个不同实例之间画一条线,精灵类的编码如下所示(这只是__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

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

【问题讨论】:

    标签: python pygame sprite


    【解决方案1】:

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

    【讨论】:

    • 那么sprite.rect.midleftsprite.rect.midright 也能正常工作,还是有其他方法可以获得中等值?
    • 是的,当然。 This 页面提供了可用于 rect 对象的方法和属性的完整列表。
    • 我刚刚测试了midleftmidright 并且它有效!非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 2019-05-07
    • 1970-01-01
    • 1970-01-01
    • 2020-09-14
    相关资源
    最近更新 更多