ppathak35

floodfill

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