0

Trying to implement flood fill algorithm in pygame just as a learning experience. I have a good start I think and it is mostly functional, but after several seconds of working properly, it gives me a max recursion depth error.

import pygame, random, math
from pygame.locals import *

class GameMain():
done = False
color_bg = Color('white')

def __init__(self, width=800, height=800):
    pygame.init()

    self.width, self.height = width, height
    self.screen = pygame.display.set_mode((self.width, self.height))
    self.clock = pygame.time.Clock()

def main_loop(self):
    while not self.done:
        self.handle_events()
        self.draw()
        self.clock.tick(60)
    pygame.quit()

def draw(self):
    self.screen.fill(self.color_bg)
    pygame.draw.rect(self.screen,Color("grey30"), [100,100,400,300],2)
    pygame.draw.rect(self.screen,Color("grey30"), [ 300,300,400,300],2)
    pygame.display.flip()

def handle_events(self):
    events = pygame.event.get()

    # keystates
    keys = pygame.key.get_pressed()

    # events
    for event in events:
        if event.type == pygame.QUIT:
            self.done = True
        if event.type == KEYDOWN:
            if event.key == K_ESCAPE:
                self.done = True
        if event.type == MOUSEBUTTONDOWN:
            x,y = pygame.mouse.get_pos()
            coord = [x,y]
            self.flood_fill(self.screen,coord)
def flood_fill(self,screen,coord):

    if screen.get_at((coord[0],coord[1])) == Color("grey30"):
        return
    screen.set_at((coord[0],coord[1]),Color("grey30"))
    pygame.display.flip()
    self.flood_fill(self.screen, [coord[0] +1, coord[1]])
    self.flood_fill(self.screen, [coord[0] -1, coord[1]])
    self.flood_fill(self.screen, [coord[0], coord[1]+1])
    self.flood_fill(self.screen, [coord[0], coord[1]-1])


if __name__ == "__main__":
game = GameMain()
game.main_loop()

My program runs for a second or two and changes the color of a few x,y coordinates, but then it goes crazy and I get a recursion depth error. Not sure why it works for a second and then fails.

  • Here's a link to a flood fill variant which claims to keep the recursion depth shallow (I just googled -- I didn't check it): http://www.adammil.net/blog/v126_A_More_Efficient_Flood_Fill.html Oh this one looks better: http://steve.hollasch.net/cgindex/polygons/floodfill.html – Kenny Ostrom Apr 01 '17 at 17:03

1 Answers1

0

Python isn't particularly designed or inclined to recursion operations. In the CPython implementation, the recursion depth is limited to 1000.

You can check this via:

import sys
print(sys.getrecursionlimit())

Consequently, you can override the limit by using sys.setrecursionlimit() instead.

However, that's not particularly the safe/best option.

You're better off in rewriting your code to have an iterative implementation.


EDIT: Apologies, I realized I haven't pointed where you should look at the code.

The recursion actually happens in your flood_fill() method, as it actually calls self.floor_fill() again four (4) times.


For more info, you can read up: http://seriously.dontusethiscode.com/2013/04/14/setrecursionlimit.html

Kevin Lloyd Bernal
  • 355
  • 3
  • 8
  • 24