0

I'm writing a sorting algorithm visualisation in python using pygame. I've come to an end, everything seems to work just fine, except the fact that the Pygame Window stops responding after a few seconds.

I've tried all solutions I could find, but none worked so far. If anyone could give me an insight into this code, I'd be grateful

import random
import time

import pygame
import color_constants as colors
import ctypes
from pygame.locals import *


def readData():
    listOfNumbers = []
    data = dict()

    print("Lowest Value: ")
    numLow = int(input())

    print("Highest Value: ")
    numHigh = int(input())

    print("Length of list: ")
    n = int(input())

    for i in range(0, n):
        listOfNumbers.append(random.randint(numLow, numHigh))

    data.update({'lst': listOfNumbers})
    data.update({'numLow': numLow})
    data.update({'numHigh': numHigh})
    data.update({'n': n})

    return data


def checkSorting(lst):
    ok = True

    for i in range(0, len(lst) - 1):
        if lst[i] > lst[i + 1]:
            ok = False

    if ok is True:
        print("Sorting Check, OK = True")
        return True
    else:
        print("Sorting Check, OK = False")
        return False


def drawRect(lst):
    for i in range(len(lst)):
        rect = Rect(i * RECT_W, 0, RECT_W, RECT_H * lst[i])
        rect.bottom = SCREEN_H
        pygame.draw.rect(screen, BLACK, rect)
        pygame.display.flip()


def bubblesort(lst):
    for i in range(0, len(lst) - 1):
        for j in range(i + 1, len(lst)):
            if lst[i] > lst[j]:
                aux = lst[i]
                lst[i] = lst[j]
                lst[j] = aux

                drawRect(lst)

    return lst


if __name__ == "__main__":
    # Setting up the color literals
    BLACK = colors.BLACK
    WHITE = colors.WHITE

    # Getting the user's screen size
    user32 = ctypes.windll.user32
    SCREENSIZE = user32.GetSystemMetrics(0)-100, user32.GetSystemMetrics(1)-100
    SCREEN_W = SCREENSIZE[0]
    SCREEN_H = SCREENSIZE[1]

    data = readData()

    # Setting and scaling the size of rectangle based on the number of elements (n)
    # and the highest number (numHigh)
    RECT_W = SCREEN_W // data['n']
    RECT_H = SCREEN_H / (data['numHigh'])

    pygame.init()
    screen = pygame.display.set_mode(SCREENSIZE)
    screen.fill(WHITE)

    running = True
    while running:
        pygame.event.pump()

        bubblesort(data['lst'])

    pygame.quit()
Leonard V.
  • 115
  • 1
  • 8
  • If possible, provide a reproducible example so people can run it on their own machines and see the problem you're experiencing. – Rolv Apneseth Jul 19 '21 at 15:21

1 Answers1

0

I can't figure out how to use your program, but you are missing an event loop and I know that I've had problems with a program going unresponsive if missing one of those. Your code for the main loop should look like this:

running = True
while running:
    pygame.event.pump()

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    bubblesort(data["lst"])

Let me know if this solves your problem.

Rolv Apneseth
  • 2,078
  • 2
  • 7
  • 19
  • Also I'm sure input will cause problems, when running your code I just hard coded the variables. Instead of inputs consider making some UI elements for the user to input those fields. – Rolv Apneseth Jul 19 '21 at 15:32
  • 1
    Yes, input will cause problems if we go above a specific threshold. I was planning on doing some UI, but I wanted to see the visualisation done first. Unfortunately, the above code did not solve the issue. Let me see if I can make a reproducible example real quick – Leonard V. Jul 19 '21 at 15:36
  • Well try hard coding the inputs as well and see if that fixes it. Another thing I changed with the code was the `SCREENSIZE` variable as I'm running this on Linux – Rolv Apneseth Jul 19 '21 at 15:38