简体   繁体   中英

How to use sides of a sprite rect in pygame?

I am attempting to draw a line between two different instances of the same sprite class, the sprite class being coded in as shown here (this is just the __init__ function):

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)

I have tried to do this using the code here:

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

I have simplified the above code, to highlight what I have actually done. The actual code for the above is found here , it is commented out on line 25. The rest of the code can also be found in that repository for reference.

I had hoped that this would work however I got this error instead:

TypeError: Invalid start point argument

So what am I doing wrong here? Thanks in advance.

sprite.rect.left and sprite.rect.right and not valid points because they are each a single integer. What you probably want is sprite.rect.topleft and sprite.rect.topright , or the bottom or mid equivalents.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM