I’m currently working on a drawing program, and attempted to implement this flood fill program.
I tried to adapt it to Pygame, however for some reason it only seems to partially work; if I leave both x lines (x+1, x-1) the program will crash. If I remove 1 x line then everything works fine. I’m not sure what may be causing this. Any help determining the issue would be appreciated, and preferable to a complete rewrite.
full_canvas = flood_fill(full_canvas,mouse_history[-1][0],mouse_history[-1][1],brush_colour,old_colour,mouse_history[-1])
def flood_fill(image,x,y,new_colour,old_colour,start_pixel):
pixel = image.get_at((x,y))
pixel_colour = (pixel[0],pixel[1],pixel[2])
if x < 0 or x > image.get_width() or y < 0 or y > image.get_height() or pixel_colour == new_colour or pixel_colour != old_colour or pixel[3] < 1:
return
image.set_at((x,y),new_colour)
flood_fill(image,x+1,y,new_colour,old_colour,start_pixel)
flood_fill(image,x-1,y,new_colour,old_colour,start_pixel)
flood_fill(image,x,y+1,new_colour,old_colour,start_pixel)
flood_fill(image,x,y-1,new_colour,old_colour,start_pixel)
return image
New contributor
Oyavo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.