-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
79 lines (65 loc) · 2.42 KB
/
Copy pathmain.py
File metadata and controls
79 lines (65 loc) · 2.42 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
import pygame
pygame.init()
import sortingUtils
from sortingUtils import colorDataSet, isSorted
from sortingManager import SortingManager
WIDTH, HEIGHT = 1280, 700
SCREEN = pygame.display.set_mode((WIDTH, HEIGHT))
def showDataToScreen(array):
scaleFactor = (HEIGHT - 50) / max(array)
SCREEN.fill("black")
gap = WIDTH / len(array)
for x in range(len(array)):
if x in sortingUtils.comparedIndices:
color = "red"
elif x in sortingUtils.selectedIndices:
color = "green"
else:
color = "white"
#array[x] * scaleFactor
rect = (x * gap, HEIGHT - (array[x] * scaleFactor), 1, array[x] * scaleFactor)
pygame.draw.rect(SCREEN, color, rect)
def displayStats():
font = pygame.font.SysFont('Times New Roman', 20)
stats_text = font.render(f"Algorithm: {sortingUtils.algorithmName} | Swaps: {sortingUtils.swaps} | Comparisons: {sortingUtils.comparisons} | Visual Delay: {sortingUtils.delay}ms | Visual Time: {sortingUtils.sortTimeVisual: .2f}s | Sort Time: {sortingUtils.sortTime * 1000: .5f} ms", True, (255, 255, 255))
SCREEN.blit(stats_text, (10, 10))
def updateSorting():
if sortingUtils.sorting:
try:
if sortingUtils.colorAnim is None:
next(sortingManager.generatorFunc)
else:
next(sortingUtils.colorAnim)
except StopIteration:
if sortingUtils.colorAnim is None:
sortingUtils.colorAnim = colorDataSet(sortingUtils.data, len(sortingUtils.data))
else:
sortingUtils.colorAnim = None
sortingUtils.sorting = False
def handleEvents():
for event in pygame.event.get():
if event.type == pygame.QUIT:
return False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
if isSorted(sortingUtils.data):
print("already sorted")
else:
sortingUtils.sorting = not sortingUtils.sorting
if event.key == pygame.K_s:
sortingManager.switchSort()
return True
def render():
SCREEN.fill("black")
showDataToScreen(sortingUtils.data)
displayStats()
sortingManager = SortingManager()
def main():
running = True
while running:
running = handleEvents()
updateSorting()
render()
pygame.display.flip()
if __name__ == "__main__":
main()