From a69b71128936a069fe71934b3ff008c67d62549a Mon Sep 17 00:00:00 2001 From: Lyx Date: Fri, 21 Mar 2025 17:01:20 +0100 Subject: [PATCH] Finished the two main gamemodes and added a menu system --- .gitignore | 3 +- functions.py | 13 +++++++- gamemodes.py | 23 ++++++++++---- main.py | 40 +++++++++++++++++++++++-- submenus.py | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++ wordlist.py | 57 +++++++++++++++++++++-------------- 6 files changed, 189 insertions(+), 32 deletions(-) create mode 100644 submenus.py diff --git a/.gitignore b/.gitignore index 18a59c8..b910113 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ __pychace__ -*.pyc \ No newline at end of file +*.pyc +.venv \ No newline at end of file diff --git a/functions.py b/functions.py index 7aa61f0..4e6c88d 100644 --- a/functions.py +++ b/functions.py @@ -1,7 +1,6 @@ from alphabet import alphabet from os import system, name - def binarian(word): wordBin = "" word = word.lower() @@ -14,6 +13,18 @@ def binarian(word): wordBin += letter return wordBin +import re + +def unbinarian(encoded_text): + from alphabet import alphabet + + reverse_alphabet = {v: k for k, v in alphabet.items()} + + decoded = re.sub(r"\(\d{5}\)", lambda m: reverse_alphabet.get(m.group(), m.group()), encoded_text) + + return decoded + + def clear(): # for windows diff --git a/gamemodes.py b/gamemodes.py index 23eaca6..454dea4 100644 --- a/gamemodes.py +++ b/gamemodes.py @@ -5,10 +5,7 @@ from functions import binarian from functions import clear def norToBin(level): - wordsInLevel = [] - for word, word_level in words.items(): - if word_level == level: - wordsInLevel.append(word) + wordsInLevel = words[level].copy() word = random.choice(wordsInLevel) correctTranslation = binarian(word) @@ -18,6 +15,20 @@ def norToBin(level): if userIn == correctTranslation: print(f'Yes the word {word} does translate to {userIn}!') else: - print(f'No, the word {word} translates to {correctTranslation}. Better study some more!') + print(f'No, the word {word} translates to {correctTranslation}, not {userIn}. Better study some more!') + input("Hit ENTER to continue") -norToBin(2) \ No newline at end of file +def binToNor(level): + wordsInLevel = words[level].copy() + word = random.choice(wordsInLevel) + correctTranslation = word + word = binarian(word) + + print(f'Translate {word} into english!') + userIn = input("Write anwser\n>") + clear() + if userIn == correctTranslation: + print(f'Yes the word {word} does translate to {userIn}!') + else: + print(f'No, the word {word} translates to {correctTranslation}, not {userIn}. Better study some more!') + input("Hit ENTER to continue") \ No newline at end of file diff --git a/main.py b/main.py index aa706d6..fd09b48 100644 --- a/main.py +++ b/main.py @@ -1,4 +1,40 @@ from functions import binarian -from alphabet import alphabet +from functions import unbinarian +from submenus import entoBin +from submenus import binToEn +from functions import clear +import readchar -print(binarian("I am making duolingo for binary")) \ No newline at end of file +def main_menu(): + options = ["Translate from English to Binarian", "Translate from Binarian to English", "Exit"] + selected = 0 + + while True: + clear() + print("Use arrow keys to navigate and Enter to select:\n") + + for i, option in enumerate(options): + prefix = "> " if i == selected else " " + print(f"{prefix}{option}") + + key = readchar.readkey() + + if key == readchar.key.UP and selected > 0: + selected -= 1 + elif key == readchar.key.DOWN and selected < len(options) - 1: + selected += 1 + elif key == readchar.key.ENTER: + if options[selected] == "Exit": + print("Exiting...") + clear() + break + elif options[selected] == "Translate from English to Binarian": + entoBin() + elif option[selected == "Translate from Binarian to English"]: + binToEn() + else: + print(f"Selected: {options[selected]}") + input("Press Enter to return to menu...") + +if __name__ == "__main__": + main_menu() diff --git a/submenus.py b/submenus.py new file mode 100644 index 0000000..22c6b9b --- /dev/null +++ b/submenus.py @@ -0,0 +1,85 @@ +import readchar +from gamemodes import norToBin +from gamemodes import binToNor +from functions import clear +def entoBin(): + options = ["Level 1", "Level 2", "Level 3","Level 4", "Level 5" , "Back to main menu"] + selected = 0 + + while True: + clear() + print("Select a level to start translating!") + print("Use arrow keys to navigate and Enter to select:\n") + + for i, option in enumerate(options): + prefix = "> " if i == selected else " " + print(f"{prefix}{option}") + + key = readchar.readkey() + + if key == readchar.key.UP and selected > 0: + selected -= 1 + elif key == readchar.key.DOWN and selected < len(options) - 1: + selected += 1 + elif key == readchar.key.ENTER: + if options[selected] == "Level 1": + clear() + norToBin(1) + elif options[selected] == "Level 2": + clear() + norToBin(2) + elif options[selected] == "Level 3": + clear() + norToBin(3) + elif options[selected] == "Level 4": + clear() + norToBin(4) + elif options[selected] == "Level 5": + clear() + norToBin(5) + elif options[selected] == "Back to main menu": + print("Exiting...") + break + else: + print(f"Selected: {options[selected]}") + +def binToEn(): + options = ["Level 1", "Level 2", "Level 3","Level 4", "Level 5" , "Back to main menu"] + selected = 0 + + while True: + clear() + print("Select a level to start translating!") + print("Use arrow keys to navigate and Enter to select:\n") + + for i, option in enumerate(options): + prefix = "> " if i == selected else " " + print(f"{prefix}{option}") + + key = readchar.readkey() + + if key == readchar.key.UP and selected > 0: + selected -= 1 + elif key == readchar.key.DOWN and selected < len(options) - 1: + selected += 1 + elif key == readchar.key.ENTER: + if options[selected] == "Level 1": + clear() + binToNor(1) + elif options[selected] == "Level 2": + clear() + binToNor(2) + elif options[selected] == "Level 3": + clear() + binToNor(3) + elif options[selected] == "Level 4": + clear() + binToNor(4) + elif options[selected] == "Level 5": + clear() + binToNor(5) + elif options[selected] == "Back to main menu": + print("Exiting...") + break + else: + print(f"Selected: {options[selected]}") diff --git a/wordlist.py b/wordlist.py index a86c0f6..bde93c9 100644 --- a/wordlist.py +++ b/wordlist.py @@ -1,24 +1,37 @@ words = { - "cat": 1, - "dog": 1, - "fish": 1, - "apple": 1, - "ball": 1, - "house": 2, - "school": 2, - "tree": 2, - "friend": 2, - "computer": 3, - "programming": 3, - "language": 3, - "chicken": 3, - "celebrate": 4, - "dinosaur": 4, - "philosophy": 4, - "challenge": 4, - "understanding": 5, - "universe": 5, - "consciousness": 5, - "mathematics": 5, - "microprocessor": 5 + 1: [ + "aa", "add", "all", "ass", "bell", "book", "boss", "call", "cell", "cool", + "deed", "door", "doll", "egg", "fall", "feed", "fool", "good", "hall", "hill", + "jazz", "kiss", "lass", "look", "mall", "mess", "mood", "moon", "pass", "pool", + "quiz", "reed", "root", "seed", "sell", "soon", "tall", "tool", "wall", "wood", + "yell", "zoom", "beer", "boil", "bull", "cull", "dull", "fell", "gull", "will" + ], + 2: [ + "apple", "badge", "beast", "brass", "bread", "bribe", "broom", "cabin", "camel", "candy", + "cello", "chalk", "charm", "chess", "clerk", "cloak", "clown", "crash", "demon", "drain", + "drink", "dwarf", "eagle", "earth", "fable", "flame", "frost", "glove", "grand", "grape", + "haunt", "hotel", "hover", "image", "jelly", "juice", "latch", "lemon", "magic", "mango", + "merry", "music", "ocean", "onion", "penny", "plaza", "rider", "robot", "shadow", "spine" + ], + 3: [ + "ankle", "arise", "arrow", "baker", "basil", "beech", "bison", "blaze", "bloom", "brittle", + "bubble", "cactus", "canvas", "carrot", "cedar", "cherry", "circle", "coral", "cotton", "dancer", + "detour", "donkey", "dragon", "drawer", "eclair", "elixir", "fencer", "fiddle", "frozen", "guitar", + "hazard", "helmet", "insect", "island", "jigsaw", "ladder", "lawyer", "legend", "marble", "mirror", + "needle", "nickel", "orchid", "pencil", "pirate", "planet", "prince", "puzzle", "rescue", "rocket" + ], + 4: [ + "abstract", "alchemy", "bachelor", "blueprint", "brisket", "cascade", "chopper", "clarity", "compass", "complex", + "cosmos", "crystal", "dolphin", "emerald", "fathom", "feather", "flamingo", "furnace", "gazelle", "gorilla", + "harpoon", "horizon", "iceberg", "incisor", "jasmine", "juggler", "kangaroo", "lantern", "lobster", "luminous", + "meadow", "midnight", "nebula", "nomadic", "obelisk", "octagon", "paradox", "parrot", "phoenix", "pioneer", + "porcupine", "quasar", "radiant", "rhapsody", "sapphire", "satchel", "sheriff", "spectrum", "tempest", "vortex" + ], + 5: [ + "abundance", "adventure", "arithmetic", "blacksmith", "butterfly", "catastrophe", "chandelier", "chromatic", "conductor", "constellation", + "delirious", "dynamism", "elaborate", "electricity", "ephemeral", "excursion", "firebrand", "fossilized", "geography", "handcrafted", + "illuminate", "inevitable", "jeopardize", "journalist", "labyrinth", "magnificent", "metaphor", "momentous", "nightshade", "noteworthy", + "opportunity", "philosophy", "quintuplet", "resilience", "silhouette", "sophisticated", "subterranean", "symmetry", "tapestries", "thunderbolt", + "trapezoidal", "translucent", "ubiquitous", "vulnerability", "wanderlust", "whimsical", "wilderness", "xylophone", "zephyr", "zoological" + ] }