'I have a maze generator using recursive backtracking, how do I stop the window crashing when I try and move it

Every time I wan't to interact or move the window, the window crashes after a few seconds. It stops running and windows returns a "python is not responding" error. Is this a problem with the code? I have made one other program using pygame which was interactive, however it didn't have this issue. The only differnce I can see between the two is the following code within each function.

def function():
    done = False
    while not done:
        for event in pygame.event.get():
            if event.type == pygame.quit:
                done = True
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    pygame.quit()

The code

import random
import time
import pygame

black = (0, 0, 0)
white = (255, 255, 255)
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
yellow = (255, 255, 0)

size = width, height = 850, 850

pygame.init()
pygame.mixer.init()

screen = pygame.display.set_mode(size)
screen.fill(white)

cell_width = 40
x, y = 40, 40

grid = []
stack_list = []
closed_list = []

path = {}

Create the grid

def build_grid(x, y, cell_width):
    for n in range(20):
        y = y + 40
        x = 40
        for m in range(20):
            pygame.draw.line(screen, black, [x + cell_width, y], [x + cell_width, y + cell_width], 2)  # East wall
            pygame.draw.line(screen, black, [x, y], [x, y + cell_width], 2)  # West wall
            pygame.draw.line(screen, black, [x, y], [x + cell_width, y], 2)  # North wall
            pygame.draw.line(screen, black, [x, y + cell_width], [x + cell_width, y + cell_width], 2)  # South wall

            grid.append((x, y))
            x = x + 40
            pygame.display.update()

Create the maze by knocking down the walls

def knockdown_east_wall(x, y):
    pygame.draw.rect(screen, yellow, (x + 1, y + 1, 79, 39), 0)
    pygame.display.update()


def knockdown_west_wall(x, y):
    pygame.draw.rect(screen, yellow, (x - cell_width + 1, y + 1, 79, 39), 0)
    pygame.display.update()


def knockdown_north_wall(x, y):
    pygame.draw.rect(screen, yellow, (x + 1, y - cell_width + 1, 39, 79), 0)
    pygame.display.update()


def knockdown_south_wall(x, y):
    pygame.draw.rect(screen, yellow, (x + 1, y + 1, 39, 79), 0)
    pygame.display.update()

if backtracking is occurring, place a green square to show where the position is

def locator(x, y):
    pygame.draw.rect(screen, green, (x + 1, y + 1, 39, 39), 0)
    pygame.display.update()

after the green square has been placed, place a yellow square

def backtracking_cell(x, y):
    pygame.draw.rect(screen, yellow, (x + 1, y + 1, 39, 39), 0)
    pygame.display.update()

when the maze has done generating, place blue squares along the path to the start, to show the solved maze

def path_tracker(x, y):
    pygame.draw.rect(screen, blue, (x + 15, y + 15, 10, 10), 0)
    pygame.display.update()

create a list of available directions that the path can go

def maze(x, y):
    stack_list.append((x, y))
    closed_list.append((x, y))

    while len(stack_list) > 0:
        time.sleep(2)
        cell = []

        if (x + cell_width, y) not in closed_list and (x + cell_width, y) in grid:
            cell.append("East")

        if (x - cell_width, y) not in closed_list and (x - cell_width, y) in grid:
            cell.append("West")

        if (x, y + cell_width) not in closed_list and (x, y + cell_width) in grid:
            cell.append("South")

        if (x, y - cell_width) not in closed_list and (x, y - cell_width) in grid:
            cell.append("North")

        if len(cell) > 0:
            current_cell = (random.choice(cell))

Randomly generate the walls that are knocked down, making sure not to track over what has already been generated


            if current_cell == "East":
                knockdown_east_wall(x, y)
                path[(x + cell_width, y)] = x, y
                x = x + cell_width
                closed_list.append((x, y))
                stack_list.append((x, y))
                print("now going east")
                print(cell)

            elif current_cell == "West":
                knockdown_west_wall(x, y)
                path[(x - cell_width, y)] = x, y
                x = x - cell_width
                closed_list.append((x, y))
                stack_list.append((x, y))
                print("now going west")
                print(cell)

            elif current_cell == "North":
                knockdown_north_wall(x, y)
                path[(x, y - cell_width)] = x, y
                y = y - cell_width
                closed_list.append((x, y))
                stack_list.append((x, y))
                print("now going north")
                print(cell)

            elif current_cell == "South":
                knockdown_south_wall(x, y)
                path[(x, y + cell_width)] = x, y
                y = y + cell_width
                closed_list.append((x, y))
                stack_list.append((x, y))
                print("now going south")
                print(cell)

if there is nowhere else to go, back track until there is an empty space

        else:
            x, y = stack_list.pop()
            locator(x, y)
            time.sleep(0.8)
            backtracking_cell(x, y)
            print(cell)

call the path tracker function place blue squares in each cell along the path until it reaches the top left corner

def path_tracer(x, y):
    path_tracker(x, y)
    while (x, y) != (40, 40):
        x, y = path[x, y]
        path_tracker(x, y)
        time.sleep(0.8)

call each function and pass in arguments to start

build_grid(40, 0, 40)
maze(x, y)
path_tracer(800, 800)




Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source