Repository navigation
Expand file tree
/
Copy pathgui.py
More file actions
155 lines (137 loc) · 5.02 KB
/
Copy pathgui.py
File metadata and controls
155 lines (137 loc) · 5.02 KB
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
import pygame
from grid import Grid
# SETTING GLOBAL VARIABLES
# Setting number of frames per second
FPS = 60
# Setting color variables
COLORS = {
'RED': (255, 0, 0),
'GREEN': (0, 255, 0),
'WHITE': (255, 255, 255),
'BLACK': (0, 0, 0),
'LIGHT_GRAY': (200, 200, 200)
}
# Setting the screen size
WIDTH = 270
HEIGHT = 270
# Setting koef to increase the screen size
SCREEN_KOEF = 2
# Setting the final width/height of the screen
SCREEN_WIDTH = WIDTH*SCREEN_KOEF
SCREEN_HEIGHT = HEIGHT*SCREEN_KOEF
# Setting the size of a subgrid
SUBGRID_SIZE = (WIDTH // 3)*SCREEN_KOEF
# Setting the size of a cell
CELL_SIZE = SUBGRID_SIZE // 3
class GUI:
'''
Class which defines all actions with the display
'''
def __init__(self):
self.display = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) # Initialize a window for display
self.display.fill(COLORS['WHITE'])
self.font = pygame.font.SysFont('arial', 50)
self.clock = pygame.time.Clock() # Initialize clock
self.selected = ()
self.variants = {}
def wait(self) -> None:
'''
Wait before updating the display
:return:
'''
# Set number of frames per second
self.clock.tick(FPS)
def draw_grid(self) -> None:
'''
Draw grid on the display
:return:
'''
### Draw Minor Lines
for x in range(0, SCREEN_WIDTH, CELL_SIZE): # draw vertical lines
pygame.draw.line(self.display, COLORS['LIGHT_GRAY'], (x, 0), (x, SCREEN_HEIGHT))
for y in range(0, SCREEN_HEIGHT, CELL_SIZE): # draw horizontal lines
pygame.draw.line(self.display, COLORS['LIGHT_GRAY'], (0, y), (SCREEN_WIDTH, y))
### Draw Major Lines
for x in range(0, SCREEN_WIDTH, SUBGRID_SIZE): # draw vertical lines
pygame.draw.line(self.display, COLORS['BLACK'], (x, 0), (x, SCREEN_WIDTH))
for y in range(0, SCREEN_HEIGHT, SUBGRID_SIZE): # draw horizontal lines
pygame.draw.line(self.display, COLORS['BLACK'], (0, y), (SCREEN_HEIGHT, y))
def display_cell_value(self, cell: tuple, value: int) -> None:
'''
Display value in a cell
:param cell: Coordinates of a cell
:param value: Value to display
:return:
'''
x = cell[1] * CELL_SIZE + CELL_SIZE / 3
y = cell[0] * CELL_SIZE + CELL_SIZE / 12
if value != 0:
text = self.font.render(str(value), True, COLORS['BLACK'])
self.display.blit(text, (x, y))
elif cell in self.variants:
x = cell[1]
y = cell[0]
text = self.font.render(str(self.variants[cell]), True, COLORS['LIGHT_GRAY'])
self.display.blit(text, (x * CELL_SIZE + CELL_SIZE - CELL_SIZE / 2, y * CELL_SIZE))
else:
pygame.draw.rect(self.display, COLORS['WHITE'], (x, y, CELL_SIZE/2, CELL_SIZE/2))
def display_grid_values(self, grid: Grid) -> None:
'''
Display all grid values
:param grid: Grid object
:return:
'''
size = len(grid.grid)
for j in range(size):
for i in range(size):
cell = (i, j)
self.display_cell_value(cell, grid.get_cell_value(cell))
def update_display(self) -> None:
'''
Clear the display of all data by filling it with white color
:return:
'''
self.display.fill(COLORS['WHITE'])
def get_selected_cell(self, pos: tuple) -> tuple or None:
'''
Get a cell which is selected by mouse
:param pos: Position of a mouse
:return: Coordinates of a selected cell or None
'''
if pos[0] < SCREEN_WIDTH and pos[1] < SCREEN_HEIGHT:
x = int(pos[1] // CELL_SIZE)
y = int(pos[0] // CELL_SIZE)
cell = (x, y)
return cell
else:
return None
def select_cell(self, cell: tuple) -> None:
'''
Display that cell was selected
:param cell: Coordinates of a selected cell
:return:
'''
self.selected = cell
y, x = self.selected
pygame.draw.rect(self.display, COLORS['RED'], (x*CELL_SIZE, y*CELL_SIZE, CELL_SIZE, CELL_SIZE), 3)
def add_variant(self, value: int) -> None:
'''
Add variant to cell dictionary
:param value: Chosen user variant for selected cell
:return:
'''
self.variants[self.selected] = value
def display_message(self, text_type: str) -> None:
'''
Display text to display
:param text_type: Chosen type of text to show on the display
:return:
'''
text = ''
self.display.fill(COLORS['WHITE'])
if text_type == 'victory':
text = 'Sudoku is completed!'
elif text_type == 'bad_grid':
text = 'Board cant be solved!'
text = self.font.render(text, True, COLORS['BLACK'])
self.display.blit(text, (SCREEN_WIDTH/12, SCREEN_HEIGHT/2.5))