-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
106 lines (84 loc) · 3.21 KB
/
main.py
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import pyautogui
import cv2
import numpy as np
import time
def locate_using_image(input_image):
# Take a screenshot and convert to grayscale
screenshot = pyautogui.screenshot()
screenshot = cv2.cvtColor(np.array(screenshot), cv2.COLOR_RGB2GRAY)
result = cv2.matchTemplate(screenshot, input_image, cv2.TM_CCOEFF_NORMED)
return cv2.minMaxLoc(result)
def locate_and_click_element_by_image(image_path, confidence=0.6):
try:
# Read the button image and convert to grayscale
input_image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
input_image_height, input_image_width = input_image.shape[:2]
min_val, max_val, min_loc, max_loc = locate_using_image(input_image=input_image)
if max_val >= confidence:
print(f"Button found with confidence {max_val:.2f}")
center_x = max_loc[0] + input_image_width // 2
center_y = max_loc[1] + input_image_height // 2
pyautogui.moveTo(center_x, center_y)
pyautogui.click()
print("Button clicked!")
return True
else:
print("Button not found.")
return False
except pyautogui.ImageNotFoundException:
print("Button image not found on the screen.")
return False
except cv2.error as e:
print(f"OpenCV error: {e}")
return False
def locate_by_image(image_path, confidence=0.6):
try:
# Read the button image and convert to grayscale
input_image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
min_val, max_val, min_loc, max_loc = locate_using_image(input_image=input_image)
if max_val >= confidence:
print(f"Image found with confidence {max_val:.2f}")
return True
else:
print("Image not found.")
return False
except pyautogui.ImageNotFoundException:
print("Image not found on the screen.")
return False
except cv2.error as e:
print(f"OpenCV error: {e}")
return False
def locate_hero(hero_name):
try:
pyautogui.write(hero_name)
pyautogui.press('enter')
print(f"Typed {hero_name} and enter.")
for _ in range(len(hero_name)):
pyautogui.press('backspace')
except Exception as e:
print(f"An error occurred: {e}")
def check_game_acceptance():
image_path = f'static/before_pick.png'
return locate_by_image(image_path=image_path, confidence=0.7)
def accept_game(language):
image_path = f'static/accept_button_{language}.png'
while True:
locate_and_click_element_by_image(image_path=image_path, confidence=0.6)
if check_game_acceptance():
break
time.sleep(1)
def check_hero_pick():
image_path = f'static/after_pick.png'
return locate_by_image(image_path=image_path, confidence=0.7)
def pick_hero():
image_path = f'static/pick_hero_button.png'
while True:
# Set full name of your preferred hero
locate_hero("pudge")
locate_and_click_element_by_image(image_path=image_path, confidence=0.7)
if check_hero_pick():
break
if __name__ == "__main__":
# Dota language (Ukrainian/English)
accept_game(language="ukr")
pick_hero()