Advertisement
cookertron

Fill Demo

Jan 14th, 2020
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.94 KB | None | 0 0
  1. # a quick fill demo by anthony cook fb.com/groups/pygame
  2. # use the left mouse button to draw a shape
  3. # use the right mouse button to fill
  4.  
  5. import pygame
  6.  
  7. class fill:
  8.     class node:
  9.         def __init__(s, parent, x, y):
  10.             global W, H
  11.             for xx in range(x, W):
  12.                 if parent.pixelArray[xx, y] != parent.replacedColor:
  13.                     xx -= 1
  14.                     break
  15.             s.x = xx
  16.             s.y = y
  17.             s.parent = parent
  18.  
  19.         def do(s):
  20.             global W, H
  21.            
  22.             topNode = 0
  23.             bottomNode = 0
  24.             newNodes = []
  25.  
  26.             for x in range(s.x, 0, -1):
  27.                 if s.y - 1 > 0:
  28.                     if s.parent.pixelArray[x][s.y - 1] == s.parent.replacedColor:
  29.                         if topNode == 0:
  30.                             newNodes.append([x, s.y - 1])
  31.                             topNode = 1
  32.                     elif s.parent.pixelArray[x][s.y - 1] != s.parent.replacedColor and topNode == 1:
  33.                         topNode = 0
  34.  
  35.                 s.parent.pixelArray[x, s.y] = s.parent.newColor
  36.                 if s.parent.pixelArray[x - 1, s.y] != s.parent.replacedColor:
  37.                     break
  38.                
  39.                 if s.y + 1 <= H:
  40.                     if s.parent.pixelArray[x][s.y + 1] == s.parent.replacedColor:
  41.                         if bottomNode == 0:
  42.                             newNodes.append([x, s.y + 1])
  43.                             bottomNode = 1
  44.                     elif s.parent.pixelArray[x][s.y + 1] != s.parent.replacedColor and bottomNode == 1:
  45.                         bottomNode = 0
  46.             return newNodes
  47.                
  48.  
  49.     def __init__(s, surface, x, y, color):
  50.         s.pixelArray = pygame.PixelArray(surface)
  51.         s.replacedColor = s.pixelArray[x][y]
  52.         if color == s.replacedColor:
  53.             s.pixelArray.close()
  54.             del s.pixelArray
  55.             return
  56.         s.newColor = color
  57.         s.nodes = [s.node(s, x, y)]
  58.         while s.nodes:
  59.             newNodes = []
  60.             for n in s.nodes:
  61.                 newNodes += n.do()
  62.             s.nodes = [s.node(s, xy[0], xy[1]) for xy in newNodes]
  63.  
  64.         s.pixelArray.close()
  65.         del s.pixelArray
  66.  
  67. W, H = 1280, 720
  68. HW, HH = 640, 360
  69.  
  70. pygame.init()
  71. DS = pygame.display.set_mode((W, H))
  72.  
  73. FILL_COLOR = DS.map_rgb((255, 0, 0))
  74.  
  75. mousePressed = False
  76.  
  77. # press ESC to exit demo
  78. while True:
  79.     e = pygame.event.get()
  80.     if pygame.key.get_pressed()[pygame.K_ESCAPE]: break
  81.  
  82.     mb = pygame.mouse.get_pressed()
  83.     mx, my = pygame.mouse.get_pos()
  84.     if mb[0] and not mousePressed:
  85.         px, py = mx, my
  86.         mousePressed = True
  87.     if mousePressed:
  88.         if not mb[0]: mousePressed = False
  89.         pygame.draw.line(DS, (255, 255, 255), (px, py), (mx, my))
  90.         px, py = mx, my
  91.    
  92.     if mb[2]:
  93.         fill(DS, mx, my, FILL_COLOR)
  94.        
  95.     pygame.display.update()
  96.  
  97. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement