-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscreen.py
113 lines (93 loc) · 3.77 KB
/
screen.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
import pygame
from tkinter import *
from random import randint
from settings import *
class ScreenVisual():
def __init__(self,COLS,sortMethod,delay,drawGridLines,WIN):
self.PIXEL_SIZE = WIDTH // COLS
self.ROWS = self.COLS = COLS
self.sortMethod = sortMethod
self.delay = delay
self.drawGridLines = drawGridLines
self.WIN = WIN
self.grid = self.init_grid(BG_COLOR)
def init_grid(self, color):
"""This function returns grids"""
grid = []
for i in range(self.ROWS):
grid.append([])
for _ in range(self.COLS):
grid[i].append(color)
return grid
def draw_grid(self):
"""This function draws grids on pygame screen"""
for i,row in enumerate(self.grid):
for j,pixel in enumerate(row):
pygame.draw.rect(self.WIN, pixel,(i * self.PIXEL_SIZE, j * self.PIXEL_SIZE, self.PIXEL_SIZE, self.PIXEL_SIZE))
if self.drawGridLines:
for i in range(self.ROWS + 1):
pygame.draw.line(self.WIN, BLACK, (0, i* self.PIXEL_SIZE), (WIDTH, i * self.PIXEL_SIZE))
for i in range(self.COLS + 1):
pygame.draw.line(self.WIN, BLACK, (i* self.PIXEL_SIZE, 0), (i * self.PIXEL_SIZE, HEIGHT - TOOLBAR_HEIGHT))
def draw(self):
"""This function draws pygame screen"""
self.WIN.fill(BG_COLOR)
self.draw_grid()
pygame.display.update()
def generate_arr(self):
"""This function generates a random array"""
arr = []
for _ in range(0, self.ROWS):
arr.append(randint(1,self.COLS))
return arr
def drawArr(self,arr,colour,delay=-1):
"""This function draws the given array on pygame screen"""
for i in range(0,len(arr)):
for j in range(self.COLS-1,self.ROWS-arr[i],-1):
self.grid[i][j] = colour
if delay != -1:
self.slowExec(delay)
def empGrids(self):
"""This function resets all the grids to white"""
for i in range(self.ROWS):
for j in range(self.COLS):
self.grid[i][j] = WHITE
def slowExec(self,delay):
"""This function slows the execution,
to create animation on pygame screen"""
self.draw()
pygame.display.update()
pygame.time.delay(delay)
def fastDisp(self,arr,i):
k = self.COLS-1
while k != self.ROWS - arr[i]:
if k != self.ROWS - arr[i]:
self.grid[i][k] = YELLOW
k -= 1
def dispSort(self,arr,l,m=-1,fstDispIndex=-1):
"""This function displays realtime changes in array on pygame screen"""
k1 = k2 = self.COLS-1
self.empGrids()
self.drawArr(arr,PURPLE)
if fstDispIndex != -1:
self.fastDisp(arr,fstDispIndex)
pygame.display.update()
if m!=-1:
while k1 != self.ROWS - arr[l] or k2 != self.ROWS - arr[m]:
if k1 != self.ROWS - arr[l]:
self.grid[l][k1] = RED
k1 -= 1
if k2 != self.ROWS - arr[m]:
self.grid[m][k2] = GREEN
k2 -= 1
pygame.display.update()
self.slowExec(self.delay)
else:
while k1 != self.ROWS - arr[l]:
if k1 != self.ROWS - arr[l]:
self.grid[l][k1] = RED
k1 -= 1
pygame.display.update()
self.slowExec(self.delay)
self.empGrids()
self.drawArr(arr,PURPLE)