forked from LucasPilla/Sorting-Algorithms-Visualizer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
68 lines (55 loc) · 2.56 KB
/
main.py
File metadata and controls
68 lines (55 loc) · 2.56 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
import pygame
from random import randint
from time import time
from algs import algorithmsDict
import display
# Declared in display.py
# 1. global variables : numBars, delay, do_sorting, paused, timer_space_bar
# 2. widgets : sizeBox, delayBox, algorithmBox, playButton, stopButton
def main():
numbers = []
running = True
display.algorithmBox.add_options(list(algorithmsDict.keys()))
current_alg = None
alg_iterator = None
timer_delay = time()
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE and display.do_sorting:
display.paused = not display.paused
display.timer_space_bar = time()
display.updateWidgets(event)
display.delay = (display.delayBox.value-display.delayBox.rect.x-6)/1000 # delay is in ms
if display.playButton.isActive: # play button clicked
display.playButton.isActive = False
display.do_sorting = True
current_alg = display.algorithmBox.get_active_option()
display.numBars = int(display.sizeBox.text)
numbers = [randint(10, 400) for i in range(display.numBars)] # random list to be sorted
alg_iterator = algorithmsDict[current_alg](numbers, 0, display.numBars-1) # initialize iterator
if display.stopButton.isActive: # stop button clicked
display.stopButton.isActive = False
display.do_sorting = False
display.paused = False
try: # deplete generator to display sorted numbers
while True:
numbers, redBar1, redBar2, blueBar1, blueBar2 = next(alg_iterator)
except StopIteration:
pass
if display.do_sorting and not display.paused: # sorting animation
try:
if time()-timer_delay >= display.delay:
numbers, redBar1, redBar2, blueBar1, blueBar2 = next(alg_iterator)
display.drawInterface(numbers, redBar1, redBar2, blueBar1, blueBar2)
timer_delay = time()
except StopIteration:
display.do_sorting = False
elif display.do_sorting and display.paused: # animation paused
display.drawInterface(numbers, -1, -1, -1, -1)
else: # no animation
a_set = set(range(display.numBars))
display.drawInterface(numbers, -1, -1, -1, -1, greenRows=a_set)
if __name__ == '__main__':
main()