weixin_45536989
2019-09-13 12:08
采纳率: 40%
浏览 644

python调用一个函数里返回的变量不成功

我要写一个监控LOL击杀记录的功能,但是代码有问题,虽然可以监控到击杀记录,但是不能将监控的结果输出出来。

import win32gui
import win32api
import win32con
from win32gui import *
import time

from PIL import Image
from PIL import ImageGrab
import imagehash
import pymouse,pykeyboard,os,sys
from pymouse import *
from pykeyboard import PyKeyboard
import schedule
import cv2
import numpy as np
import matplotlib.pyplot as plt 
from threading import Thread,Lock
m = PyMouse()
k = PyKeyboard()
titles = set()
lock=Lock()
i=0
reward = 0

#获取电脑上的窗口句柄
def foo(hwnd,mouse):
    if IsWindow(hwnd) and IsWindowEnabled(hwnd) and IsWindowVisible(hwnd):
        titles.add(GetWindowText(hwnd))



def playGame():
    """Click the game icon in the simulator to enter and displays to the specified location"""
    EnumWindows(foo, 0)
    list = []
    for title in titles:
        if title:
           list.append(title)
    for title in list:
        a = 'League of Legends (TM) Client'
        if title.find(a) != -1:
            hwnd = win32gui.FindWindow(0,a)
            win32gui.SetWindowPos(hwnd, win32con.HWND_TOP,0,0,1282,790,win32con.SWP_SHOWWINDOW)
            hwnd = win32gui.FindWindow(0,a)
            size = win32gui.GetWindowRect(hwnd)
            #print(size)
            # 在模拟器点击游戏图标进入游戏
            #win32api.SetCursorPos([size[0] + 410, size[1] + 186])
            #win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP | win32con.MOUSEEVENTF_LEFTDOWN, 0, 0)
            #win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTUP | win32con.MOUSEEVENTF_RIGHTDOWN, 0, 0)
            #time.sleep(10)
            return size


def jianc():
    """Click to implement in the game"""

    # 点击我知道
    reward = 0
    size = playGame()
    #time.sleep(15)
    #print(size)
    topx, topy = size[0], size[1]
    #print(topx)
    #print(topy)
    ImageGrab.grab((topx + 496, topy + 125, topx + 787, topy + 155)).save(r'C:\Users\Administrator\Desktop\game\model/cheshi'+str(i)+'.jpg','JPEG')
    pic = np.array(Image.open(r'C:\Users\Administrator\Desktop\game\model/cheshi'+str(i)+'.jpg'))
    N = 125
    for a in range(pic.shape[0]):
        for j in range(pic.shape[1]):
            if (pic[a][j][0] > N)&(pic[a][j][1] > N)&(pic[a][j][2] > N):
                pic.itemset((a,j,0),255)
                pic.itemset((a,j,1),255)
                pic.itemset((a,j,2),255)
            else:
                pic.itemset((a,j,0),0)
                pic.itemset((a,j,1),0)
                pic.itemset((a,j,2),0)

    #plt.imshow(pic) 
    plt.axis('off')
    cv2.imencode('.jpg', pic)[1].tofile(r'C:\Users\Administrator\Desktop\game\model\检测/cheshi'+str(i)+'.jpg')
    #cv2.imwrite(r'C:\Users\Administrator\Desktop\game\model/检测/cheshi'+str(i)+'.jpg',pic) 
    #plt.savefig("result.jpg")
    #plt.show()
    #im.save(r'C:\Users\Administrator\Desktop\123/'+str(c)+'.jpg','JPEG') #设置保存路径和图片格式
    hash_size = 6
    hash1 = imagehash.average_hash(Image.open(r'C:\Users\Administrator\Desktop\game\model/检测/cheshi'+str(i)+'.jpg'), hash_size=hash_size)
    hash2 = imagehash.average_hash(Image.open(r'C:\Users\Administrator\Desktop\game\截图\击杀了一名.png'), hash_size=hash_size)
    hash3 = imagehash.average_hash(Image.open(r'C:\Users\Administrator\Desktop\game\截图\你已经被击杀.png'), hash_size=hash_size)
    hash4 = imagehash.average_hash(Image.open(r'C:\Users\Administrator\Desktop\game\截图\摧毁防御塔.png'), hash_size=hash_size)
    hash5 = imagehash.average_hash(Image.open(r'C:\Users\Administrator\Desktop\game\截图\双杀.png'), hash_size=hash_size)
    hash6 = imagehash.average_hash(Image.open(r'C:\Users\Administrator\Desktop\game\截图\三杀.png'), hash_size=hash_size)
    a = (1 - (hash1 - hash2) / len(hash1.hash) ** 2)
    a1 = (1 - (hash1 - hash3) / len(hash1.hash) ** 2)
    a2 = (1 - (hash1 - hash4) / len(hash1.hash) ** 2)
    a3 = (1 - (hash1 - hash5) / len(hash1.hash) ** 2)
    a4 = (1 - (hash1 - hash6) / len(hash1.hash) ** 2)
    #print("你被击杀了",a1)
    if a > 0.76 and a<0.83:
        reward=1
        print("你击杀了",reward,"人!!!")
        #win32api.SetCursorPos([topx + 290, topy + 310])
        #win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP | win32con.MOUSEEVENTF_LEFTDOWN, 0, 0)
        #win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTUP | win32con.MOUSEEVENTF_RIGHTDOWN, 0, 0)
    elif a1>0.84:
        reward=-1
        print("你被击杀了",reward,"人!!!")
        return reward
    elif a2>0.9:
        reward=1
        print("你摧毁了防御塔",reward,"人!!!")
    elif a3>1:
        reward=2
        print("你双杀了","人!!!")
    elif a4>1:
        reward=3
        print("你三杀了","人!!!")
        return reward
    #
    #
def main(reward):
    n=0
    print(reward)
    schedule.every(4).seconds.do(jianc)#执行定时截屏
    while True:                                     #循环
        schedule.run_pending()
        n = n+1
        time.sleep(4)
        reward=jianc()
        return reward




if __name__ == '__main__':
    #print(reward)
    lock.acquire()
    l=0
    while True:                                     #循环
        l = l+1
        time.sleep(4)
        main(reward)
        print('得分',reward)
    lock.release()

这就是代码,识别准确率比较低,但是最重要的是最后得到的结果不能输出,
我想要的是,识别一张图片输出一个reward,然后在识别下一个图片。
应该怎么改呀,大佬们。

if a > 0.76 and a reward=1
print("你击杀了",reward,"人!!!")
#win32api.SetCursorPos([topx + 290, topy + 310])
#win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP | win32con.MOUSEEVENTF_LEFTDOWN, 0, 0)
#win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTUP | win32con.MOUSEEVENTF_RIGHTDOWN, 0, 0)
elif a1>0.84:
reward=-1
print("你被击杀了",reward,"人!!!")
return reward
elif a2>0.9:
reward=1
print("你摧毁了防御塔",reward,"人!!!")
elif a3>1:
reward=2
print("你双杀了","人!!!")
elif a4>1:
reward=3
print("你三杀了","人!!!")
return reward

            这一段代码怎么改才能把reward返回出来,求大佬出来指点一下




            把循环拿到外面就好了
import win32gui
import win32api
import win32con
from win32gui import *
import time

from PIL import Image
from PIL import ImageGrab
import imagehash
import pymouse,pykeyboard,os,sys
from pymouse import *
from pykeyboard import PyKeyboard
import schedule
import cv2
import numpy as np
import matplotlib.pyplot as plt 
from threading import Thread,Lock
m = PyMouse()
k = PyKeyboard()
titles = set()
lock=Lock()
i=0
#reward = 0

#获取电脑上的窗口句柄
def foo(hwnd,mouse):
    if IsWindow(hwnd) and IsWindowEnabled(hwnd) and IsWindowVisible(hwnd):
        titles.add(GetWindowText(hwnd))



def playGame():
    """Click the game icon in the simulator to enter and displays to the specified location"""
    EnumWindows(foo, 0)
    list = []
    for title in titles:
        if title:
           list.append(title)
    for title in list:
        a = 'League of Legends (TM) Client'
        if title.find(a) != -1:
            hwnd = win32gui.FindWindow(0,a)
            win32gui.SetWindowPos(hwnd, win32con.HWND_TOP,0,0,1282,790,win32con.SWP_SHOWWINDOW)
            hwnd = win32gui.FindWindow(0,a)
            size = win32gui.GetWindowRect(hwnd)
            #print(size)
            # 在模拟器点击游戏图标进入游戏
            #win32api.SetCursorPos([size[0] + 410, size[1] + 186])
            #win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP | win32con.MOUSEEVENTF_LEFTDOWN, 0, 0)
            #win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTUP | win32con.MOUSEEVENTF_RIGHTDOWN, 0, 0)
            #time.sleep(10)
            return size


def jianc():
    """Click to implement in the game"""

    # 点击我知道
    #reward = 0
    size = playGame()
    #time.sleep(15)
    #print(size)
    topx, topy = size[0], size[1]
    #print(topx)
    #print(topy)
    ImageGrab.grab((topx + 496, topy + 125, topx + 787, topy + 155)).save(r'C:\Users\Administrator\Desktop\game\model/cheshi'+str(i)+'.jpg','JPEG')
    pic = np.array(Image.open(r'C:\Users\Administrator\Desktop\game\model/cheshi'+str(i)+'.jpg'))
    N = 125
    for a in range(pic.shape[0]):
        for j in range(pic.shape[1]):
            if (pic[a][j][0] > N)&(pic[a][j][1] > N)&(pic[a][j][2] > N):
                pic.itemset((a,j,0),255)
                pic.itemset((a,j,1),255)
                pic.itemset((a,j,2),255)
            else:
                pic.itemset((a,j,0),0)
                pic.itemset((a,j,1),0)
                pic.itemset((a,j,2),0)

    #plt.imshow(pic) 
    plt.axis('off')
    cv2.imencode('.jpg', pic)[1].tofile(r'C:\Users\Administrator\Desktop\game\model\检测/cheshi'+str(i)+'.jpg')
    #cv2.imwrite(r'C:\Users\Administrator\Desktop\game\model/检测/cheshi'+str(i)+'.jpg',pic) 
    #plt.savefig("result.jpg")
    #plt.show()
    #im.save(r'C:\Users\Administrator\Desktop\123/'+str(c)+'.jpg','JPEG') #设置保存路径和图片格式
    hash_size = 6
    hash1 = imagehash.average_hash(Image.open(r'C:\Users\Administrator\Desktop\game\model/检测/cheshi'+str(i)+'.jpg'), hash_size=hash_size)
    hash2 = imagehash.average_hash(Image.open(r'C:\Users\Administrator\Desktop\game\截图\击杀了一名.png'), hash_size=hash_size)
    hash3 = imagehash.average_hash(Image.open(r'C:\Users\Administrator\Desktop\game\截图\你已经被击杀.png'), hash_size=hash_size)
    hash4 = imagehash.average_hash(Image.open(r'C:\Users\Administrator\Desktop\game\截图\摧毁防御塔.png'), hash_size=hash_size)
    hash5 = imagehash.average_hash(Image.open(r'C:\Users\Administrator\Desktop\game\截图\双杀.png'), hash_size=hash_size)
    hash6 = imagehash.average_hash(Image.open(r'C:\Users\Administrator\Desktop\game\截图\三杀.png'), hash_size=hash_size)


    return hash1,hash2,hash3,hash4,hash5,hash6
    #
    #
def main(hash1,hash2,hash3,hash4,hash5,hash6):
    a = (1 - (hash1 - hash2) / len(hash1.hash) ** 2)
    a1 = (1 - (hash1 - hash3) / len(hash1.hash) ** 2)
    a2 = (1 - (hash1 - hash4) / len(hash1.hash) ** 2)
    a3 = (1 - (hash1 - hash5) / len(hash1.hash) ** 2)
    a4 = (1 - (hash1 - hash6) / len(hash1.hash) ** 2)
    #print("你被击杀了",a1)
    return a,a1,a2,a3,a4




def defen():
    #print(reward)


    schedule.every(4).seconds.do(jianc)#执行定时截屏
    #lock.acquire()
    reward=0
    hash1,hash2,hash3,hash4,hash5,hash6=jianc()
    a,a1,a2,a3,a4=main(hash1,hash2,hash3,hash4,hash5,hash6)

        #time.sleep(4)
        #reward=jianc()                                     #循环

    if a > 0.76 and a<0.83:
        reward=1
            #print("你击杀了",reward,"人!!!")
        #win32api.SetCursorPos([topx + 290, topy + 310])
        #win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP | win32con.MOUSEEVENTF_LEFTDOWN, 0, 0)
        #win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTUP | win32con.MOUSEEVENTF_RIGHTDOWN, 0, 0)
    elif a1>0.84:
        reward=-1
            #print("你被击杀了",reward,"人!!!")
        #return reward
    elif a2>0.9:
        reward=1
            #print("你摧毁了防御塔",reward,"人!!!")
    elif a3>1:
        reward=2
            #print("你双杀了","人!!!")
    elif a4>1:
        reward=3
            #print("你三杀了","人!!!")
    else :
        #print(reward)
        reward=0





        #print('得分',a,a1,a2,a3,a4,reward)
    #time.sleep(4)
    #lock.release()
    return reward


if __name__ == '__main__':
    l=0
    while True:                                     #循环
        schedule.run_pending()
        l = l+1
        #defen()
        reward=defen()
        #print(reward)
        time.sleep(4)

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dabocaiqq 2019-09-14 21:30
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题