monkey334
monkey334

Reputation: 337

pygame - How to split the X and Y coordinates from get_pos

Sorry for all these questions, I really don't mean to bother you guys.

But the problem I'm having is that I don't know how to split the X and Y coordinates from the pygame get_pos function, as get_pos gets the X and Y coordinates in one.

Is there a way to split them into separate variables?

Thanks in advance

Upvotes: 5

Views: 8291

Answers (3)

nont
nont

Reputation: 9519

get_pos returns an array/tuple of two values

so you could do something like this:

X,Y = 0,1
p = pygame.mouse.get_pos()
mouse_pos = Vec2d(p[X],p[Y])

or even more simply

x,y = pygame.mouse.get_pos()

Also, this page has a button that lets you search for examples of the functions you're interested in.

Upvotes: 5

embert
embert

Reputation: 7592

If its a tuple, can't you just unpack it like

x, y = get_pos()

Upvotes: 3

Nigel Tufnel
Nigel Tufnel

Reputation: 11524

get_pos returns a tuple. You can do a sequence unpacking:

x, y = pygame.mouse.get_pos()

To quote a great man (and Python documentation):

This is called, appropriately enough, sequence unpacking and works for any sequence on the right-hand side. Sequence unpacking requires the list of variables on the left to have the same number of elements as the length of the sequence. Note that multiple assignment is really just a combination of tuple packing and sequence unpacking

Upvotes: 8

Related Questions