I have made a game (I like to code when in my spare time) where words are taken at random from a list and displayed on a label. If they type it correctly, their wordcount
goes up by 1 and the word is removed from the list. If they get it wrong, the word remains. If they enter nothing in an attempt to skip the long words, they get a skip. After their wordcount
has reached 12 and their time is less than 60 seconds, they get the new high score.
try:
from tkinter import *
except ImportError:
from Tkinter import *
import time
import random
time_score = 0
high_score = 60.00
name = "A really slow typer"
words = ["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"]
wordcount = 0
skips = 0
start_time = time.time()
def restart(event):
global wordcount, skips, time_score
start_time = time.time()
wordcount = 0
skips = 0
time_score = 0
label.config(text="")
time_scoreLabel.config(text="Time: " + str(time_score) + "s")
wordcounter.config(text="Words: " + str(wordcount))
skipcounter.config(text="Skips: " + str(skips))
wronglabel.config(text="")
hilab.pack_forget()
enter.pack_forget()
time_scoreLabel.pack_forget()
wordcounter.pack_forget()
skipcounter.pack_forget()
wronglabel.pack_forget()
entry.pack_forget()
time_scoreLabel.pack()
wordcounter.pack()
skipcounter.pack()
wronglabel.pack()
entry.pack()
entry.focus_set()
root.bind("<Return>", check)
shuffle()
def shuffle():
global word, time_score, start_time
go.pack_forget()
while wordcount < 12:
time_score = time.time() - start_time
time_score = round(time_score,2)
word = random.choice(words)
label.config(text=str(word))
time_scoreLabel.config(text="Time: " + str(time_score) + "s")
return
if wordcount == 12:
end()
def no(event):
pass
def end():
global time_score, start_time, high_score, name
time_score = time.time() - start_time
time_score = round(time_score,2)
entry.delete(0, tkinter.END)
time_scoreLabel.pack_forget()
label.config(text="Finished")
wordcounter.config(text="Well done you did it in " + str(time_score) +
"s"
)
if float(start_time) < float(high_score):
hilab.pack()
high_score = time_score
enter.pack()
root.bind("<Return>", no)
def update():
global time_score, start_time, high_score, name
name = entry.get()
ThirdLine.config(text="The current world record is: " + str(high_score)
+ "s, by: " + name
)
entry.delete(0, tkinter.END)
def check(event):
global time_score, high_score, name, wordcount, skips, word, start_time
now = time.time()
time_score = time.time() - start_time
time_score = round(time_score,2)
if entry.get().lower() == word.lower():
time_scoreLabel.config(text="Time: " + str(time_score) + "s")
wordcount += 1
wordcounter.config(text="Words: " + str(wordcount))
wronglabel.config(text="")
words.remove(word)
shuffle()
elif entry.get().lower() != word.lower():
wronglabel.config(text="WRONG!", fg='red')
time_scoreLabel.config(text="Time: " + str(time_score) + "s")
shuffle()
entry.delete(0, tkinter.END)
root = Tk()
root.title("SpeedTypr")
root.geometry = ("750x600")
Header = Label(root, text="SpeedTypr", font=('Titilium Web', 32)
, bg='Red')
Header.pack(fill=X)
seperator1 = Frame(root, height=2, width=2, bg='black')
seperator1.pack(fill=X)
FirstLine = Label(root, text="The aim of the game is to type"
"whatever you"
"see on the screen as quickly as possible"
)
FirstLine.pack()
SecondLine = Label(root, text="You will be shown 12 words at"
"random and you have to type them all"
"correctly as shown"
)
SecondLine.pack()
ThirdLine = Label(root, text="The current world record is: " +
str(high_score) + "s, by: " + name
)
ThirdLine.pack()
SeventhLine = Label(root, text="When you are ready, press go"
"to begin!"
)
SeventhLine.pack()
label = Label(root, font=("Helvetica", 60))
label.pack()
time_scoreLabel = Label(root, text="Time: " + str(time_score) +
"s", font=('Helvetica', 14)
)
time_scoreLabel.pack()
wordcounter = Label(root, text="Words: " + str(wordcount),
font =("Helvetica", 14)
)
wordcounter.pack()
skipcounter = Label(root, text="Skips: " + str(skips),
font =("Helvetica", 14)
)
skipcounter.pack()
wronglabel = Label(root, text="", font =("Helvetica, 14"))
wronglabel.pack()
go = Button(root, text="GO!", command=shuffle)
go.pack()
hilab = Label(root, text="Well done, you got the high score!"
"Enter your name below"
"Press [TAB] to restart!"
)
enter = Button(root, text="Enter", command=update)
entry = Entry(root)
root.bind("<Return>", check)
root.bind("<Tab>", restart)
entry.pack()
entry.focus_set()
root.mainloop()
I wanted to share my code with you guys to find out if there is anything that can be improved in terms of code shortening or even bugs that I could've missed out when writing the program. Also, I made this in Python 3 and used the try
and except
to try and help it work in Python 2. I do not have Python 2 so could you please also try and see if it works on there.