-
Notifications
You must be signed in to change notification settings - Fork 0
/
tools.py
119 lines (86 loc) · 2.38 KB
/
tools.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
import pygame
from colors import *
HEIGHT = 650
WIDTH = 900
class Vertex:
def __init__(self, row, col, width, total_rows):
self.row = row
self.col = col
self.color = GRID
self.width = width
self.neighbors = []
self.x = row * width
self.y = col * width
self.total_rows = total_rows
def get_current_position(self):
return self.row, self.col
def make_start(self):
self.color = CYAN
def make_closed(self):
self.color = WHITE
def make_open(self):
self.color = GRAY
def make_obstacle(self):
self.color = RED
def make_end(self):
self.color = MAGENTA
def make_shortest_path(self):
self.color = LIME
def is_start(self):
return self.color == CYAN
def is_closed(self):
return self.color == WHITE
def is_open(self):
return self.color == GRAY
def is_obstacle(self):
return self.color == RED
def is_end(self):
return self.color == MAGENTA
def reset_grid(self):
self.color = GRID
def draw(self, win):
pygame.draw.rect(win, self.color, (self.x, self.y, self.width, self.width))
def update_neighbor_nodes(self, grid):
self.neighbors = []
# DOWN
if self.row < self.total_rows - 1 and not grid[self.row + 1][self.col].is_obstacle():
self.neighbors.append(grid[self.row + 1][self.col])
# UP
if self.row > 0 and not grid[self.row - 1][self.col].is_obstacle():
self.neighbors.append(grid[self.row - 1][self.col])
# RIGHT
if self.col < self.total_rows - 1 and not grid[self.row][self.col + 1].is_obstacle():
self.neighbors.append(grid[self.row][self.col + 1])
# LEFT
if self.col > 0 and not grid[self.row][self.col - 1].is_obstacle():
self.neighbors.append(grid[self.row][self.col - 1])
def __lt__(self, other):
return False
def make_grid(rows, width):
grid = []
gap = width // rows
for i in range(rows):
grid.append([])
for j in range(rows):
vertex = Vertex(i, j, gap, rows)
grid[i].append(vertex)
return grid
def draw_grid(win, rows, width):
gap = width // rows
for i in range(rows):
pygame.draw.line(win, DARK_GRAY, (0, i * gap), (width, i * gap))
for j in range(rows):
pygame.draw.line(win, DARK_GRAY, (j * gap, 0), (j * gap, width))
def get_clicked_pos(pos, rows, width):
gap = width // rows
y, x = pos
row = y // gap
col = x // gap
return row, col
def draw(win, grid, rows, width):
win.fill(GRID)
for row in grid:
for node in row:
node.draw(win)
draw_grid(win, rows, width)
pygame.display.flip()