How to determine if a sprite has been pressed in pygame

Im new to pygame, working with sprites right now. My question is: how to determine if a sprite has been clicked? I want to do something when the sprite was pressed just like the button.

thank:)

[Changed]

thanks Steven. Also, is there a way to find out who the sprite that was clicked on is? Here is a sample code

boxes = pygame.sprite.Group()
for color, location in [([255, 0, 0], [0, 0]),
                        ([0, 255, 0], [60, 60]),
                        ([0, 0, 255], [120, 120])]:
    boxes.add(UpDownBox(color, location)

for example, I click the sprite at location [0,0], the program should print its color or its location. Thanks again:)

+5
source share
2 answers

, - Pygame, , rect . , , pygame.mouse.get_pos(). rect sprite rect, pygame.sprite.collide_rect() rect .

.

+6

: Rect.collidepoint(x, y)

#in event handling:
if event.type == MOUSEMOTION: x,y = event.pos

for box in boxes:
    if box.rect.collidepoint(x,y): print 'yay!'

Rect Sprite . :

+1
source

All Articles