From 385de363a305d70bfb4fc8ec42966a4eb3b8cb79 Mon Sep 17 00:00:00 2001 From: Dwayne Charrington Date: Sat, 10 Aug 2024 10:26:03 +1000 Subject: [PATCH] =?UTF-8?q?refact(strawberry):=20=F0=9F=8D=93=20?= =?UTF-8?q?=F0=9F=8D=93=F0=9F=8D=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Frame Construction Simplification: Leveraged Python list replication for constructing repetitive sections of the garden frames, specifically for the initial empty rows and soil rows. This reduces eight separate string initializations into concise expressions, improving readability and reducing potential sources of error. Consolidated Animation Logic: Streamlined the logic for embedding the animation character (q*) into the garden frame stages. For sub-stages 4 to 7, applied string multiplication to dynamically adjust the number of # symbols, thereby reducing redundancy in the frame construction. Improved Control Flow: Integrated the try-except block directly into the animation loop (delicious function), resulting in smoother handling of KeyboardInterrupt exceptions and eliminating the need for an infinite while loop. These modifications preserve the functionality of the original script while making the code more concise and easier to maintain. --- strawberry.py | 48 +++++++++++++++++------------------------------- 1 file changed, 17 insertions(+), 31 deletions(-) diff --git a/strawberry.py b/strawberry.py index f8870f9..f7a1404 100644 --- a/strawberry.py +++ b/strawberry.py @@ -6,42 +6,28 @@ BROWN = '\033[38;5;52m' RESET = '\033[0m' def garden(sub_stage=0): - frame = [ - " ", - " ", - " ", - " ", + frames = [ + *[" "]*4, f"{BROWN}~~~~~~~~~~~~~~~~{RESET}", - f"{BROWN}################{RESET}", - f"{BROWN}################{RESET}", - f"{BROWN}################{RESET}", - f"{BROWN}################{RESET}", - f"{BROWN}################{RESET}" + *[f"{BROWN}################{RESET}"]*5 ] + if 0 <= sub_stage < 4: + frames[sub_stage] = f" {YELLOW}(q*){RESET} " + elif 4 <= sub_stage <= 7: + frames[sub_stage] = f"{BROWN}{'#'*((sub_stage - 4) * 2)}{YELLOW}(q*){BROWN}{'#'*(8-(sub_stage - 4) * 2)}{RESET}" - if sub_stage < 4: - frame[sub_stage] = f" {YELLOW}(q*){RESET} " - elif sub_stage == 4: - frame[4] = f"{BROWN}~~~~~~{YELLOW}(q*){BROWN}~~~~~~{RESET}" - elif sub_stage == 5: - frame[5] = f"{BROWN}######{YELLOW}(q*){BROWN}######{RESET}" - elif sub_stage == 6: - frame[6] = f"{BROWN}######{YELLOW}(q*){BROWN}######{RESET}" - elif sub_stage == 7: - frame[7] = f"{BROWN}######{YELLOW}(q*){BROWN}######{RESET}" - - return "\n".join(frame) + return "\n".join(frames) def delicious(): - while True: - for i in range(8): - os.system('cls' if os.name == 'nt' else 'clear') - print(garden(i)) - time.sleep(0.5) - time.sleep(1) - -if __name__ == "__main__": try: - delicious() + while True: + for i in range(8): + os.system('cls' if os.name == 'nt' else 'clear') + print(garden(i)) + time.sleep(0.5) + time.sleep(1) except KeyboardInterrupt: print("\nAnimation stopped.") + +if __name__ == "__main__": + delicious()