From: mar77i Date: Tue, 20 Aug 2024 09:38:08 +0000 (+0200) Subject: scale door rects using a method X-Git-Url: https://git.mar77i.info/?a=commitdiff_plain;h=a963333f69fa5d60d3d58aa338a2c2d50d950777;p=elevator scale door rects using a method --- diff --git a/elevator.py b/elevator.py index 44435aa..487121a 100755 --- a/elevator.py +++ b/elevator.py @@ -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, ),