这里是 pygame python 代码的两部分:
a = self.img.get_rect(topleft = (self.x, self.y))
print(a.topleft)
这段代码的输出是:
(200, 189)
另一个 pygame 代码:
a = self.img.get_rect(topleft = (self.x, self.y)).center
print(a)
这段代码的输出:
(234, 213)
注:The value of self.x is 200 and self.y is 200
所以现在我的问题是我们如何将 .center 放在 pygame rect object or the variable a
之后当我打印 a 的值时,它会发生变化,pygame 矩形对象之后的 .center 会做什么?
最佳答案
pygame.Surface.get_rect.get_rect()
返回一个具有 Surface 对象大小的矩形,但它返回一个始终从 (0, 0) 开始的矩形,因为 Surface 对象没有位置。矩形的位置可以通过关键字参数指定。例如,可以使用关键字参数 topleft
指定矩形的左上角。
pygame.Rect
对象具有各种虚拟属性:
The Rect object has several virtual attributes which can be used to move and align the Rect:
x,y top, left, bottom, right topleft, bottomleft, topright, bottomright midtop, midleft, midbottom, midright center, centerx, centery size, width, height w,h
你实际上设置了一个矩形的左上角,图像的大小:
self.img.get_rect(topleft = (self.x, self.y))
最后,您可以通过读取 center
属性来获取该矩形的中心点。由于图像的大小不为 0,因此 center
与 topleft
不同。
关于python - Pygame rect.center 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68407428/