Skip to content
Advertisement

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.

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement