I'm building a multiple choice quiz game in pygame however there is an issue with my while loop.
I successfully built the game in python without the GUI and all the logic works fine, however, when I try and add the GUI to the logic it doesn't work in the same way.
In my working logic game the continuous while loop is paused at every question due to me using the 'raw_input()' function and waits for the user to answer. When I do the GUI version my questions are read in successfully but it reads them all in at once and there is no time to answer.
I've tried adding time.sleep() after reading each question but this means that the program doesn't respond to event handlers. If the event handlers did work then this would be the perfect solution, giving users a certain amount of time to answer.
This example code below won't compile as I have left out many classes and methods but wanted to show this is where my issue lies. I read dictionary keys and values in and then try and match the user input with the index of the correct answer which is always answerMain[0] before getting shuffled.
Has anyone had a similar issue or know of a possible solution?
attemptMain = {'Q': 'Question Here', 'A': 'Answer1','B': 'Answer2','c': 'Answer3','D': 'Answer4', }
def normalQuestions(self, list):
for i in list:
questionMain = self.attemptMain.keys()[i - 1]
answersMain = (self.attemptMain.values()[i - 1])
correctAnswerMain = answersMain[0]
shuffle(answersMain)
answersMainIndex = (1 + answersMain.index(correctAnswerMain) )
self.layout.questionandAnswers(questionMain, answersMain[0], answersMain[1], answersMain[2], answersMain[3])
time.sleep(10)
x = self.layout.controls()
if (x == answersMainIndex):
print("this is the correct answer")
def controls(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_1:
print("here" + str(1))
return '1'
elif event.key == pygame.K_2:
print("here" + str(2))
return '2'
elif event.key == pygame.K_3:
print("here" + str(3))
return '3'
elif event.key == pygame.K_4:
print("here" + str(4))
return '4'