From daf0e484cdecbfb8e37c869757170b6944ee0f68 Mon Sep 17 00:00:00 2001 From: CMDR-Piboy314 <92357316+CMDR-Piboy314@users.noreply.github.com> Date: Thu, 6 Jan 2022 11:21:41 +0900 Subject: [PATCH] Initial Commit --- main.py | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ text.txt | 6 +++++ 2 files changed, 88 insertions(+) create mode 100644 main.py create mode 100644 text.txt diff --git a/main.py b/main.py new file mode 100644 index 0000000..ee9566e --- /dev/null +++ b/main.py @@ -0,0 +1,82 @@ +import curses +import time +import random + +from curses import wrapper + + +def start_screen(stdscr): + stdscr.clear() + stdscr.addstr("Welcome to the Speed Typing Test!") + stdscr.addstr("\nPress any key to begin!") + stdscr.refresh() + stdscr.getkey() + + +def display_text(stdscr, target, current, wpm=0): + stdscr.addstr(target) + stdscr.addstr(1, 0, f"WPM: {wpm}") + + for i, char in enumerate(current): + correct_char = target[i] + color = curses.color_pair(1) + if char != correct_char: + color = curses.color_pair(2) + + stdscr.addstr(0, i, char, color) + + +def load_text(): + with open("text.txt", "r") as f: + lines = f.readlines() + return random.choice(lines).strip() + + +def wpm_test(stdscr): + target_text = load_text() + current_text = [] + wpm = 0 + start_time = time.time() + stdscr.nodelay(True) + + while True: + time_elapsed = max(time.time() - start_time, 1) + wpm = round((len(current_text) / (time_elapsed / 60)) / 5) + + stdscr.clear() + display_text(stdscr, target_text, current_text, wpm) + stdscr.refresh() + + if "".join(current_text) == target_text: + stdscr.nodelay(False) + break + + try: + key = stdscr.getkey() + except: + continue + + if ord(key) == 27: + break + + elif key in ("KEY_BACKSPACE", '\b', "\x7f"): + if len(current_text) > 0: + current_text.pop() + elif len(current_text) < len(target_text): + current_text.append(key) + +def main(stdscr): + curses.init_pair(1, curses.COLOR_GREEN, curses.COLOR_BLACK) + curses.init_pair(2, curses.COLOR_RED, curses.COLOR_BLACK) + curses.init_pair(3, curses.COLOR_WHITE, curses.COLOR_BLACK) + + start_screen(stdscr) + while True: + wpm_test(stdscr) + stdscr.addstr(2, 0, "You completed the test! Press any key to continue...") + key = stdscr.getkey() + + if ord(key) == 27: + break + +wrapper(main) \ No newline at end of file diff --git a/text.txt b/text.txt new file mode 100644 index 0000000..fa98173 --- /dev/null +++ b/text.txt @@ -0,0 +1,6 @@ +This is a test for the Speed Typing Test! See how fast you can type this! +The quick brown Fox jumps over the lazy Dog. +This is a random sentence so you can find out what your WPM is! +You are testing for WPM (Words Per Minute), let me know how it goes! +A long sentence for you to test your Words Per Minute! +This is the Speed Typing Test or STT for short! You can use it to calculate your WPM. \ No newline at end of file