-
Notifications
You must be signed in to change notification settings - Fork 0
/
Screen.py
185 lines (150 loc) · 5.46 KB
/
Screen.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# coding=utf-8
import time
import cv2
import numpy
import pyautogui
import win32api
import win32gui
from PIL import ImageGrab
from GameStatus import *
from ImageLoader import ImageLoader
class Screen(metaclass=Singleton):
_delay = 0.3
_imageLoader = ImageLoader('images/')
target = ImageLoader('./', need_scale=False)
@staticmethod
def log(text):
GameStatus().window.add_text(text)
def __init__(self):
t = ImageGrab.grab().convert("RGB")
self.screen = cv2.cvtColor(numpy.array(t), cv2.COLOR_RGB2BGR)
# self.ultLoader = ImageLoader('image/ult/')
# if self.have('topleft'):
# tl = self._imageLoader.get('topleft')
# res = cv2.matchTemplate(self.screen, tl, cv2.TM_CCOEFF_NORMED)
#
# min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
# x1, y1 = max_loc
# rd = self._imageLoader.get('rightdown')
# res = cv2.matchTemplate(self.screen, rd, cv2.TM_CCOEFF_NORMED)
# min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
# x2, y2 = max_loc
# # default 989
# GameStatus().y = y2 - y1
# GameStatus().use_Droid4X = True
def get_cards(self):
self.capture()
GameStatus().cards = []
GameStatus().cards += self.find_list('buster')
GameStatus().cards += self.find_list('art')
GameStatus().cards += self.find_list('quick')
def get_current_level(self):
c33 = self.chances_of('3_3')
c23 = self.chances_of('2_3')
c31 = self.chances_of('1_3')
if c33 > max(c23, c31):
self.log('3-3')
return 3
elif c23 > max(c33, c31):
self.log('2-3')
return 2
else:
self.log('1-3')
return 1
def find_list(self, name):
cards = []
res = cv2.matchTemplate(self.screen, self._imageLoader.get(name), cv2.TM_CCOEFF_NORMED)
threshold = 0.8
loc = numpy.where(res >= threshold)
x = 0
t = sorted(zip(*loc[::-1]))
for pt in t:
if abs(x - pt[0]) > 100 or x == 0:
x = pt[0]
cards.append((pt[0], pt[1]))
else:
continue
self.log(name + ': ' + str(len(cards)))
return cards
def set_delay(self, delay):
self._delay = delay
def capture(self):
t = ImageGrab.grab().convert("RGB")
self.screen = cv2.cvtColor(numpy.array(t), cv2.COLOR_RGB2BGR)
@staticmethod
def _click(x, y):
handle = win32gui.GetForegroundWindow()
x_old, y_old = win32api.GetCursorPos()
# see https://github.com/asweigart/pyautogui/issues/23
try:
pyautogui.click(x, y, 1)
except FileNotFoundError:
pass
win32api.SetCursorPos((x_old, y_old))
win32gui.SetForegroundWindow(handle)
def drag_to(self, name, loader=_imageLoader, offset=(0, 0)):
if GameStatus().game_stage == GameStage.Stopped:
return
self.log('try drag from ' + name)
p = loader.get(name)
pass
def drag_to(self, name, loader=_imageLoader, offset=(0, 0)):
if GameStatus().game_stage == GameStage.Stopped:
return
self.log('try drag' + name)
p = loader.get(name)
max_val = 0
x, y = 0, 0
while max_val < 0.8:
if GameStatus().game_stage == GameStage.Stopped:
return
self.capture()
res = cv2.matchTemplate(self.screen, p, cv2.TM_CCOEFF_NORMED)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
self.log(name + ' ' + str(max_val))
x, y = max_loc
time.sleep(self._delay)
m, n, q = p.shape
x += n / 2
y += m / 2
pyautogui.moveTo(x, y, 0)
pyautogui.dragTo(0, 200)
def click_on(self, name, repeat=False, loader=_imageLoader, offset=(0, 0)):
if GameStatus().game_stage == GameStage.Stopped:
return
self.log('try click ' + name)
p = loader.get(name)
max_val = 0
x, y = 0, 0
while max_val < 0.8:
if GameStatus().game_stage == GameStage.Stopped:
return
self.capture()
res = cv2.matchTemplate(self.screen, p, cv2.TM_CCOEFF_NORMED)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
self.log(name + ' ' + str(max_val))
x, y = max_loc
time.sleep(self._delay)
m, n, q = p.shape
x += n / 2
y += m / 2
self._click(x + offset[0], y + offset[1])
max_val = 1 if repeat else 0
while max_val > 0.8:
if GameStatus().game_stage == GameStage.Stopped:
return
time.sleep(1)
self.capture()
res = cv2.matchTemplate(self.screen, p, cv2.TM_CCOEFF_NORMED)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
if max_val > 0.8:
self._click(x + offset[0], y + offset[1])
def have(self, name, loader=_imageLoader):
return self.chances_of(name, loader) > 0.8
def chances_of(self, name, loader=_imageLoader):
self.capture()
p = loader.get(name)
res = cv2.matchTemplate(self.screen, p, cv2.TM_CCOEFF_NORMED)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
self.log('chances of ' + name + ': ' + str(max_val))
return max_val