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

Question:

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.

Asked By: roro eiad

||

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.

Answered By: Rabbid76

Thanks @Rabbid76, nice answer to this question, but I am sitll confused about the ‘arrtibute’. For instances: whenEvent.type == MOUSEBUTTONDOWN, the attribute is pos, while when Event.type == FINGERDOWN, the attribute is x, y, dx and dy. Where can I find concrete informations about these attributes?

Answered By: Tian sang
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.