繁体   English   中英

Pygame rect.center 属性

[英]Pygame rect.centre attribute

所以这里是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 rect 对象之后的 .center 有什么作用?

pygame.Surface.get_rect.get_rect()返回一个与Surface对象大小相同的矩形,但它返回一个始终从 (0, 0) 开始的矩形,因为Surface对象没有位置。 矩形的位置可以通过关键字参数指定。 例如,矩形的左上角可以用关键字参数topleft指定。

pygame.Rect对象具有各种虚拟属性:

Rect 对象有几个虚拟属性,可用于移动和对齐 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属性获得这个矩形的center 由于图像的大小不是 0, centertopleft不同。

暂无
暂无

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

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