def __init__(self, cfg): super(flashCardWidget, self ).__init__(cfg) self.ui = Ui_flashCardGame() self.ui.setupUi(self) self.questionGen = mathFlashQuestion() self.done = False self.ui.ansBox.returnPressed.connect(self.getAnswer) self.ui.ansBox.setEnabled(False)
class flashCardWidget(gameWidget): def __init__(self, cfg): super(flashCardWidget, self ).__init__(cfg) self.ui = Ui_flashCardGame() self.ui.setupUi(self) self.questionGen = mathFlashQuestion() self.done = False self.ui.ansBox.returnPressed.connect(self.getAnswer) self.ui.ansBox.setEnabled(False) def startGame(self): if self.runState == 0: fnt = self.ui.ansBox.font() fnt.setPointSize(80) self.ui.ansBox.setFont(fnt) self.curScore = 0 self.questionsAsked = 0 self.correct = 0 self.wrong = 0 self.runState = 1 self.ui.ansBox.setEnabled(True) self.setupNextQuestion() elif self.runState == 2: if not self.done: self.done = True return def setupNextQuestion(self): self.curScore = self.cfg['maxTime']*self.cfg['pointsPerSec'] self.startTime = time.time() nextq = self.questionGen.getNextQuestion(self.cfg) self.ui.valATxt.setText('%d' % nextq.top) self.ui.valBTxt.setText('%s%d' % (nextq.sign, nextq.bottom)) self.ui.ansBox.setText('') self.timer.start() def getAnswer(self): if self.runState != 1: return self.timer.stop() ansTxt = self.ui.ansBox.text() ansNum = int(ansTxt) if ansNum == self.questionGen.answer: self.correctAnswer() else: self.wrongAnswer() self.questionsAsked += 1 if self.questionsAsked < self.cfg['numQuestions']: self.setupNextQuestion() else: self.finish() def correctAnswer(self): self.correct += 1 t = self.cfg['maxTime'] t -= time.time() - self.startTime pts = max(self.cfg['minPoints'], int(t*self.cfg['pointsPerSec']*self.questionGen.ptsScale)) self.totalScore += pts self.updateScore[int].emit(self.totalScore) pal = self.ui.feedbackLabel.palette() pal.setColor(self.ui.feedbackLabel.foregroundRole(), QColor(0,255,0)) self.ui.feedbackLabel.setText('Correct!') self.ui.feedbackLabel.setPalette(pal) def wrongAnswer(self): self.wrong += 1 pal = self.ui.feedbackLabel.palette() pal.setColor(self.ui.feedbackLabel.foregroundRole(), QColor(255,0,0)) self.ui.feedbackLabel.setText('Wrong!') self.ui.feedbackLabel.setPalette(pal) def finishGame(self): fnt = self.ui.feedbackLabel.font() fnt.setPointSize(20) self.ui.feedbackLabel.setFont(fnt) self.ui.feedbackLabel.setText('Great Job! You got %d/%d right!' % (self.correct, self.correct+self.wrong)) pal = self.ui.feedbackLabel.palette() pal.setColor(self.ui.feedbackLabel.foregroundRole(), QColor(0,0,255)) self.ui.feedbackLabel.setPalette(pal) self.ui.ansBox.setEnabled(False) self.runState = 2