一回の陰陽師の掛け機のシナリオの開発を覚えます
13852 ワード
最近同僚と一绪に阴阳师を游んで、このゲームが多すぎて操作を缲り返していることに気づいて、これは完全に生命を浪费します;だからpythonで自動マウントスクリプトを書きたいです.
最初は簡単に考えましたが、該当するボタンをずっと探してクリックすればいいです.だからpyautoguiの画像の位置決めとクリック機能を直接使えばいいです.確かに実現しました.コードは以下の通りです.
また、消費されるシステムリソースも大幅に削減され(単一スレッドの場合よりも低く)、コードも簡単すぎます.
転載先:https://www.cnblogs.com/RottenLeaf/p/10017782.html
最初は簡単に考えましたが、該当するボタンをずっと探してクリックすればいいです.だからpyautoguiの画像の位置決めとクリック機能を直接使えばいいです.確かに実現しました.コードは以下の通りです.
import pyautogui,time
pyautogui.FAILSAFE = True
'''PyAutoGUI 。 pyautogui.FAILSAFE = True , ,
PyAutoGUI pyautogui.FailSafeException , '''
time.sleep(2)
def get_point(picture):
''' ; , , '''
picture = './img/' + picture
count = 5
while count > 0:
point = pyautogui.locateCenterOnScreen(picture)
if point is not None:
return point
else:
count -= 1
def get_range(picture):
''' , ,
, , , '''
picture = './img/' + picture
count = 5
while count > 0:
range = pyautogui.locateCenterOnScreen(picture, grayscale=True, confidence=0.5)
if range is not None:
return range
else:
count -= 1
def click_button(picture,accurate=True):
''' , True, , accurate=False, , '''
if accurate==True:
action = get_point(picture)
else:
action = get_range(picture)
if action is not None:
pyautogui.click(action,duration=0.5)
def cycle_fight():
''' , , '''
while True:
click_button('tiaozhan.PNG')
click_button('zhunbei.PNG')
click_button('over.PNG')
cycle_fight()
, ; , , , :
import pyautogui,time
pyautogui.FAILSAFE = True
'''PyAutoGUI 。 pyautogui.FAILSAFE = True , ,
PyAutoGUI pyautogui.FailSafeException , '''
time.sleep(2)
def get_point(picture):
''' ; , , '''
picture = './img/' + picture
count = 5
while count > 0:
point = pyautogui.locateCenterOnScreen(picture)
if point is not None:
return point
else:
count -= 1
def get_range(picture):
''' , ,
, , , '''
picture = './img/' + picture
count = 5
while count > 0:
range = pyautogui.locateCenterOnScreen(picture, grayscale=True, confidence=0.5)
if range is not None:
return range
else:
count -= 1
def click_button(picture,accurate=True):
''' , True, , accurate=False, , '''
if accurate==True:
action = get_point(picture)
else:
action = get_range(picture)
if action is not None:
pyautogui.click(action,duration=0.5)
return True # True
else:
return None #
def on_fight(sec):
''' 【 】 【 】 , , , '''
fight_run = click_button('zhunbei.PNG') #
if fight_run is not None: # , 【 】
count = 0
while True:
time.sleep(sec) # ,
fight_over = click_button('over.PNG')
if fight_over is not None:
return True
else:
count +=1 #
if count*sec > 600:
''' , , 10 , ,
, , , ,
'''
exit(1)
else:
return None # 【 】 , ,
def cycle_fight(sec):
''' , , '''
while True:
click_button('tiaozhan.PNG')
on_fight(sec)
def story_task():
''' '''
while True:
click_button('tiaoguo.PNG')
click_button('dialogue.PNG')
click_button('storyJump.PNG')
def explore_task(sec):
''' '''
while True:
click_button('fight.PNG',accurate=False)
click_button('masterFight.PNG',accurate=False)
on_fight(sec)
#explore_task()
# cycle_fight(10)
story_task()
, , :
import pyautogui,time
import threading
pyautogui.FAILSAFE = True
'''PyAutoGUI 。 pyautogui.FAILSAFE = True , ,
PyAutoGUI pyautogui.FailSafeException , '''
time.sleep(2)
class FindButton(threading.Thread):
def __init__(self, picture):
super(FindButton, self).__init__()
self.picture = './img/' + picture
def run(self):
while True:
self.point = pyautogui.locateCenterOnScreen(self.picture, confidence=0.8)
if self.point is not None:
pyautogui.click(self.point, duration=0.5)
challenge = FindButton('tiaozhan.PNG')
prepare = FindButton('zhunbei.PNG')
over = FindButton('over.PNG')
challenge.start()
prepare.start()
over.start()
, , , 。 , , ;
( , ),
また、消費されるシステムリソースも大幅に削減され(単一スレッドの場合よりも低く)、コードも簡単すぎます.
import gevent
import pyautogui
def click(picture):
picture = './img/' + picture
while True:
point = pyautogui.locateCenterOnScreen(picture, confidence=0.8)
if point is None:
gevent.sleep(1)
else:
pyautogui.click(point, duration=0.5)
def cycle_fight():
''' , , '''
gevent.joinall([ # joinall
gevent.spawn(click,'tiaozhan.PNG'), # :( , )
gevent.spawn(click,'zhunbei.PNG'),
gevent.spawn(click,'over.PNG')
])
def story_task():
'''' '''
gevent.joinall([
gevent.spawn(click, 'dialogue.PNG'),
gevent.spawn(click, 'tiaoguo.PNG'),
gevent.spawn(click, 'storyJump.PNG'),
gevent.spawn(click, 'fight.PNG'),
gevent.spawn(click, 'zhunbei.PNG'),
gevent.spawn(click, 'over.PNG')
])
def explore_task():
''' '''
gevent.joinall([
gevent.spawn(click, 'fight.PNG'),
gevent.spawn(click, 'masterFight.PNG'),
gevent.spawn(click, 'zhunbei.PNG'),
gevent.spawn(click, 'over.PNG')
])
cycle_fight()
転載先:https://www.cnblogs.com/RottenLeaf/p/10017782.html