135 lines
3.9 KiB
Python
135 lines
3.9 KiB
Python
from __future__ import print_function
|
|
|
|
from PySide import QtCore, QtGui
|
|
|
|
from player import Player
|
|
from windows import QuestionWindow
|
|
|
|
class SeopardyGame(QtGui.QWidget):
|
|
def __init__(self, questions, gamestate, parent=None):
|
|
super(SeopardyGame, self).__init__(parent)
|
|
|
|
self.questions = questions
|
|
self.gamestate = gamestate
|
|
self.players = [Player.gen_player(i) for i in range(1, 4)]
|
|
self._inOtherWindow = False
|
|
|
|
self._createGui()
|
|
|
|
self.showFullScreen()
|
|
|
|
def _createGui(self):
|
|
""" Create the board from questions. """
|
|
|
|
layout = QtGui.QVBoxLayout()
|
|
headerLayout = QtGui.QHBoxLayout()
|
|
header = QtGui.QLabel("Header")
|
|
headerLayout.addWidget(header)
|
|
headerLayout.setSizeConstraint(QtGui.QLayout.SetMinimumSize)
|
|
layout.addWidget(header, alignment=QtCore.Qt.AlignCenter)
|
|
|
|
# create board
|
|
#board = QtGui.QGridLayout(6, len(self.questions.get_sections()))
|
|
board = QtGui.QGridLayout()
|
|
board.setSizeConstraint(QtGui.QLayout.SetMaximumSize)
|
|
for i, sec in enumerate(self.questions.get_sections()):
|
|
seclabel = QtGui.QLabel(sec, alignment=QtCore.Qt.AlignCenter)
|
|
seclabel.setStyleSheet("QLabel { font-size: 30px; }")
|
|
board.addWidget(seclabel, 0, i)
|
|
for j in range(5):
|
|
b = QtGui.QPushButton(str((j+1)*100))
|
|
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.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()
|
|
for i, player in enumerate(self.players):
|
|
self.playerBar.addWidget(player)
|
|
if i != len(self.players)-1:
|
|
self.playerBar.addStretch()
|
|
layout.addLayout(self.playerBar)
|
|
|
|
self.setLayout(layout)
|
|
|
|
|
|
#def event(self, e):
|
|
# print("Event", e)
|
|
# super(SeopardyGame, self).event(e)
|
|
|
|
def keyPressEvent(self, e):
|
|
print("Mainwindow", e)
|
|
print("current widget", e.key(), self.focusWidget())
|
|
if e.key() == QtCore.Qt.Key_Escape:
|
|
self.close()
|
|
elif e.key() == QtCore.Qt.Key_E:
|
|
if self._inOtherWindow:
|
|
return
|
|
self._inOtherWindow = True
|
|
#editResultsWin = EditResultsWindow()
|
|
#editResultsWin.exec_()
|
|
self._inOtherWindow = False
|
|
|
|
def go_to_question(self, section, number):
|
|
if self._inOtherWindow:
|
|
return
|
|
self._inOtherWindow = True
|
|
print("question window", section, number)
|
|
answers = self.gamestate.get_answers(section, number)
|
|
|
|
qwin = QuestionWindow(self.players, section, number, self.questions.get_question(section, number), answers, self)
|
|
qwin.showFullScreen()
|
|
qwin.exec_()
|
|
|
|
self._inOtherWindow = False
|
|
|
|
answers = qwin.get_answers()
|
|
self.gamestate.set_answers(section, number, answers)
|
|
|
|
# add points to players
|
|
# FIXME: Note that we cannot add points when revisiting a question
|
|
self._set_player_points(answers)
|
|
|
|
# restyle the button
|
|
self._restyle_button(section, number, answers)
|
|
|
|
def _set_player_points(self, answers, rollback=False):
|
|
for player, correct in answers.get_tries():
|
|
prefix = 1
|
|
if rollback:
|
|
prefix *= -1
|
|
if not correct:
|
|
prefix *= -1
|
|
player.add_points(answers.points()*prefix)
|
|
|
|
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())
|