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
Asked Answered
B

1

6

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.

Bellicose answered 17/12, 2021 at 12:19 Comment(0)
D
2

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.

Diphthong answered 17/12, 2021 at 12:22 Comment(2)
firstly, thanks I now understood that event.pos[] can take two parameters 0 or 1 (zero return x cursor and one for y cursor) but pygame.mouse.getpos() return (x,y) cursor together,right?Bellicose
@roroeiad Sorry you lack the basics. pygame.mouse.get_pos() returns a Tuple and event.pos is a Tuple. You get (x, y) from both. However, you can get the components of a Tuple with t[i]. e.g.: pygame.mouse.get_pos()[0] or event.pos[0].Diphthong

© 2022 - 2024 — McMap. All rights reserved.