Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 675
monkey334 Avatar asked Dec 07 '22 03:12

monkey334


2 Answers

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

like image 85
Nigel Tufnel Avatar answered Dec 27 '22 10:12

Nigel Tufnel


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.

like image 45
nont Avatar answered Dec 27 '22 10:12

nont