Reputation: 33
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
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