我的目标是创建一个名为findList()
的函数,该函数保存点和屏幕表面的给定参数。在pygame中填充给定的颜色
我的目标是计算该点的颜色,然后返回填充颜色的列表。
例如,如果有一个屏幕上有一个红色圆圈,并且该点位于红色圆圈内,我希望能够返回一个包含该圆圈所有点的列表。
基本上,点扩大,避免所有其他颜色和屏幕的边缘,直到它不能再扩大;然后函数返回所有创建点的列表。
这里是我的尝试:
def findList(point,screen):
directions=[(0,0)]
myList=[]
myList.append(point)
startColour=screen.get_at(point)
i=0
loop=0
while True:
loop=loop+1
print(loop)
directions=[]
print("Length of myList: "+str(len(myList)))
for i in range(len(myList)):
if myList[i][0]+1<WINDOWWIDTH and screen.get_at((myList[i][0]+1,myList[i [1]))==startColour and myList[i][0]+1 not in myList and myList[i][0]+1 not in directions:
directions.append((myList[i][0]+1,myList[i][1]))
if myList[i][0]-1>0 and screen.get_at((myList[i][0]-1,myList[i][1]))==startColour and myList[i][0]-1 not in myList and myList[i][0]-1 not in directions:
directions.append((myList[i][0]-1,myList[i][1]))
if myList[i][1]+1<WINDOWHEIGHT and screen.get_at((myList[i][0],myList[i][1]+1))==startColour and myList[i][1]+1 not in myList and myList[i][1]+1 not in directions:
directions.append((myList[i][0],myList[i][1]+1))
if myList[i][1]-1>0 and screen.get_at((myList[i][0],myList[i][1]-1))==startColour and myList[i][1]-1 not in myList and myList[i][1]-1 not in directions:
directions.append((myList[i][0],myList[i][1]-1))
print(len(directions))
for i in directions:
myList.append(i)
if len(directions)==0:
break
print("Exited loop.")
return myList
我知道的编码风格是可怕的,其中很大一部分可能是无用的,但一般的问题是功能(可能)的作品,但刻意缓慢,似乎增加以前添加的像素很多,直到我可怜的小上网本不能再处理它正在使用的100 000个点。
如果有人可以为更好的功能提出一些建议,那真的会对我有帮助,因为只有13岁,我对这些混乱的代码感到困惑(请注意ifs中的4个条件)。
感谢您的建议,我意识到如果条件,但我会环顾网络上的这些'洪水填充'算法你说。 – user2592835
你确定他不是在寻找BFS算法吗?这就是我用来做扫雷的一次... –
我会看看'BFS'算法,但我的主要问题是找到一个不递归的算法。 – user2592835