- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我想知道填充 Pygame 表面的一部分的好方法。我想要的最好例子是油漆桶在 MS Paint 中的工作方式。
例如,如果在白色表面上用黑色绘制了一个圆圈,我想填充圆圈内的白色(或任何形状)。
为了让您了解我在做什么,我正在制作一个像素艺术工具,并且我正在开发一个类似于 MS Paint 中的水桶的功能。 (观看:http://imgur.com/a/ogtPV)
我试过使用 Surface.get_at()
和 Surface.set_at()
一堆来填充,但是一旦你得到大约 100x100 像素的区域来填充, 它滞后太多。
我也愿意接受任何其他不会滞后的方法。
最佳答案
我找到了一个大约需要 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()
关于python - 如何在 Pygame 表面实现洪水填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41656764/
我有以 xyz 点格式表示 3D 表面(即地震断层平面)的数据。我想创建这些表面的 3D 表示。我使用 rgl 和 akima 取得了一些成功,但是它无法真正处理可能会自行折叠或在同一 x,y 点具有
我正在尝试将此 X、Y、Z 数据集拟合到未知表面。 不幸的是,线性拟合不足以显示表面数据。我认为多项式拟合可能适合这种情况。另外,问题是我不知道如何建立多项式拟合函数来完成曲面拟合。 任何帮助都会很棒
我已经用plotly构建了一个表面图表,并且我正在尝试根据我自己的文本获得hoverinfo。奇怪的是它不再工作了。 library(plotly) x % layout(dragmode = "tu
我有以下数据: library(rgl) x y,y->z,z->x) zmat <- matrix(data = z, nrow = 6, ncol = 5, byrow = FALSE) surf
我正在使用 DXVA 视频解码器。它工作正常,但我想与另一个 IDirect3D9 设备对象共享解压缩的表面。 我读了this文件,我调用 IDirectXVideoDecoderService::C
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
import pygame, sys, os.path pygame.init() # set up the colours # R G B BLACK = ( 0,
我的目标是在 pygame 实例内显示一个颜色图,以 49 个元素 ndarray 的形式反射(reflect)实时输入,范围从 -6 到 2,标准化为 0 到 1 的值。到目前为止,我正在使用 ma
我在 Visual C# -> surface -> v2.0 -> MS visual C# 2010 express 中的 Surface Application (WPF) 模板中工作。 我正在
我正在尝试在 JavaFX 中实现我自己的 3D 表面动画,但我不理解它应该工作的一切,有人可以帮助我理解哪个应该放在哪里吗? 已经知道使用类构建Mesh需要类对象TraingleMesh然后必须使用
根据我的阅读,我不相信 SurfaceView 可以设置动画,但我会问这个问题: 我在 ViewFlipper 中有一个 surfaceView 对象。当 ViewFlipper 向左或向右移动到新的
我想在 android 屏幕上有一个图像,图像的不同部分可以点击。我的意思是,如果它是 3 个圆圈的图像,我希望能够单击这些圆圈中的每一个, 然后我可以为每个可点击的圆圈添加不同的功能。对于下图中的示
我有一个通过kinect获得的点集,现在我想创建一个网格。我正在尝试使用 CGAL 库并且正在关注 this example . 我使用的是 VS2010,它运行没有任何错误,但是当然它没有在行中
在让我的 SurfaceView 显示我的相机预览时遇到一点问题。我在这里查看了一些问题并通过 Google 搜索了一些 tuts,但我认为这可能是我这边的一个小错误,我只是没有看到。 代码 publ
任何人都可以为我指出一些类(class)或对以下情况提出任何建议吗? 我有一个 SurfaceView,它有一个背景图像,我希望在其上绘制其他位图。我想支持以下操作: 点击一下,新的位图就会添加到背景
我正在尝试学习表面 View 并且我确实读到了它。 所以,我试着制作了一个游戏,我认为它可以帮助我更好地学习。 我创建了一个表面 View 类,如下所示: class SnakeEngine exte
我希望笑脸 div(在用户进入墙壁后显示)将覆盖主迷宫表面而不改变笑脸大小:你能帮帮我吗? 这是 fiddle 链接: http://jsfiddle.net/uqcLn/66/ 这是笑脸 div:
我有一组 (x,y,z) 点,这些点具有相应的法线和值。所以数据的形式是 [x y z nx ny nz c]。我想在这些垂直于这些法线的点上绘制一个 3D 表面,并且具有与该值对应的颜色。所以我想要
我有一个不是函数图的表面的 3D 数据集。数据只是 3D 中的一堆点,我唯一能想到的就是在 Matlab 中尝试 scatter3。 Surf 将不起作用,因为表面不是函数图。 使用 scatter3
假设我有一个函数,例如: 现在,我想绘制它的曲面图(matplotlib plot_surface )。我使用 np.arange(stop,end,increment) 构造了三个数组。 在这里,我
我是一名优秀的程序员,十分优秀!