-
Notifications
You must be signed in to change notification settings - Fork 0
/
grid.py
62 lines (51 loc) · 1.64 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
import pygame
from cell import Cell
from cell_types import CellTypes
from colors import GREY, WHITE
class Grid:
def __init__(self, win, rows, width, matrix):
# Attributes for drawing grid on window
self.win = win
self.width = width
self.rows = rows
self.start = None
self.end = None
# Organize grid according to matrix
self.matrix = self.make_grid(matrix)
# Get matrix
def get_matrix(self):
return self.matrix
# Get grid start and end
def get_start_end(self):
return self.start, self.end
# Organize grid according to matrix
def make_grid(self, matrix):
grid = []
cell_width = self.width // self.rows
for i in range(self.rows):
grid.append([])
for j in range(self.rows):
cell = Cell(i, j, cell_width, self.rows, matrix[j][i])
if cell.cell_type == CellTypes.start:
self.start = cell
elif cell.cell_type == CellTypes.end:
self.end = cell
grid[i].append(cell)
return grid
# Draw Grid lines between cells
def draw_grid_lines(self):
gap = self.width // self.rows
# TODO: Check this
for i in range(self.rows):
pygame.draw.line(self.win, GREY, (0, i * gap), (self.width, i * gap))
for j in range(self.rows):
pygame.draw.line(self.win, GREY, (j * gap, 0), (j * gap, self.width))
# Draw grid on window based on its attributes
def draw(self, see_every_cost):
# self.win.fill(WHITE)
pygame.draw.rect(self.win, WHITE, (0, 0, self.width, self.width))
for row in self.matrix:
for cell in row:
cell.draw(self.win, see_every_cost)
self.draw_grid_lines()
pygame.display.update()