2

I'm trying to tell when a sprite, which must be part of a particular group (pygame.sprite.Group()), is clicked on. Currently I've tried creating a sprite which is just the mouses position and totally invisible, adding it to its own group, and using this code:

clickedList = pygame.sprite.spritecollide(guess1, mice, False)

where guess1 is the sprite getting clicked on and mice is the group containing the sprite that has the position of the mouse.

When I try this, I am told that "Group has no attribute rect". Where do I go from here?

4
  • 4
    Are you 100% sure that guess1 is a Sprite instance? It doesn't look like that.
    – sloth
    Nov 18, 2019 at 15:29
  • Does this answer your question? how to detect if the sprite has been clicked in pygame
    – Valentino
    Nov 18, 2019 at 19:06
  • @sloth It is a pygame.sprite.Group() Do I need to use the sprite itself?
    – Nico
    Nov 19, 2019 at 8:44
  • @Valentino No, the example linked in the answer is gone, and I don't quite get what the rest of it is saying, because that's pretty much what I'm doing, at least as far as I can tell.
    – Nico
    Nov 19, 2019 at 8:47

1 Answer 1

1

If you've a sprite (my_sprite) and you want to verify if the mouse is on the sprite, then you've to get the .rect attribute of the pygame.sprite.Sprite object and to test if the mouse is in the rectangular area by .collidepoint():

mouse_pos = pygame.mouse.get_pos()
if my_sprite.rect.collidepoint(mouse_pos):
    # [...]

The Sprites in a pygame.sprite.Group can iterate through. So the test can be done in a loop:

mouse_pos = pygame.mouse.get_pos()
for sprite in mice:
    if sprite.rect.collidepoint(mouse_pos):
        # [...]

Or get a list of the Sprites within the Group, where the mouse is on it. If the Sprites are not overlapping, then the list will contain 0 or 1 element:

mouse_pos = pygame.mouse.get_pos()
clicked_list = [sprite for sprite in mice if sprite.rect.collidepoint(mouse_pos)]

if any(clicked_list):
    clicked_sprite = clicked_list[0]
    # [...]
6
  • I'm Using the second one, but it always returns as false. mousePos = pygame.mouse.get_pos() for Circle in guess1: if Circle.rect.collidepoint(mousePos): ##... Is it possible the issue is with the sprite itself rather than the condition, or am I just missing something stupid here? (sorry about the code, it won't format properly in comments.)
    – Nico
    Nov 19, 2019 at 14:09
  • I'm using this to define the class class Circles(pygame.sprite.Sprite): def __init__ (self, value, x, y): pygame.sprite.Sprite.__init__(self) self.value = value circleImage = pygame.image.load ("Outline.png") if value == 11: circleImage = pygame.image.load ("Unknown.png") self.image = pygame.Surface([40,40]) self.image.set_colorkey(WHITE) self.image.blit(circleImage,(0,0)) self.rect = self.image.get_rect() self.rect.x = x self.rect.y = y and drawing using display.flip
    – Nico
    Nov 19, 2019 at 14:13
  • If its easier, I've got all the code in a google doc here
    – Nico
    Nov 19, 2019 at 14:17
  • Yes, it is a group.
    – Nico
    Nov 19, 2019 at 14:28
  • @Nico But it seems to work fine guess1 are the 4 circles in the 2nd row. If you want to change the color of the circles, then you've to add self.image.blit(circleImage,(0,0)) at the end of changeColour
    – Rabbid76
    Nov 19, 2019 at 14:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.