简体   繁体   English

如何在pygame中使用图像的中心设置图像的位置?

[英]How to set the position of an image using the center of the image in pygame?

I am using python 2.7, and im using pygame to make a basic game. 我正在使用python 2.7,而我正在使用pygame制作基本游戏。

(code can be found here: http://github.com/ProgrammerKid/snake ) (snake.py is my main file) (可在此处找到代码: http ://github.com/ProgrammerKid/snake)(snake.py是我的主文件)

My game was initially supposed to mock the snake game, but it turned out to be more like pacman... so my problem is that since in pygame, the position of an image is always set to the top left corner of the image.... is there was i can make it the center of the image... 我的游戏原本应该模拟蛇游戏,但结果却更像是吃豆子……所以我的问题是,由于在pygame中,图像的位置始终设置为图像的左上角。 ..有没有我可以使它成为图像的中心...

because the position of the killer/alien is at the top left, and same with the pacman/snake... so the two objects would literally have to be overlapping for the alien to kill the pacman.... i want it so that they only overlap a little....: check out this picture: http://i.stack.imgur.com/N88GX.png 因为杀手/外星人的位置在左上角,并且与吃豆子/蛇一样...所以两个对象在字面上必须是重叠的,外星人才能杀死吃豆子....我想要这样它们只重叠一点...。:查看此图片: http : //i.stack.imgur.com/N88GX.png

see: Pygame Rect Reference 请参阅: Pygame Rect参考

you probably already know: 您可能已经知道:

imageObject = pygame.image.load(filename)
rect = imageObject.get_rect()

Now try: 现在尝试:

rect.center = (newX, newY)

Or, better: 或更好:

rect.move(xDistance, yDistance)

Good Luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM