svs
svs

Reputation: 441

how to check if you cklicked (with your mouse) in an defined area in python/pygame

I want to check if you clicked in an defined area, for example an area from 0,0 to 400,40 (pixel coordinates). I have this:

x = 0
y = 0
(mouse_posx, mouse_posy) = pygame.mouse.get_pos()
press = pygame.key.get_pressed()
if mouse_posx > x and mouse_posx < x+400 and  mouse_posy > y and mouse_posy < y+40 and press != 0:
    function()

I get no error but it does nothing. Can someone tell me what i am doing wrong?

Upvotes: 0

Views: 51

Answers (1)

David Goulet
David Goulet

Reputation: 53

What you want to do is have your program listen for events. Once the event of mouse button is triggered, then record the mouse position. Then verify if the mouse position is inside the box.

Pygame mouse

Oh and also, I dont think the event of mouse button gets recorded in the > pygame.keys.get_pressed <. I believe the pygame.keys.get_pressed is just for the keyboard. I may be wrong, im using my mobile and dont have a computer with me.

Upvotes: 1

Related Questions