]> git.mar77i.info Git - elevator/commitdiff
scale door rects using a method
authormar77i <mar77i@protonmail.ch>
Tue, 20 Aug 2024 09:38:08 +0000 (11:38 +0200)
committermar77i <mar77i@protonmail.ch>
Tue, 20 Aug 2024 09:41:13 +0000 (11:41 +0200)
elevator.py

index 44435aa46453ea2c0b80a7d2bcede6756a24eae0..487121a1fad4a66ff1061e04bb6bbfb8e53bc645 100755 (executable)
@@ -157,6 +157,9 @@ class Door:
             self.elevator.panel_prio = False
         return True
 
+    def scale(self, topleft, size):
+        return Rect(topleft * self.rect.size, size * self.rect.size)
+
     def draw(self, surf):
         # clean me up!
         subsurf = surf.subsurface(self.rect)
@@ -180,27 +183,22 @@ class Door:
         right_current = right_open - (right_open - right_closed) * state_fract
 
         if left_current < door_size[0]:
-            rect = Rect(
-                Vec(0, self.DOOR_MARGIN[1]) * self.rect.size,
-                Vec(left_current, door_size[1]) * self.rect.size,
+            rect = self.scale(
+                Vec(0, self.DOOR_MARGIN[1]), Vec(left_current, door_size[1])
             )
         else:
-            rect = Rect(
-                Vec(left_current - door_size[0], self.DOOR_MARGIN[1]) * self.rect.size,
-                door_size * self.rect.size,
+            rect = self.scale(
+                Vec(left_current - door_size[0], self.DOOR_MARGIN[1]), door_size
             )
         pygame.draw.rect(subsurf, self.FOREGROUND, rect)
 
         if right_current > (1 - door_size[0]):
-            rect = Rect(
-                Vec(right_current, self.DOOR_MARGIN[1]) * self.rect.size,
-                Vec(1 - right_current, door_size[1]) * self.rect.size,
+            rect = self.scale(
+                Vec(right_current, self.DOOR_MARGIN[1]),
+                Vec(1 - right_current, door_size[1]),
             )
         else:
-            rect = Rect(
-                Vec(right_current, self.DOOR_MARGIN[1]) * self.rect.size,
-                door_size * self.rect.size,
-            )
+            rect = self.scale(Vec(right_current, self.DOOR_MARGIN[1]), door_size)
         pygame.draw.rect(subsurf, self.FOREGROUND, rect)
         pygame.draw.circle(
             surf,
@@ -380,16 +378,16 @@ class ElevatorPanel:
             self.INNER_SIZE * self.outer_rect.size,
         )
         num_buttons = num_levels + 1
-        button_outer_size = int(self.inner_rect.height / num_buttons)
-        button_inner_size = int(self.inner_rect.height * self.BUTTON_SIZE / num_buttons)
+        button_outer_size = self.inner_rect.height / num_buttons
+        button_inner_size = self.inner_rect.height * self.BUTTON_SIZE / num_buttons
         button_space = button_outer_size - button_inner_size
-        self.font = pygame.font.Font(None, size=button_outer_size * 2 // 3)
+        self.font = pygame.font.Font(None, size=int(button_outer_size * 2 / 3))
         self.buttons = [
             Rect(
                 (
                     self.inner_rect.centerx - button_inner_size // 2,
                     self.inner_rect.top
-                    + int((self.inner_rect.height - button_space) / num_buttons)
+                    + (self.inner_rect.height - button_space) / num_buttons
                     * (num_buttons - 1 - i)
                     + button_space,
                 ),