#coding=utf-8
from PIL import Image
import win32api
import win32gui
import win32ui
import win32con
import math

_useless = [(237,237,237),(247,247,247)]
_squareWidth,_squareHeight,_blocka = 23,15,28.3
_range = 4      #避免重复分的正方形版块大小
_intervalConflict= 600      #3*3格内的点击相隔时间 ms
_interval = 60     #实际点击的时间间隔 ms

def ColorComp(color1,color2):
    deltaR = abs(color1[0]-color2[0])
    deltaG = abs(color1[1]-color2[1])
    deltaB = abs(color1[2]-color2[2])
    dvalue =  math.sqrt(deltaR*deltaR + deltaG*deltaG + deltaB*deltaB)
    return dvalue<50

def getImpx():
    hwnd = 0
    hwndDC = win32gui.GetWindowDC(hwnd)
    mfcDC = win32ui.CreateDCFromHandle(hwndDC)
    saveDC = mfcDC.CreateCompatibleDC()
    saveBitmap = win32ui.CreateBitmap()
    MonitorDev = win32api.EnumDisplayMonitors(None,None)
    w,h=MonitorDev[0][2][2],MonitorDev[0][2][3]
    saveBitmap.CreateCompatibleBitmap(mfcDC,w,h)
    saveDC.SelectObject(saveBitmap)
    saveDC.BitBlt((0,0),(w,h),mfcDC,(0,0),win32con.SRCCOPY)
    saveBitmap.SaveBitmapFile(saveDC,"_.bmp")
    im= Image.open("_.bmp")
    return im.load()

def isInColorset(c1,cs):
    for c in cs:
        if ColorComp(c1,c):
            return True
    return False



while True:
    print u'please start game in 5 secs,move mouse to left top block'
    win32api.Sleep(5000)
    firstx,firsty = win32api.GetCursorPos()
    impx = getImpx()
    colorset = []
    square = [[0 for l in range(_squareHeight)] for l in range(_squareWidth)]
    for i in range(_squareWidth):
        for j in range(_squareHeight):
            thiscolor = impx[firstx + int(round(_blocka * i)),firsty + int(round(_blocka * j))]
            if thiscolor in _useless:
                square[i][j] = -1
                continue            
            if not isInColorset(thiscolor,colorset):
                colorset.append(thiscolor)
            for k,v in enumerate(colorset):                
                if ColorComp(thiscolor,v):
                    square[i][j] = k
    if(len(colorset)!=10):
        print len(colorset)
        print "wrong mouse position,plz start again"
        raw_input()
    else:
        break


allValidPoints = []
while True:
    endGame = True
    empty = True
    for i in range(_squareWidth):
        for j in range(_squareHeight):
            if(square[i][j]!=-1):
                empty = False
                break
        if not empty:
            break
    if empty:
        break    
    curValidPoints = []

           
    for i1 in range(_range):
        for j1 in range(_range):
            for i2 in range(0,_squareWidth,_range):
                for j2 in range(0,_squareHeight,_range):
                    i,j = i1+i2,j1+j2
                    if(i>=_squareWidth or j>=_squareHeight):
                        continue
                    if(square[i][j]!=-1):
                        continue
                    cl,cr,cu,cd = 0,0,0,0
                    bl,br,bu,bd = -1,-1,-1,-1
                    pl,pr,pu,pd = (-1,-1),(-1,-1),(-1,-1),(-1,-1)

                    cl = i - 1
                    while cl>=0:
                        if square[cl][j]!=-1:
                            bl = square[cl][j]
                            pl = (cl,j)
                            break
                        else:
                            cl = cl-1
                    cr = i+1
                    while cr<_squareWidth:
                        if(square[cr][j]!=-1):
                           br = square[cr][j]
                           pr = (cr,j)
                           break
                        else:
                            cr = cr+1
                    cu = j -1
                    while cu>=0:
                        if(square[i][cu]!=-1):
                            bu = square[i][cu]
                            pu = (i,cu)
                            break
                        else:
                            cu = cu -1
                    cd = j+1
                    while cd<_squareHeight:
                        if(square[i][cd]!=-1):
                            bd = square[i][cd]
                            pd = (i,cd)
                            break
                        else:
                            cd = cd +1
                    temp = (bl,br,bu,bd)
                    temp1 = (pl,pr,pu,pd)
                    #print i,j,temp

                    p = lambda x:sum([1 for m in temp if m==x])

                    found = False
                    fx = []
                    for k in temp:
                        if(k!=-1 and (p(k)==2 or p(k)==4)):
                            found = True
                            fx.append(k)
                    if found:
                        #print i,j,temp,temp1
                        endGame = False
                        curValidPoints.append(((i,j),fx,temp1))                
               
                        for n,k in enumerate(temp):
                            if(k in fx):
                                square[temp1[n][0]][temp1[n][1]] = -1
    allValidPoints.append(curValidPoints)
    if(endGame):
        break

def click(x,y):
    win32api.SetCursorPos((x,y))
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,0,0,0,0)
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,0,0,0,0)

def distanceSI(orderedS,time):
    oLen  = len(orderedS)
    allTimes = [time for l in range(oLen)]
    dis = lambda x,y: (abs(x[0]-y[0])<=3 and abs(x[1]-y[1])<=3) 
    
    resultS = []

    for i in range(oLen):
        if(i>0):
            for j in range(i-1,-1,-1):
                totaltime = reduce(lambda x,y:x+y,allTimes[j+1:i+1])
                if(totaltime>_intervalConflict):            ##总计时间超出冲突的最大时间可不处理
                    break
                if(dis(orderedS[i],orderedS[j])):      
                    deltaTime = _intervalConflict
                    allTimes[i] += deltaTime
                    break
        resultS.append((orderedS[i],allTimes[i]))
    return resultS

if(len(allValidPoints)>0):
    tier = 1
    li,lj = 0,0
    allSquaresForClick = [[0 for l in range(_squareHeight)] for l in range(_squareWidth)]
    for pTier in allValidPoints:        
        for p in pTier:
            allSquaresForClick[p[0][0]][p[0][1]] = tier            
        tier +=1
    orderedSquaresForClick = []
    for n in range(1,tier):
        for i1 in range(_range):
            for j1 in range(_range):
                for i in range(0,_squareWidth,_range):
                    for j in range(0,_squareHeight,_range):                
                        ti,tj = i+i1,j+j1
                        if(ti<_squareWidth and tj<_squareHeight and allSquaresForClick[ti][tj]==n):                            
                            orderedSquaresForClick.append((ti,tj))

    orderedSI = distanceSI(orderedSquaresForClick,_interval)
    for si in orderedSI:
        x = firstx+int(round((si[0][0])*_blocka))
        y = firsty+int(round((si[0][1])*_blocka))
        win32api.Sleep(si[1])
        click(x,y)        
        print si

if(endGame and (not empty)):
    print "Faild...."
else:
    print "Success"
raw_input()
posted on 2012-08-15 18:25  fenix  阅读(695)  评论(0编辑  收藏  举报