Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
loeasy68
GitHub Repository: loeasy68/loeasy68.github.io
Path: blob/main/Typing test/typingTest.py
2937 views
1
import curses
2
from curses import wrapper
3
import time
4
import random
5
6
7
def start_screen(stdscr):
8
stdscr.clear()
9
stdscr.addstr("Welcome to the Speed Typing Test!")
10
stdscr.addstr("\nPress any key to begin!")
11
stdscr.refresh()
12
stdscr.getkey()
13
14
def display_text(stdscr, target, current, wpm=0):
15
stdscr.addstr(target)
16
stdscr.addstr(1, 0, f"WPM: {wpm}")
17
18
for i, char in enumerate(current):
19
correct_char = target[i]
20
color = curses.color_pair(1)
21
if char != correct_char:
22
color = curses.color_pair(2)
23
24
stdscr.addstr(0, i, char, color)
25
26
def load_text():
27
lines = """
28
Hello world my name is Tim and I am the best at making tutorials!
29
This is another test block of text for this project!
30
Subscribe to Tech With Tim on YouTube.
31
Okay this is just another test to make sure the app is working okay!
32
The quick brown fox jumped over the rest of the sentence that I forget.
33
"""
34
return random.choice(lines).strip
35
36
def wpm_test(stdscr):
37
target_text = load_text()
38
current_text = []
39
wpm = 0
40
start_time = time.time()
41
stdscr.nodelay(True)
42
43
while True:
44
time_elapsed = max(time.time() - start_time, 1)
45
wpm = round((len(current_text) / (time_elapsed / 60)) / 5)
46
47
stdscr.clear()
48
display_text(stdscr, target_text, current_text, wpm)
49
stdscr.refresh()
50
51
if "".join(current_text) == target_text:
52
stdscr.nodelay(False)
53
break
54
55
try:
56
key = stdscr.getkey()
57
except:
58
continue
59
60
if ord(key) == 27:
61
break
62
63
if key in ("KEY_BACKSPACE", '\b', "\x7f"):
64
if len(current_text) > 0:
65
current_text.pop()
66
elif len(current_text) < len(target_text):
67
current_text.append(key)
68
69
70
def main(stdscr):
71
curses.init_pair(1, curses.COLOR_GREEN, curses.COLOR_BLACK)
72
curses.init_pair(2, curses.COLOR_RED, curses.COLOR_BLACK)
73
curses.init_pair(3, curses.COLOR_WHITE, curses.COLOR_BLACK)
74
75
start_screen(stdscr)
76
while True:
77
wpm_test(stdscr)
78
stdscr.addstr(2, 0, "You completed the text! Press any key to continue...")
79
key = stdscr.getkey()
80
81
if ord(key) == 27:
82
break
83
84
wrapper(main)
85
86