-
Notifications
You must be signed in to change notification settings - Fork 0
/
grid.py
156 lines (101 loc) · 4.93 KB
/
grid.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
import pygame
from cell import Cell
GREY = (130, 127, 125)
class Grid:
def __init__(self, win, grid_size, grid_dimensions, grid_position):
self.win = win
self.grid_size = grid_size
self.total_rows, self.total_columns = grid_size
self.width, self.height = grid_dimensions
self.gap = self.width // self.total_columns
self.line_color = GREY
self.grid_position = grid_position
self.x, self.y = self.grid_position
self.raw_grid = self.init_cells()
def init_cells(self):
"""Populates grid with cell objects"""
raw_grid = []
for i in range(self.total_rows):
raw_grid.append([])
for j in range(self.total_columns):
raw_grid[i].append(Cell(i, j, self.gap, self.grid_size, self.grid_position))
return raw_grid
def draw_grid_lines(self) -> None:
"""Draws horizontal and vertical grid lines"""
for i in range(self.total_rows + 1):
pygame.draw.line(self.win, self.line_color, (self.x, self.y + i * self.gap), (self.x + self.width, self.y + i * self.gap))
for j in range(self.total_columns + 1):
pygame.draw.line(self.win, self.line_color, (self.x + j * self.gap, self.y), (self.x + j * self.gap, self.y + self.height))
def update_neighbors_for_every_cell(self) -> None:
for row in self.raw_grid:
for cell in row:
cell.update_neighbors(self.raw_grid)
def update_neighbors_by_direction_for_every_cell(self) -> None:
for row in self.raw_grid:
for cell in row:
cell.update_neighbors_by_direction(self.raw_grid)
def draw_under_grid_lines(self) -> None:
"""Method meant to be called before drawing grid lines
so cells would look divided by the lines"""
for row in self.raw_grid:
for cell in row:
if not cell.is_wall():
cell.draw(self.win)
def draw_over_grid_lines(self) -> None:
"""Method meant to be called after drawing grid lines
so cells would look like they are connected"""
for row in self.raw_grid:
for cell in row:
if cell.is_wall():
cell.draw(self.win)
def get_cell(self, row: int, col: int) -> Cell:
"""Returns cell object based on row and column"""
return self.raw_grid[row][col]
def get_rc_of_under_mouse_cell(self, mpos) -> tuple:
"""Returns row and column of under the mouse cell based on mouse position"""
x, y = mpos
row = (y - self.y) // self.gap
col = (x - self.x) // self.gap
return int(row), int(col)
def clear(self, start_end_except = False, barrier_except = False) -> None:
"""Clears the grid with an optional cell type exception"""
if start_end_except == True and barrier_except == True:
for row in self.raw_grid:
for cell in row:
if not cell.is_start() and not cell.is_end() and not cell.is_parcel() and not cell.is_wall():
cell.reset()
elif start_end_except == True:
for row in self.raw_grid:
for cell in row:
if not cell.is_start() and not cell.is_end() and not cell.is_parcel():
cell.reset()
else:
for row in self.raw_grid:
for cell in row:
cell.reset()
def draw_grid_frame(self) -> None:
pygame.draw.line(self.win, GREY, ((self.x - 3, self.y)), (self.x - 3, self.y + self.height), width=5)
pygame.draw.line(self.win, GREY, ((self.x + self.width + 3, self.y)), (self.x + self.width + 3, self.y + self.height), width=5)
pygame.draw.line(self.win, GREY, ((0,self.y - 2)), (self.win.get_width(), self.y - 2), width=5)
pygame.draw.line(self.win, GREY, ((0, self.y + self.height + 2)), (self.win.get_width(), self.y + self.height + 2), width=5)
def make_all_cells_wall(self, start_end_except=False) -> None:
"""Recoloring all cells with wall color"""
if start_end_except:
for row in self.raw_grid:
for cell in row:
if not cell.is_start() and not cell.is_end() and not cell.is_parcel():
cell.make_wall()
else:
for row in self.raw_grid:
for cell in row:
cell.make_wall()
def mouse_on_the_grid(self) -> bool:
"""Checks if mouse on the grid"""
mpos = pygame.mouse.get_pos()
if (mpos[0] > self.x and mpos[0] < (self.x + self.width)
and mpos[1] > self.y and mpos[1] < (self.y + self.height)):
return True
else:
return False
def __getitem__(self, row):
return self.raw_grid[row]