python 中有没有办法将 属性 装饰器与 pygame 的 rect 数据类型一起使用?
Is there a way in python to use the property decorator with pygame's rect data type?
我正在尝试通过使用 @property
装饰器为 return 具有对象的 x、y、宽度和高度的新矩形对象提供自定义 class 矩形属性.
@property
def rect(self):
return pygame.Rect(self.x, self.y, self.width, self.height)
@rect.setter
def rect(self, new_rect):
self.x = new_rect.x
self.y = new_rect.y
self.width = new_rect.width
self.height = new_rect.height
我可以制作一个 setter 来用新的 rect 完全替换 rect,适当地更新对象的属性,但是有没有办法让它在 rect 的属性更新时工作(即 object.rect.bottomright = [5, 3]
将创建矩形,更新属性 bottomright 并将更改应用于对象的 x、y、宽度和高度)?如果有我遗漏的更好的方法,那也将不胜感激。
我避免让 rect
属性包含 x
、y
、width
和 height
,因为我不想必须任何时候我想访问这些公共属性之一时都可以访问 rect
属性,但我意识到我可以简单地将 class 子 pygame.Rect
class 来获得这种效果结果属性的数量。
我正在尝试通过使用 @property
装饰器为 return 具有对象的 x、y、宽度和高度的新矩形对象提供自定义 class 矩形属性.
@property
def rect(self):
return pygame.Rect(self.x, self.y, self.width, self.height)
@rect.setter
def rect(self, new_rect):
self.x = new_rect.x
self.y = new_rect.y
self.width = new_rect.width
self.height = new_rect.height
我可以制作一个 setter 来用新的 rect 完全替换 rect,适当地更新对象的属性,但是有没有办法让它在 rect 的属性更新时工作(即 object.rect.bottomright = [5, 3]
将创建矩形,更新属性 bottomright 并将更改应用于对象的 x、y、宽度和高度)?如果有我遗漏的更好的方法,那也将不胜感激。
我避免让 rect
属性包含 x
、y
、width
和 height
,因为我不想必须任何时候我想访问这些公共属性之一时都可以访问 rect
属性,但我意识到我可以简单地将 class 子 pygame.Rect
class 来获得这种效果结果属性的数量。