2013-10-09 67 views
0

直到我取消最小化遊戲之前,代碼完美無誤地完成了工作。經過大量嘗試找出SetCursorPos不起作用的原因之後,我認爲它沒有出現任何問題,但它拒絕移動到位置在遊戲的窗口。到目前爲止的代碼是:Python的SetCursorPos錯誤?或者網絡遊戲導致它?

from PIL import ImageGrab 
import os 
import time 
import win32con , win32api 

x_pad = 2 
y_pad = 27 


def start(): 
    #location of first menu 
    mousePos((682, 519)) 
    leftClick() 
    time.sleep(.1) 
    leftClick() 
    time.sleep(.5) 

    #location of second menu 
    mousePos((1208, 528)) 
    leftClick() 
    time.sleep(.5) 

    #location of third menu 
    mousePos((921, 479)) 
    leftClick() 
    time.sleep(.5) 

    #location of fourth menu 
    mousePos((651, 350)) 
    leftClick() 
    time.sleep(.5) 

def screenGrab(): 
    box = (x_pad+1,y_pad+1,x_pad+1362,y_pad+770) 
    im = ImageGrab.grab(box) 
    im.save(os.getcwd() + '\\full_snap__' + str(int(time.time())) + 
'.png', 'PNG') 



def mousePos(cord): 
    win32api.SetCursorPos((x_pad + cord[0], y_pad + cord[1])) 


def get_cords(): 
    x,y = win32api.GetCursorPos() 
    x = x - x_pad 
    y = y - y_pad 
    print x,y 


def leftClick(): 
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,0,0) 
    time.sleep(.1) 
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,0,0) 
    print "Click." 


def main(): 
    pass 


if __name__ == "__MAIN__": 
    main() 

當我運行的start()我得到這個:

Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)] on win32 
Type "copyright", "credits" or "license()" for more information. 
>>> ================================ RESTART ================================ 
>>> 
>>> start() 
'Traceback (most recent call last): 
    File "<pyshell#0>", line 1, in <module> 
    start() 
    File "C:\Users\User\Desktop\py\game.py", line 12, in start 
    mousePos((682, 519)) 
    File "C:\Users\User\Desktop\py\game.py", line 42, in mousePos 
     win32api.SetCursorPos((x_pad + cord[0], y_pad + cord[1])) 
     error: (0, 'SetCursorPos', 'No error message is available')' 
    >>> 

我不知道爲什麼會這樣,我已經從這個網站tryed很多的解決方案,這樣可以是遊戲嗎? 我想知道的是,如果遊戲是錯誤的原因,或者我錯過了某些東西,並且是否有可行的解決方案。

+0

你的代碼縮進了所有錯誤,並有無效的字符串標記,這是瘋狂的發送語法高亮。你可以重新修復和修復你的代碼,以便我們可以看到發生了什麼? – 2013-10-09 23:46:55

+0

對不起,只是給我一些時間這樣做。 – Alex

+0

這是更好嗎? – Alex

回答