Advertisement
Guest User

Automation

a guest
Nov 16th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.89 KB | None | 0 0
  1. import time
  2. import pyautogui
  3. from PIL import Image
  4. import numpy as np
  5. from cv2 import cv2
  6.  
  7. # Основная функция по поиску изображения
  8. def find_img(template_path,sleep):
  9.     img_rgb = pyautogui.screenshot() # Скриншот текущего экрана
  10.     img_rgb.save('D:/images/general.jpg')  
  11.     img_rgb = cv2.imread('D:/images/general.jpg') # Шаблон
  12.  
  13.     template = cv2.imread(template_path)  # Изображение, искомое в шаблоне
  14.  
  15.  
  16.     res = cv2.matchTemplate(img_rgb, template, cv2.TM_CCOEFF_NORMED)
  17.  
  18.     if str(res[0]) != '[1. 1. 1. ... 1. 1. 1.]':
  19.         threshold = .7
  20.  
  21.         loc = np.where(res >= threshold)  
  22.         # Проверка совпадения
  23.         for pt in zip(*loc[::-1]):
  24.             x = int(pt[0])
  25.             y = int(pt[1])
  26.             print(x, y) # Координаты совпадения
  27.             pyautogui.moveTo(x, y, duration=0.7)  # Переход по координатам
  28.             pyautogui.click()
  29.             time.sleep(sleep)
  30.             break
  31.         else:
  32.             global a
  33.             a = False
  34.             print(a)
  35.  
  36. # Пропуск заданий
  37. def skip():
  38.     pyautogui.click()
  39.     time.sleep(2.5)
  40.  
  41.  
  42. # Запуск и начало игры
  43. def startgame():
  44.     find_img('D:/images/icon.jpg', 3)
  45.     find_img('D:/images/play_bn.jpg', 25)
  46.     skip()
  47.     find_img('D:/images/play.jpg',3)
  48.     find_img('D:/images/play2.jpg',45)
  49.     find_img('D:/images/confirm.jpg',5)
  50.  
  51. startgame()
  52.  
  53.  
  54. a == False
  55. while a == False:
  56.     find_img('D:/images/hunter_hero_power.jpg',0.5)
  57.     find_img('D:/images/end_turn.jpg',0.5)
  58.     a == True
  59.     while a == True:
  60.         find_img('D:/images/hunter_hero_power.jpg',0)
  61.         find_img('D:/images/end_turn.jpg',0)
  62.     else:
  63.         time.sleep(5)
  64.     find_img('D:/images/defeat.jpg',0)
  65.     find_img('D:/images/win.jpg', 0)
  66.  
  67.  
  68. # Надо сделать так, чтобы пока на экране не было картинки "Победа" или "Поражение" (D:/images/defeat.jpg или D:/images/win.jpg) последовательно выполнялось следующее:
  69. # Проверялось, если сейчас имеется картинка "Закончить ход" (То есть идёт ли сейчас наш ход(D:/images/end_turn.jpg)), и поочерёдно выполнялся поиск и нажатие на кнопку силы героя
  70. # (find_img('D:/images/hunter_hero_power.jpg', 0)) и нажатие на кнопку закончить ход (find_img('D:/images/end_turn.jpg',0)) затем ожидание (постоянный поиск картинки "Закончить ход")
  71. # и повторение до тех пор, пока не появится картинка "Поражение" или "Победа"(D:/images/defeat.jpg или D:/images/win.jpg)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement