From my previous question, I got great feedback which helped improve my code which I submitted. I went back and built upon that and came back to see if my code is an improvement.
try:
from tkinter import *
except ImportError:
from Tkinter import *
import time
import random
with open("NAME_FILE.txt") as f:
name = "".join(f.readlines())
with open("HIGH_SCORE.txt") as f:
high_score = "".join(f.readlines())
with open("WORDS_FILE.txt") as f:
WORDS = list(map(lambda s: s.strip('\n'), f.readlines()))
time_score = 0
word_count = 0
skips = 0
def shuffle():
global word, time_score, start_time
go_btn.pack_forget()
start_time = time.time()
while word_count < 12:
word = random.choice(WORDS)
label.config(text=str(word))
time_score_label.config(text="Time: " + str(time_score) + "s")
return
if word_count == 12:
end()
def restart(event):
global word_count, skips, time_score
start_time = time.time()
word_count = 0
skips = 0
time_score = 0
label.config(text="")
time_score_label.config(text="Time: " + str(time_score) + "s")
word_counter.config(text="Words: " + str(word_count))
skip_counter.config(text="Skips: " + str(skips))
wrong_label.config(text="")
high_score_label.pack_forget()
enter.pack_forget()
time_score_label.pack_forget()
word_counter.pack_forget()
skip_counter.pack_forget()
wrong_label.pack_forget()
entry.pack_forget()
time_score_label.pack()
word_counter.pack()
skip_counter.pack()
wrong_label.pack()
entry.pack()
entry.focus_set()
root.bind("<Return>", check)
shuffle()
def no(event):
pass
def new_score():
global start_time, time_score, high_score
if float(time_score) < float(high_score):
high_score_label.pack()
high_score = time_score
enter.pack()
root.bind("<Return>", no)
def write():
with open("HIGH_SCORE.txt", 'w') as f:
f.write(str(time_score))
with open("NAME_FILE.txt", 'w') as a:
a.write(str(name))
def end():
global time_score, start_time, high_score, name
entry.delete(0, END)
time_score_label.pack_forget()
label.config(text="Finished")
word_counter.config(text="Well done you did it in " + str(time_score) +"s")
new_score()
root.bind("<Return>", no)
def reset(event):
with open("NAME_FILE.txt", 'w') as f:
f.write("A really slow typer")
with open("HIGH_SCORE.txt") as a:
a.write("60.0")
def update():
global time_score, start_time, high_score, name
name = entry.get()
body.config(text="The aim of the game is to type whatever you see on the"
" screen as quickly as possible!" + '\n'"You will be"
" shown 12 words at random and you have to type them all"
" correctly as shown" + '\n' + "The current world record"
" is " + str(high_score) + "s, by " + name + '\n' +
"When you are ready, press go to begin!" + '\n' + "Press"
" [SHIFT] to reset the scores.")
write()
entry.delete(0, END)
def check(event):
global time_score, high_score, name, word_count, skips, word, start_time
if entry.get().lower() == word.lower():
update_right()
elif entry.get().lower() != word.lower():
update_wrong()
if len(entry.get()) < 1:
update_skip()
update_time()
shuffle()
entry.delete(0, END)
def update_time():
global time_score, start_time
score_score = time.time() - start_time
time_score += score_score
time_score = round(time_score,2)
def update_skip():
global time_score, high_score, name, word_count, skips, word, start_time
skips += 1
skip_counter.config(text="Skips: " + str(skips))
wrong_label.config(text="SKIPPED!", fg='red')
time_score_label.config(text="Time: " + str(time_score) + "s")
def update_right():
global time_score, high_score, name, word_count, skips, word, start_time
word_count += 1
WORDS.remove(word)
time_score_label.config(text="Time: " + str(time_score) + "s")
word_counter.config(text="Words: " + str(word_count))
wrong_label.config(text="")
def update_wrong():
global time_score, high_score, name, word_count, skips, word, start_time
wrong_label.config(text="WRONG!", fg='red')
time_score_label.config(text="Time: " + str(time_score) + "s")
root = Tk()
root.title("SpeedTypr")
root.geometry = ("750x600")
header = Label(root, text="SpeedTypr", font=('Titilium Web', 32)
, bg='Red')
seperator = Frame(root, height=2, width=2, bg='black')
body = Label(root, text="The aim of the game is to type whatever you see on"
" the screen as quickly as possible!" + '\n'
"You will be shown 12 words at random and you have"
" to type them all correctly as shown" + '\n' +
"The current world record is: " + str(high_score) +
"s, by: " + name + '\n' + "When you are ready, press"
" go to begin!" + '\n' + "Press [SHIFT] to reset"
" the scores")
label = Label(root, font=("Helvetica", 60))
time_score_label = Label(root, text="Time: " + str(time_score) +
"s", font=('Helvetica', 14))
word_counter = Label(root, text="Words: " + str(word_count),
font =("Helvetica", 14))
skip_counter = Label(root, text="Skips: " + str(skips),
font =("Helvetica", 14))
wrong_label = Label(root, text="", font =("Helvetica, 14"))
go_btn = Button(root, text="GO!", command=shuffle)
high_score_label = Label(root, text="Well done, you got the high score!"
+ '\n' + "Enter your name above" + '\n' + "Press [TAB]"
" to restart!")
enter = Button(root, text="Enter", command=update)
entry = Entry(root)
root.bind("<Return>", check)
root.bind("<Tab>", restart)
root.bind("<Shift-L>", reset)
header.pack(fill=X)
seperator.pack(fill=X)
body.pack()
label.pack()
time_score_label.pack()
word_counter.pack()
skip_counter.pack()
wrong_label.pack()
go_btn.pack()
entry.pack()
entry.focus_set()
root.mainloop()
if __name__ == "__main__":
print("Functional")
I've tried to group my code into chunks and increase the amount of functions which now have (or should have) a single purpose and I now use text files to store the data.
HIGH_SCORE.txt
60.0
NAME_FILE.txt
A really slow typer
WORDS_FILE.txt
Games Development Keyboard Speed Typer Anything Alpha Zealous Accurate Basics Shortcut Purpose Window Counter Fortress Modification Computer Science History Football Basketball Solid Phantom Battlefield Advanced Warfare Download Upload Antidisestablishmentarianism Supercalifragilisticexpialidocious Discombobulation Liberated Assassin Brotherhood Revelation Unity Syndicate Victory
Also, while testing my program before, I realised that the timer wouldn't reset after the tab key was pressed. That is now fixed. The program should now work in Python 2 as well as Python 3 (correct me if I am wrong).