# Python 工具庫
## 工具庫
- 下載 [snp.py](https://github.com/mzshieh/snp2018/blob/master/project/snp.py) 檔案, 並放置到要執行的程式碼的目錄下
- 在程式碼最上面加上 `import snp`
## Token base input
- `snp.get_float()` 回傳一個浮點數, 例如 `3.14`, `-0.000001`, `123.456`
- `snp.get_int()` 回傳一個整數, 例如 `2147`, `-101`
- `snp.get_str()` 回傳一個字串, `snp2018`, `140.113.0.1`
- 範例程式碼 1
```python=
# file: test.py
import snp
f = snp.get_float();
print(f)
i = snp.get_int();
print(i)
s = snp.get_str();
print(s)
```
- 執行範例 1
3.14
`3.14`
134
`134`
snp2018
`snp2018`
- 執行範例 2
1.2.3 1.99
`Not float. Please try again`
`1.99`
0x123 abcd 1000
`Not int. Please try again`
`Not int. Please try again`
`1000`
HI
`HI`
## 截圖比對
### function
- `snp.locateOnScreen(image)`
- 回傳 `(left,top,w,h)` 表示在螢幕上找到的 image 的位置還有 image 寬與高
- 如果找不到足夠像的會回傳 `None`
- sample
```python=
import snp
import pyautogui
p = snp.locateOnScreen('dino.png')
if p != None:
pyautogui.click(p[0]+p[2]//2,p[1]+p[3]//2)
```
- `snp.locateCenterOnScreen(image)`
- 回傳 `(x,y)` 表示在螢幕上找到的 image 的中心位置,
- 如果找不到足夠像的會回傳 `None`
- sample
```python=
import snp
import pyautogui
p = snp.locateCenterOnScreen('dino.png')
if p != None:
pyautogui.click(p[0],p[1])
```
- `snp.locateAllOnScreen(image)`
- 回傳一個 generator 表示可以在螢幕上找到的所有足夠相似的位置,每個用 (left,top,w,h) 表示
- sample
```python=
import snp
import pyautogui
for p in snp.locateAllOnScreen('dino.png'):
x, y = p[0]+p[2]//2, p[1]+p[3]//2
pyautogui.click(x,y)
```
- `snp.setScale( r )`
- 使用 Mac retina 螢幕的的同學, 需要在 import snp 之後加入下面的程式碼, 截圖的座標才會正確
```python=
import snp
snp.setScale(2) # for Mac retina
```
### 選用參數
- 在要使用的 function 自訂參數
- 例如`snp.locateCenterOnScreen(image, threshold=0.9)` 表示相似度要在 0.9 以上的匹配才會被回傳
- 例如`snp.locateCenterOnScreen(image, region(100,100,50,100))` 表示只會截圖 (100,100) 到 (150,200) 為對角線的矩形範圍
- 可以設定的參數
- `threshold` 表示相似度百分比的門檻,預設為 `0.87`
- `region` 表示要截圖的範圍,以 `(left,top,w,h)` 表示,預設為`(0,0,600,200)`,愈大的截圖範圍,截圖頻率會越低,不建議截一半螢幕大小以上的面積。