Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does ''event.pos[0]'' mean in the pygame library? I saw an example using it to get the X axis of the cursor and ignore the Y axis

I don't understand how it works. I don't know if I understood the purpose of this function wrong. I tried to search what posx=event.pos[0] means but all I found was that if you want to take x, write the code of posx,posy=pygame.mouse.get_pos() and then take posx. But I still can't understand the method he followed in the example I saw.

like image 655
roro eiad Avatar asked Dec 17 '21 12:12

roro eiad


People also ask

What is event POS pygame?

pygame. mouse. getpos() returns the current position of the mouse. The pos attribute stores the position of the mouse when the event occurred.

What does for event in pygame event get () do?

for event in pygame. event. get() handles the internal events an retrieves a list of external events (the events are removed from the internal event queue). If you press the close button of the window, than the causes the QUIT event and you'll get the event by for event in pygame.


1 Answers

See pygame.event module. The MOUSEMOTION, MOUSEBUTTONUP and MOUSEBUTTONDOWN events provide a position property pos with the position of the mouse cursor. pos is a tuple with 2 components, the x and y coordinate.

e.g.:

for event in pygame.event.get():
    if event.type == pygame.MOUSEBUTTONDOWN:
        print("mouse cursor x", event.pos[0])
        print("mouse cursor y", event.pos[1])

pygame.mouse.get_pos() returns a Tuple and event.pos is a Tuple. Both give you the position of the mouse pointer as a tuple with 2 components:

ex, ey = event.pos
mx, my = pygame.mouse.get_pos()

pygame.mouse.getpos() returns the current position of the mouse. The pos attribute stores the position of the mouse when the event occurred. Note that you can call pygame.event.get() much later than the event occurred. If you want to know the position of the mouse at the time of the event, you can call it up using the pos attribute.

like image 105
Rabbid76 Avatar answered Oct 17 '22 12:10

Rabbid76