-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
90 lines (62 loc) · 2.72 KB
/
Copy pathmain.py
File metadata and controls
90 lines (62 loc) · 2.72 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
80
81
82
83
84
85
86
87
88
89
90
import pygame
from utils import draw, generate_starting_list
from algorithms import bubble_sort, insertion_sort, selection_sort
pygame.init()
# Import after to initialize font
from draw_information import DrawInformation
def main():
run = True # Variable for while loop
clock = pygame.time.Clock() # regulates how quickly game will run
# Variables to avoid hard coding multiple times
n = 25
min_val= 0
max_val = 100
lst = generate_starting_list(n, min_val, max_val)
draw_info = DrawInformation(800, 600, lst)
draw_info.set_list(lst)
sorting = False
ascending = True
algorithm = bubble_sort
algorithm_name = "Bubble Sort"
algorithm_generator = None
# Pygame event loop. Without a loop, game will end automatically
while run:
clock.tick(30) # FPS
if sorting:
try:
next(algorithm_generator)
# When exception is thrown, we know it is done sorting
except StopIteration:
sorting = False # Accept this and move on
else:
draw(draw_info, algorithm_name, ascending)
pygame.display.update() # Updates display
# event.get() returns list of all events that have occured since last loop
for event in pygame.event.get():
if event.type == pygame.QUIT: # Manual handle of 'X' button
run = False
if event.type != pygame.KEYDOWN:
continue
if event.key == pygame.K_r:
lst = generate_starting_list(n, min_val, max_val)
draw_info.set_list(lst)
sorting = False
elif event.key == pygame.K_SPACE and sorting == False:
sorting = True
algorithm_generator = algorithm(draw_info, ascending)
elif event.key == pygame.K_a and not sorting:
ascending = True
elif event.key == pygame.K_d and not sorting:
ascending = False
elif event.key == pygame.K_i and not sorting:
algorithm = insertion_sort
algorithm_name = "Insertion Sort"
elif event.key == pygame.K_b and not sorting:
algorithm = bubble_sort
algorithm_name = "Bubble Sort"
elif event.key == pygame.K_s and not sorting:
algorithm = selection_sort
algorithm_name = "Selection Sort"
pygame.quit()
if __name__ == "__main__": # Makes sure module is running
main()