### Get Screen Resolution with pywin32 Source: https://github.com/haungwanjun/mhxy_fz/blob/master/README.md Retrieves the screen's width and height using pywin32. No specific setup required beyond importing the library. ```python def resolution(): # 获取屏幕分辨率 return win32api.GetSystemMetrics(0), win32api.GetSystemMetrics(1) ``` -------------------------------- ### Get MHXY Window Info with pywin32 Source: https://github.com/haungwanjun/mhxy_fz/blob/master/README.md Finds the '《梦幻西游》手游' window and returns its rectangular coordinates. Returns None if the window is not found. Requires pywin32. ```python def get_window_info(): wdname = u'《梦幻西游》手游' handle = win32gui.FindWindow(0, wdname) # 获取窗口句柄 if handle == 0: return None else: return win32gui.GetWindowRect(handle) ``` -------------------------------- ### Create Tkinter GUI for MHXY_FZ Source: https://github.com/haungwanjun/mhxy_fz/blob/master/README.md This snippet initializes the main Tkinter window and creates buttons for various game functions like 'Shimen', 'Bangpai', 'Baotu', 'Zhuagui', and 'Stop'. Each button is configured to trigger a specific function via a MyThread class when clicked. ```python # 启动 if __name__ == "__main__": screen_resolution = resolution() # print(screen_resolution) window_size = get_window_info() print(window_size) global is_start # 创建主窗口 root = tk.Tk() root.title("梦幻西游手游辅助") root.minsize(300, 250) root.maxsize(300, 250) # 创建按钮 button_shimen = tk.Button(root, text=u"师门", command=lambda: MyThread(shimen, window_size), width = 15,height = 2) button_shimen.place(relx=0.2, rely=0.15, width=200) button_shimen.pack() button_bangpai = tk.Button(root, text="帮派", command=lambda: MyThread(bang_pai, window_size), width = 15,height = 2) button_bangpai.place(relx=0.2, rely=0.35, width=200) button_bangpai.pack() button_baotu = tk.Button(root, text="宝图", command=lambda: MyThread(baotu,window_size), width = 15,height = 2) button_baotu.place(relx=0.4, rely=0.55, width=200) button_baotu.pack() button_zhuagui = tk.Button(root, text="带队抓鬼", command=lambda: MyThread(zhua_gui, window_size), width = 15,height = 2) button_zhuagui.place(relx=0.4, rely=0.65, width=100) button_zhuagui.pack() button_tingzhi = tk.Button(root,text=u"停止", command=lambda: MyThread(stop), width = 15,height = 2) button_tingzhi.place(relx=0.4, rely=0.85, width=200) button_tingzhi.pack() root.mainloop() ``` -------------------------------- ### MHXY Shimen Task Automation with PIL Source: https://github.com/haungwanjun/mhxy_fz/blob/master/README.md Automates the 'Shimen' (sect task) in MHXY by comparing captured screen regions with predefined image hashes. Requires PIL, ImageGrab, and custom functions like get_hash, hamming, get_posx, get_posy, and move_click. The function operates within the provided window_size. ```python #师门任务 def shimen(window_size): global is_start is_start = True topx, topy = window_size[0], window_size[1] # 使用按钮(比如是x1,y1,x2,y2) shiyong = Image.open("shiyong_jpg") shiyong_hash = get_hash(shiyong) # shiyong.show() # print(shiyong_hash) # 购买宠物(比如是x1,y1,x2,y2) goumai_cw = Image.open("goumai_cw_jpg") goumai_cw_hash = get_hash(goumai_cw) # 上交药品按钮(比如是x1,y1,x2,y2) shangjiao_yp = Image.open("shangjiao_yp_jpg") shangjiao_yp_hash = get_hash(shangjiao_yp) # 上交宠物按钮 shangjiao_cw = Image.open("shangjiao_cw_jpg") shangjiao_cw_hash = get_hash(shangjiao_cw) #师门任务栏 shimen = Image.open("shimen_jpg") shimen_hash = get_hash(shimen) # 师门任务栏 shimen_songxin = Image.open("shimen_songxin_jpg") shimen_songxin_hash = get_hash(shimen_songxin) # 药店购买 goumai_yp = Image.open("goumai_yp_jpg") goumai_yp_hash = get_hash(goumai_yp) # 商城购买 goumai_sc = Image.open("goumai_sc_jpg") goumai_sc_hash = get_hash(goumai_sc) i=0 count = 0 while is_start: time.sleep(2) i=i+1 print("第%i次循环" %i) # 使用按钮(635 ,510 ,710 ,540) img_shiyong = ImageGrab.grab((topx + get_posx(635, window_size), topy + get_posy(510, window_size), topx + get_posx(710, window_size), topy + get_posy(540, window_size))) # img_shiyong.show() # img_shiyong.save("shiyong.jpg") # print("显示图片") # print(get_hash(img_shiyong)) # print(hamming(get_hash(img_shiyong), shiyong_hash, 20)) if hamming(get_hash(img_shiyong), shiyong_hash, 20): move_click(topx + get_posx(670, window_size), topy + get_posy(525, window_size)) print("点击使用") time.sleep(3) continue # 购买宠物610,505,740,540 img_goumai_cw = ImageGrab.grab((topx + get_posx(610, window_size), topy + get_posy(505, window_size), topx + get_posx(740, window_size), topy + get_posy(540, window_size))) # img_goumai_cw.save("goumai_cw.jpg") if hamming(get_hash(img_goumai_cw), goumai_cw_hash, 20): move_click(topx +get_posx(680, window_size), topy + get_posy(520, window_size)) print("点击购买宠物") time.sleep(3) continue # 药店购买 570,470,700,505 img_goumai_yp = ImageGrab.grab((topx + get_posx(560, window_size), topy + get_posy(460, window_size), topx + get_posx(690, window_size), topy + get_posy(500, window_size))) # img_goumai_yp.save("goumai_yp.jpg") if hamming(get_hash(img_goumai_yp), goumai_yp_hash, 20): move_click(topx + get_posx(620, window_size), topy + get_posy(480, window_size)) print("点击购买药") time.sleep(3) continue # 商城购买 520,520,715,550 img_goumai_sc = ImageGrab.grab((topx + get_posx(580, window_size), topy + get_posy(515, window_size), topx + get_posx(715, window_size), topy + get_posy(550, window_size))) # img_goumai_sc.save("goumai_sc.jpg") if hamming(get_hash(img_goumai_sc), goumai_sc_hash, 20): move_click(topx + get_posx(650, window_size), topy + get_posy(530, window_size)) print("点击商城购买") time.sleep(3) continue ``` -------------------------------- ### Capture and Compare Game Interface Elements Source: https://github.com/haungwanjun/mhxy_fz/blob/master/README.md This Python code snippet captures images of game interface elements like 'submit item' and 'submit pet' buttons, calculates their perceptual hash, and compares it with pre-defined hashes using Hamming distance. If the distance is below a threshold, it simulates a click action. ```python # 上交药品按钮 600,470,700,500 img_shangjiao_yp = ImageGrab.grab((topx + get_posx(590, window_size), topy + get_posy(465, window_size), topx + get_posx(695, window_size), topy + get_posy(490, window_size))) # img_shangjiao_yp .save("shangjiao_yp.jpg") if hamming(get_hash(img_shangjiao_yp), shangjiao_yp_hash, 20): move_click(topx + get_posx(640, window_size), topy + get_posy(478, window_size)) print("点击药品上交") time.sleep(3) continue # 上交宠物按钮 img_shangjiao_cw = ImageGrab.grab((topx + get_posx(500, window_size), topy + get_posy(460, window_size), topx + get_posx(600, window_size), topy + get_posy(500, window_size))) # img_shangjiao_cw.save("shangjiao_cw.jpg") if hamming(get_hash(img_shangjiao_cw), shangjiao_cw_hash, 20): move_click(topx + get_posx(550, window_size), topy + get_posy(480, window_size)) print("点击宠物上交") time.sleep(3) continue # 师门任务按钮 img_shimen_songxin = ImageGrab.grab((topx + get_posx(620, window_size), topy + get_posy(345, window_size), topx + get_posx(700, window_size), topy + get_posy(370, window_size))) # img_shimen_songxin .save("shimen_songxin1.jpg") if hamming(get_hash(img_shimen_songxin), shimen_songxin_hash, 30): move_click(topx + get_posx(660, window_size), topy + get_posy(360, window_size)) print("点击师门任务") time.sleep(3) continue # # 师门任务栏 630,150,665,175 img_shimen = ImageGrab.grab((topx + get_posx(630, window_size), topy + get_posy(160, window_size), topx + get_posx(670, window_size), topy + get_posy(180, window_size))) # img_shimen.save("shimen.jpg") if hamming(get_hash(img_shimen), shimen_hash, 40): move_click(topx + get_posx(650, window_size), topy + get_posy(170, window_size)) print("点击师门任务栏") time.sleep(3) continue ``` -------------------------------- ### Simulate Mouse Click with pywin32 Source: https://github.com/haungwanjun/mhxy_fz/blob/master/README.md Moves the mouse cursor to the specified coordinates (x, y) and performs a left-click. Includes an optional delay. Requires pywin32 and time/random imports. ```python def move_click(x, y, t=0): # 移动鼠标并点击左键 win32api.SetCursorPos((x, y)) # 设置鼠标位置(x, y) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN | win32con.MOUSEEVENTF_LEFTUP, x, y, 0, 0) # 点击鼠标左键 if t == 0: time.sleep(random.random()*2+1) # sleep一下 else: time.sleep(t) return 0 ``` -------------------------------- ### Import and Use Base64 Encoded Images Source: https://github.com/haungwanjun/mhxy_fz/blob/master/README.md This code snippet demonstrates how to import base64 encoded image data from a generated Python file and decode it back into image files. It defines a function `get_pic` to handle the decoding and saving process, and then calls it for each image variable. ```python import base64 # 从.py文件获取图片 def get_pic(pic_code, pic_name): image = open(pic_name, 'wb') image.write(base64.b64decode(pic_code)) image.close() # 导入图片 get_pic(bangpai_jpg, 'bangpai_jpg') get_pic(bangpai_renwu_jpg, 'bangpai_renwu_jpg') get_pic(bangpai_renwu2_jpg, 'bangpai_renwu2_jpg') get_pic(goumai_cw_jpg, 'goumai_cw_jpg') get_pic(goumai_sc_jpg, 'goumai_sc_jpg') get_pic(goumai_yp_jpg, 'goumai_yp_jpg') get_pic(shangjiao_cw_jpg, 'shangjiao_cw_jpg') get_pic(shangjiao_yp_jpg, 'shangjiao_yp_jpg') get_pic(shimen_jpg, 'shimen_jpg') get_pic(shimen_songxin_jpg, 'shimen_songxin_jpg') get_pic(shiyong_jpg, 'shiyong_jpg') ``` -------------------------------- ### Convert Images to Python Files for PyInstaller Source: https://github.com/haungwanjun/mhxy_fz/blob/master/README.md This utility function converts image files into Python files by encoding them as base64 strings. This is useful for including image assets in PyInstaller executables. The script takes a list of image filenames and a desired output Python filename. ```python #!/usr/bin/env python # _*_ coding:utf-8 _*_ import base64 def pic2py(picture_names, py_name): """ 将图像文件转换为py文件 :param picture_name: :return: """ write_data = [] for picture_name in picture_names: filename = picture_name.replace('.', '_') open_pic = open("%s" % picture_name, 'rb') b64str = base64.b64encode(open_pic.read()) open_pic.close() # 注意这边b64str一定要加上.decode() write_data.append('%s = "%s"\n' % (filename, b64str.decode())) f = open('%s.py' % py_name, 'w+') for data in write_data: f.write(data) f.close() if __name__ == '__main__': pics = ["bangpai.jpg", "bangpai_renwu.jpg", "bangpai_renwu2.jpg", "goumai_cw.jpg","goumai_sc.jpg","goumai_yp.jpg","shangjiao_cw.jpg" ,"shangjiao_yp.jpg","shimen.jpg","shimen_songxin.jpg","shiyong.jpg","qiecuo.jpg", "qiecuo_yulin.jpg"] pic2py(pics, 'memory_pic') # 将pics里面的图片写到 memory_pic.py 中 print("ok") ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.