繁体   English   中英

编辑 setter / getter 的属性

[英]editing attribute of setter / getter

我正在使用位置在左上角的相机制作游戏。 我希望有这样的行为,即我将相机的位置设置为播放器,并且播放器将位于我的屏幕中心。

camera.pos = player.pos

然后我做了一个滚动相机,我只想影响相机的x

camera.pos.x = player.pos.x

这里的问题是它获取了 getter,然后覆盖了它的 x 位置,并且不影响实际位置。 我如何获得上述行为? 谢谢你。

下面是系统的重要部分。

class Vector:
    def __init__(self, x: float | int = 0, y: float | int = 0):
        self.x, self.y = x, y
    
    def __sub__(self, other: any) -> Vector:
        if isinstance(other, (int, float)):
            return Vector(self.x - other, self.y - other)
        if isinstance(other, Vector):
            return Vector(self.x - other.x, self.y - other.y)
class Camera:
    def __init__(self, pos: Vector = Vector(),...):
        self._pos = pos
...
    @property
    def pos(self):
        """
        The current position of the camera.
        """
        return self._pos + Display.center

    @pos.setter
    def pos(self, pos: Vector):
        self._pos = pos - Display.center

我猜@Tim Roberts 是对的。 我最终创建了一个私有和公共属性,并每帧更新一次私有属性。 谢谢你。

暂无
暂无

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

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