[toc]
## Python開發心得
相關學習材料:https://naotu.baidu.com/file/bc6e9fbaecf5319bced0918131be3941
## 基礎語法
### while循環比for循環更加靈活
- for循環
- while循環:while可以使用死循環,且可以根據元素下標開始循環位置
```python
# 程序入口、控制中心_單次執行
def startOnce(self):
# 1、打開任務文件夾
var_index = 0
# 輪詢次數
polling_count = 1
images = os.listdir("./images/task_" + self.dir)
while var_index < len(images) and polling_count <= self.max: # listdir的參數是文件夾的路徑
filename = images.__getitem__(var_index)
print("[step-" + str(var_index + 1) + "]======================" + filename)
if var_index < self.index:
print("跳過第" + str(var_index + 1) + "個步驟")
var_index += 1
continue
# 2、先截取游戲區域大圖,然后返回小圖在大圖中的坐標
# 1、第一種截圖方法,只能截取電腦主屏幕
# self.screenshot()
# 2、第二種截圖方法,可以截取進程所在顯示器的主屏幕
start_XY = self.cW.grab_picture()
src = cv2.imread('temp.png')
target = cv2.imread('./images/task_' + self.dir + '/' + filename)
# self.find_button(target,full_screen)
pos = crab.find_position(target, src, start_XY[0], start_XY[1])
if pos is None:
if filename.__contains__("once_"):
print("該按鈕點擊一次就夠了哦~即將跳過當前操作")
var_index += 1
continue
elif polling_count == self.max:
self.mode = 'Polling'
# 啟動輪詢模式,并從當前節點開始向后執行
print("第" + str(polling_count) + "次嘗試點擊,但是未找到按鈕[" + filename + "]輪詢模式啟動……")
self.index = self.polling_start(var_index)
var_index = self.index + 1
polling_count = 1
continue
else:
print("第" + str(polling_count) + "次嘗試點擊,但是未找到按鈕[" + filename + "]休息2s后繼續……")
polling_count += 1
time.sleep(2)
else:
print("系統正在操作按鈕:" + filename)
if self.mode == 'Polling':
self.mode = ''
print("輪詢模式已關閉")
self.mouse.move(pos[0], pos[1])
time.sleep(self.rate)
self.mouse.press(pos[0], pos[1])
# 指示當前執行的步驟序號
var_index += 1
if filename.__contains__("_delay"):
time.sleep(5)
else:
time.sleep(self.rate)
continue
```
包的安裝: