-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.py
More file actions
84 lines (63 loc) · 2.24 KB
/
node.py
File metadata and controls
84 lines (63 loc) · 2.24 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
import pygame
from constants import white, red, green, gray, purple, blue1, blue2, square_size, width
class Node:
def __init__(self, row, col):
self.row = row
self.col = col
self.x = row * square_size
self.y = col * square_size
self.color = white
self.weight = 1
self.neighbours = []
def get_position(self):
return self.row, self.col
def is_wall(self):
return self.color == gray
def is_default(self):
return self.color == white
def is_path(self):
return self.color == purple
def reset_color(self):
self.color = white
def reset_weight(self):
self.weight = 1
# User places these node objects with mouse clicks
def place_start(self):
self.color = green
def place_end(self):
self.color = red
def place_wall(self):
self.color = gray
def place_weight(self):
self.weight = 9
# These three draw functions will be called by algorithm, not by user
def draw_open(self):
self.color = blue1
def draw_visited(self):
self.color = blue2
def draw_path(self):
self.color = purple
def add_neighbours(self, grid):
self.neighbours = []
# Up
if self.row > 0:
neighbour = grid[self.row - 1][self.col]
if not neighbour.is_wall():
self.neighbours.append(neighbour)
# Right
if self.col < width // square_size - 1:
neighbour = grid[self.row][self.col + 1]
if not neighbour.is_wall():
self.neighbours.append(neighbour)
# Down
if self.row < width // square_size - 1:
neighbour = grid[self.row + 1][self.col]
if not neighbour.is_wall():
self.neighbours.append(neighbour)
# Left
if self.col > 0:
neighbour = grid[self.row][self.col - 1]
if not neighbour.is_wall():
self.neighbours.append(neighbour)
def draw(self, win):
pygame.draw.rect(win, self.color, (self.x, self.y, square_size, square_size))