Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
556 views
in Technique[技术] by (71.8m points)

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

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.stack.imgur.com/sGPFt.gif

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...