Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # a quick fill demo by anthony cook fb.com/groups/pygame
- # use the left mouse button to draw a shape
- # use the right mouse button to fill
- import pygame
- class fill:
- class node:
- def __init__(s, parent, x, y):
- global W, H
- for xx in range(x, W):
- if parent.pixelArray[xx, y] != parent.replacedColor:
- xx -= 1
- break
- s.x = xx
- s.y = y
- s.parent = parent
- def do(s):
- global W, H
- topNode = 0
- bottomNode = 0
- newNodes = []
- for x in range(s.x, 0, -1):
- if s.y - 1 > 0:
- if s.parent.pixelArray[x][s.y - 1] == s.parent.replacedColor:
- if topNode == 0:
- newNodes.append([x, s.y - 1])
- topNode = 1
- elif s.parent.pixelArray[x][s.y - 1] != s.parent.replacedColor and topNode == 1:
- topNode = 0
- s.parent.pixelArray[x, s.y] = s.parent.newColor
- if s.parent.pixelArray[x - 1, s.y] != s.parent.replacedColor:
- break
- if s.y + 1 <= H:
- if s.parent.pixelArray[x][s.y + 1] == s.parent.replacedColor:
- if bottomNode == 0:
- newNodes.append([x, s.y + 1])
- bottomNode = 1
- elif s.parent.pixelArray[x][s.y + 1] != s.parent.replacedColor and bottomNode == 1:
- bottomNode = 0
- return newNodes
- def __init__(s, surface, x, y, color):
- s.pixelArray = pygame.PixelArray(surface)
- s.replacedColor = s.pixelArray[x][y]
- if color == s.replacedColor:
- s.pixelArray.close()
- del s.pixelArray
- return
- s.newColor = color
- s.nodes = [s.node(s, x, y)]
- while s.nodes:
- newNodes = []
- for n in s.nodes:
- newNodes += n.do()
- s.nodes = [s.node(s, xy[0], xy[1]) for xy in newNodes]
- s.pixelArray.close()
- del s.pixelArray
- W, H = 1280, 720
- HW, HH = 640, 360
- pygame.init()
- DS = pygame.display.set_mode((W, H))
- FILL_COLOR = DS.map_rgb((255, 0, 0))
- mousePressed = False
- # press ESC to exit demo
- while True:
- e = pygame.event.get()
- if pygame.key.get_pressed()[pygame.K_ESCAPE]: break
- mb = pygame.mouse.get_pressed()
- mx, my = pygame.mouse.get_pos()
- if mb[0] and not mousePressed:
- px, py = mx, my
- mousePressed = True
- if mousePressed:
- if not mb[0]: mousePressed = False
- pygame.draw.line(DS, (255, 255, 255), (px, py), (mx, my))
- px, py = mx, my
- if mb[2]:
- fill(DS, mx, my, FILL_COLOR)
- pygame.display.update()
- pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement