Python Sikuli:多图标排序

Python Sikuli:多图标排序,python,image,sorting,sikuli,Python,Image,Sorting,Sikuli,目标:单击页面中的所有“星星”,方法是先移动行,然后移动Sikuli中的列 示例:星星排列成网格状,如下所示: * * * * * * * * * * * * * * * * * * * * 编辑:这是要单击的顺序: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 首先单击左上角的一个,然后单击其右侧的星星,依此类推。接下来转到第二排左上角的星星并重复

目标:单击页面中的所有“星星”,方法是先移动行,然后移动Sikuli中的列

示例:星星排列成网格状,如下所示:

* * * * *
* * * * *
* * * * *
* * * * *
编辑:这是要单击的顺序:

1   2   3   4   5
6   7   8   9   10
11  12  13  14  15
16  17  18  19  20
21  22  23  24  25 
首先单击左上角的一个,然后单击其右侧的星星,依此类推。接下来转到第二排左上角的星星并重复

我当前的代码:

def by_x(match):
    return match.x
def by_y(match):
    return match.y
stars = findAll("imgOfStar")
sorted_stars_x = sorted(stars, key=by_x)
sorted_stars_y = sorted(stars, key=by_y)
for icon in sorted_stars_x:
    for icon2 in sorted_stars_y:
        click("imgOfStar")

这可能不是最优雅的方式,但这是我能想到的第一件事:

def by_y(match):
    return match.y
stars = findAll(imageOfStars)
sorted_stars_y = sorted(stars, key=by_y)
finalStars = []
count = 0
for x in range(5): #if you know your grid is 5x5
    finalStars.append(sorted(sorted_stars_y[count:count + 5])) #see explanation, if needed
    count += 5
for x in finalStars:
    click(x)
说明:示例中的前五颗星应具有匹配的y值,即它们都应位于顶行。现在,您只需对它们的x值进行排序,并将它们附加到一个列表中,然后继续下一个5,依此类推

如果您的网格的大小之前还不知道,您可以通过几种不同的方法来实现这一点-- 如果你的网格总是完全正方形,你可以找到你的星星数的平方根:

 import math #or import sqrt from math, if the square root is the only math function you need.
 def by_y(match):
    return match.y
stars = findAll(imageOfStars)
sorted_stars_y = sorted(stars, key=by_y)
finalStars = []
count = 0
rows = math.sqrt(len(stars))
for x in range(rows):
    finalStars.append(sorted(sorted_stars_y[count:count + rows]))
    count += rows 
如果您的网格不是完全正方形,那么您可以做其他一些事情,但除非您正在寻找,否则这个答案会变得有点长,因此我们将把讨论留到后面:)

编辑: 由于您知道列数始终为5,因此可以找到如下行数:

 rows = (len(stars) / 5)
 rowCount = 0
 count = 0
然后,您可以使用while循环来迭代您的星星:

while rowCount < rows:
    finalStars.append(sorted(sorted_Stars_y[count:count+ 5]))
    count += 5
    rowCount += 1
行计数<行时
:
finalStars.append(排序(排序后的星号[count:count+5]))
计数+=5
行计数+=1

说到底,这会帮你完成任务,但@Tenzin给出的答案更为优雅:)

你可以做出定义,明确你喜欢如何在星星间移动。 屏幕本身具有x.y位置。要从左上角开始到右下角结束,您需要 匹配。y,匹配。x

然后您需要查找星号(“stars.png”)。 你按照你定义的顺序对这些星星进行排序

然后使用for循环来处理星星

示例代码:

class Stars():
     def order(match):
          return match.y, match.x
     # Find all icons 
     icons = findAll("stars.png")
     # Sort all the stars. 
     sorted_icons = sorted(icons, key=order)
     # Click on every star. 
     for icon in sorted_icons:
          click(icon)

谢谢你的回答。但我不知道行数。我只知道列数,是5。