-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
72 lines (63 loc) · 2.28 KB
/
main.py
File metadata and controls
72 lines (63 loc) · 2.28 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
import pygame
from app import App
import random
from algorithm import Algo
pygame.init()
def generate_list(n):
"""
:param n -> int: list length
:return: list: a random list of numbers from 1 -> n
"""
return random.sample(range(1, n + 1) , n)
def main():
app = App(800, 800, "Algorithm Visualizer")
LIST_LENGTH = 80
fps = 30
lst = generate_list(LIST_LENGTH)
algo = Algo(lst, app)
exit = False
sorting = False
sorting_algo = algo.bubble_sort()
algo_name = "bubble_sort"
clock = pygame.time.Clock()
while not exit:
clock.tick(fps)
if not sorting:
app.draw_app(algo.lst, sorting, sorted_elements=algo.sorted_elements)
else:
try:
next(sorting_algo)
except StopIteration:
sorting = False
#Draw buttons
x, y = pygame.mouse.get_pos()
app.draw_btn((x, y))
#checking pressed value and change algorithm
if not sorting:
#Can only change algorithm when the list is not being sorted
for button, name in app.buttons.items():
if button.clicked((x,y)):
algo_name = name
sorting_algo = algo.choose_sort(algo_name=algo_name)
#Display the current sorting algo and their info
app.display_sort(algo_name, algo.swap, algo.comparison)
if app.reset_button.clicked((x, y)):
#Reset the sorted list(the orange blocks), generate new list
sorting = False
sorting_algo = algo.reset_list(new_list=generate_list(LIST_LENGTH),algo_name=algo_name)
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit = True
if event.type == pygame.MOUSEBUTTONDOWN:
x, y = event.pos
if app.play_button.clicked((x, y)):
if sorting:
sorting_algo = algo.choose_sort(algo_name="reset")
else:
if algo.is_sorted():
sorting_algo = algo.reset_list(new_list=generate_list(LIST_LENGTH),algo_name=algo_name)
else:
sorting = True
pygame.display.update()
if __name__ == "__main__":
main()