我想知道一个填充Pygame表面部分的好方法。 我想要的最好的例子是油漆桶在MS Paint中的工作方式。
例如,如果在白色表面上有一个黑色圆圈,我想在圆圈内填充白色(或任何形状)。
为了让您了解我正在做什么,我正在制作一个像素艺术工具,而且我正在开发一个与MS Paint相似的功能。 (观察:http://imgur.com/a/ogtPV)
我尝试使用Surface.get_at()
和Surface.set_at()
来填充,但是一旦你想要填充一个区域的100x100像素,它就会落后太多。
我也对其他任何不落后的方法持开放态度。
答案 0 :(得分:1)
我找到了一个方法,60 ms
区域约为100x100
,2000 ms
区域约为1000x1000
。代码中的解释。
import random
import pygame
pygame.init()
screen = pygame.display.set_mode((1024, 640))
clock = pygame.time.Clock()
image = pygame.image.load('delete_image.png').convert()
def fill(surface, position, fill_color):
fill_color = surface.map_rgb(fill_color) # Convert the color to mapped integer value.
surf_array = pygame.surfarray.pixels2d(surface) # Create an array from the surface.
current_color = surf_array[position] # Get the mapped integer color value.
# 'frontier' is a list where we put the pixels that's we haven't checked. Imagine that we first check one pixel and
# then expand like rings on the water. 'frontier' are the pixels on the edge of the pool of pixels we have checked.
#
# During each loop we get the position of a pixel. If that pixel contains the same color as the ones we've checked
# we paint it with our 'fill_color' and put all its neighbours into the 'frontier' list. If not, we check the next
# one in our list, until it's empty.
frontier = [position]
while len(frontier) > 0:
x, y = frontier.pop()
try: # Add a try-except block in case the position is outside the surface.
if surf_array[x, y] != current_color:
continue
except IndexError:
continue
surf_array[x, y] = fill_color
# Then we append the neighbours of the pixel in the current position to our 'frontier' list.
frontier.append((x + 1, y)) # Right.
frontier.append((x - 1, y)) # Left.
frontier.append((x, y + 1)) # Down.
frontier.append((x, y - 1)) # Up.
pygame.surfarray.blit_array(surface, surf_array)
while True:
clock.tick(30)
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit()
elif event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
color = random.choice(tuple(pygame.color.THECOLORS.values()))
print('Running')
time = pygame.time.get_ticks()
fill(image, event.pos, color)
print('Finished in {} ms'.format(pygame.time.get_ticks() - time))
screen.blit(image, (0, 0))
pygame.display.update()
答案 1 :(得分:1)
此算法称为" flood fill"。 Pygame没有内置的泛洪填充功能。最简单的选择可能是使用OpenCV库。 Google"安装opencv"为您的平台。
然后你可以将它包装在Pygame表面的函数中:
disable cache
这将使用颜色填充连接区域。