-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwindow.py
More file actions
executable file
·145 lines (121 loc) · 5.79 KB
/
window.py
File metadata and controls
executable file
·145 lines (121 loc) · 5.79 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
import pygame
import time
import config
from visualizer.grid_visualizer import GridVisualizer
from algorithms import astar, dijkstra, bfs, dfs, greedy_bfs, bidirectional_bfs, randomized_dfs, rrt
from menu import Menu
# Dictionary: name -> algorithm class
ALGORITHMS = {
'A*': astar.AStar,
'Dijkstra': dijkstra.Dijkstra,
'BFS': bfs.BFS,
'DFS': dfs.DFS,
'Greedy BFS': greedy_bfs.GreedyBestFirst,
'Bidir. BFS': bidirectional_bfs.BidirectionalBFS,
'Rand. DFS': randomized_dfs.RandomizedDFS,
'RRT': rrt.RRT
}
def start_game(width, height, num_rows, num_cols):
execution_time = None
pygame.init() # must initialize pygame first
font = pygame.font.SysFont(None, 24)
window = pygame.display.set_mode((width, height))
pygame.display.set_caption("Pathfinding Visualizer")
left_mouse_held = False
right_mouse_held = False
# Initialize visualizer and menu
visualizer = GridVisualizer(window, num_rows, num_cols, width, height)
menu = Menu(list(ALGORITHMS.keys()), width, config.MENU_HEIGHT)
pathfinder_gen = None
running = True
while running:
window.fill((255, 255, 255))
visualizer.draw_grid()
menu.draw(window)
# Draw execution time at bottom right
if execution_time is not None:
text = f"Execution time: {execution_time:.3f} s"
text_surf = font.render(text, True, (0, 0, 0))
text_rect = text_surf.get_rect()
padding = 6
x = width - text_rect.width - padding
y = height - text_rect.height - padding
bg_rect = pygame.Rect(x - padding, y - padding, text_rect.width + 2 * padding,
text_rect.height + 2 * padding)
pygame.draw.rect(window, (255, 255, 255), bg_rect) # opaque white background
pygame.draw.rect(window, (0, 0, 0), bg_rect, 1) # black border
window.blit(text_surf, (x, y))
pygame.display.flip()
# Run algorithm generator
if pathfinder_gen:
try:
next(pathfinder_gen)
except StopIteration:
pathfinder_gen = None
if 'start_time' in locals():
execution_time = time.time() - start_time
del start_time
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# ---------------- MOUSE DOWN ----------------
elif event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1: # left click
# Menu click
if event.pos[1] <= config.MENU_HEIGHT:
changed = menu.handle_click(event.pos)
if changed:
visualizer.reset_path()
pathfinder_gen = None
execution_time = None
continue
left_mouse_held = True
row, col = visualizer.get_cell(event.pos)
if not visualizer.in_bounds(row, col):
continue
# Set start / goal / wall
if not visualizer.any_start_defined():
visualizer.set_start_position(row, col)
elif not visualizer.any_goal_defined() and visualizer.grid[row][col].cell_type != config.START:
visualizer.set_goal_position(row, col)
elif visualizer.grid[row][col].cell_type not in (config.START, config.GOAL):
visualizer.set_wall_position(row, col)
elif event.button == 3: # right click
right_mouse_held = True
row, col = visualizer.get_cell(event.pos)
if visualizer.in_bounds(row, col):
visualizer.clear_position(row, col)
# ---------------- MOUSE UP ----------------
elif event.type == pygame.MOUSEBUTTONUP:
if event.button == 1:
left_mouse_held = False
elif event.button == 3:
right_mouse_held = False
# ---------------- MOUSE MOTION ----------------
elif event.type == pygame.MOUSEMOTION:
row, col = visualizer.get_cell(event.pos)
if not visualizer.in_bounds(row, col):
continue
if left_mouse_held:
if visualizer.any_start_defined() and visualizer.any_goal_defined():
cell_type = visualizer.grid[row][col].cell_type
if cell_type not in (config.START, config.GOAL):
visualizer.set_wall_position(row, col)
elif right_mouse_held:
visualizer.clear_position(row, col)
# ---------------- KEYDOWN ----------------
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
start_pos = visualizer.get_start_position()
goal_pos = visualizer.get_goal_position()
if start_pos and goal_pos:
selected_name = menu.get_selected()
AlgClass = ALGORITHMS[selected_name]
# pass GridCell objects
pathfinder_gen = AlgClass(visualizer.get_grid(), start_pos, goal_pos, delay=0.01).run()
start_time = time.time()
elif event.key == pygame.K_r:
visualizer.reset_grid()
pathfinder_gen = None
execution_time = None
pygame.quit()