-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
163 lines (127 loc) · 6.05 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
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
###> Imports
import pygame, sys, random
from pygame.constants import QUIT,KEYDOWN,K_UP, K_RIGHT, K_DOWN, K_LEFT, SHOWN, USEREVENT
from pygame import init, quit, Vector2
from Graph import Graph
from buttons import Button
from Panel import Panel
###> Setting up pygame
init()
pygame.mixer.pre_init(44100, -16, 2, 512) #! handles a lot of things. Used here to remove delay before sound plays
clock = pygame.time.Clock()
w = int(input("Please enter the horizontal resolution of your screen: "))
h = int(input("Please enter the vertical resolution of your screen: "))
print(f"The program should have started with a window size of {w} x {h}")
print("If the program window is not fully visible then please enter a larger resolution, else change the resolution of your screen")
w = w - 400
h = h - 60
hor, ver, panelHor = w, h, 375
grid_size = 25
pygame.display.set_caption("Graph Search Visualizer")
pygame.display.set_icon(pygame.image.load("icon.png"))
canvas = pygame.Surface((hor, ver))
# panelSurf = pygame.Surface((panelHor - grid_size, ver - grid_size*2))
panel = Panel(hor + grid_size // 2, grid_size // 2, panelHor - grid_size, ver - grid_size , (164, 250, 107))
screen = pygame.display.set_mode((hor + panelHor, ver))
#> Colors
panelColor = (164, 250, 107) #(136, 248, 71)
#> Buttons
g = Graph(canvas, screen, 25, False)
Button.surfaceX = hor + grid_size // 2
Button.surfaceY = grid_size // 2
def draw_grid(width, length, size):
grid_surf = pygame.Surface((width, length))
grid_surf.set_colorkey((0, 0, 0))
for hor in range(length // size):
pygame.draw.line(grid_surf, (160, 160, 160), (0, size * hor), (width, size * hor))
for ver in range(width // size):
pygame.draw.line(grid_surf, (160, 160, 160), (size * ver, 0), (size * ver, length))
return grid_surf
#> Main Loop
#>
loop = True
speed = 250
click = pygame.mixer.Sound("mouse_click.wav")
click.set_volume(0.1)
while(loop):
mouse = pygame.mouse.get_pos()
#$ Event Loop
for event in pygame.event.get():
#$ QUIT event
if (event.type == QUIT):
loop = False
if(canvas.get_rect().collidepoint(mouse)):
if(event.type == pygame.MOUSEBUTTONDOWN):
if(event.button == 1):
if not(g.addWeight(mouse)):
g.addNode(mouse)
g.addEdge(mouse)
elif (event.button == 3):
g.removeNode(mouse)
elif (panel.mouseOnPanel(mouse)):
if(event.type == pygame.MOUSEBUTTONDOWN):
if(event.button == 1):
click.play()
if(g.isEmpty() and panel.directed_btn.detect_click()):
g.directed = panel.directed_btn.detect_toggle()
if(panel.clear_btn.detect_click()):
g.reset()
elif(panel.bfs_btn.detect_click()):
panel.bfs_btn.detect_toggle()
g.runAlgorithm(panel, draw_grid(hor, ver, grid_size), "BFS", speed)
panel.bfs_btn.toggled = False
elif(panel.ucs_btn.detect_click()):
panel.ucs_btn.detect_toggle()
g.runAlgorithm(panel, draw_grid(hor,ver, grid_size), "UCS", speed)
panel.ucs_btn.toggled = False
elif(panel.dfs_btn.detect_click()):
panel.dfs_btn.detect_toggle()
g.runAlgorithm(panel, draw_grid(hor,ver, grid_size), "DFS", speed)
panel.dfs_btn.toggled = False
elif(panel.ID_btn.detect_click()):
panel.ID_btn.detect_toggle()
max_depth = (g.input_depth_limit(mouse))
g.runAlgorithm(panel, draw_grid(hor,ver, grid_size), "ITD", speed, max_depth)
panel.ID_btn.toggled = False
elif(panel.DLS_btn.detect_click()):
panel.DLS_btn.detect_toggle()
max_depth = (g.input_depth_limit(mouse))
g.runAlgorithm(panel, draw_grid(hor,ver, grid_size), "DLS", speed, max_depth)
panel.DLS_btn.toggled = False
elif(panel.greedy_btn.detect_click()):
panel.greedy_btn.detect_toggle()
g.runAlgorithm(panel, draw_grid(hor,ver, grid_size), "GRY", speed)
panel.greedy_btn.toggled = False
elif(panel.aStar_btn.detect_click()):
panel.aStar_btn.detect_toggle()
g.runAlgorithm(panel, draw_grid(hor,ver, grid_size), "AST", speed)
panel.aStar_btn.toggled = False
elif (panel.speed_btn.detect_click()):
speed = panel.speed_control()
if (panel.showH_btn.detect_click()):
g.showHeuristic = panel.showH_btn.detect_toggle()
if (panel.showC_btn.detect_click()):
g.showCost = panel.showC_btn.detect_toggle()
canvas.fill( (255, 255, 255))
canvas.blit(draw_grid(hor, ver, grid_size), (0,0))
panel.fill()
screen.fill(panelColor)
pygame.draw.rect(screen, (0,0,0), (hor, 0, panelHor, ver), 10)
panel.displayMessage("Draw a Graph, then select an algorithm to run on the graph. nl > ADD Node [Left CLick]. nl > Remove Node [Right Click]. nl > Select Node [Left Click]")
g.draw_edges()
g.draw_nodes(mouse)
# g.input_depth_limit(mouse)
# box = pygame.Rect(500, 300, 50, 50)#prompt_rect.width + 20, prompt_rect.height + 30)
# pygame.draw.rect(canvas, (0,0,0), box)
panel.draw_btns()
panel.btnDetect_hover(mouse)
#$ Update Display
screen.blit(canvas, (0,0))
screen.blit(panel.panel_surf, (panel.cordX, panel.cordY))
pygame.display.update()
clock.tick(60)
#> One loop only (Debugging)
# pygame.time.delay(3000)
# loop = False
quit()
sys.exit()