1

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.

ModoUnreal
  • 642
  • 5
  • 15

1 Answers1

2

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.

Alexandre Pinho
  • 178
  • 2
  • 7
  • So would `sprite.rect.midleft` or `sprite.rect.midright` work as well, or is there another way of getting the mid equivalents? – ModoUnreal Sep 02 '17 at 22:10
  • Yes, absolutely. [This](https://www.pygame.org/docs/ref/rect.html) page provides a complete list of methods and attributes available for rect objects. – Alexandre Pinho Sep 02 '17 at 22:14
  • I just tested `midleft` and `midright` and it works! Thanks so much! – ModoUnreal Sep 02 '17 at 22:15