我正在pygame中构建一个多项选择测验游戏,但是我的while循环有一个问题。你知道吗
我成功地用python构建了这个游戏,没有GUI,所有的逻辑都可以正常工作,但是当我尝试将GUI添加到逻辑中时,它的工作方式就不一样了。你知道吗
在我的工作逻辑游戏中,由于我使用“raw\u input()”函数,连续while循环在每个问题上都会暂停,并等待用户回答。当我做GUI版本时,我的问题被成功地读入,但是它一次读入所有问题,没有时间回答。你知道吗
我试着添加时间。睡眠()但这意味着程序不响应事件处理程序。如果事件处理程序能够工作,那么这将是一个完美的解决方案,给用户一定的时间来回答。你知道吗
下面的示例代码不会编译,因为我遗漏了许多类和方法,但希望说明这就是我的问题所在。我阅读了中的字典键和值,然后尝试将用户输入与正确答案的索引相匹配,该索引在被洗牌之前始终是answerMain[0]。你知道吗
有没有人遇到过类似的问题或知道可能的解决方案?你知道吗
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'
下面是一个问答游戏的例子。我使用链接答案的
dt = clock.tick(fps)
解。只需将time
变量减去dt,如果它低于0,则切换到下一个问题。用户可以输入1-4,然后与问题元组的最后一个元素进行比较,以检查答案是否正确。你知道吗相关问题 更多 >
编程相关推荐