seopardy/player.py

84 lines
2.4 KiB
Python
Raw Normal View History

2013-11-06 20:42:00 +01:00
from __future__ import print_function
2013-12-01 22:41:44 +01:00
import yaml
2013-11-06 20:42:00 +01:00
from PySide import QtGui, QtCore
class Player(QtGui.QWidget):
DEFAULT_PLAYERS = [
("Foo", QtGui.QColor(255, 0, 0)),
("Bar", QtGui.QColor(0, 255, 0)),
("Baz", QtGui.QColor(0, 0, 255)),
("Blubb", QtGui.QColor(0, 255, 255)),
("Murr", QtGui.QColor(255, 0, 255)),
2013-12-01 22:41:44 +01:00
("Maunz", QtGui.QColor(255, 255, 0)),
2013-11-06 20:42:00 +01:00
]
def __init__(self, name, color, points=0, parent=None):
super(Player, self).__init__(parent)
self.name = name
self.color = color
self.points = points
2013-11-18 17:47:03 +01:00
self._setup_gui()
2013-11-06 20:42:00 +01:00
2013-11-18 17:47:03 +01:00
def _setup_gui(self):
2013-11-06 20:42:00 +01:00
self.layout = QtGui.QVBoxLayout()
2013-11-18 17:47:03 +01:00
self.name_label = QtGui.QLabel(self.name, self)
2013-11-06 20:42:00 +01:00
self.name_label.setStyleSheet("QLabel { font-size: 40px; background-color: %s; color: white; }" % (self.color.name(),))
self.layout.addWidget(self.name_label)
2013-11-18 17:47:03 +01:00
self.points_label = QtGui.QLabel(str(self.points), self)
2013-11-06 20:42:00 +01:00
self.points_label.setStyleSheet("QLabel { font-size: 40px;}")
self.layout.addWidget(self.points_label, alignment=QtCore.Qt.AlignRight)
self.setLayout(self.layout)
def change_name(self, new_name):
self.name = new_name
2013-11-21 15:23:48 +01:00
self.name_label.setText(new_name)
2013-11-06 20:42:00 +01:00
2013-11-21 15:23:48 +01:00
def add_points(self, amount):
2013-11-06 20:42:00 +01:00
self.points += amount
2013-11-22 02:28:24 +01:00
self.points_label.setText("% 5s" % (self.points,))
2013-11-06 20:42:00 +01:00
@classmethod
2013-11-18 17:47:03 +01:00
def gen_player(clazz, num, parent=None):
2013-11-06 20:42:00 +01:00
if num > len(clazz.DEFAULT_PLAYERS):
raise ValueError("You tried to generate too many players")
2013-11-18 17:47:03 +01:00
return clazz(*clazz.DEFAULT_PLAYERS[num-1], parent=parent)
2013-11-06 20:42:00 +01:00
2013-12-01 22:41:44 +01:00
@classmethod
def _yaml_representer(clazz, dumper, data):
#def __init__(self, name, color, points=0, parent=None):
return dumper.represent_mapping(u'!player', {
'name': data.name,
'points': data.points,
'color': data.color.name()
})
@classmethod
def _yaml_constructor(clazz, loader, node):
pdict = loader.construct_mapping(node)
pdict["color"] = QtGui.QColor(pdict["color"])
return Player(**pdict)
2013-11-21 15:38:21 +01:00
class ButtonEvent(QtCore.QEvent):
eventType = QtCore.QEvent.Type(QtCore.QEvent.registerEventType())
def __init__(self, playerno):
super(ButtonEvent, self).__init__(self.eventType)
if playerno < 0 or playerno > 10:
raise ValueError("That player number is just not plausable")
self.playerno = playerno
def get_playerno(self):
return self.playerno
2013-11-25 12:06:07 +01:00
nobodyColor = QtGui.QColor(128, 128, 128)
2013-12-01 22:41:44 +01:00
# yaml dumpers/loaders
yaml.add_representer(Player, Player._yaml_representer)
yaml.add_constructor(u'!player', Player._yaml_constructor)