三种《谁能坚持50秒》的智能闪避算法,让电脑玩高难度游戏

import subprocess,string
import win32ui,win32con,pythoncom,win32gui,win32process,win32api
import time,math
import string
from PIL import ImageGrab,ImageDraw
import os
from ctypes import *

x0=247
y0=108
x1=683
y1=543
centx=(x0+x1)/2
centy=(y0+y1)/2
halfw=centx-x0
halfh=centy-y0
thick=26

subblue = (
        0, 0, 0, 0,
        0, 0, 0, 0,
        -0.55, -0.55, 1, -50 )
rotw=int(halfw*0.707)
roth=int(halfh*0.707)
def findspace(im,px0,py0):
    minx0=x0
    minx1=x1
    miny0=y0
    miny1=y1
    for rat in [-0.176327, -0.36397, -0.57735, -0.8391, 0, 0.176327, 0.36397, 0.57735, 0.8391]:
        for x in xrange(px0, minx0, -1):
            if(y0+(x0-x)*rat<y0 or y0+(x0-x)*rat>=y1):continue
            if im.getpixel((x-x0,y0+(x0-x)*rat-y0))[2]>60:
                minx0=x
                break
        for x in xrange(px0, minx1):
            if(y0+(x-x0)*rat<y0 or y0+(x-x0)*rat>=y1):continue
            if im.getpixel((x-x0,y0+(x-x0)*rat-y0))[2]>60:
                minx1=x
                break
        for y in xrange(py0, miny0, -1):
            if(x0+(y0-y)*rat<x0 or x0+(y0-y)*rat>=x1):continue
            if im.getpixel((x0+(y0-y)*rat-x0,y-y0))[2]>60:
                miny0=y
                break
        for y in xrange(py0, miny1):
            if(x0+(y-y0)*rat<x0 or x0+(y-y0)*rat>=x1):continue
            if im.getpixel((x0+(y-y0)*rat-x0,y-y0))[2]>60:
                miny1=y
                break
    return (minx1-minx0)*(miny1-miny0), (minx1+minx0)/2, (miny1+miny0)/2, minx1-minx0, miny1-miny0
frmcnt=0
def findpos():
    global frmcnt
    im0 = ImageGrab.grab()     
    print "rect:",x0,y0,x1,y1
    im=im0.crop((x0,y0,x1,y1))
    del im0
    frmcnt+=1
    
    im.save("c:\\tmp_0_%3d.png"%frmcnt)
    
    
    im=im.convert("RGB",subblue)
    maxarea=-1
    maxcx=0
    maxcy=0
    maxw =0
    maxh =0
    rectrec=[]
    for x in xrange(x0+thick,x1-thick+1,(x1-x0-thick-thick)/4):
        for y in xrange(y0+thick,y1-thick+1,(y1-y0-thick-thick)/4):
            area,cx,cy,tw,th=findspace(im,x,y)
            rectrec.append([cx-x0-tw/2,cy-y0-th/2,cx-x0+tw/2,cy-y0+th/2])
            if maxarea<area:
                maxcx=cx
                maxcy=cy
                maxarea = area
                maxw = tw
                maxh = th
    imd=ImageDraw.Draw(im)
    reccol=128
    for r in rectrec:
        imd.rectangle(r, outline=reccol)
        reccol+=2024
    imd.rectangle((maxcx-maxw/2-x0, maxcy-maxh/2-y0, maxcx+maxw/2-x0, maxcy+maxh/2-y0),outline=(255,255,255))
    del imd
    im.save("c:\\tmp%3d.png"%frmcnt)
    del im
    avliable=0
    if maxw>430 and maxh>430:
        avliable=0
        #exit(0)
    #return (0,0,0,0,0,0)
    return  (maxcx,maxcy,maxarea,avliable,maxw,maxh)
       
def mm(point):
    #if(point[0]<0 or point[1]<0):return
    windll.user32.SetCursorPos(point[0], point[1])
    win32api.mouse_event(win32con.MOUSEEVENTF_MOVE, 0, 0)
    
def discenssion():
    print x0,y0,x1,y1,(centx,centy)
    while True:
        #res=findpos1()
        res=findpos()
        print res, math.sqrt(res[2])/4000
        if(res[3]==1): mm((res[0], res[1]))       
        #time.sleep(math.sqrt(res[2])/4000)
    #time.sleep(0.05)
    #grab()
    
windll.user32.SetCursorPos(centx, centy)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0)
time.sleep(0.05)
windll.user32.SetCursorPos(x0+thick, centy+thick)
win32api.mouse_event(win32con.MOUSEEVENTF_MOVE, 0, 0)


#discenssion()
#exit()
for s in file("cmd.txt"):
    v = s.split()
    print string.atof(v[0]), string.atof(v[1]), string.atof(v[2])
    time.sleep(string.atof(v[0]))
    windll.user32.SetCursorPos(int(string.atof(v[1])*(x1-x0-2*thick)+(x0+thick)),
                               int(string.atof(v[2])*(y1-y0-2*thick)+(y0+thick)))
    win32api.mouse_event(win32con.MOUSEEVENTF_MOVE, 0, 0)

win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0)
    
exit()

#rotate 45 degress, todo: predect    
def findpos1():
    im0 = ImageGrab.grab()     
    im=im0.crop((x0-rotw,y0-roth,x1+rotw,y1+roth))
    im0=None
    im=im.convert("RGB",subblue)
    im.paste((0,0,0), (0,           0,              2*(halfw+rotw), roth))
    im.paste((0,0,0), (0,           2*halfh+roth,   2*(halfw+rotw), 2*(halfh+roth)))
    im.paste((0,0,0), (0,           roth,           rotw,           2*halfh+roth))
    im.paste((0,0,0), (2*halfw+rotw,roth,           2*(halfw+rotw), 2*halfh+roth))
    im=im.rotate(45)
    x=0
    y=0
    rangv=[]
    rangh=[]
    for v in im.getdata():
        x+=1
        if x>=im.size[0]:
            x=0
            y+=1
        if v[2]>60:
            if x not in rangv:
                rangv.append(x)
            if y not in rangh:
                rangh.append(y)
    rangv.sort()
    rangh.sort()
    #merge
    lastv=0
    maxposx=0
    maxxv=0
    maxposy=0
    maxyv=0
    v0=-1
    for v in rangv:
        if v!=v0:
            if(v0>0):
                if maxxv<(v-v0):
                    maxxv=v-v0
                    maxposx=(v+v0)/2
                #print "%d), (%d,"%(v0,v),
            else:
                maxposx=v/2
                maxxv=v
                #print "(%d,"%(v),
        v0=v+1
    if maxxv<(x1+rotw-v0):
        maxxv=x1+rotw-v0
        maxposx=(x1+rotw+v0)/2
    v0=-1
    for v in rangh:
        if v!=v0:
            if(v0>0):
                if maxyv<(v-v0):
                    maxyv=v-v0
                    maxposy=(v+v0)/2
                
                #print "%d), (%d,"%(v0,v),
            else:
                maxposy=v/2
                maxyv=v                
                #print "(%d,"%(v),
        v0=v+1
    if maxyv<(y1+roth-v0):
        maxyv=y1+roth-v0
        maxposy=(y1+roth+v0)/2
    #print "%d)"%v0
    posx=maxposx-(halfw+rotw)
    posy=maxposy-(halfh+roth)
    x=int(posx*0.707+posy*0.707)+centx
    y=int(posy*0.707-posx*0.707)+centy
    '''
    if x<x0+thick:x=x+thick
    if y<y0+thick:y=y+thick
    if x>x1-thick:x=x1-thick
    if y<y1-thick:y=y1-thick'''
    print im.size, maxposx, maxposy, posx, posy, x, y, min(maxyv,maxxv)
    if min(maxyv,maxxv)<=1:
        exit(0)
    if min(maxyv,maxxv)<=2*thick :
        im.save("c:\\screenshot.png")   
        exit(0)
    
    return (x,y,min(maxyv,maxxv))
    
    #print "vert", rangv
    #print "hori", rangh
    #im.save("c:\\screenshot.png")   
    

time.sleep(2)
windll.user32.SetCursorPos(x0+thick, y0+thick)
win32api.mouse_event(win32con.MOUSEEVENTF_MOVE, 0, 0)
time.sleep(8)
windll.user32.SetCursorPos(x1-thick, y1-thick)
win32api.mouse_event(win32con.MOUSEEVENTF_MOVE, 0, 0)
time.sleep(2)
windll.user32.SetCursorPos(x0+thick, y0+thick)
win32api.mouse_event(win32con.MOUSEEVENTF_MOVE, 0, 0)
time.sleep(8)
windll.user32.SetCursorPos(x1-thick, y1-thick)
win32api.mouse_event(win32con.MOUSEEVENTF_MOVE, 0, 0)
findpos1()
exit()



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值