From fbbf4e88aefc6c64d979150d385667b8aa2d8278 Mon Sep 17 00:00:00 2001 From: Mambouna Date: Wed, 9 Jul 2025 13:24:20 +0200 Subject: [PATCH 1/3] Made width and height properties. --- src/pgzero/actor.py | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/src/pgzero/actor.py b/src/pgzero/actor.py index e8697e9f..6fc248ca 100644 --- a/src/pgzero/actor.py +++ b/src/pgzero/actor.py @@ -240,6 +240,28 @@ def _calc_anchor(self): else: self._anchor = transform_anchor(ax, ay, ow, oh, self._angle) + # TODO: Currently, there is an issue with __setattr__() being used on all + # assignments. width and height can't be set anymore, but there is + # neither warning nor error when trying to do so, which is not good + # enough. + @property + def width(self): + return self._width + + @width.setter + def width(self, val): + print("WARNING: width of an actor can't be set directly. Change the" + " actors image to change its dimensions.") + + @property + def height(self): + return self._height + + @height.setter + def height(self, val): + print("WARNING: height of an actor can't be set directly. Change the" + " actors image to change its dimensions.") + @property def angle(self): return self._angle @@ -252,8 +274,8 @@ def angle(self, angle): ra = radians(angle) sin_a = sin(ra) cos_a = cos(ra) - self.height = abs(w * sin_a) + abs(h * cos_a) - self.width = abs(w * cos_a) + abs(h * sin_a) + self._height = abs(w * sin_a) + abs(h * cos_a) + self._width = abs(w * cos_a) + abs(h * sin_a) ax, ay = self._untransformed_anchor p = self.pos self._anchor = transform_anchor(ax, ay, w, h, angle) @@ -331,7 +353,7 @@ def image(self, image): def _update_pos(self): p = self.pos - self.width, self.height = self._orig_surf.get_size() + self._width, self._height = self._orig_surf.get_size() self._calc_anchor() self.pos = p From 27d5a69877c50098a338dd401e96162e8ef54d79 Mon Sep 17 00:00:00 2001 From: Mambouna Date: Wed, 9 Jul 2025 19:55:54 +0200 Subject: [PATCH 2/3] Removed width and height from delegated attributes to ZRect. --- src/pgzero/actor.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/pgzero/actor.py b/src/pgzero/actor.py index 6fc248ca..046b59f5 100644 --- a/src/pgzero/actor.py +++ b/src/pgzero/actor.py @@ -101,7 +101,10 @@ def _set_opacity(actor, current_surface): class Actor: EXPECTED_INIT_KWARGS = SYMBOLIC_POSITIONS DELEGATED_ATTRIBUTES = [ - a for a in dir(rect.ZRect) if not a.startswith("_") + a for a in dir(rect.ZRect) if not a.startswith("_") and + # To prevent rect width and height being accessible without those + # implicating changes to the actors' image width and height. + not a == "width" and not a == "height" ] function_order = [_set_opacity, _set_angle] From 208e87e06cbdfecd7fa0f1180220264be8cfee32 Mon Sep 17 00:00:00 2001 From: Mambouna Date: Wed, 9 Jul 2025 20:03:59 +0200 Subject: [PATCH 3/3] Removed old comment. --- src/pgzero/actor.py | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/src/pgzero/actor.py b/src/pgzero/actor.py index 046b59f5..5130c88c 100644 --- a/src/pgzero/actor.py +++ b/src/pgzero/actor.py @@ -243,27 +243,23 @@ def _calc_anchor(self): else: self._anchor = transform_anchor(ax, ay, ow, oh, self._angle) - # TODO: Currently, there is an issue with __setattr__() being used on all - # assignments. width and height can't be set anymore, but there is - # neither warning nor error when trying to do so, which is not good - # enough. @property def width(self): return self._width @width.setter - def width(self, val): - print("WARNING: width of an actor can't be set directly. Change the" - " actors image to change its dimensions.") + def width(self, _): + print("WARNING: The width of an actor can't be set directly. Change " + "the actors image to change its dimensions.") @property def height(self): return self._height @height.setter - def height(self, val): - print("WARNING: height of an actor can't be set directly. Change the" - " actors image to change its dimensions.") + def height(self, _): + print("WARNING: The height of an actor can't be set directly. Change " + "the actors image to change its dimensions.") @property def angle(self):