Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I change the Pygame coordinate system so that the center of the window is (0,0)?

Tags:

python

pygame

I want to change pygame coordinate system so that the center of the window is (0,0) and has negative values as in the image. Is there a possible way to convert the coordinates?

https://i.sstatic.net/sGPFt.gif

like image 899
Coder Avatar asked Oct 18 '25 19:10

Coder


1 Answers

PyGame uses a coordinate system where the top left corner is (0,0). That cannot be changed.

If you want to move the origin of the coordinate system to the center of the screen, then you have to translate the coordinates by yourself. For instance write a function that translates the coordinates:

def center_origin(surf, p):
    return (p[0] + surf.get_width() // 2, p[1] + surf.get_height() // 2)
screen.blit(my_image, center_origin(screen, (x, y)))

Or use a lambda expression:

center_origin = lambda p: (p[0] + screen.get_width() // 2, p[1] + screen.get_height() // 2)
screen.blit(my_image, center_origin((x, y))
like image 79
Rabbid76 Avatar answered Oct 21 '25 08:10

Rabbid76



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!