from db import nederlandse_woorden, woorden import random import time import curses RED = "\033[31m" GREEN = "\033[32m" BLUE = "\033[34m" RESET = "\033[0m" BOLD = "\033[1m" UNDERLINE = "\033[4m" YELLOW = "\033[33m" MAGENTA = "\033[35m" CYAN = "\033[36m" def random_zinnen(aantal): gemaaktezinnen = [] for _ in range(aantal): onderwerp = random.choice(woorden["znw"]) werkwoord = random.choice(woorden["ww"]) lijdend_voorwerp = random.choice(woorden["lv"]) plaats = random.choice(woorden["vz"]) + " " + random.choice(woorden["znw"]) zin = onderwerp + " " + werkwoord + " " + lijdend_voorwerp + " " + plaats gemaaktezinnen.append(zin[0].upper() + zin[1:] + ".") return gemaaktezinnen def curses_engine(stdscr, items, mode="woorden"): curses.curs_set(1) curses.start_color() curses.use_default_colors() curses.init_pair(1, curses.COLOR_WHITE, curses.COLOR_BLACK) curses.init_pair(2, curses.COLOR_BLUE, curses.COLOR_BLACK) curses.init_pair(3, curses.COLOR_RED, curses.COLOR_BLACK) curses.init_pair(4, curses.COLOR_GREEN, curses.COLOR_BLACK) total_chars = 0 correct_chars = 0 start_all = time.time() for item in items: typed = "" mistakes = set() while True: stdscr.clear() y, x = 2, 0 for i in range(len(item)): if i >= len(typed): stdscr.addstr(y, x + i, item[i], curses.color_pair(1) | curses.A_UNDERLINE) else: if typed[i] == item[i]: stdscr.addstr(y, x + i, typed[i], curses.color_pair(2)) else: stdscr.addstr(y, x + i, typed[i], curses.color_pair(3) | curses.A_BOLD) stdscr.move(y, x + len(typed)) stdscr.refresh() key = stdscr.get_wch() if isinstance(key, str) and key.isprintable(): idx = len(typed) if idx < len(item): total_chars += 1 if key == item[idx]: correct_chars += 1 else: mistakes.add(idx) typed += key elif key in (curses.KEY_BACKSPACE, "\b", 127): typed = typed[:-1] if typed == item: stdscr.clear() y, x = 2, 0 stdscr.addstr(y, x, item, curses.color_pair(4) | curses.A_BOLD) stdscr.refresh() curses.napms(700) break stdscr.clear() total_time = round(time.time() - start_all, 2) accuracy = round((correct_chars / total_chars) * 100, 2) if total_chars else 0 stdscr.addstr(2, 0, f"modus: {mode}") stdscr.addstr(3, 0, f"correcte letters: {correct_chars}") stdscr.addstr(4, 0, f"totalale letters: {total_chars}") stdscr.addstr(5, 0, f"accuracy (first try): {accuracy}%") stdscr.addstr(6, 0, f"tijd: {total_time}s") stdscr.refresh() stdscr.getch() def start(aantal): woorden_list = random.sample(nederlandse_woorden, aantal) curses.wrapper(lambda stdscr: curses_engine(stdscr, woorden_list, "woorden")) def zinnen(zinnen_lijst): curses.wrapper(lambda stdscr: curses_engine(stdscr, zinnen_lijst, "zinnen")) menu = f""" bens typcurses {UNDERLINE}Menu:{RESET} 1. Start 50 woorden 2. Start kies aantal woorden 3. laad eigen woorden (nog niet) 4. zinnen (beta) 5. {BOLD}Afsluiten{RESET} """ while True: print(menu) keuze = input(" > ").strip() if not keuze.isdigit(): print("Voer een nummer in.\n") continue keuze = int(keuze) if keuze == 1: start(50) elif keuze == 2: start(int(input("hoeveel woorden? :"))) elif keuze == 4: aantal = int(input("hoeveel zinnen? :")) zinnen(random_zinnen(aantal)) elif keuze == 5: break else: print("ongeldige optie\n")