1

How would I assign the variables x, y to the x and y position, respectivly, of a the mouse pointer when it is pressed down (clicked).

Heres what I have thus far:

from pygame import*
init()
SIZE = (width, height)
screen = display.set_mode(SIZE)
position = 0
Running = True
while Running:
    for evnt in event.get():
         if evnt.type == QUIT:
             Running = False 
         elif evnt.type == MOUSEBUTTONDOWN:
             position = evnt.pos

Currently, position is in the form (x position, y position). How would I access the individual elements?

Thanks in advance.

2 Answers 2

2

position is a tuple so you can

position = evnt.pos
x = position[0]
y = position[1]

or

x, y = position

or even

x, y = evnt.pos
Sign up to request clarification or add additional context in comments.

Comments

0

I have not used pygame in a while nor do i still have it so i can't test this code but I'm pretty sure its correct. first you test if the mouses left button is clicked

The get_pressed function returns three values for each button on a mouse i may be wrong thinking the first value returned is the value for mouse button one.

If the mouse is clicked, we proceed to get the mouses coords and store them.

if pygame.mouse.get_pressed()[0]
    cord = pygame.mouse.get_pressed()

Also, from what i understand pygame does not use a z value as the library is only a 2D library so this gives you a X, Y value.

1 Comment

this is not correct - get_pressed gives you a tuple like (1,0,0) meaning button one is pressed so it won't give you coordinates check this out here: pygame.org/docs/ref/mouse.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.