-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
122 lines (101 loc) · 3.35 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
import pygame, sys, pygame_widgets
from pygame.locals import QUIT
#Import classes and functions
from ArrayBars import ArrayBars
from pygame_widgets.slider import Slider
from pygame_widgets.textbox import TextBox
from pygame_widgets.button import Button
from pygame_widgets.dropdown import Dropdown
from display_text import display_text
#Global variables
WIN_HEIGHT = 400
WIN_WIDTH = 500
FPS = 40
BG_COLOR = '#FFFFFF'
PRIMARY_TEXT_COLOR = '#D10000'
WIN_SIZE = (WIN_WIDTH, WIN_HEIGHT)
#Pygame initializers
pygame.init()
win = pygame.display.set_mode(WIN_SIZE)
pygame.display.set_caption('Algorithm Visualiser')
clock = pygame.time.Clock()
#Function to handle window updates
def update_window(events, ArrayBars):
win.fill(BG_COLOR)
apply_settings()
arrayBars.draw(win)
#Text displays
display_text(win, 90, 100, 'Array size:', PRIMARY_TEXT_COLOR, 18)
display_text(win, 250, 50, 'SORTING VISUALIZER', '#000000', 40)
display_text(win, 75, 160, 'Passes: ' + str(ArrayBars.num_passes),
'#000000', 15)
display_text(win, 75, 180, 'Comparisons: ' + str(ArrayBars.num_comps),
'#000000', 15)
display_text(win, 75, 200, 'Swaps: ' + str(ArrayBars.num_swaps),
'#000000', 15)
pygame_widgets.update(events)
pygame.display.update()
size_slider_display.setText(size_slider.getValue())
#Function to handle windwo updates done by ArrayBars class
def update_window_array_bars():
global arrayBars
update_window(pygame.event.get(), arrayBars)
#Function to update settings
def apply_settings():
arrayBars.set_size(size_slider.getValue())
arrayBars.algorithm = dropdown.getSelected()
#Initializing classes
#Slider and number display for speed and array size
size_slider = Slider(win, 50, 120, 150, 10, min=90, max=450, step=75)
size_slider_display = TextBox(win,
180,
90,
30,
20,
fontSize=15,
borderThickness=1,
radius=2,
colour=BG_COLOR)
size_slider_display.disable()
#Button display for shuffle and sort
shuffle_button = Button(win,
363,
100,
50,
30,
text='Shuffle',
radius=2,
onClick=lambda: arrayBars.shuffle())
shuffle_button = Button(win,
420,
100,
50,
30,
text='Sort',
radius=2,
onClick=lambda: arrayBars.sort())
#Dropdown display
dropdown = Dropdown(
win,
240,
100,
115,
30,
name='Select Algorithm',
choices=['Bubble Sort', 'Insertion Sort', 'Selection Sort'],
inactiveColour=PRIMARY_TEXT_COLOR,
textColour=BG_COLOR,
borderRadius=4)
arrayBars = ArrayBars(size_slider.getValue(), PRIMARY_TEXT_COLOR,
update_window_array_bars)
#Game loop
run = True
while run:
events = pygame.event.get()
for event in events:
if event.type == QUIT:
run = False
pygame.quit()
sys.exit()
update_window(events, arrayBars)
clock.tick(30)