Start window for player naming
This commit is contained in:
parent
5cf6231a90
commit
ca8f014161
34
game.py
34
game.py
|
@ -3,7 +3,7 @@ from __future__ import print_function
|
||||||
from PySide import QtCore, QtGui
|
from PySide import QtCore, QtGui
|
||||||
|
|
||||||
from player import Player
|
from player import Player
|
||||||
from windows import QuestionWindow, EditAnswersWindow
|
from windows import QuestionWindow, EditAnswersWindow, PlayerStartWindow
|
||||||
from gamestate import QuestionAnswers
|
from gamestate import QuestionAnswers
|
||||||
|
|
||||||
class SeopardyGame(QtGui.QWidget):
|
class SeopardyGame(QtGui.QWidget):
|
||||||
|
@ -12,13 +12,21 @@ class SeopardyGame(QtGui.QWidget):
|
||||||
|
|
||||||
self.questions = questions
|
self.questions = questions
|
||||||
self.gamestate = gamestate
|
self.gamestate = gamestate
|
||||||
self.players = [Player.gen_player(i, parent=self) for i in range(1, 4)]
|
#self.players = [Player.gen_player(i, parent=self) for i in range(1, 4)]
|
||||||
|
self.players = []
|
||||||
|
for i in range(3):
|
||||||
|
self.add_player()
|
||||||
|
|
||||||
self._inOtherWindow = False
|
self._inOtherWindow = False
|
||||||
|
|
||||||
self._createGui()
|
self._createGui()
|
||||||
|
|
||||||
self.showFullScreen()
|
self.showFullScreen()
|
||||||
|
|
||||||
|
# initiate starting the game!
|
||||||
|
QtCore.QCoreApplication.postEvent(self, GameStartEvent())
|
||||||
|
|
||||||
|
def add_player(self):
|
||||||
|
self.players.append(Player.gen_player(len(self.players)+1, self))
|
||||||
|
|
||||||
def _createGui(self):
|
def _createGui(self):
|
||||||
""" Create the board from questions. """
|
""" Create the board from questions. """
|
||||||
|
|
||||||
|
@ -64,6 +72,19 @@ class SeopardyGame(QtGui.QWidget):
|
||||||
|
|
||||||
self.board.itemAtPosition(number, secno-1).widget().setFocus()
|
self.board.itemAtPosition(number, secno-1).widget().setFocus()
|
||||||
|
|
||||||
|
def event(self, e):
|
||||||
|
if e.type() == GameStartEvent.eventType:
|
||||||
|
# start window
|
||||||
|
if not self._inOtherWindow:
|
||||||
|
self._inOtherWindow = True
|
||||||
|
x = PlayerStartWindow(self.players, self)
|
||||||
|
x.exec_()
|
||||||
|
self._inOtherWindow = False
|
||||||
|
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
return super(SeopardyGame, self).event(e)
|
||||||
|
|
||||||
def keyPressEvent(self, e):
|
def keyPressEvent(self, e):
|
||||||
if e.key() == QtCore.Qt.Key_Escape:
|
if e.key() == QtCore.Qt.Key_Escape:
|
||||||
self.close()
|
self.close()
|
||||||
|
@ -193,3 +214,8 @@ class FocusKeyGrabber(QtCore.QObject):
|
||||||
self._parent.setFocusToQuestion(self._secno, self._number+1)
|
self._parent.setFocusToQuestion(self._secno, self._number+1)
|
||||||
return True
|
return True
|
||||||
return QtCore.QObject.eventFilter(self, obj, e)
|
return QtCore.QObject.eventFilter(self, obj, e)
|
||||||
|
|
||||||
|
class GameStartEvent(QtCore.QEvent):
|
||||||
|
eventType = QtCore.QEvent.Type(QtCore.QEvent.registerEventType())
|
||||||
|
def __init__(self):
|
||||||
|
super(GameStartEvent, self).__init__(self.eventType)
|
||||||
|
|
70
windows.py
70
windows.py
|
@ -268,3 +268,73 @@ class KeyGrabber(QtCore.QObject):
|
||||||
self._parent.keyPressEvent(e)
|
self._parent.keyPressEvent(e)
|
||||||
return True
|
return True
|
||||||
return QtCore.QObject.eventFilter(self, obj, e)
|
return QtCore.QObject.eventFilter(self, obj, e)
|
||||||
|
|
||||||
|
class PlayerStartWindow(QtGui.QDialog):
|
||||||
|
def __init__(self, players, parent):
|
||||||
|
super(PlayerStartWindow, self).__init__(parent)
|
||||||
|
self._players = players
|
||||||
|
self._parent = parent
|
||||||
|
|
||||||
|
self._playerLineEdits = []
|
||||||
|
|
||||||
|
#print(self.disconnect(self, QtCore.SIGNAL("accepted()"), self, QtCore.SLOT("accept()")))
|
||||||
|
self._setupGui()
|
||||||
|
|
||||||
|
self.show()
|
||||||
|
|
||||||
|
# center window
|
||||||
|
g = QtGui.QApplication.desktop().screenGeometry()
|
||||||
|
cPos = self.rect()
|
||||||
|
self.move(g.width()/2 - cPos.width()/2, g.height()/2 - cPos.height()/2)
|
||||||
|
|
||||||
|
def event(self, e):
|
||||||
|
if e.type() == ButtonEvent.eventType:
|
||||||
|
if e.get_playerno() <= len(self.players)+1:
|
||||||
|
print("Button", e.get_playerno())
|
||||||
|
if e.get_playerno() == len(self.players)+1:
|
||||||
|
# add a new player
|
||||||
|
self._parent.add_player()
|
||||||
|
print("Player", self._players)
|
||||||
|
self._guiAddPlayer(self._players[-1])
|
||||||
|
self.playergrid.itemAtPosition(e.get_playerno()-1, 1).selectAll()
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
return super(PlayerStartWindow, self).event(e)
|
||||||
|
|
||||||
|
def keyPressEvent(self, e):
|
||||||
|
# needed to circumvent dialogclosing when enter is pressed
|
||||||
|
if e.key() == QtCore.Qt.Key_Escape:
|
||||||
|
self.close()
|
||||||
|
elif e.key() == QtCore.Qt.Key_K:
|
||||||
|
# FIXME: Remove this
|
||||||
|
QtCore.QCoreApplication.postEvent(self, ButtonEvent(1))
|
||||||
|
|
||||||
|
def _setupGui(self):
|
||||||
|
self.layout = QtGui.QVBoxLayout()
|
||||||
|
self.playerGrid = QtGui.QGridLayout()
|
||||||
|
|
||||||
|
for player in self._players:
|
||||||
|
self._guiAddPlayer(player)
|
||||||
|
|
||||||
|
self.layout.addLayout(self.playerGrid)
|
||||||
|
|
||||||
|
startbtn = QtGui.QPushButton("\nStart\n", self)
|
||||||
|
startbtn.setStyleSheet("QPushButton { font-size: 40px; }")
|
||||||
|
startbtn.clicked.connect(self.close)
|
||||||
|
self.layout.addWidget(startbtn)
|
||||||
|
|
||||||
|
self.setLayout(self.layout)
|
||||||
|
|
||||||
|
def _guiAddPlayer(self, player):
|
||||||
|
row = self.playerGrid.rowCount()
|
||||||
|
|
||||||
|
label = QtGui.QLabel("Player %d" % (row,), self)
|
||||||
|
label.setStyleSheet("QLabel { font-size: 40px; background-color: %s; color: white; }" % (player.color.name(),))
|
||||||
|
self.playerGrid.addWidget(label, row, 0)
|
||||||
|
|
||||||
|
edit = QtGui.QLineEdit(player.name, self)
|
||||||
|
edit.setStyleSheet("QLineEdit { font-size: 40px; }")
|
||||||
|
edit.returnPressed.connect(lambda widget=edit, player=player: player.change_name(edit.text()))
|
||||||
|
self.playerGrid.addWidget(edit, row, 1)
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue