-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.py
48 lines (33 loc) · 1.58 KB
/
helpers.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
import random
import pygame
def draw(draw_info, algo_name, ascending):
draw_info.window.fill(draw_info.BACKGROUND_COLOR)
title = draw_info.LARGE_FONT.render(f"{algo_name} - {'Ascending' if ascending else 'Descending'}", 1, draw_info.GREEN)
draw_info.window.blit(title, (draw_info.width/2 - title.get_width()/2 , 5))
controls = draw_info.FONT.render("R - Reset | SPACE - Start Sorting | A - Ascending | D - Descending", 1, draw_info.BLACK)
draw_info.window.blit(controls, (draw_info.width/2 - controls.get_width()/2 , 45))
sorting = draw_info.FONT.render("I - Insertion Sort | B - Bubble Sort", 1, draw_info.BLACK)
draw_info.window.blit(sorting, (draw_info.width/2 - sorting.get_width()/2 , 75))
draw_list(draw_info)
pygame.display.update()
def draw_list(draw_info, color_positions={}, clear_bg=False):
lst = draw_info.lst
if clear_bg:
clear_rect = (draw_info.SIDE_PAD//2, draw_info.TOP_PAD,
draw_info.width - draw_info.SIDE_PAD, draw_info.height - draw_info.TOP_PAD)
pygame.draw.rect(draw_info.window, draw_info.BACKGROUND_COLOR, clear_rect)
for i, val in enumerate(lst):
x = draw_info.start_x + i * draw_info.block_width
y = draw_info.height - (val - draw_info.min_val) * draw_info.block_height
color = draw_info.GRADIENTS[i % 3]
if i in color_positions:
color = color_positions[i]
pygame.draw.rect(draw_info.window, color, (x, y, draw_info.block_width, draw_info.height))
if clear_bg:
pygame.display.update()
def generate_starting_list(n, min_val, max_val):
lst = []
for _ in range(n):
val = random.randint(min_val, max_val)
lst.append(val)
return lst