Theo Theo - 1 year ago 73
Python Question

Pygame - How do I check if my rect has already been clicked?

What I'm trying to do is if a rect has been clicked, it is selected and text will display, but if its clicked again, then it is de-seleced and the text goes away.

list_of_rect is a list of coordinates (x, y, width, height) representing the position and size of the rect.

render_display just shows the screen with text.

if event.type == pygame.MOUSEBUTTONUP and event.button == 1:
x, y = event.pos
for i in range(len(list_of_rect)):
j = list_of_rect[i]
if j[0][0] <= x <= (j[0][0] + j[0][2]) and j[0][1] <= y <= \
(j[0][1] + j[0][3]):
render_display(screen, text)

EDIT: One idea I was thinking was to keep track of the rectangle that has been clicked on. But I'm not sure how to implement this

Answer Source

Try having a list, like this:

rects_clicked = []

Then, in your event code:

if j not in rects_clicked:
    #undisplay text
    #display text
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download