Skip to content

Commit d42bd2e

Browse files
better UI
1 parent 97f1261 commit d42bd2e

File tree

1 file changed

+32
-24
lines changed

1 file changed

+32
-24
lines changed

‎main.py‎

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,22 @@
55

66

77
class Screen:
8-
def __init__(self, size, n):
9-
width, height = size // 2 * 3, size
10-
print(width, height)
11-
self.width = width
12-
self.height = height
13-
self.top_pad = height / 4
14-
self.font_size = height // 20
15-
self.n = n
16-
self.window = pygame.display.set_mode((width, height))
8+
def __init__(self, size):
9+
self.bars = 50
10+
self.tick = 80
11+
self.width = size // 2 * 3
12+
self.height = size
13+
self.top_pad = self.height / 4
14+
self.font_size = self.height // 20
15+
self.window = pygame.display.set_mode((self.width, self.height))
1716
pygame.display.set_caption("Sorting Algorithm Visualizer")
1817
self.setAscending()
1918
self.setAlgo()
2019
self.genList()
2120
self.update()
2221

2322
def genList(self):
24-
self.list = [random.randint(1, 100) for _ in range(self.n)]
23+
self.list = [random.randint(1, 100) for _ in range(self.bars)]
2524
self.len_list = len(self.list)
2625
self.sorted_list = sorted(self.list)
2726
self.reverse_list = sorted(self.list, reverse=True)
@@ -32,27 +31,39 @@ def genList(self):
3231
def update(self):
3332
self.gen = self.algo()
3433
self.window.fill((0, 0, 0))
35-
# TODO use buttons
36-
controls = [
34+
titles1 = [
35+
f"ARROWS - {self.bars} Bars {self.tick} Tick",
3736
"SPACE - Play/Pause",
3837
"R - Reset",
38+
]
39+
for i, title in enumerate(titles1):
40+
self.window.blit(
41+
pygame.font.Font(None, self.font_size).render(
42+
title,
43+
1,
44+
(255, 255, 255),
45+
),
46+
(10, 10 + self.font_size * i),
47+
)
48+
49+
titles2 = [
3950
"A - Ascending",
4051
"D - Descending",
4152
]
42-
for i, control in enumerate(controls):
53+
for i, title in enumerate(titles2):
4354
color = 255, 255, 255
44-
if control[0] == "A" and self.ascending:
55+
if title[0] == "A" and self.ascending:
4556
color = 0, 255, 255
46-
if control[0] == "D" and not self.ascending:
57+
if title[0] == "D" and not self.ascending:
4758
color = 0, 255, 255
4859

4960
self.window.blit(
5061
pygame.font.Font(None, self.font_size).render(
51-
control,
62+
title,
5263
1,
5364
color,
5465
),
55-
(10, 10 + self.font_size * i),
66+
(self.width // 2, 10 + self.font_size * i),
5667
)
5768

5869
algorithms = [f"{name[0]} - {name}" for name in self.algorithms.keys()]
@@ -67,7 +78,7 @@ def update(self):
6778
1,
6879
color,
6980
),
70-
(self.width // 2, 10 + self.font_size * i),
81+
(self.width / 4 * 3, 10 + self.font_size * i),
7182
)
7283

7384
self.drawList()
@@ -150,15 +161,11 @@ def insertionSort(self):
150161

151162

152163
def main(size=600):
153-
# TODO number of bars slider
154-
bars = 50
155164
sorting = False
156-
screen = Screen(size, bars)
165+
screen = Screen(size)
157166
clock = pygame.time.Clock()
158-
# TODO tick slider
159-
tick = 120
160167
while True:
161-
clock.tick(tick)
168+
clock.tick(screen.getTick())
162169
if sorting:
163170
try:
164171
next(screen.getGen())
@@ -181,6 +188,7 @@ def main(size=600):
181188
screen.setAscending(True)
182189
elif key == pygame.K_d:
183190
screen.setAscending(False)
191+
184192
if key in screen.getAlgoKeys():
185193
screen.setAlgo(key)
186194
screen.update()

0 commit comments

Comments
 (0)