From 66f64510f829ca0e39c82eba4116b386dbe13438 Mon Sep 17 00:00:00 2001 From: jerick Date: Sat, 9 Aug 2025 16:57:56 -0400 Subject: [PATCH] displays team scores on LCD --- random_timer.py | 73 +++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 59 insertions(+), 14 deletions(-) diff --git a/random_timer.py b/random_timer.py index 72346fb..33c9a00 100755 --- a/random_timer.py +++ b/random_timer.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 """ -Catchphrase with LCD Output (Centered & Clean) +Catchphrase with LCD Output (Scores & Reset) """ import random @@ -21,11 +21,16 @@ lcd = CharLCD(I2C_CHIP, I2C_ADDRESS) def lcd_print(text, line=0): """Print text centered on given LCD line (0 or 1), padded to clear leftovers.""" lcd.cursor_pos = (line, 0) - lcd.write_string(text.center(16)) # 16 chars wide + lcd.write_string(text.center(16)) + +def lcd_update_scores(): + """Update the bottom LCD line with team scores (T1 left, T2 right).""" + lcd.cursor_pos = (1, 0) + score_str = f"{score_t1}{' ' * 14}{score_t2}" + lcd.write_string(score_str) # ---------- HELPERS ---------- def load_words(path=WORDS_FILE): - """Return a list of non-empty stripped lines from the given file.""" try: with open(path, "r", encoding="utf-8") as f: words = [line.strip() for line in f if line.strip()] @@ -37,7 +42,6 @@ def load_words(path=WORDS_FILE): sys.exit(1) def pick_random(words): - """Return a random word from the list.""" return random.choice(words) # ---------- TIMER THREAD ---------- @@ -65,27 +69,52 @@ class CountdownTimer(threading.Thread): def is_running(self): return self._running.is_set() +# ---------- SCORE VARIABLES ---------- +score_t1 = 0 +score_t2 = 0 + +def add_score(team): + global score_t1, score_t2 + if team == 1: + score_t1 += 1 + if score_t1 >= 7: + print("šŸ† Team 1 wins! Scores reset.") + score_t1 = score_t2 = 0 + elif team == 2: + score_t2 += 1 + if score_t2 >= 7: + print("šŸ† Team 2 wins! Scores reset.") + score_t1 = score_t2 = 0 + lcd_update_scores() + +def reset_scores(): + global score_t1, score_t2 + score_t1 = score_t2 = 0 + lcd_update_scores() + # ---------- MAIN LOOP ---------- def main(): + global score_t1, score_t2 words = load_words() timer_thread = None - print("=== Catchphrase, but Better (LCD) ===") + print("=== Catchphrase, with LCD & Scores ===") print("Commands:") - print(" start - begin the 30-second timer and show a word immediately") - print(" next - show another random word while the timer is running") - print(" stop - cancel the timer early") + print(" start - start 30s timer & show a word") + print(" next - show another word while timer runs") + print(" stop - stop timer early") + print(" t1 - add point to Team 1") + print(" t2 - add point to Team 2") + print(" reset - reset both scores to 0") print(" exit / quit - leave the program") + lcd_print("Ready", line=0) + lcd_update_scores() + def announce_finish(): print("\nā° Time is up! Returning to menu.") lcd_print("Time is up!", line=0) - print("=== Catchphrase ===") - print("Commands:") - print(" start - begin the 30-second timer and show a word immediately") - print(" next - show another random word while the timer is running") - print(" stop - cancel the timer early") - print(" exit / quit - leave the program") + lcd_update_scores() while True: try: @@ -98,6 +127,7 @@ def main(): timer_thread.stop() timer_thread.join() lcd_print("Goodbye!", line=0) + lcd_update_scores() print("šŸ‘‹ Goodbye!") break @@ -112,6 +142,7 @@ def main(): word = pick_random(words) print(word) lcd_print(word, line=0) + lcd_update_scores() print(f"[+] 30-second timer started. ({TIMER_SECONDS}s)") elif cmd == "next": @@ -121,16 +152,30 @@ def main(): word = pick_random(words) print(word) lcd_print(word, line=0) + lcd_update_scores() elif cmd == "stop": if timer_thread and timer_thread.is_running: timer_thread.stop() timer_thread.join() lcd_print("Stopped early", line=0) + lcd_update_scores() print("[+] Timer stopped early.") else: print("[!] There is no running timer to stop.") + elif cmd == "t1": + add_score(1) + print(f"Team 1: {score_t1}, Team 2: {score_t2}") + + elif cmd == "t2": + add_score(2) + print(f"Team 1: {score_t1}, Team 2: {score_t2}") + + elif cmd == "reset": + reset_scores() + print("Scores reset.") + else: print(f"[!] Unknown command: {cmd}")