-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
50 lines (36 loc) · 1.12 KB
/
utils.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
import pygame
import colors
from collections import namedtuple
Path = namedtuple('Path', ['path', 'length'])
def restore_path(came_from, end):
"""
:param came_from: dict which stores information about previous node for all visited nodes
:param end: target node
:return path: list of nodes which represt the path from start to end node
"""
path = [end]
current = came_from[end]
length = end.weight
while current:
path.append(current)
length += current.weight
current = came_from[current]
return Path(path[::-1], length)
def _draw_path(path, grid):
for node in path:
node.color = colors.PATH_COLOR
grid.display_grid()
pygame.time.delay(35)
pygame.display.update()
def get_clicked_node_position(grid):
x, y = pygame.mouse.get_pos()
return x // grid.node_size, y // grid.node_size
def draw_path(find_path):
def inner(grid):
if grid.found_path:
return
path = find_path(grid)
_draw_path(path.path, grid)
grid.found_path = True
grid.path_length = path.length
return inner