-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
91 lines (65 loc) · 2.41 KB
/
main.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
85
86
87
88
89
90
91
import pygame
import colors
from grid import Grid
from path_finding_algorithms import bfs, dfs, dijkstra, a_star
from utils import get_clicked_node_position
WIDTH = 1200
HEIGHT = 600
NODE_SIZE = 25
WINDOW = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Path Visualizer")
FONT = 'Calibri'
def handle_mouse_events(grid_instance):
# Handle left mouse button click
if pygame.mouse.get_pressed()[0]:
col, row = get_clicked_node_position(grid_instance)
node = grid_instance.grid[row][col]
node.change_color(colors.WALL_COLOR)
# Handle right mouse button click
if pygame.mouse.get_pressed()[2]:
col, row = get_clicked_node_position(grid_instance)
node = grid_instance.grid[row][col]
node.change_color(colors.DEFAULT_COLOR)
def main():
pygame.init()
font = pygame.font.SysFont(FONT, NODE_SIZE // 2, bold=True)
grid_instance = Grid(WIDTH, HEIGHT, NODE_SIZE, WINDOW, font)
# Mainloop of the program
run = True
while run:
WINDOW.fill(colors.WHITE)
grid_instance.display_grid()
handle_mouse_events(grid_instance)
# Handle keyboard events
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
# Clear the screen
if event.key == pygame.K_c:
grid_instance.setup()
# Refresh the grid
if event.key == pygame.K_r:
grid_instance.refresh_grid()
# Assign nodes random weight
if event.key == pygame.K_w:
grid_instance.assign_weights_to_nodes()
# Toggle show weights
if event.key == pygame.K_t:
grid_instance.toggle_show_weights()
# BFS visualization
if event.key == pygame.K_1:
bfs(grid_instance)
# DFS visualization
if event.key == pygame.K_2:
dfs(grid_instance)
# Dijkstra visualization
if event.key == pygame.K_3:
dijkstra(grid_instance)
# A* visualization
if event.key == pygame.K_4:
a_star(grid_instance)
pygame.display.update()
pygame.quit()
if __name__ == '__main__':
main()