autoFight

#!/usr/bin/env python
# -*- coding: cp936 -*-
'''
Created on 2011-11-6

@author: caojiaju
'''
import subprocess
import win32ui,win32con,pythoncom,win32gui,win32process,win32api   
import time 
import string

class AutoFht:
    def __init__(self):
        self.initConfig()
        return
    
    def initConfig(self):
        #进入场景点
        #self.enterScreenPosition = (564,538)
        self.enterScreenPosition = (856,343)
        
        self.savePosition = (284,694)
        self.enterScreentWait = 2 
        
        #退出场景
        #self.exitSetPosition = (301, 759)
        #self.exitRtnMapPosition= (697,428)
        
        #家
        self.exitSetPosition = (314,710)
        self.exitRtnMapPosition= (708,386)
        
        #刷怪次数
        self.enterScreenCount = -1  # -1表示不限制
        
        #小场景约定个数
        self.smallScreentCount = 3
        
        #单场景跑步前进秒数
        self.throughScreenSeconds = 12          #单场景行走描述
        self.singleScreenSkillExcuteCount = 1   #单场景技能执行重复次数
        self.singleScreenSkillExcuteInteval = 6  #6秒
        
        #技能执行定义
        self.skillList = [('A','U' ,'O','O','O') , 5 , ('D','U' ,'O','O','O')]
        
        #游戏窗体句柄
        self.gameHandle = 93257912
        self.keyGameHandle = 131580
        
        return
    
    def do(self):
        enterCountTemp = 0 
        while (enterCountTemp < self.enterScreenCount or self.enterScreenCount == -1):
            print 'Enter sceen ,times is =' , enterCountTemp + 1
            self.enterScreen()
            
            self.screenLoop()
            
            self.exitScreen()
            
            enterCountTemp = enterCountTemp + 1
            self.waitMoment(6)
            
            self.click(self.gameHandle, self.savePosition)
    
    def exitScreen(self):
        print '    Exit Sceen'

        self.click(self.gameHandle, self.exitSetPosition)
        self.waitMoment(1)
        self.click(self.gameHandle, self.exitRtnMapPosition)
    def enterScreen(self):
        print '    Enter Sceen'
        
        #self.click(self.keyGameHandle, self.enterScreenPosition)
        self.click(self.gameHandle, self.enterScreenPosition)
        return
    
    def screenLoop(self):
        print '    Enter Sceen lOOP'
        self.waitMoment(self.enterScreentWait)
        
        index = 0 
        while (index < self.smallScreentCount):
            print '        start Sceen fight Round ', index +1 
            self.runToEnd(self.throughScreenSeconds)
            self.autoFight()
            index = index + 1
        
        #self.autoFight()
        
    def autoFight(self):
        indexF = 0 
        
        while (indexF < self.singleScreenSkillExcuteCount):
            
            #send A
            self.keyPress('A')
            
            #等待1秒
            self.waitMoment(0.5)
            
            #send U
            self.keyPress(' ')
            self.waitMoment(0.5)
            
            for i in range(30):
                print '                skill excute index ', i +1 
                self.keyPress('U')
                self.waitMoment(0.4)
                self.keyPress('O')
                self.waitMoment(0.5)
                self.keyPress(' ')
                
            indexF = indexF + 1
            
            self.keyPress('D')
        
    def waitMoment(self,secs):
        win32api.Sleep(int(secs*1000))
    
    def keyDown(self,key):
        tmp = ord(key)
        win32gui.SendMessage(self.gameHandle, win32con.WM_KEYDOWN, tmp,0)
        return
    def keyUp(self,key):
        tmp = ord(key)
        win32gui.SendMessage(self.gameHandle, win32con.WM_KEYUP , tmp,0)       
        return
    def keyPress(self,key):
        self.keyDown(key)
        self.waitMoment(0.2)
        self.keyUp(key)
        return
    def runToEnd(self,secs):
        #D 调整方向
        self.keyPress('D')
        
        #等待1秒
        self.waitMoment(1)
                
        #DD 移动目标
        self.keyPress('D')
        self.waitMoment(0.05)
        self.keyDown('D')
        
        
        # 等待
        self.waitMoment(self.throughScreenSeconds)
        
        #D 弹起
        self.keyUp('D')
        
        #self.waitMoment(1)
        
    def click(self,handle, pos):
        client_pos = pos
        client_pos = win32gui.ScreenToClient(handle, pos)
        tmp = win32api.MAKELONG(client_pos[0], client_pos[1])
        #win32gui.SendMessage(self.gameHandle, win32con.WM_ACTIVATE, win32con.WA_ACTIVE, 0)
        win32api.SendMessage(handle, win32con.WM_LBUTTONDOWN, win32con.MK_LBUTTON, tmp)
        win32api.SendMessage(handle, win32con.WM_LBUTTONUP, win32con.MK_LBUTTON, tmp)

auto = AutoFht()
def main():
    print 'Starting ......'
    
    time.sleep(5)
    auto.keyGameHandle = win32gui.FindWindowEx(0, 0, 0, "造梦西游3,造梦西游大闹天庭篇,4399造梦西游3小游戏 - 视频播放器")
    if auto.keyGameHandle == 0:
        return
    
    win32gui.EnumChildWindows(auto.keyGameHandle,ttt,0)
    
    
    #auto.gameHandle = 394120
    print 'gameHandle handle is =',auto.gameHandle
    print 'gameHandle handle is =',auto.keyGameHandle
    
    auto.do()

    return

def ttt(hwnd,param):
    auto.gameHandle = hwnd
    return
def test():
    print 'Starting ......'
    
    #time.sleep(2)
    auto = AutoFht()
    auto.keyGameHandle = win32gui.FindWindowEx(0, 0, 0, "造梦西游3,造梦西游大闹天庭篇,4399造梦西游3小游戏 - 视频播放器")
    #auto.gameHandle = 394120
    win32gui.EnumChildWindows(auto.keyGameHandle,ttt,0)
    return
    for i in range(10):
        auto.gameHandle = win32gui.FindWindowEx(auto.keyGameHandle,0,'MacromediaFlashPlayerActiveX',0)
        print 'gameHandle handle is =',auto.gameHandle
    print 'gameHandle handle is =',auto.keyGameHandle
    #auto.exitScreen()
    return    
if __name__ == '__main__':
    #test()
    main()

你可能感兴趣的:(autoFight)