]> git.mar77i.info Git - elevator/commitdiff
less code in update methods. reformat the code
authormar77i <mar77i@protonmail.ch>
Tue, 20 Aug 2024 07:32:09 +0000 (09:32 +0200)
committermar77i <mar77i@protonmail.ch>
Tue, 20 Aug 2024 07:32:09 +0000 (09:32 +0200)
elevator.py

index b938b61b3eeacde3c0972a7edf4950908df87d2a..e0c3d59ff02f2f16534185f710e8de0154feee18 100755 (executable)
@@ -64,7 +64,7 @@ BELL = [
             Vec(0.8, 0.8),
             Vec(0.75, 0.7),
             Vec(0.7, 0.3),
-        ]
+        ],
     ),
     Circle("goldenrod", Vec(0.5, 0.3), 0.2),
 ]
@@ -80,8 +80,8 @@ def beep():
     bio = BytesIO()
     assert 0 <= volume <= 1
     for n in range(int(length_sec * SAMPLERATE)):
-        value = int(sin(n * freq * tau / SAMPLERATE) * 2 ** abs(FORMAT) * volume)
-        bio.write(pack(f"<{'h' * CHANNELS}", *(value,) * CHANNELS))
+        sample = int(sin(n * freq * tau / SAMPLERATE) * 2 ** abs(FORMAT) * volume)
+        bio.write(pack(f"<{'h' * CHANNELS}", *(sample,) * CHANNELS))
     pygame.mixer.find_channel().play(pygame.mixer.Sound(buffer=bio.getvalue()))
 
 
@@ -138,30 +138,28 @@ class Door:
         If a timeout is set and expired, toggle the door.
         If a direction is set, move the door, and check if we're done opening/closing
         """
-        dirty = self.direction != 0
         if self.timeout is not None and time() >= self.timeout:
             self.timeout = None
             if self.state == self.CLOSED:
                 self.direction = self.OPEN_DIRECTION
             elif self.state == self.OPEN:
                 self.direction = self.CLOSE_DIRECTION
-            dirty = True
+        if not self.direction:
+            return False
         self.state += self.direction
-        if self.direction:
-            if self.state <= self.OPEN:
-                self.state = self.OPEN
-                self.direction = 0
-                self.timeout = time() + self.OPEN_TIMEOUT_SEC
-            elif self.state >= self.CLOSED:
-                self.state = self.CLOSED
-                self.direction = 0
-        return dirty
+        if self.state <= self.OPEN:
+            self.state = self.OPEN
+            self.direction = 0
+            self.timeout = time() + self.OPEN_TIMEOUT_SEC
+        elif self.state >= self.CLOSED:
+            self.state = self.CLOSED
+            self.direction = 0
+        return True
 
     def draw(self, surf):
         # clean me up!
         subsurf = surf.subsurface(self.rect)
-        level, mod = divmod(self.elevator.current_level, Elevator.LEVEL_STEPS)
-        if mod == 0 and level == self.level and self.elevator.destination in (None, level):
+        if self.elevator.get_whole_level() == self.level:
             background = Elevator.CABIN_COLOR
         else:
             background = self.BACKGROUND
@@ -259,18 +257,16 @@ class Elevator:
         if self.destination is None:
             return self.pick_destination()
         dest = self.destination * self.LEVEL_STEPS
-        if dest == self.current_level:
+        if dest > self.current_level:
+            self.current_level = min(dest, self.current_level + self.SPEED)
+        elif dest < self.current_level:
+            self.current_level = max(dest, self.current_level - self.SPEED)
+        else:
             state = self.doors[self.destination].state
             if state == Door.CLOSED:
                 self.doors[self.destination].open()
             elif state == Door.OPEN:
                 self.destination = None
-        self.current_level = (min if dest > self.current_level else max)(
-            dest,
-            self.current_level + self.SPEED * (
-                1 if dest > self.current_level else -1
-            )
-        )
         return True
 
     def draw(self, surf):
@@ -282,14 +278,16 @@ class Elevator:
             surf_size * Vec(self.WIDTH, shaft_height),
         )
         cabin_rect = Rect(
-            surf_size * Vec(
+            surf_size
+            * Vec(
                 self.OFF_X + self.CABIN_MARGINS[0],
-                self.MARGIN_Y + self.CABIN_MARGINS[1] + cabin_height * (
-                    len(self.doors) - 1
-                    - self.current_level / self.LEVEL_STEPS
-                ),
+                self.MARGIN_Y
+                + self.CABIN_MARGINS[1]
+                + cabin_height
+                * (len(self.doors) - 1 - self.current_level / self.LEVEL_STEPS),
             ),
-            surf_size * Vec(
+            surf_size
+            * Vec(
                 self.WIDTH - 2 * self.CABIN_MARGINS[0],
                 cabin_height - 2 * self.CABIN_MARGINS[1],
             ),
@@ -309,7 +307,9 @@ class Elevator:
         """
         Set a queued_dest if no destination or queued_dest is set.
         """
-        if self.destination is None and self.queued_dest is None:
+        if self.get_current_dest() is None and not (
+            self.get_whole_level() == level and self.doors[level].state == Door.OPEN
+        ):
             self.queued_dest = level
 
     def get_whole_level(self):
@@ -326,7 +326,10 @@ class Elevator:
         if self.doors[call_level].state == Door.OPEN:
             return
         level_queue = {
-            self.get_whole_level(), *self.call_queue, self.queued_dest, self.destination
+            self.get_whole_level(),
+            *self.call_queue,
+            self.queued_dest,
+            self.destination,
         }
         if call_level not in level_queue:
             self.call_queue.append(call_level)
@@ -380,23 +383,25 @@ class ElevatorPanel:
             Rect(
                 (
                     self.inner_rect.centerx - button_inner_size // 2,
-                    self.inner_rect.top + int(
-                        (self.inner_rect.height - button_space) / num_buttons
-                    ) * (num_buttons - 1 - i) + button_space,
+                    self.inner_rect.top
+                    + int((self.inner_rect.height - button_space) / num_buttons)
+                    * (num_buttons - 1 - i)
+                    + button_space,
                 ),
                 (button_inner_size, button_inner_size),
-            ) for i in range(num_buttons)
+            )
+            for i in range(num_buttons)
         ]
         last_topleft = self.buttons[0].topleft
         self.buttons.extend(
             (
                 Rect(
                     (last_topleft[0] - button_outer_size, last_topleft[1]),
-                    (button_inner_size, button_inner_size)
+                    (button_inner_size, button_inner_size),
                 ),
                 Rect(
                     (last_topleft[0] + button_outer_size, last_topleft[1]),
-                    (button_inner_size, button_inner_size)
+                    (button_inner_size, button_inner_size),
                 ),
             )
         )
@@ -417,7 +422,7 @@ class ElevatorPanel:
         for shape in BELL:
             shape.draw(surf, self.buttons[0])
 
-        for button, s in zip(self.buttons[self.num_levels + 1:], self.OPEN_CLOSE_STR):
+        for button, s in zip(self.buttons[self.num_levels + 1 :], self.OPEN_CLOSE_STR):
             fs = self.font.render(s, True, self.LABEL_COLOR)
             surf.blit(fs, button.center - Vec(fs.get_size()) // 2)
 
@@ -426,7 +431,7 @@ class ElevatorApp:
     FPS = 60
     SET_MODE_KWARGS = {
         "flags": pygame.FULLSCREEN,
-        #"size": (1024, 768),
+        # "size": (1024, 768),
     }
     BACKGROUND = "green4"