Pythonic way to handle coordinates

Pierre-Alain Dorange pdorange at pas-de-pub-merci.mac.com
Sat Jan 17 09:48:12 CET 2009


Hi,
I'm used python for 3 months now to develop small arcade games (with
pygame module).

I just got a question about coordinates handling.
My games are in 2D so i deal with x,y coordinates for sprites (but woudl
be the same u-issue in 3D).

At the beginning i simply use x and y to pass to my methods
ie : sprite.move(x,y)
easy

after looking closely to pygame code, i noticed that pygame implement a
Rect class but no Point class, and instead require tuple for
coordinates.
ie : pygame.draw.line(surface,color,start,end)
where start and end are tuple, like (x,y).

I found this nice and rewrote my API to use tuples instead of 2
variables...
so i got could do :
        loc=(x,y)
        sprite.move(loc) or sprite.move((x,y))
        ...
        loc=sprite.get_pos()

Nice, but i got a problem when my methodes return tuple, as it's
unmutable i could not modify them and i got to used tempory variables to
do that, not very clean :
        loc=sprite.get_pos()
        loc[0]+=10  < ERROR
        ...
        x=loc[0]+10
        loc=(x,loc[1])
i could also do :
        (x,y)=sprite.get_pos()
        x+=10
        sprite.set_pos((x,y))
The second method could not be used when i'm instead one of my method, i
need to used the first one.

What is the elegant way to handle coordinates ?
Do i need to continue using tuples or do i need to write a Point class.
I feel a point class would be nice, because i could implement operators
with it ? But i think Point class must exist allready ? 

-- 
Pierre-Alain Dorange

Ce message est sous licence Creative Commons "by-nc-sa-2.0"
        <http://creativecommons.org/licenses/by-nc-sa/2.0/fr/>



More information about the Python-list mailing list