-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
167 lines (134 loc) · 4.47 KB
/
main.py
File metadata and controls
167 lines (134 loc) · 4.47 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import pygame
import sys
from settings import WIDTH, HEIGHT, ROWS, TOOLBAR_HEIGHT, WINDOW_HEIGHT, WHITE, BLACK, BUTTON_COLOR, BUTTON_HOVER, TEXT_COLOR
from grid import make_grid, draw, get_clicked_pos
from algorithm import dijkstra
pygame.init()
pygame.font.init()
WIN = pygame.display.set_mode((WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption("Dijkstra Path Finding Algorithm")
FONT = pygame.font.SysFont('arial', 20)
class Button:
def __init__(self, x, y, width, height, text, action_code):
self.rect = pygame.Rect(x, y, width, height)
self.text = text
self.action_code = action_code
self.is_hovered = False
def draw(self, win):
color = BUTTON_HOVER if self.is_hovered else BUTTON_COLOR
pygame.draw.rect(win, color, self.rect)
pygame.draw.rect(win, BLACK, self.rect, 2)
text_surf = FONT.render(self.text, True, TEXT_COLOR)
text_rect = text_surf.get_rect(center=self.rect.center)
win.blit(text_surf, text_rect)
def check_hover(self, pos):
self.is_hovered = self.rect.collidepoint(pos)
def is_clicked(self, pos):
return self.rect.collidepoint(pos)
def main(win):
GAP = 20
COLS = WIDTH // GAP
ROWS_GRID = (HEIGHT - TOOLBAR_HEIGHT) // GAP
grid = make_grid(COLS, ROWS_GRID, GAP)
start = None
end = None
btn_h = 40
btn_y = HEIGHT - btn_h - 10
btn_w = 110
gap_btn = 10
buttons = [
Button(20, btn_y, btn_w, btn_h, "Set Start", "SET_START"),
Button(20 + btn_w + gap_btn, btn_y, btn_w, btn_h, "Set End", "SET_END"),
Button(20 + 2 * (btn_w + gap_btn), btn_y, btn_w, btn_h, "Draw Walls", "DRAW_WALL"),
Button(20 + 3 * (btn_w + gap_btn), btn_y, btn_w, btn_h, "Run Dijkstra", "RUN"),
Button(20 + 4 * (btn_w + gap_btn), btn_y, btn_w, btn_h, "Reset", "RESET")
]
current_mode = "SET_START"
def draw_window():
draw(win, grid, COLS, ROWS_GRID, GAP)
panel_y = ROWS_GRID * GAP
pygame.draw.rect(win, WHITE, (0, panel_y, WIDTH, HEIGHT - panel_y))
pygame.draw.line(win, BLACK, (0, panel_y), (WIDTH, panel_y), 2)
mouse_pos = pygame.mouse.get_pos()
for btn in buttons:
btn.check_hover(mouse_pos)
btn.draw(win)
status_text = FONT.render(f"Mode: {current_mode}", True, BLACK)
win.blit(status_text, (WIDTH - 200, btn_y + 10))
pygame.display.update()
run = True
while run:
draw_window()
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if pygame.mouse.get_pressed()[0]:
pos = pygame.mouse.get_pos()
if pos[1] < ROWS_GRID * GAP and pos[0] < COLS * GAP:
row, col = get_clicked_pos(pos, GAP)
node = grid[row][col]
if current_mode == "SET_START":
if not start and node != end:
start = node
start.make_start()
current_mode = "SET_END"
elif start and node != start and node != end:
start.reset()
start = node
start.make_start()
elif current_mode == "SET_END":
if not end and node != start:
end = node
end.make_end()
current_mode = "DRAW_WALL"
elif end and node != end and node != start:
end.reset()
end = node
end.make_end()
elif current_mode == "DRAW_WALL":
if node != start and node != end:
node.make_barrier()
else:
for btn in buttons:
if btn.is_clicked(pos):
if btn.action_code == "SET_START":
current_mode = "SET_START"
elif btn.action_code == "SET_END":
current_mode = "SET_END"
elif btn.action_code == "DRAW_WALL":
current_mode = "DRAW_WALL"
elif btn.action_code == "RUN":
if start and end:
for row in grid:
for node in row:
node.update_neighbors(grid)
dijkstra(draw_window, grid, start, end)
elif btn.action_code == "RESET":
start = None
end = None
grid = make_grid(COLS, ROWS_GRID, GAP)
current_mode = "SET_START"
elif pygame.mouse.get_pressed()[2]:
pos = pygame.mouse.get_pos()
if pos[1] < ROWS_GRID * GAP and pos[0] < COLS * GAP:
row, col = get_clicked_pos(pos, GAP)
node = grid[row][col]
node.reset()
if node == start:
start = None
elif node == end:
end = None
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE and start and end:
for row in grid:
for node in row:
node.update_neighbors(grid)
dijkstra(draw_window, grid, start, end)
if event.key == pygame.K_c:
start = None
end = None
grid = make_grid(COLS, ROWS_GRID, GAP)
pygame.quit()
sys.exit()
if __name__ == "__main__":
main(WIN)