Robert Yin
Robert Yin

Reputation: 33

How can I check if someone already clicked on an area in pygame

So i want to be able to know whether or not someone has clicked on a certain part of a pygame screen previously. Is this possible? If so, how do you do it?

ex.

area = pg.Rect(38, 34, 126, 132)
if #user has clicked within 'area' previously:
    print("You already clicked here")
else:
    #do whatever

sorry if this looks a little weird, This is my first time asking a question here

Upvotes: 2

Views: 51

Answers (1)

The Big Kahuna
The Big Kahuna

Reputation: 2110

You can use pygames collidepoint to check if a point is in the rect

area = pg.Rect(38, 34, 126, 132)
mouse_x, mouse_y = pg.mouse.get_pos()
clicked_before = False


if click:
    if area.collidepoint(mouse_x,mouse_y):
        if clicked_before:
            print("you already clicked here before")
        else:
            clicked_before = True
            #do whatever

you can then do this for a list of areas

areas = [pg.Rect(38, 34, 126, 132)...]
clicked_before = [False...]

for i, area in enumerate(areas): #enumerate gets the object and its position
    if area.collidepoint(mouse_x,mouse_y):
            if clicked_before[i]:
                print("you already clicked here before")
            else:
                clicked_before[i] = True
                #do whatever

Upvotes: 1

Related Questions