Answering questions works

master
Sebastian Lohff 11 years ago
parent 17b8ba4047
commit a6742ddfc5

@ -12,6 +12,7 @@ class SeopardyGame(QtGui.QWidget):
self.questions = questions
self.gamestate = gamestate
self.players = [Player.gen_player(i) for i in range(1, 4)]
self._inQuestion = False
self._createGui()
@ -40,9 +41,10 @@ class SeopardyGame(QtGui.QWidget):
b.setSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding)
b.setAutoDefault(False)
b.setStyleSheet("QPushButton { font-size: 60px; }")
b.clicked.connect(lambda sec=sec, j=j: self.tfunc(sec, j+1))
b.clicked.connect(lambda sec=sec, j=j: self.go_to_question(sec, j+1))
board.addWidget(b, j+1, i)
layout.addLayout(board)
self.board = board
# create player bar
self.playerBar = QtGui.QHBoxLayout()
@ -54,19 +56,60 @@ class SeopardyGame(QtGui.QWidget):
self.setLayout(layout)
def keyPressEvent(self, e):
print("Mainwindow", e)
if e.key() == QtCore.Qt.Key_Escape:
self.close()
def tfunc(self, section, number):
def go_to_question(self, section, number):
if self._inQuestion:
return
self._inQuestion = True
print("question window", section, number)
qwin = QuestionWindow(self.players, section, number, self.questions.get_question(section, number), self)
qwin.showFullScreen()
qwin.exec_()
results = qwin.get_results()
for result in results:
print("result", result)
self._inQuestion = False
answers = qwin.get_answers()
# add points to players
# FIXME: Note that we cannot add points when revisiting a question
for player, correct in answers.get_tries():
points = number*100
if not correct:
points *= -1
player.add_points(points)
# restyle the button
self._restyle_button(section, number, answers)
def _restyle_button(self, sec, qno, answers):
secno = self.questions.get_number_from_section(sec)
btn = self.board.itemAtPosition(qno, secno-1).widget()
print("btn", btn, type(btn))
btnstr = ""
btncolor = None
# restrict number of tries shown
numTries = 4 if answers.got_answered() else 3
tries = answers.get_tries()
if len(tries) > numTries:
btnstr = "...\n"
for player, correct in answers.get_tries()[-numTries:]:
if correct:
btncolor = player.color
prefix = "+"
else:
prefix = "-"
btnstr += "%s%s\n" % (prefix, player.name)
if not answers.got_answered():
btnstr += "+nobody"
btncolor = QtGui.QColor(128, 128, 128)
btn.setStyleSheet("QPushButton { background-color: %s; color: white; font-size: 20px; border: none; }" % (btncolor.name(),))
btn.setText(btnstr.strip())

@ -4,10 +4,25 @@ class GameState(object):
def __init__(self, statefile=None):
pass
class QuestionAnswer(object):
def __init__(self, section, question, player, answerno, correct):
class QuestionAnswers(object):
def __init__(self, section, qnumber):
self.section = section
self.question = question
self.player = player
self.answerno = answerno
self.correct = correct
self.qnumber = qnumber
self.nobody_answered = False
self.tries = []
def add_try(self, player, correct):
self.tries.append((player, correct))
def nobody_knew(self):
self.nobody_answered = True
def got_answered(self):
return not self.nobody_answered
def get_tries(self):
return self.tries
def is_answered(self):
return self.nobody_answered or any([c for (u, c) in self.tries])

@ -37,7 +37,7 @@ class Player(QtGui.QWidget):
def add_points(self, amount):
self.points += amount
self.points_label.set_text(str(self.points))
self.points_label.setText(str(self.points))
@classmethod
def gen_player(clazz, num):

@ -32,6 +32,12 @@ class Questions(object):
raise ValueError("question parameter needs to be an integer between 1 and 5")
return self.get_questions(section)[question-1]
def get_number_from_section(self, section):
for i,s in enumerate(self._questions, 1):
if s["Section"] == section:
return i
raise ValueError("Section '%s' does not exist" % section)
def _read_questions(self):
f = None

@ -1,6 +1,6 @@
from PySide import QtGui, QtCore
from gamestate import QuestionAnswer
from gamestate import QuestionAnswers
#class QuestionWindow(QtGui.QWidget):
class QuestionWindow(QtGui.QDialog):
@ -11,13 +11,13 @@ class QuestionWindow(QtGui.QDialog):
self.section = section
self.qnumber = qnumber
self.question = question
self.results = []
self.answers = QuestionAnswers(self.section, self.qnumber)
self._setupGui()
self.setWindowTitle("Seopardy")
self.setWindowTitle("Seopardy - %s - %d" % (section, qnumber*100))
def get_results(self):
return self.results
def get_answers(self):
return self.answers
def _mkQuestionLabel(self, text):
question = QtGui.QLabel(text, alignment=QtCore.Qt.AlignCenter)
@ -56,19 +56,19 @@ class QuestionWindow(QtGui.QDialog):
def keyPressEvent(self, e):
print("question", e, e.key())
if e.key() == QtCore.Qt.Key_Escape:
if not self.answers.is_answered():
self.answers.nobody_knew()
self.close()
elif e.key() >= ord('1') and e.key() <= ord(str(len(self.players))):
elif not self.answers.is_answered() and e.key() >= ord('1') and e.key() <= ord(str(len(self.players))):
player = self.players[e.key() - ord('1')]
qawin = QuestionAnswerWindow(player, self)
res = qawin.exec_()
if res == QuestionAnswerWindow.CORRECT:
print("Done!")
q = QuestionAnswer(self.section, self.qnumber, player, len(self.results)+1, True)
self.results.append(q)
self.answers.add_try(player, correct=True)
self.accept()
elif res == QuestionAnswerWindow.WRONG:
q = QuestionAnswer(self.section, self.qnumber, player, len(self.results)+1, False)
self.results.append(q)
self.answers.add_try(player, correct=False)
class QuestionAnswerWindow(QtGui.QDialog):
CORRECT = 1
@ -87,7 +87,6 @@ class QuestionAnswerWindow(QtGui.QDialog):
cPos = self.rect()
self.move(g.width() - cPos.width(), g.height() - cPos.height())
def _setupGui(self):
self.layout = QtGui.QVBoxLayout()
@ -109,4 +108,3 @@ class QuestionAnswerWindow(QtGui.QDialog):
self.layout.addLayout(btnbox)
self.setLayout(self.layout)

Loading…
Cancel
Save