added score multiplier based on the time it takes to answer

This commit is contained in:
Zdenek Borovec 2019-09-03 18:45:00 +02:00
parent 05117ca03b
commit 7388cfdce7
2 changed files with 40 additions and 24 deletions

View file

@ -1,2 +1,2 @@
0 0.0
question will appear here/question will appear here question will appear here/question will appear here

View file

@ -2,9 +2,10 @@ from tkinter import *
from tkinter import filedialog from tkinter import filedialog
from tkinter import messagebox from tkinter import messagebox
import random import random
import time
def swapSides(): def swapSides(): # prohodi sloupce v souboru testu (a tim i zmeni co je otazka a co odpoved)
global test global test
global score global score
@ -17,8 +18,9 @@ def swapSides():
newQuestion() newQuestion()
def saveTest(): def saveTest(): # ulozi soubor testu
print(test) print(test)
print(filepath)
with open(filepath, mode='w', encoding = "UTF-8") as file: with open(filepath, mode='w', encoding = "UTF-8") as file:
file.write(str(test[2])+'\n') file.write(str(test[2])+'\n')
for i in range(len(test[0])): for i in range(len(test[0])):
@ -34,37 +36,39 @@ def saveTest():
file.write('\n') file.write('\n')
def newQuestion(): def newQuestion(): # nastavi novou otazku
global questionPlace global questionPlace
global timeAsked
questionPlace = random.randint(0, len(test[0])-1) questionPlace = random.randint(0, len(test[0])-1)
newQuestion = test[0][questionPlace][0] newQuestion = test[0][questionPlace][0]
question.set(newQuestion) question.set(newQuestion)
timeAsked = time.time()-1
def on_closing(): def on_closing(): #zobrazi se pri zavirani programu, pokud se uzivatel rozhodne program zavrit, tak prvne ulozi highscore
if messagebox.askokcancel("Quit", "Do you want to quit?"): if messagebox.askokcancel("Quit", "Do you want to quit?"):
saveTest() saveTest()
root.destroy() root.destroy()
def submitAnswer(uselessArgument): def submitAnswer(uselessArgument): # precte pole pro zadani odpovedi, zhodnoti ji, a vhodnym zpusobem zmeni score a highscore
global score global score
if answerEntry.get().lower() in test[1] [questionPlace]: if answerEntry.get().lower() in test[1] [questionPlace]:
score += 1 score += timeMultiplier
currentScore.set('Score: %s' % (score)) currentScore.set('Score: %s' % (score))
else: else:
score = 0 score = 0
currentScore.set('Score: %s' % (score)) currentScore.set('Score: %s' % (score))
if score > test[2]: if score > test[2]:
test[2] = score test[2] = score
highScore.set('High score: %d' % (test[2])) highScore.set('High score: %s' % (test[2]))
answerEntry.delete(0, 'end') answerEntry.delete(0, 'end')
newQuestion() newQuestion()
currentScore.set('Score: %s' % score) currentScore.set('Score: %s' % round(score, 2))
highScore.set('High score: %d' % test[2]) highScore.set('High score: %s' % round(test[2], 2))
def formatTest(filepath): def formatTest(filepath): # precte soubor a formatuje jej do listu test
global score global score
with open(filepath, encoding = "UTF-8") as file: with open(filepath, encoding = "UTF-8") as file:
file = file.readlines() file = file.readlines()
@ -72,7 +76,7 @@ def formatTest(filepath):
sloupec1 = [] sloupec1 = []
sloupec2 = [] sloupec2 = []
highScore = int(file[0].strip('\n')) highScore = float(file[0].strip('\n'))
score = 0 score = 0
for line in file[1:]: for line in file[1:]:
@ -84,11 +88,11 @@ def formatTest(filepath):
return sloupec1, sloupec2, highScore return sloupec1, sloupec2, highScore
def getExtension(filename): # returns the last four characters in a filename as a string def getExtension(filename): # vrati posledni ctyry znaky jmena souboru
return filename[-4:None] return filename[-4:None]
def openTest(): def openTest(): # ziska adresu testu a nastavi seznam test aby byl novy test, pote reqestuje novou otazku
global test global test
global filepath global filepath
saveTest() saveTest()
@ -101,10 +105,11 @@ def openTest():
else: else:
messagebox.showinfo("ERROR: unrecognized file", "We are sorry but files with the '%s' appendix are not supported, we support only '.tst' files." % (getExtension(filepath))) messagebox.showinfo("ERROR: unrecognized file", "We are sorry but files with the '%s' appendix are not supported, we support only '.tst' files." % (getExtension(filepath)))
test = list(formatTest(filepath)) test = list(formatTest(filepath))
newQuestion() #newQuestion()
submitAnswer(0)
def makeMenu(root): def makeMenu(root): # vytvori menu
menu = Menu(root) menu = Menu(root)
root.config(menu=menu) root.config(menu=menu)
@ -114,7 +119,7 @@ def makeMenu(root):
return menu return menu
def makeRoot(): def makeRoot(): # vytvori hlavni okno
root = Tk() root = Tk()
root.geometry('500x400') root.geometry('500x400')
root.title('Test Me') root.title('Test Me')
@ -124,15 +129,16 @@ def makeRoot():
root = makeRoot() root = makeRoot()
filepath = 'programfiles/default.tst' filepath = 'programfiles/default.tst'
test = list(formatTest('programfiles/default.tst')) test = list(formatTest('programfiles/default.tst'))
questionPlace = 0 questionPlace = 0
score = 0 score = 0.0
currentScore = StringVar() currentScore = StringVar()
highScore = StringVar() highScore = StringVar()
question = StringVar() question = StringVar()
timeMultiplier = StringVar() timeMultiplier = 1
anotherSpacingLabel = Label(bg='gray', padx=5) anotherSpacingLabel = Label(bg='gray', padx=5)
currentScoreLabel = Label(root, textvariable=currentScore, bg='gray') currentScoreLabel = Label(root, textvariable=currentScore, bg='gray')
@ -141,7 +147,7 @@ questionLabel = Label(root, textvariable=question, width=30)
spacingLabel = Label(bg='gray', padx=10) spacingLabel = Label(bg='gray', padx=10)
answerEntry = Entry(root, width=30) answerEntry = Entry(root, width=30)
answerButton = Button(root, text='submit', command=lambda: submitAnswer(0)) answerButton = Button(root, text='submit', command=lambda: submitAnswer(0))
timeMultiplierLabel = Label(root, textvariable=timeMultiplier, bg='gray') timeMultiplierLabel = Label(root, text='Your time bonus is: %s' %timeMultiplier, bg='gray')
anotherSpacingLabel.grid(row=0, column=0) anotherSpacingLabel.grid(row=0, column=0)
currentScoreLabel.grid(row=0, column=1, columnspan=2, sticky=W) currentScoreLabel.grid(row=0, column=1, columnspan=2, sticky=W)
@ -150,16 +156,26 @@ questionLabel.grid(row=1, column=2, sticky=W)
spacingLabel.grid(row=1, column=3) spacingLabel.grid(row=1, column=3)
answerEntry.grid(row=1, column=4, sticky=E) answerEntry.grid(row=1, column=4, sticky=E)
answerButton.grid(row=1, column=5, sticky=W) answerButton.grid(row=1, column=5, sticky=W)
#timeMultiplierLabel.grid(row=2, column=1, columnspan=5) timeMultiplierLabel.grid(row=2, column=1, columnspan=5)
currentScore.set('Score: %s' %(score)) currentScore.set('Score: %s' %(score))
highScore.set('High score: %d' %(test[2])) highScore.set('High score is: %s' %(test[2]))
question.set('question will appear here') question.set('question will appear here')
timeMultiplier.set('Time multiplier is: 0')
root.protocol("WM_DELETE_WINDOW", on_closing) root.protocol("WM_DELETE_WINDOW", on_closing)
answerEntry.bind('<Return>', submitAnswer) answerEntry.bind('<Return>', submitAnswer)
menu = makeMenu(root) menu = makeMenu(root)
root.mainloop() running = True
while(running):
try:
if time.time() - timeAsked >=100:
timeMultiplier = 1
else:
timeMultiplier = (1/(time.time()-timeAsked)*100)
timeMultiplierLabel.configure(text='Your time bonus is: %s' %round(timeMultiplier, 2))
except:
None
root.update()