作者doghib (lun)
看板Python
标题[范例] MSN宝石方块机器人
时间Sat Jan 26 22:22:45 2008
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