Skip to content

Commit

Permalink
自己的方法
Browse files Browse the repository at this point in the history
  • Loading branch information
TsingWei committed Oct 2, 2019
1 parent a73cee9 commit d04d31f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
20 changes: 17 additions & 3 deletions automator.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from target import TargetType
from cv import UIMatcher
import uiautomator2 as u2
import time,random


class Automator:
Expand All @@ -23,10 +24,23 @@ def start(self):
# 简单粗暴的方式,处理 “XX之光” 的荣誉显示。
# 当然,也可以使用图像探测的模式。
self.d.click(550, 1650)

self._upgrade(2)
self._upgrade(4)
# self._upgrade(random.randint(1,9))
# self._upgrade(random.randint(1,9))
# 滑动屏幕,收割金币。
self._swipe()

def _upgrade(self,id):
self.d.click(1000, 1100)
sx, sy=self._get_position(id)
self.d.click(sx, sy)
time.sleep(0.5)
self.d.click(860, 1760)
time.sleep(0.5)
self.d.click(1000, 1100)


def _swipe(self):
"""
滑动屏幕,收割金币。
Expand All @@ -35,7 +49,7 @@ def _swipe(self):
# 横向滑动,共 3 次。
sx, sy = self._get_position(i * 3 + 1)
ex, ey = self._get_position(i * 3 + 3)
self.d.swipe(sx, sy, ex, ey)
self.d.swipe(sx-100, sy+70, ex, ey)

@staticmethod
def _get_position(key):
Expand Down Expand Up @@ -67,7 +81,7 @@ def _match_target(self, target: TargetType):
"""
# 获取当前屏幕快照
screen = self.d.screenshot(format="opencv")

# 由于 OpenCV 的模板匹配有时会智障,故我们探测次数实现冗余。
counter = 6
while counter != 0:
Expand Down
8 changes: 8 additions & 0 deletions cv.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ def match(screen, target: TargetType):
# 获取对应货物的图片。
# 有个要点:通过截屏制作货物图片时,请在快照为实际大小的模式下截屏。
template = cv2.imread(target.value)
# cv2.imshow(screen,1)
height=len(screen)
width=len(screen[0])
screen = screen[int(0.7*height):height,int(0.5*width):width]
cv2.namedWindow("Image")
cv2.imshow("Image", screen)
cv2.waitKey(0)
cv2.destroyAllWindows()
# 获取货物图片的宽高。
th, tw = template.shape[:2]

Expand Down
14 changes: 14 additions & 0 deletions target.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,17 @@ class TargetType(Enum):
Wood = 'targets/Wood.jpg'
Food = 'targets/Food.jpg'
Box = 'targets/Box.jpg'

# Bag = 'targets/Bag.jpg'
# Iron = 'targets/Iron.jpg'
# Grass = 'targets/Grass.jpg'
# Sofa = 'targets/Sofa.jpg'
# Book = 'targets/Book.jpg'
# Plant = 'targets/Plant.jpg'
# Quilt = 'targets/Quilt.jpg'
# Shoes = 'targets/Shoes.jpg'
# Dogfood = 'targets/Dogfood.jpg'
# Chicken = 'targets/Chicken.jpg'
# Tool = 'targets/Tool.jpg'
# Microphone = 'targets/Microphone.jpg'
# Oil = 'targets/Oil.jpg'

0 comments on commit d04d31f

Please sign in to comment.