-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrid.py
More file actions
109 lines (87 loc) · 2.63 KB
/
grid.py
File metadata and controls
109 lines (87 loc) · 2.63 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
from cell import *
class Grid:
def __init__(self, display, size=50):
self.display = display
self.size = size
self.min_width = min(self.display.get_size())
self.cell_side = self.min_width/self.size
self.cell_list = [ [ Cell(self.display, (x, y), grid_size=self.size) for x in range(self.size) ] for y in range(self.size) ]
# self.cell_list = [ [ Cell(self.display, (c, r), grid_size=self.size) for c in range(self.size) ] for r in range(self.size) ]
self.connect_cells()
def __iter__(self):
for row in self.cell_list:
yield row
def __getitem__(self, index):
return self.cell_list[index]
def connect_cells(self):
for row in self.cell_list:
for cell in row:
cell.connect_with_neighbors(self.cell_list)
def reset(self):
self.cell_list = [ [ Cell(self.display, (x, y), grid_size=self.size) for x in range(self.size) ] for y in range(self.size) ]
self.connect_cells()
def draw(self):
for r in self.cell_list:
for cell in r:
cell.draw()
def main():
pygame.init()
DISPLAY = pygame.display.set_mode((600, 600))
pygame.display.set_caption('Grid Test')
grid = Grid(DISPLAY, 15)
checking = True
choosing_start = False
choosing_end = False
start_cell = None
end_cell = None
def check_cells(cell_list):
global choosing_end, choosing_start, start_cell, end_cell
while checking:
for row in cell_list:
for cell in row:
if cell.is_clicked():
if choosing_start:
if start_cell != None:
start_cell.role = None
start_cell = cell
cell.role = 1
start_cell = cell
# print(f"Cell at {cell.position} is the start")
choosing_start = False
elif choosing_end:
if end_cell != None:
end_cell.role = None
# print(f"Cell at {cell.position} is the end")
cell.role = 0
choosing_end = False
end_cell = cell
# for wall in cell.walls:
# wall.hide()
# if cell.is_clicked(2):
# for wall in cell.walls:
# wall.show()
clicking_thread = Thread(target=lambda x=None: check_cells(grid.cell_list))
clicking_thread.start()
def update_display():
DISPLAY.fill((255, 255, 255))
grid.draw()
pygame.display.update()
running = True
while running:
# choosing_start = False
# choosing_end = False
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
checking = not checking
elif event.key == pygame.K_s:
choosing_start = True
elif event.key == pygame.K_e:
choosing_end = True
update_display()
checking = False
pygame.quit()
if __name__ == '__main__':
main()