【问题标题】:Sikuli: Multiple icon sortingSikuli:多图标排序
【发布时间】:2015-03-12 04:07:04
【问题描述】:

目标:通过在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")

【问题讨论】:

    标签: python image sorting sikuli


    【解决方案1】:

    您可以做出一个定义,明确您喜欢如何穿越星星。 屏幕本身有一个 x.y 位置。要从左上角开始并在右下角结束,您需要 匹配.y,匹配.x。

    然后你需要 findAll("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)
    

    【讨论】:

      【解决方案2】:

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

      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 值进行排序,并将它们附加到一个列表中,然后继续进行接下来的五个,依此类推。

      如果事先不知道网格的大小,您可以通过几种不同的方式来完成此操作—— 如果您的网格总是完全正方形,您可以找到星星数的平方根:

       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
      

      说到底,这将为您完成工作,但@Tenzin 的回答更优雅:)

      【讨论】:

      • 谢谢您的回答。但我不知道行数。我只知道列数,也就是5。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-15
      • 1970-01-01
      • 2018-12-06
      • 1970-01-01
      • 1970-01-01
      • 2017-09-20
      • 2013-01-02
      相关资源
      最近更新 更多