-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode.py
84 lines (65 loc) · 2.43 KB
/
node.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
import pygame
import colors
import random
class Node:
def __init__(self, x, y, node_size):
"""
:param x: column of the node in a Grid
:param y: row of the node in a Grid
:param node_size: cell size, which should be displayed on the screen
"""
self.x = x
self.y = y
self.size = node_size
self.color = colors.DEFAULT_COLOR
self.distance = float("inf")
self._neighbors = None
self.weight = 1
@property
def neighbors(self):
return self._neighbors
def _display_visited_node(self, surface):
""" Display node with custom border """
pygame.draw.rect(surface, self.color,
(self.x * self.size, self.y * self.size, self.size, self.size))
pygame.draw.rect(surface, colors.NODE_BORDER_COLOR,
(self.x * self.size, self.y * self.size, self.size, self.size), 1)
def display_node(self, surface):
if self.color == colors.PROCESSED_NODE_COLOR:
self._display_visited_node(surface)
return
span = 0
if self.color == colors.DEFAULT_COLOR:
span = 1
pygame.draw.rect(surface, self.color,
(self.x * self.size, self.y * self.size, self.size, self.size), span)
def change_color(self, color):
""" Change node's color if it is not start or end node """
if self.color not in (colors.START_NODE_COLOR, colors.END_NODE_COLOR):
self.color = color
def get_neighbors(self, grid):
"""
Updates node's neighbors
:param grid: instance of the class grid
"""
rows = grid.rows
cols = grid.cols
ways = [(0, 1), (1, 0), (-1, 0), (0, -1)]
neighbors = []
for dx, dy in ways:
if 0 <= self.x + dx < cols and 0 <= self.y + dy < rows:
neighbors.append(grid.grid[self.y + dy][self.x + dx])
self._neighbors = neighbors
def assign_weight(self, use_default=False):
"""
If use_default flag is True assigns default node weight 1
otherwise assign random weight from 1 to 20
Weight of the node represents the cost of transition from any neighbor node
to this node.
"""
if not use_default:
self.weight = random.randint(1, 20)
else:
self.weight = 1
def __lt__(self, other):
return False