接下來這幾天狀態應該都很糟,糟糕到我真的想放棄了,連同事都來關心我臉色很差,總之能寫一篇就算一篇吧
接下來我想要指定操作的視窗大小,這樣模擬器不管有多大,都可以隨著大小做調整
那麼就先來寫一個Listener
from pynput.mouse import Listener
class Game_Listener(Listener):
def __init__(self, q_cont_rsp):
Listener.__init__(self, on_click=self.on_click)
self.q_cont_rsp = q_cont_rsp
self.time = 0
# self.listen_click = False
def on_click(self, x, y, button, pressed):
# print(x, y, button, button.name, pressed)
if button.name == 'left' and pressed == False:
self.time += 1
self.q_cont_rsp.put(['on_click', self.time, x, y])
監聽click狀態,並且一樣透過Queue傳送出去
再來寫一個Controller
class Game_Controller():
def __init__(self, q_cont_rsp):
self.q_cont_rsp = q_cont_rsp
def game_listen(self):
self.game_listener = Game_Listener(self.q_cont_rsp)
self.game_listener.daemon = True
self.game_listener.name = 'game_listener'
self.game_listener.start()
def game_listen_stop(self):
self.game_listener.stop()
這邊要注意的是,Listener本身有繼承Thread套件,因為我這邊也不是官方的寫法,所以需要自行控制thread功能,也就是要寫start()和stop()指令
好這邊你們可能會有個疑問(其實並沒有),根本沒有控制甚麼東西啊,取名叫Controller好像有點不太對,不過之後也會用pynput來控制鍵鼠,所以也打算寫在這邊
那接下來我們要使用on_click這個功能
main
class Application(Thread):
range_x1:int
range_y1:int
range_x2:int
range_y2:int
def __init__(self, gui_rsp, cont_rsp) -> None:
Thread.__init__(self)
self.q_gui_rsp = gui_rsp
self.q_cont_rsp = cont_rsp
def run(self):
while True:
if not self.q_gui_rsp.empty():
msg = self.q_gui_rsp.get(True, 0.5)
self.handle_gui_msg(msg)
if not self.q_cont_rsp.empty():
msg = self.q_cont_rsp.get(True, 0.5)
self.handle_cont_msg(msg)
sleep(0.1)
def handle_gui_msg(self, msg):
global cont
if msg == 'set_range':
cont.game_listen()
def handle_cont_msg(self, msg):
global gui, cont
if type(msg) == list:
if msg[0] == 'on_click':
# print(msg)
if msg[1] == 1:
self.range_x1, self.range_y1 = msg[2], msg[3]
elif msg[1] == 2:
self.range_x2, self.range_y2 = msg[2], msg[3]
# cont.game_listen_stop()
gui.set_range_result(self.range_x1, self.range_y1, self.range_x2, self.range_y2)
if __name__ == '__main__':
q_gui_rsp = Queue()
q_cont_rsp = Queue()
app = Application(q_gui_rsp, q_cont_rsp)
cont = Game_Controller(q_cont_rsp)
app.start()
gui = GUI_Handler(q_gui_rsp)
gui.window.mainloop()
gui
def set_range(self):
self.q_gui_rsp.put("set_range")
self.button_set_range.config(text="setting")
pass
def set_range_result(self, x1, y1, x2, y2):
self.context_range_start.config(text = str(x1)+","+str(y1))
self.context_range_end.config(text = str(x2)+","+str(y2))
self.button_set_range.config(text="set range")
self.button_set_range['state'] = 'normal'
接著運行起來試試
好了 明天可能要針對畫面進行辨識了