-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathui_pygame.py
355 lines (319 loc) · 16.9 KB
/
ui_pygame.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
"""This file creates a user interface (UI) with PyGame that shows the board and that can be interacted with by a
human player by clicking buttons"""
from board import Row
from player import CrossPossibility
import pygame
class PyGameUi(object):
"""creates a board with PyGame to interact with (as a human Player) in conformity with the rules"""
pygame.init()
scale_factor = 3/4
# define colors
black = (0, 0, 0) # Klassenvariabeln
dark_grey = (120, 120, 120)
light_grey = (205, 205, 205)
white = (255, 255, 255)
light_red = (255, 156, 163)
red = (255, 103, 115)
light_yellow = (248, 220, 127)
yellow = (251, 212, 85)
yellow_vibrant = (255, 195, 0)
light_green = (184, 221, 196)
green = (142, 220, 166)
light_blue = (197, 220, 242)
blue = (149, 198, 248)
blue_vibrant = (0, 129, 255)
green_vibrant = (62, 224, 109)
red_vibrant = (255, 0, 20)
# define lengths, coordinates, and proportions of objects appearing on the board
screen_x_length = int(1216 * scale_factor)
screen_y_length = int(650 * scale_factor)
box_x = int(32 * scale_factor)
box_y = box_x
box_y_distance = int(126 * scale_factor)
box_x_length = int(1152 * scale_factor)
box_y_length = int(118 * scale_factor)
button_length = int(80 * scale_factor)
button_x_distance = int(92 * scale_factor)
button_y = int(50 * scale_factor)
button_x = button_y
button_text_y = int(70 * scale_factor)
button_mark_y = int(50 * scale_factor) # here
circle_diameter = int(72 * scale_factor)
circle_radius = circle_diameter // 2
circle_x = int(1112 * scale_factor)
circle_text_x_offset = int(-10 * scale_factor)
circle_y = int(90 * scale_factor)
circle_text_y_offset = int(6 * scale_factor)
penalty_box_x = int(784 * scale_factor)
penalty_box_y = (536 * scale_factor)
penalty_box_x_length = int(400 * scale_factor)
penalty_box_y_length = int(60 * scale_factor)
penalty_button_length = button_length // 2
penalty_button_x_offset = int(146 * scale_factor)
penalty_text_x_offset = int(16 * scale_factor)
penalty_text_y_offset = (penalty_box_y_length - penalty_button_length) // 2
skip_button_x = int(680 * scale_factor)
skip_button_x_length = int(75 * scale_factor)
dice_text_x_offset = int(225 * scale_factor)
dice_text_y_offset = int(40 * scale_factor)
player_mode_x_offset = int(205 * scale_factor)
player_mode_y_offset = dice_text_y_offset * 2
font_numbers_size = int(28 * scale_factor)
font_lock_size = int(50 * scale_factor)
def __init__(self):
size = (PyGameUi.screen_x_length, PyGameUi.screen_y_length)
self.screen = pygame.display.set_mode(size)
self.is_mouse_down = False
self.last_action = None
self.crosses_by_color = [set(), set(), set(), set()]
self.penalties = 0
self.lst_eyes = [0, 0, 0, 0, 0, 0]
self.is_turn_invalid = False
self.is_active_player = True
def show_board(self) -> None:
"""shows board with PyGame functions"""
pygame.display.set_caption("Qwixx Board")
if self.is_turn_invalid:
self.screen.fill(PyGameUi.red_vibrant)
else:
self.screen.fill(PyGameUi.white)
font = pygame.font.SysFont('Comic Sans MS', PyGameUi.font_numbers_size, True, False)
lock = pygame.font.SysFont('Comic Sans MS', PyGameUi.font_lock_size, True, False)
self._render_colored_rows(font, lock)
self._render_penalties(font)
self._render_skip_button(font)
self._render_dice(font)
self._show_player_mode(font)
clock = pygame.time.Clock()
clock.tick(60)
pygame.display.flip()
def _render_colored_rows(self, font, lock) -> None:
"""draws the colored rows and creates buttons in these lines"""
for row in range(4):
for event in pygame.event.get(): # User did something
if event.type == pygame.QUIT: # If user clicked close
PyGameUi.close()
return
inactive_color, background_color, active_color = PyGameUi.convert_number_to_color(row)
pygame.draw.rect(self.screen, background_color,
[PyGameUi.box_x, PyGameUi.box_y + PyGameUi.box_y_distance * row, PyGameUi.box_x_length,
PyGameUi.box_y_length], 0) # box behind the buttons
for eyes in range(0, 11):
self.button(eyes, PyGameUi.button_length, PyGameUi.button_length, inactive_color, active_color)
text = font.render(f"{int(eyes + 2)}", True, PyGameUi.white)
if row < 2:
self.screen.blit(text, [PyGameUi.button_length + PyGameUi.button_x_distance * eyes,
PyGameUi.box_y_distance * row + PyGameUi.button_text_y])
else:
self.screen.blit(text, [PyGameUi.button_length + PyGameUi.button_x_distance * (10 - eyes),
PyGameUi.box_y_distance * row + PyGameUi.button_text_y])
self.button(12, PyGameUi.circle_diameter, PyGameUi.circle_diameter, inactive_color, active_color, True)
text = lock.render("*", True, PyGameUi.white)
self.screen.blit(text, [PyGameUi.circle_x + PyGameUi.circle_text_x_offset,
PyGameUi.circle_y * (row + 1) + PyGameUi.circle_radius * (
row - 1) + PyGameUi.circle_text_y_offset])
def _render_penalties(self, font) -> None:
"""draws the penalty row and creates four buttons in that line"""
pygame.draw.rect(self.screen, PyGameUi.light_grey,
[PyGameUi.penalty_box_x, PyGameUi.penalty_box_y, PyGameUi.penalty_box_x_length,
PyGameUi.penalty_box_y_length], 0)
for eyes in range(1, 5):
self.button(eyes, PyGameUi.penalty_button_length, PyGameUi.penalty_button_length, PyGameUi.dark_grey,
PyGameUi.black)
text = font.render("penalties", True, PyGameUi.white)
self.screen.blit(text, [PyGameUi.penalty_box_x + PyGameUi.penalty_text_x_offset,
PyGameUi.penalty_box_y + PyGameUi.penalty_text_y_offset])
def _render_skip_button(self, font) -> None:
"""draws a skip button"""
pygame.draw.rect(self.screen, PyGameUi.light_grey,
[PyGameUi.skip_button_x, PyGameUi.penalty_box_y, PyGameUi.skip_button_x_length,
PyGameUi.penalty_box_y_length], 0)
self.button(0, PyGameUi.skip_button_x_length, PyGameUi.penalty_box_y_length, PyGameUi.light_grey,
PyGameUi.dark_grey)
text = font.render("skip", True, PyGameUi.white)
self.screen.blit(text, [PyGameUi.skip_button_x + PyGameUi.penalty_text_y_offset,
PyGameUi.penalty_box_y + PyGameUi.penalty_text_y_offset])
def _render_dice(self, font) -> None:
"""renders the dice onto the board"""
for dice in range(len(self.lst_eyes)):
text = font.render(f"{self.lst_eyes[dice]}", True, self.convert_number_to_color(dice, True))
self.screen.blit(text,
[PyGameUi.button_length + PyGameUi.button_x_distance * dice,
PyGameUi.penalty_box_y + PyGameUi.penalty_text_y_offset])
text = font.render("your dice", True, PyGameUi.dark_grey)
self.screen.blit(text, [PyGameUi.box_x + PyGameUi.dice_text_x_offset,
PyGameUi.penalty_box_y + PyGameUi.dice_text_y_offset])
def show_options_on_board(self, possibility_lst) -> None:
"""marks the fields that can be crossed for a turn"""
font = pygame.font.SysFont('Comic Sans MS', PyGameUi.font_numbers_size, True, False)
for possibility in possibility_lst:
if len(possibility) == 0 or possibility[0].row == 4:
continue
# don't have to look at double turns because fields are marked anyway in single turn option
row = possibility[0].row
eyes = int(possibility[0].eyes)
assert (eyes is not None)
text = font.render("°", True, PyGameUi.black)
if row < 2:
self.screen.blit(text, [PyGameUi.button_length + PyGameUi.button_x_distance * (eyes - 2),
PyGameUi.box_y_distance * row + PyGameUi.button_mark_y])
else:
self.screen.blit(text, [PyGameUi.button_length + PyGameUi.button_x_distance * (12 - eyes),
PyGameUi.box_y_distance * row + PyGameUi.button_mark_y])
clock = pygame.time.Clock()
clock.tick(5)
pygame.display.flip()
def _show_player_mode(self, font) -> None:
"""shows whether the player is active or passive """
if self.is_active_player:
player_mode = "active player"
else:
player_mode = "passive player"
text = font.render(f"{player_mode}", True, PyGameUi.dark_grey)
self.screen.blit(text, [PyGameUi.box_x + PyGameUi.player_mode_x_offset,
PyGameUi.penalty_box_y + PyGameUi.player_mode_y_offset])
def get_turn(self) -> CrossPossibility:
"""waits for a player's action, returns it, and resets it"""
while self.last_action is None:
self.show_board()
last_action = self.last_action
self.last_action = None
return last_action
def button(self, eyes, w, h, inactive_color, active_color, circle=False) -> None:
"""makes the appearance of buttons interactive"""
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
x, y = PyGameUi.convert_eyes_to_coordinates(PyGameUi.convert_color_to_row(active_color), eyes, circle)
if click[0] == 0:
self.is_mouse_down = False
if PyGameUi.is_mouse_over_button(x, y, w, h, circle, mouse):
self._choose_color_for_button_under_mouse(x, y, w, h, active_color, click, circle)
else:
self._choose_color_for_button_independently_of_mouse(x, y, w, h, active_color, inactive_color, circle)
def _choose_color_for_button_under_mouse(self, x, y, w, h, active_color, click, circle) -> None:
"""choose color for button when the cursor is pointed at it"""
if circle:
pygame.draw.circle(self.screen, active_color, [x, y], w // 2, 0)
else:
pygame.draw.rect(self.screen, active_color, (x, y, w, h))
if click[0] == 1:
self._click_button(x, active_color)
def _choose_color_for_button_independently_of_mouse(self, x, y, w, h, active_color, inactive_color, circle) -> None:
"""choose color for button when the cursor isn't pointed at it"""
eyes = PyGameUi.convert_coordinates_to_eyes(active_color, x)
if active_color == PyGameUi.red_vibrant and eyes in self.crosses_by_color[0]:
inactive_color = active_color
if active_color == PyGameUi.yellow_vibrant and eyes in self.crosses_by_color[1]:
inactive_color = active_color
if active_color == PyGameUi.green_vibrant and eyes in self.crosses_by_color[2]:
inactive_color = active_color
if active_color == PyGameUi.blue_vibrant and eyes in self.crosses_by_color[3]:
inactive_color = active_color
if active_color == PyGameUi.black and eyes <= self.penalties:
inactive_color = active_color
if circle:
pygame.draw.circle(self.screen, inactive_color, [x, y], w // 2, 0)
else:
pygame.draw.rect(self.screen, inactive_color, (x, y, w, h))
def _click_button(self, x, active_color) -> bool: # comparable to 'cross()'
"""sets a cross chosen by the player, chooses a penalty, or skips one turn"""
if self.is_mouse_down or self.last_action is not None:
return False
self.is_mouse_down = True
self.is_turn_invalid = False
row = active_color
eyes = PyGameUi.convert_coordinates_to_eyes(row, x)
if eyes is not None:
if row == PyGameUi.red_vibrant:
self.last_action = CrossPossibility(Row.RED, eyes)
if row == PyGameUi.yellow_vibrant:
self.last_action = CrossPossibility(Row.YELLOW, eyes)
if row == PyGameUi.green_vibrant:
self.last_action = CrossPossibility(Row.GREEN, eyes)
if row == PyGameUi.blue_vibrant:
self.last_action = CrossPossibility(Row.BLUE, eyes)
if row == PyGameUi.black and eyes - 1 == self.penalties:
self.last_action = CrossPossibility(4, None)
if row == PyGameUi.dark_grey:
self.last_action = "skip"
@staticmethod
def close() -> None:
"""closes the game"""
pygame.quit()
@staticmethod
def is_mouse_over_button(x, y, w, h, circle, mouse) -> bool:
"""checks whether the cursor is pointed on a button"""
return (not circle and x < mouse[0] < x + w and y < mouse[1] < y + h) or \
(circle and x - w / 2 < mouse[0] < x + w / 2 and y - h / 2 < mouse[1] < y + h / 2)
@staticmethod
def convert_coordinates_to_eyes(row, x) -> float:
"""converts the coordinates of a button to the number on the button (or the index of the penalty)"""
if row in (PyGameUi.red_vibrant, PyGameUi.yellow_vibrant):
return (x - PyGameUi.button_x) // PyGameUi.button_x_distance + 2 # + 2 because eyes in index from 0 -11 -> 2 - 13
elif row in (PyGameUi.green_vibrant, PyGameUi.blue_vibrant):
return 12 - ((x - PyGameUi.button_x) // PyGameUi.button_x_distance) # eyes originally in index from 0 -11 -> 12 - 1
else: # penalties
return (x - PyGameUi.penalty_box_x - PyGameUi.penalty_button_x_offset) // (
PyGameUi.penalty_button_length + PyGameUi.penalty_text_y_offset)
@staticmethod
def convert_eyes_to_coordinates(row, eyes, circle) -> tuple: # todo is tuple the right type?
"""converts the number on a button (or the index of a penalty) to the coordinates of the button"""
assert (row in range(6))
if circle:
return PyGameUi.circle_x, PyGameUi.circle_y * (row + 1) + PyGameUi.circle_radius * row
if row < 2:
return (PyGameUi.button_x + PyGameUi.button_x_distance * eyes,
PyGameUi.button_y * (row + 1) + (PyGameUi.box_y_distance - PyGameUi.button_y) * row) # x, y
if row < 4:
return (PyGameUi.button_x + PyGameUi.button_x_distance * (10 - eyes)), (PyGameUi.button_y * (row + 1) + (
PyGameUi.box_y_distance - PyGameUi.button_y) * row) # todo why 10 and not 12?
if row == 4:
return (PyGameUi.penalty_box_x + PyGameUi.penalty_button_x_offset + (
PyGameUi.penalty_button_length + PyGameUi.penalty_text_y_offset) * eyes,
PyGameUi.penalty_box_y + PyGameUi.penalty_text_y_offset)
return PyGameUi.skip_button_x, PyGameUi.penalty_box_y
@staticmethod
def convert_color_to_row(color) -> int:
"""converts the color of a button to a row number"""
if color == PyGameUi.red_vibrant:
return 0
if color == PyGameUi.yellow_vibrant:
return 1
if color == PyGameUi.green_vibrant:
return 2
if color == PyGameUi.blue_vibrant:
return 3
if color == PyGameUi.black:
return 4
if color == PyGameUi.dark_grey:
return 5
@staticmethod
def convert_number_to_color(number, is_dice=False) -> tuple:
"""converts numbers of dice or rows to one or a tuple of colors"""
if is_dice:
if number in (0, 1):
return PyGameUi.dark_grey
if number == 2:
return PyGameUi.red
if number == 3:
return PyGameUi.yellow_vibrant
if number == 4:
return PyGameUi.green_vibrant
if number == 5:
return PyGameUi.blue_vibrant
else: # inactive, background, active
if number == 0:
return PyGameUi.red, PyGameUi.light_red, PyGameUi.red_vibrant
if number == 1:
return PyGameUi.yellow, PyGameUi.light_yellow, PyGameUi.yellow_vibrant
if number == 2:
return PyGameUi.green, PyGameUi.light_green, PyGameUi.green_vibrant
if number == 3:
return PyGameUi.blue, PyGameUi.light_blue, PyGameUi.blue_vibrant
if number == 4:
return PyGameUi.light_grey, PyGameUi.dark_grey, PyGameUi.black
if __name__ == "__main__":
pygame_board = PyGameUi()
pygame_board.show_board()
# test driven development (tdd -> bool)
# done: skip button, play, ranking,python tutorials, show possible turns on UI