Search code examples
pythongraphicspygamefreezeblit

pygame display becomes unresponsive when clicking outside of the display


I am very new to using the pygame library, and graphics in general.

I have a recursive function that is solving a sudoku puzzle by means of backtracking. I'm trying to use pygame to visualize this process.

Each time the recursive function tries a new valid number, it makes this call:

self.engine.draw_font(str(num), self.engine.get_position(i, j))

The function it calls renders that number onto a new surface:

def draw_font(self, string, position, bold=False):
    font = pg.font.SysFont(self.font, 25, bold=bold)
    textSurface = font.render(string, False, self.colors['text'])
    self.update(textSurface, position)

I then blit that surface onto the display, and update that portion of the display:

def update(self, surface, position):
    rect = pg.Rect(position[0], position[1], 25, 25)
    color = self.display.get_at(position)
    self.display.fill(color, rect)
    self.display.blit(surface, position)
    pg.display.update(rect)

This process does works well, with the numbers being updated on the screen as I expect them to be. However, if I try to interact with the display at all such as: clicking outside of the display, trying to move the display, or even just minimizing the display then the pygame window freezes.

What do I need to do differently to make my display more friendly to work with?


Solution

  • You have to handle the events in the recursive function. See pygame.event.get() respectively pygame.event.pump():

    For each frame of your game, you will need to make some sort of call to the event queue. This ensures your program can internally interact with the rest of the operating system.

    Call pygame.event.pump() at the beginn of the recursive function.