python3 游戏挂机脚本
作者:互联网
import pyautogui
import numpy as np
import PIL
import pytesseract
from PIL import Image
import time
import cv2
import os
import keyboard
# find_list = ['img/wait.jpg', 'img/end.jpg']
genting_list = ['img/Genting/start.bmp', 'img/Genting/commit.bmp', 'img/Genting/exit.bmp', 'img/Genting/next.bmp']
equip_list = ['img/Genting/equip1.bmp', 'img/Genting/equip2.bmp']
def find_on_template(target, template):
theight, twidth = target.shape[:2]
result = cv2.matchTemplate(target, template, cv2.TM_SQDIFF_NORMED)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
# 如果匹配度小于99%,就认为没有找到。
if min_val > 0.1:
return None
strmin_val = str(min_val)
# print(strmin_val)
x = min_loc[0] + twidth // 2
y = min_loc[1] + theight // 2
return x, y
def get_position(image):
# target = cv2.imread("target.jpg")
target = pyautogui.screenshot()
target = np.array(target)
template = cv2.imread(image, dtype=np.uint8)
# 获得模板图片的高宽尺寸
theight, twidth = template.shape[:2]
# 执行模板匹配,采用的匹配方式cv2.TM_SQDIFF_NORMED
result = cv2.matchTemplate(target, template, cv2.TM_SQDIFF_NORMED)
# 归一化处理
cv2.normalize(result, result, 0, 1, cv2.NORM_MINMAX, -1)
# 寻找矩阵(一维数组当做向量,用Mat定义)中的最大值和最小值的匹配结果及其位置
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
return min_loc[0] + twidth / 2, min_loc[1] + theight / 2
def find_and_click(target_dir, left_or_right):
screen_img = pyautogui.screenshot()
screen_img = cv2.cvtColor(np.asarray(screen_img), cv2.COLOR_RGB2BGR)
target = cv2.imread(target_dir)
position = find_on_template(target, screen_img)
if position is not None:
if left_or_right:
pyautogui.moveTo(position[0], position[1])
pyautogui.mouseDown()
time.sleep(0.1)
pyautogui.mouseUp()
else:
pyautogui.rightClick(x=position[0], y=position[1])
# pyautogui.moveTo(position[0], position[1])
# pyautogui.click()
return True
return False
def auto_genting():
switch_on = False
cards_list = make_img_list('img/Genting/cards')
screenWidth, screenHeight = pyautogui.size()
while True:
if keyboard.is_pressed('f5'):
print('开始!')
switch_on = True
elif keyboard.is_pressed('f6'):
print('结束!')
switch_on = False
if switch_on:
pyautogui.moveTo(screenWidth/2, screenHeight/2)
# 开始or结束
for each in genting_list:
find_and_click(each, True)
# 装备
for each in equip_list:
find_and_click(each, False)
# 搜牌
for each in cards_list:
find_and_click(each, True)
# 时间节点
def make_img_list(img_dir):
res = []
for root, dirs, files in os.walk(img_dir):
for file in files:
res.append(os.path.join(root, file))
return res
if __name__ == '__main__':
auto_genting()
# main()
标签:pyautogui,target,img,min,list,cv2,挂机,python3,游戏 来源: https://blog.csdn.net/weixin_42126002/article/details/115708437