Python 板


LINE

import psyco psyco.full() import wx, win32api, win32con, time from PIL import Image, ImageEnhance, ImageGrab #从像素中找出颜色 def ColorTable(pixel): if pixel[0] == pixel[1] == pixel[2]: return 'W' elif pixel[0] >200: if pixel[2] == 0: if pixel[1] == 0: return 'R' elif pixel[1] == 255: return 'Y' elif pixel[1] > 0: return 'O' elif pixel[2] > 200: return 'M' else: return '?' elif pixel[1] == 255: if pixel[0] == pixel[2] == 0: return 'G' elif pixel[2] == 255: return 'C' else: return '?' else: return '?' class MyFrame(wx.Frame): def __init__(self): wx.Frame.__init__(self, None, title="MSN", size=(424,768), pos=(0,0), style=wx.DEFAULT_FRAME_STYLE ^ wx.MAXIMIZE_BOX | wx.STAY_ON_TOP ) p = wx.Panel(self) self.going = True self.box = (618, 205, 978, 565) self.find = False self.pos = '' self.last = '' self.allow = 0 #开始钮 start = wx.Button(p, -1, "Start", pos=(200, 700)) self.Bind(wx.EVT_BUTTON, self.OnStart, start) def OnStart(self, evt): #初始位置设定(画面为1024 * 768,MSN宝石方块往左拉到最大) x = 640 y = 225 while 1: #影响处理 self.ImageHandle() self.last = self.pos self.find = False time.sleep(2) def Move(self): if self.find: win32api.SetCursorPos([x + int(self.pos[0][0]*45), y + int(self.pos[0][1]*45)]) time.sleep(0.4) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0) time.sleep(0.1) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0) win32api.SetCursorPos([x + int(self.pos[1][0]*45), y + int(self.pos[1][1]*45)]) time.sleep(0.2) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0) time.sleep(0.1) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0) else: time.sleep(1) def ImageHandle(self): #从画面撷取影像,效果同等於 PrintScreen Im = ImageGrab.grab(self.box) #颜色加强 color = ImageEnhance.Color(Im) im = color.enhance(25) count = 0 double_check = {} V_table = {} for i in range(8): V_table[i] = [] y = 25 i = 0 #从撷取下来的影像中,依照特定位置获得像素,并传回颜色 while y < im.size[1]: x = 22 while x < im.size[0]: V_table[i].append(ColorTable(im.getpixel((x, y)))) #如果颜色为红色、没有、或是未知,则要做进一步确认(橘色加强後可 能判断为红色) if V_table[i][-1] == 'R' or not V_table[i][-1] or V_table[i][-1] == '?': double_check[count] = {} #纪录哪一列、第几个、与画面位置 double_check[count]['row'] = i double_check[count]['index'] = len(V_table[i]) -1 double_check[count]['pos'] = ((x, y)) count += 1 x += 45 i += 1 y += 45 #加强系数降低,使颜色判别有更多条件 im = color.enhance(5) i = 0 while i < count: pos = double_check[i]['pos'] t = ColorTable(im.getpixel((pos[0] - 3, pos[1]-17))) #将确认之後结果回传 V_table[double_check[i]['row']][double_check[i]['index']] = t i += 1 #寻找配对之结果,先寻找纵向(上下三个),再寻找横向(左右三个) self.Find(7, 7, V_table) self.Find(7, 0, V_table) self.Find(0, 7, V_table) self.Find(0, 0, V_table) if self.find: self.pos = ((self.pos[0][1], self.pos[0][0]), (self.pos[1][1], self.pos[1][0])) return H_table = {} for i in range(8): H_table[i] = [] for j in range(8): H_table[i].append(V_table[j][i]) self.Find(7, 7, H_table) self.Find(7, 0, H_table) self.Find(0, 7, H_table) self.Find(0, 0, H_table) #寻找配对,宝石方块为8*8,再此分割成为4个4*4,并利用类似象限分法判别 def Find(self, X, Y, table): y = 0 if X == 0: x_sign = 1 else: x_sign = -1 if Y == 0: y_sign = 1 else: y_sign = -1 while y < 4 and not self.find: y = abs(Y - y) x = 0 while x < 4 and not self.find: x = abs(X - x) if table[x][y] == table[x+1*x_sign][y]: #1 xxox if table[x][y] == table[x + 3*x_sign][y]: self.find = True self.pos = ((x + 2*x_sign, y), (x + 3*x_sign, y)) #2 xxo # x elif table[x][y] == table[x+2*x_sign][y+1*y_sign]: self.find = True self.pos = ((x + 2*x_sign, y), (x + 2*x_sign, y + 1*y_sign)) if (x > 0 and x_sign == 1) or (x < 7 and x_sign == -1): #3 oxx # x if table[x][y] == table[x -1*x_sign][y+1*y_sign]: self.find = True self.pos = ((x - 1*x_sign, y), (x - 1*x_sign, y + 1*y_sign)) if (x > 1 and x_sign == 1) or (x < 6 and x_sign == -1): #4 xoxx if table[x][y] == table[x-2*x_sign][y]: self.find = True self.pos = ((x-1*x_sign, y), (x-2*x_sign, y)) if (y > 0 and y_sign == 1) or (y < 7 and y_sign == -1): #5 x # xxo if table[x][y] == table[x+2*x_sign][y-1*y_sign]: self.find = True self.pos = ((x + 2*x_sign, y), (x + 2*x_sign, y - 1*y_sign)) if (x > 0 and x_sign == 1) or (x < 7 and x_sign == -1): #6 x # o xx if table[x][y] == table[x -1*x_sign][y -1*y_sign]: self.find = True self.pos = ((x -1*x_sign, y), (x -1*x_sign, y - 1*y_sign)) elif table[x][y] == table[x + 2*x_sign][y]: #1 xox # x if table[x][y] == table[x + 1*x_sign][y + 1*y_sign]: self.find = True self.pos = ((x + 1*x_sign, y), (x + 1*x_sign, y + 1*y_sign)) #2 x # xox if (y > 0 and y_sign == 1) or (y < 7 and y_sign == -1): if table[x][y] == table[x + 1*x_sign][y - 1*y_sign]: self.find = True self.pos = ((x + 1*x_sign, y), (x + 1*x_sign, y - 1*y_sign)) if self.find: if self.pos == self.last: if self.allow < 2: self.allow += 1 else: self.find = False self.allow = 0 else: self.allow = 0 x = abs(x - X) x += 1 y = abs(y - Y) y += 1 app = wx.PySimpleApp() frm = MyFrame() frm.Show() app.MainLoop() 程式跑一跑大概五万多就挂了,这边大概提到怎麽撷取萤幕还有一些影像处理动作 这边只写了基本的一种比对,给大家参考参考 不过程式要结束比较麻烦因为他一直在跑回圈... 最好是用直接点两下的方式开启,要结束Alt + Tab & Ctrl + F4结束掉 --



※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 122.127.53.38 ※ 编辑: doghib 来自: 122.127.53.38 (01/26 22:29)
1F:推 rs6000:虽然看不懂~不过还是支持大大:) 01/26 23:04
2F:推 blc:如果可以对应directx的游戏… (邪笑) 01/27 10:32







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:Gossiping站内搜寻

TOP