Day2
那昨天实现了这个自动挖土,我发现这个yb也是很扯0的东西,所以今天简单优化优化,完了再简单优化一下双手,写个yb吧。
首先依旧是库一小堆儿
from PIL import Image
import pyautogui
import random
import pygetwindow as gw
import time
然后那既然是优化那肯定是面向对象优化了,对吧,那就简单封装一下吧。
那窗口函数会吧?
def get_window(title):
return gw.getWindowsWithTitle(title)[0]
那输出新button尺寸也会吧?
def resize_image(original_path, scale_x, scale_y, save_path):
image = Image.open(original_path)
new_size = (int(image.width * scale_x), int(image.height * scale_y))
resized_image = image.resize(new_size, Image.ANTIALIAS)
resized_image.save(save_path)
return save_path
再写个小小的随机时延也会吧?
def random_delay(min_delay, max_delay):
delay = random.uniform(min_delay, max_delay)
time.sleep(delay)
return delay
最后一个简单的click函数也会吧?
当然后期还需要增加双击的操作。(如果有人帮我写那就更好了
def click_target(image_path, offset_x_ratio, offset_y_ratio, randomSacle_x, randomSacle_y, clickTimes, confidenceThreshold, duration = 5):
start_time = time.time()
end_time = start_time + duration
click_count = 0
while time.time() < end_time and click_count < clickTimes:
try:
match = pyautogui.locateOnScreen(image_path, confidence=confidenceThreshold)
if match:
print(f"找到一个匹配位置:{match}")
random_x = 2 * random.random() - 1
random_y = 2 * random.random() - 1
clickPos_x = match.left + match.width * offset_x_ratio + random_x * randomSacle_x
clickPos_y = match.top + match.height * offset_y_ratio + random_y * randomSacle_y
pyautogui.click(clickPos_x, clickPos_y, button='left')
click_count += 1
except Exception as e:
print("Error:", e)
time.sleep(1)
print("已经完成")
那这边我是考虑到了以后可能会有更复杂的这个任务所以集成了一个多顺序按钮的这么一个函数,当然g了一下
def click_sequence(click_operations: List[Tuple[str, float, float, float, float, int, float, int]], max_successes: int):
success_count = 0
while success_count < max_successes:
for operation in click_operations:
image_path, offset_x_ratio, offset_y_ratio, randomScale_x, randomScale_y, clickTimes, confidenceThreshold, duration = operation
click_target(image_path, offset_x_ratio, offset_y_ratio, randomScale_x, randomScale_y, clickTimes, confidenceThreshold, duration)
time.sleep(random_delay(0.5, 3))
success_count += 1
print(f"已完成{max_successes}次数的循环点击")
然后最简单的main函数会写吧?
def main():
window = get_window('叉叉xy')
eventButtonPath = resize_image(r"E:\footbook\eventButton1.png", window.width/1132, window.height/667, 'E:\\footbook\\eventButton_newtest1.png')
yunbiaoButton1Path = resize_image(r"E:\footbook\yunbiaoButton1.png", window.width/1095, window.height/646, 'E:\\footbook\\yunbiaoButton_newtest1.png')
yunbiaoButton2Path = resize_imagexx(r"E:\footbook\yunbiaoButton2.png", window.width/1094, window.height/645, 'E:\\footbook\\yunbiaoButton_newtest2.png')
yunbiaoButton3Path = resize_image(r"E:\footbook\yunbiaoButton3.png", window.width/1094, window.height/645, 'E:\\footbook\\yunbiaoButton_newtest3.png')
click_target(eventButtonPath, 0.5, 0.8, 5, 2, 1, 0.7)
time.sleep(random_delay(0.5, 3))
click_target(yunbiaoButton1Path, 0.8, 0.5, 5, 2, 1, 0.85)
# time.sleep(random_delay(0.5, 3))
# click_target(yunbiaoButton2Path, 0.5, 0.5, 30, 5, 1, 0.85, 300) # 设置了时间限制为300秒
# time.sleep(random_delay(0.5, 3))
# click_target(yunbiaoButton3Path, 0.5, 0.5, 20, 5, 1, 0.85, 300) # 设置了时间限制为300秒
click_operations = [
(yunbiaoButton2Path, 0.5, 0.5, 30, 5, 1, 0.85, 300),
(yunbiaoButton3Path, 0.5, 0.5, 20, 5, 1, 0.85, 100)
]
click_sequence(click_operations, 3)
loop_clicks(yunbiaoButton2Path, yunbiaoButton3Path, 0.5, 0.5, 30, 5, 0.85, 350, 2)
if __name__ == "__main__":
main()
完事儿!!!
那上面其中注释部分就是我一开始没有做这个集成函数直接用这个click写的这么一个过程,但是发现只能yb一次对吧,但是咱们的yb是有三次的嘛,对吧。那今天就搞到这了。下班!!!
标签:Jupyter,python,0.5,image,random,Day2,delay,time,click From: https://blog.csdn.net/qq_52933098/article/details/139747611