import pyautogui
import keyboard
def fixed_loop():
print(' 循环选择待命')
while True:
if keyboard.is_pressed('4'):
Royal = ['Royal.png', 'Royal1.png', 'Royal2.png', 'Royal3.png', 'Royal4.png', 'Royal5.png', 'Royal6.png']
x = 0
num = 0
for i in Royal:
pos = pyautogui.locateCenterOnScreen(Royal[num], grayscale=False, confidence=0.6, region=(50, 55, 310, 230))
num += 1
if pos is not None:
x = x + 1
print(x)
if x >= 1:
cloak_image = pyautogui.locateCenterOnScreen('Cloak.png', grayscale=False, confidence=0.6, region=(50, 55, 310, 230))
if cloak_image is None:
pyautogui.sleep(0.3)
print(' 有披风有红衣')
pyautogui.typewrite(['d', 'd', 'd', 'd'], 0.1)
pyautogui.typewrite(['w', 'w', 'w', 'w'], 0.1)
pyautogui.typewrite(['e', 'e', 'e', 'e'], 0.1)
pyautogui.typewrite(['f', 'f', 'f', 'f'], 0.1)
pyautogui.typewrite(['q', 'q', 'q', 'q'], 0.1)
print(' 固定起手结束')
break
if cloak_image is not None:
pyautogui.sleep(0.3)
print(' 无披风有红衣')
pyautogui.typewrite(['d', 'd', 'd', 'd'], 0.1)
pyautogui.typewrite(['w', 'w', 'w', 'w'], 0.1)
pyautogui.typewrite(['f', 'f', 'f', 'f'], 0.1)
pyautogui.typewrite(['q', 'q', 'q', 'q'], 0.1)
print(' 固定起手结束')
break
if x == 0:
cloak_image = pyautogui.locateCenterOnScreen('Cloak.png', grayscale=False, confidence=0.6, region=(50, 55, 310, 230))
if cloak_image is not None:
pyautogui.sleep(0.3)
print(' 无披风无红衣')
pyautogui.typewrite(['d', 'd', 'd', 'd'], 0.1)
pyautogui.typewrite(['r', 'r', 'r', 'r'], 0.1)
pyautogui.typewrite(['w', 'w', 'w', 'w'], 0.1)
pyautogui.typewrite(['f', 'f', 'f', 'f'], 0.1)
pyautogui.typewrite(['q', 'q', 'q', 'q'], 0.1)
print(' 固定起手结束')
break
if cloak_image is None:
pyautogui.sleep(0.3)
print(' 有披风无红衣')
pyautogui.typewrite(['d', 'd', 'd', 'd'], 0.1)
pyautogui.typewrite(['r', 'r', 'r', 'r'], 0.1)
pyautogui.typewrite(['w', 'w', 'w', 'w'], 0.1)
pyautogui.typewrite(['e', 'e', 'e', 'e'], 0.1)
pyautogui.typewrite(['f', 'f', 'f', 'f'], 0.1)
pyautogui.typewrite(['q', 'q', 'q', 'q'], 0.1)
print(' 固定起手结束')
break
if keyboard.is_pressed('5'):
print(' 进入智能循环')
break
else:
pyautogui.sleep(0.1)
if __name__ == '__main__':
fixed_loop()
def smart_loop():
while True:
FrostD = ['D.png', 'D1.png', 'D2.png', 'D3.png']
FrostF = ['F.png', 'F1.png', 'F2.png', 'F3.png']
FrostR = ['R.png', 'R1.png', 'R2.png', 'R3.png']
d = 0
f = 0
r = 0
num = 0
for i in FrostR:
pos = pyautogui.locateCenterOnScreen(FrostR[num], grayscale=False, confidence=0.8, region=(880, 1000, 80, 80))
num += 1
if pos is not None:
r = r + 1
num = 0
for i in FrostD:
pos = pyautogui.locateCenterOnScreen(FrostD[num], grayscale=False, confidence=0.8, region=(970, 1000, 80, 80))
num += 1
if pos is not None:
d = d + 1
num = 0
for i in FrostF:
pos = pyautogui.locateCenterOnScreen(FrostF[num], grayscale=False, confidence=0.8, region=(1050, 1000, 80, 80))
num += 1
if pos is not None:
f = f + 1
print(d, f, r)
if d >= 1:
pyautogui.sleep(0.4)
pyautogui.typewrite(['d', 'd', 'd', 'd'], 0.1)
if f >= 1:
pyautogui.sleep(0.4)
pyautogui.typewrite(['w', 'w', 'w', 'w'], 0.1)
pyautogui.typewrite(['f', 'f', 'f', 'f'], 0.1)
if r >= 1:
pyautogui.sleep(0.4)
pyautogui.typewrite(['r', 'r', 'r', 'r'], 0.1)
else:
pyautogui.typewrite(['q', 'q', 'q', 'q'], 0.01)
if keyboard.is_pressed('3'):
print(" 智能循环结束")
fixed_loop()
if keyboard.is_pressed('ctrl'):
print(' 暂停智能循环')
pyautogui.sleep(1)
while True:
if keyboard.is_pressed('ctrl'):
pyautogui.sleep(0.3)
else:
print(' 继续智能循环')
smart_loop()
if __name__ == '__main__':
smart_loop()
百度自学能力有限,需求就是上面的功能,简化高效的写法有没有.可加100
将D_use = pyautogui.locateCenterOnScreen(FrostD[num], grayscale=True, confidence=0.9, region=(970, 1000, 80, 80))修改为D_use = pyautogui.locateCenterOnScreen(FrostD[num], grayscale=True, confidence=0.9, region=(970, 1000, 80, 80))
将pos = pyautogui.locateCenterOnScreen(FrostF[num], grayscale=True, confidence=0.9, region=(970, 1000, 80, 80))修改为pos = pyautogui.locateCenterOnScreen(FrostF[num], grayscale=True, confidence=0.9, region=(970, 1000, 80, 80))
import pyautogui
import keyboard
# 固定循环函数
def fixed_loop():
print('循环选择待命')
while True:
# 检测键盘按键
if keyboard.is_pressed('4'):
Royal = ['Royal.png', 'Royal1.png', 'Royal2.png', 'Royal3.png', 'Royal4.png', 'Royal5.png', 'Royal6.png']
x = 0
y = 0
num = 0
# 在Royal列表中逐个查找图片位置
for i in Royal:
pos = pyautogui.locateCenterOnScreen(Royal[num], grayscale=True, confidence=0.6, region=(50, 55, 310, 230))
num += 1
if pos is None:
y = y + 1
if pos is not None:
x = x + 1
print(x, y)
if x >= 1 and y <= 6:
cloak_image = pyautogui.locateCenterOnScreen('Cloak.png', grayscale=True, confidence=0.6, region=(50, 55, 310, 230))
if cloak_image is None:
pyautogui.sleep(0.3)
print('有披风有红衣')
pyautogui.typewrite(['d', 'd', 'd', 'd'], 0.1)
pyautogui.typewrite(['w', 'w', 'w', 'w'], 0.1)
pyautogui.typewrite(['e', 'e', 'e', 'e'], 0.1)
pyautogui.typewrite(['f', 'f', 'f', 'f'], 0.1)
pyautogui.typewrite(['q', 'q', 'q', 'q'], 0.1)
print('固定起手结束')
break
if cloak_image is not None:
pyautogui.sleep(0.3)
print('无披风有红衣')
pyautogui.typewrite(['d', 'd', 'd', 'd'], 0.1)
pyautogui.typewrite(['w', 'w', 'w', 'w'], 0.1)
pyautogui.typewrite(['f', 'f', 'f', 'f'], 0.1)
pyautogui.typewrite(['q', 'q', 'q', 'q'], 0.1)
print('固定起手结束')
break
if x == 0 and y == 7:
cloak_image = pyautogui.locateCenterOnScreen('Cloak.png', grayscale=True, confidence=0.6, region=(50, 55, 310, 230))
if cloak_image is not None:
pyautogui.sleep(0.3)
print('无披风无红衣')
pyautogui.typewrite(['d', 'd', 'd', 'd'], 0.1)
pyautogui.typewrite(['r', 'r', 'r', 'r'], 0.1)
pyautogui.typewrite(['w', 'w', 'w', 'w'], 0.1)
pyautogui.typewrite(['f', 'f', 'f', 'f'], 0.1)
pyautogui.typewrite(['q', 'q', 'q', 'q'], 0.1)
print('固定起手结束')
break
if cloak_image is None:
pyautogui.sleep(0.3)
print('有披风无红衣')
pyautogui.typewrite(['d', 'd', 'd', 'd'], 0.1)
pyautogui.typewrite(['r', 'r', 'r', 'r'], 0.1)
pyautogui.typewrite(['w', 'w', 'w', 'w'], 0.1)
pyautogui.typewrite(['e', 'e', 'e', 'e'], 0.1)
pyautogui.typewrite(['f', 'f', 'f', 'f'], 0.1)
pyautogui.typewrite(['q', 'q', 'q', 'q'], 0.1)
print('固定起手结束')
break
if keyboard.is_pressed('5'):
print('进入智能循环')
break
else:
pyautogui.sleep(0.1)
if __name__ == '__main__':
fixed_loop()
# 智能循环函数
def smart_loop():
while True:
FrostD = ['D.png', 'D1.png', 'D2.png', 'D3.png']
FrostF = ['F.png', 'F1.png', 'F2.png', 'F3.png']
FrostR = ['R.png', 'R1.png', 'R2.png', 'R3.png']
d = 0
dd = 0
f = 0
ff = 0
r = 0
rr = 0
num = 0
# 在FrostD列表中逐个查找图片位置
for i in FrostD:
D_use = pyautogui.locateCenterOnScreen(FrostD[num], grayscale=True, confidence=0.9, region=(970, 1000, 80, 80))
num += 1
if D_use is None:
d = d + 1
if D_use is not None:
dd = dd + 1
# 在FrostF列表中逐个查找图片位置
for i in FrostF:
pos = pyautogui.locateCenterOnScreen(FrostF[num], grayscale=True, confidence=0.9, region=(970, 1000, 80, 80))
num += 1
if pos is None:
f = f + 1
if pos is not None:
ff = ff + 1
# 在FrostR列表中逐个查找图片位置
for i in FrostR:
pos = pyautogui.locateCenterOnScreen(FrostR[num], grayscale=True, confidence=0.9, region=(970, 1000, 80, 80))
num += 1
if pos is None:
r = r + 1
if pos is not None:
rr = rr + 1
print(d, dd, f, ff, r, rr)
if d >= 4 and dd <= 0:
pyautogui.sleep(0.4)
pyautogui.typewrite(['d', 'd', 'd', 'd'], 0.1)
print('发现D技能,准备按下')
if f >= 4 and ff <= 0:
pyautogui.sleep(0.4)
pyautogui.typewrite(['f', 'f', 'f', 'f'], 0.1)
print('发现F技能,准备按下')
if r >= 4 and rr <= 0:
pyautogui.sleep(0.4)
pyautogui.typewrite(['r', 'r', 'r', 'r'], 0.1)
print('发现R技能,准备按下')
pyautogui.sleep(0.1)
if __name__ == '__main__':
smart_loop()
不知道你这个问题是否已经解决, 如果还没有解决的话: 通过cv2的Template Matching算法,匹配到小图在大图的位置,使用pyautogui模拟按钮操作。
由于问题本身没有给出具体的代码,无法直接对其进行优化,下面给出一些通用方法,希望能较大程度上对你的代码进行优化:
尽量避免使用鼠标和键盘的控制,比如使用API来替代鼠标键盘操作,这样可以大大减少延时和误差。
避免使用全局变量,全局变量会影响代码的可读性和可维护性。如果需要使用全局变量,可以使用类的实例变量来代替。
尽量使用向量、矩阵运算,这样可以将代码的时间复杂度降低至o(1)。
将一些常用的函数封装为类,比如计算器类、表格类、图表类等等,这样可以使得代码的模块化更加明显、更易于调试。
尽量避免使用递归,递归调用会占用大量的栈空间,容易引起栈溢出。
在使用函数的时候,不要使用过多的参数,使用指针代替这些过多的参数可以大大提高函数的效率。
最后需要注意的是优化代码并不是使用一种通用性的方法就可以完毕,优化需要根据具体情况具体分析,可以针对性的进行一些小优化,从而使得代码运行的效率更高。
你这个代码总体来说还可以的撒,主要增加一些注释,同时把变量命名规范一下,便于后续维护修改就可以了
个人觉得的可以优化改进的地方:
一个是空格缩进问题,同一个代码块之间不需要多空一行,紧挨着就行;
while循环最好使用一个布尔变量来控制,直接使用True,如果代码逻辑不对,容易进入死循环;
我看你使用到了很多for循环:
判断一个变量是否为NOne 直接 if cloak_image 即可。
我做了以下优化:
import pyautogui
import keyboard
# 固定循环选手
def fixed_loop():
print('循环选择待命')
while True:
# 按4键启动固定循环
if keyboard.is_pressed('4'):
# 查找图片获取选手信息
Royal = ['Royal.png', 'Royal1.png', 'Royal2.png', 'Royal3.png', 'Royal4.png', 'Royal5.png', 'Royal6.png']
x = 0
num = 0
for i in Royal:
pos = pyautogui.locateCenterOnScreen(Royal[num], grayscale=False, confidence=0.6,
region=(50, 55, 310, 230))
num += 1
if pos is not None:
x += 1
print(x)
# 判断选手信息 执行对应操作
if x >= 1:
cloak_image = pyautogui.locateCenterOnScreen('Cloak.png', grayscale=False,
confidence=0.6, region=(50, 55, 310, 230))
if cloak_image is None:
pyautogui.typewrite(['d', 'd', 'd', 'd'], 0.1)
pyautogui.typewrite(['w', 'w', 'w', 'w'], 0.1)
pyautogui.typewrite(['e', 'e', 'e', 'e'], 0.1)
pyautogui.typewrite(['f', 'f', 'f', 'f'], 0.1)
pyautogui.typewrite(['q', 'q', 'q', 'q'], 0.1)
print('固定起手结束')
break
# 省略其他判断...
# 按5键退出固定循环
if keyboard.is_pressed('5'):
print('进入智能循环')
break
else:
pyautogui.sleep(0.1)
# 智能循环选手
def smart_loop():
while True:
# 查找技能图片
FrostD = ['D.png', 'D1.png', 'D2.png', 'D3.png']
FrostF = ['F.png', 'F1.png', 'F2.png', 'F3.png']
FrostR = ['R.png', 'R1.png', 'R2.png', 'R3.png']
d, f, r = 0, 0, 0
num = 0
# 判断技能使用情况
for i in FrostR:
pos = pyautogui.locateCenterOnScreen(FrostR[num], grayscale=False, confidence=0.8,
region=(880, 1000, 80, 80))
num += 1
if pos is not None:
r += 1
# 省略其他技能判断...
print(d, f, r)
# 根据技能使用情况执行操作
if d >= 1:
pyautogui.typewrite(['d', 'd', 'd', 'd'], 0.1)
if f >= 1:
pyautogui.typewrite(['w', 'w', 'w', 'w'], 0.1)
pyautogui.typewrite(['f', 'f', 'f', 'f'], 0.1)
if r >= 1:
pyautogui.typewrite(['r', 'r', 'r', 'r'], 0.1)
else:
pyautogui.typewrite(['q', 'q', 'q', 'q'], 0.01)
# 按3键退出智能循环
if keyboard.is_pressed('3'):
print("智能循环结束")
fixed_loop()
# 按ctrl键暂停循环
if keyboard.is_pressed('ctrl'):
print('暂停智能循环')
while True:
if keyboard.is_pressed('ctrl'):
pyautogui.sleep(0.3)
else:
print('继续智能循环')
smart_loop()
if __name__ == '__main__':
smart_loop()