python - Pygame rect.center 属性

标签 python python-3.x object pygame rect

这里是 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,因此 centertopleft 不同。

关于python - Pygame rect.center 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68407428/

相关文章:

python - 绘制的混淆矩阵值相互重叠,总类别 90

python - 仅命名 pandas.read_csv 读取的数据集的最后一列

python - 在类上覆盖 dict()

javascript - 检查主数组中的所有元素在由标识符 javascript 标识的子数组中是否至少出现一次

带有对象和数组的 JavaScript 动态数组

java - 处理列表中对象的属性?

python - Turtle 图形 begin_fill() 函数在 MAC 上无法正常工作

python - 内存未释放 - PyQt4

python-3.x - 无法使用 InteractiveBrowserCredential 连接到 azure blob 存储

mysql - 使用 python 代码中的变量将值添加到 sql 时显示错误