# 基于python的sunflower 简易收菜脚本

By [qingyun.eth](https://paragraph.com/@qingyun) · 2022-04-10

---

sunflower 网页小游戏 简介
------------------

主要是向日葵1分钟一次收菜

比较繁琐

所以使用python的pyautogui自动化脚本

结合屏幕取点工具，定出点击坐标

半小时即可完成收菜、领reward脚本

缺陷是没做过检测

解决思路
----

### 1.自动化收菜

出生地，5个格子

第二块地，5个格子

拿到其中一块的坐标，其余自动连带即可

### 2.有几率弹出的reward

reward需要点击一次箱子

箱子属于随机出现在对话框

使用识图查坐标的函数定位到箱子

再点击

代码
--

    import pyautogui
    import time
    # 默认这项功能为True, 这项功能意味着：当鼠标的指针在屏幕的最坐上方，程序会报错；目的是为了防止程序无法停止
    pyautogui.FAILSAFE = False
    # 意味着所有pyautogui的指令都要暂停一秒；其他指令不会停顿；这样做，可以防止键盘鼠标操作太快；
    pyautogui.PAUSE = 0.2
    
    def corp(top_one_x, top_one_y):
        pyautogui.doubleClick(top_one_x,top_one_y)
        pyautogui.doubleClick(top_one_x + 85 ,top_one_y)
        pyautogui.doubleClick(top_one_x + 42 ,top_one_y + 45)
        pyautogui.doubleClick(top_one_x,top_one_y + 100)
        pyautogui.doubleClick(top_one_x + 85,top_one_y + 100)
        box()
    
    def box():
        box_location = pyautogui.locateOnScreen('箱子.png')
        print(box_location)
        if box_location != None:
            x,y = pyautogui.center(box_location)
            pyautogui.click(x,y)
            time.sleep(3)
    
        box_location = pyautogui.locateOnScreen('箱子2.png')
        print(box_location)
        if box_location != None:
            x,y = pyautogui.center(box_location)
            pyautogui.click(x,y)
            time.sleep(3)
    
        box_location = pyautogui.locateOnScreen('close按钮.png')
        print(box_location)
        if box_location != None:
            x,y = pyautogui.center(box_location)
            pyautogui.click(x,y)
            time.sleep(3)
    
    if __name__ == '__main__':
        print(pyautogui.size())   # 返回所用显示器的分辨率； 输出：Size(width=1920, height=1080)
        width,height = pyautogui.size()
        print(width,height)
    
        top_one_x = 1005
        top_one_y = 357
        field_2_x = 495
        field_2_y = 676
    
        #运行次数
        count = 2
        for i in range(1,count +1):
            print('次数：')
            print(i)
            for j in range(0,2):
                corp(top_one_x, top_one_y)
                corp(field_2_x, field_2_y)
                corp(top_one_x, top_one_y)
                corp(field_2_x, field_2_y)
            pyautogui.moveTo(1055,0,duration=1)
            time.sleep(3)
            for j in range(0,2):
                corp(top_one_x, top_one_y)
                corp(field_2_x, field_2_y)
                corp(top_one_x, top_one_y)
                corp(field_2_x, field_2_y)

---

*Originally published on [qingyun.eth](https://paragraph.com/@qingyun/python-sunflower)*
