-
Notifications
You must be signed in to change notification settings - Fork 2
/
player_view.py
143 lines (120 loc) · 4.63 KB
/
player_view.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# player_view.py
#
# The widget for viewing the player
from PyQt6.QtWidgets import (
QHBoxLayout,
QLabel,
QLineEdit,
QPushButton,
QVBoxLayout,
QWidget,
)
from PyQt6.QtCore import Qt
class PlayerBarWidget(QWidget):
def __init__(self, root, state):
super().__init__()
self.root = root
self.game_state = state
self.initUI()
def initUI(self):
self.layout = QHBoxLayout()
self.player_widgets = []
players = self.game_state.get_players()
for i, player in enumerate(players):
pw = PlayerWidget(self, player, i, self.game_state)
pw.name_label.editingFinished.connect(lambda: self.update_name(i))
self.player_widgets.append(pw)
self.layout.addWidget(pw)
self.setLayout(self.layout)
self.show()
def set_clue_value(self, value):
for p in self.player_widgets:
p.set_clue_value(value)
def player_wager(self):
players = self.game_state.get_players()
for i, p in enumerate(players):
if p.last:
self.player_widgets[i].player_wager()
def all_wager(self):
for p in self.player_widgets:
p.player_wager()
def update_root(self):
self.root.update()
def update_name(self, player):
self.game_state.update_player_name(
player, self.player_widgets[player].name_label.text())
def update(self):
players = self.game_state.get_players()
for i, player in enumerate(players):
self.player_widgets[i].update(player)
class PlayerWidget(QWidget):
def __init__(self, parent, player, idx, state):
super().__init__()
self.parent = parent
self.game_state = state
self.index = idx
self.initUI(player)
def initUI(self, player):
self.layout = QVBoxLayout()
self.adjust_bar = QWidget()
self.adjust_layout = QHBoxLayout()
self.subtract_button = QPushButton("-")
self.value_label = QLineEdit("")
value_font = self.value_label.font()
value_font.setPointSize(24)
self.value_label.setFont(value_font)
self.value_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
self.value_label.setStyleSheet("color: white;")
self.add_button = QPushButton("+")
self.adjust_layout.addWidget(self.subtract_button, 15)
self.adjust_layout.addWidget(self.value_label, 70)
self.adjust_layout.addWidget(self.add_button, 15)
self.adjust_bar.setLayout(self.adjust_layout)
self.subtract_button.clicked.connect(
lambda: self.incorrect_answer())
self.add_button.clicked.connect(
lambda: self.correct_answer())
self.score_label = QLabel(f"${player.score}")
score_font = self.score_label.font()
score_font.setPointSize(32)
self.score_label.setFont(score_font)
self.score_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
self.score_label.setStyleSheet("color: white;")
self.name_label = QLineEdit()
name_font = self.name_label.font()
name_font.setPointSize(32)
self.name_label.setFont(name_font)
self.name_label.setText(player.name)
self.name_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
self.name_label.setStyleSheet("color: white;")
self.layout.addWidget(self.adjust_bar)
self.layout.addWidget(self.score_label)
self.layout.addWidget(self.name_label)
self.setStyleSheet("background-color:#060CE9;")
self.setLayout(self.layout)
self.show()
def correct_answer(self):
self.game_state.correct_answer(
self.index, int(self.value_label.text()))
self.parent.update_root()
def incorrect_answer(self):
self.game_state.incorrect_answer(
self.index, int(self.value_label.text()))
self.parent.update_root()
def player_wager(self):
self.value_label.setText(str(""))
self.value_label.setEnabled(True)
def set_clue_value(self, value):
self.value_label.setText(str(value))
self.value_label.setEnabled(False)
def update(self, player):
if player.last:
self.setStyleSheet("background-color:#068CE9")
else:
self.setStyleSheet("background-color:#060CE9")
if player.score >= 0:
self.score_label.setText(f"${player.score}")
self.score_label.setStyleSheet("color: white;")
else:
self.score_label.setText(f"-${abs(player.score)}")
self.score_label.setStyleSheet("color: red;")