Advertisement
ppathak35

floodfill2

Jun 3rd, 2022
773
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.03 KB | None | 0 0
  1. import pygame
  2. import sys
  3. sys.setrecursionlimit(3000)
  4.  
  5. pygame.init()
  6. SCREEN = WIDTH, HEIGHT = 288, 512
  7.  
  8. info = pygame.display.Info()
  9. width = info.current_w
  10. height = info.current_h
  11.  
  12. if width >= height:
  13.     win = pygame.display.set_mode(SCREEN, pygame.NOFRAME)
  14. else:
  15.     win = pygame.display.set_mode(SCREEN, pygame.NOFRAME | pygame.SCALED | pygame.FULLSCREEN)
  16.  
  17. clock = pygame.time.Clock()
  18. FPS = 60
  19.  
  20. # COLORS **********************************************************************
  21.  
  22. WHITE = (255, 255, 255)
  23. BLUE = (30, 144,255)
  24. RED = (255, 0, 0)
  25. GREEN = (0, 255, 0)
  26. BLACK = (0, 0, 0)
  27.  
  28. colors = [BLUE, RED, GREEN]
  29.  
  30. # TEXT ************************************************************************
  31.  
  32. font = pygame.font.SysFont('freesansbold', 26)
  33. text = font.render('Flood Fill Algo test', True, WHITE)
  34.  
  35. # LOADING IMAGES **************************************************************
  36. clear = pygame.image.load('clear.png')
  37. clear = pygame.transform.scale(clear, (30, 30))
  38. clear_rect = clear.get_rect()
  39. clear_rect.x = WIDTH - 40
  40. clear_rect.y = HEIGHT - 60
  41.  
  42. clicked = False
  43. polygon = []
  44.  
  45. def floodfill(x, y, old, new):
  46.     pixel = win.get_at((x, y))
  47.     if pixel != old:
  48.         return
  49.     elif pixel == new:
  50.         return
  51.     else:
  52.         print(x, y)
  53.         win.set_at((x, y), new)
  54.         pygame.display.update()
  55.  
  56.         floodfill(x-2, y, old, new)
  57.         floodfill(x+1, y, old, new)
  58.         floodfill(x, y-1, old, new)
  59.         floodfill(x, y+2, old, new)
  60.         # floodfill(x-1, y-1, old, new)
  61.         # floodfill(x-1, y+1, old, new)
  62.         # floodfill(x+1, y-1, old, new)
  63.         # floodfill(x+1, y+1, old, new)
  64.  
  65. class Rect:
  66.     def __init__(self, x, y, c):
  67.         self.x = x
  68.         self.y = y
  69.         self.c = c
  70.         self.rect = pygame.Rect(x, y, 30, 30)
  71.  
  72.     def draw(self):
  73.         pygame.draw.rect(win, self.c, self.rect)
  74.  
  75. r1 = Rect(WIDTH-40, 10, RED)
  76. r2 = Rect(WIDTH-40, 45, GREEN)
  77. r3 = Rect(WIDTH-40, 85, BLUE)
  78.  
  79. rects = [r1, r2, r3]
  80. color = RED
  81.  
  82. running = True
  83. while running:
  84.     for event in pygame.event.get():
  85.         if event.type == pygame.QUIT:
  86.             running = False
  87.  
  88.         if event.type == pygame.KEYDOWN:
  89.             if event.key == pygame.K_ESCAPE or event.key == pygame.K_q:
  90.                 running = False
  91.  
  92.         if event.type == pygame.MOUSEBUTTONDOWN:
  93.             pos = event.pos
  94.             btn = pygame.mouse.get_pressed()
  95.             if btn[0]:
  96.                 clicked = True
  97.  
  98.             elif btn[2]:
  99.                 if pos[0] < WIDTH - 50:
  100.                     floodfill(pos[0], pos[1], (0,0,0), color)
  101.  
  102.             if pos[0] > WIDTH - 50:
  103.                 for r in rects:
  104.                     if r.rect.collidepoint(pos):
  105.                         color = r.c
  106.  
  107.             if clear_rect.collidepoint(pos):
  108.                 win.fill(BLACK)
  109.  
  110.         if event.type == pygame.MOUSEBUTTONUP:
  111.             clicked = False
  112.  
  113.         if event.type == pygame.MOUSEMOTION:
  114.             if clicked:
  115.                 pos = event.pos
  116.                 btn = pygame.mouse.get_pressed()
  117.                 if btn[0]:
  118.                     if pos[0] < WIDTH - 50:
  119.                         pygame.draw.circle(win, WHITE, pos, 5)
  120.                
  121.     pygame.draw.rect(win, WHITE, (0, 0, WIDTH-50, HEIGHT), 3)
  122.     pygame.draw.rect(win, WHITE, (WIDTH-50, 0, 50, HEIGHT), 2)
  123.  
  124.     win.blit(text, (60, 40))
  125.     win.blit(clear, clear_rect)
  126.  
  127.     for rect in rects:
  128.         rect.draw()
  129.  
  130.     clock.tick(FPS)
  131.     pygame.display.update()
  132.  
  133. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement