首页
归档
笔记
树洞
搜索
友言

文章详情

Interesting People Record Interesting.

/ Python / 文章详情

python PC端自动化萌新实战笔记

Sonder
2025-03-24
1682字
4分钟
浏览 (140)

别踩白块了

image.png

https://www.4399.com/flash/154247_3.htm

复制代码
import pyautogui
import time

# 鼠标移动到最左上角停止运行
pyautogui.FAILSAFE = True

def start():
   time.sleep(3)
   # 获取当前屏幕分辨率
   # screenWidth, screenHeight = pyautogui.size()
   # pyautogui.moveTo(x=100, y=100, duration=2, tween=pyautogui.linear)
   # 鼠标移到屏幕中央。
   # pyautogui.moveTo(130, 26)
   # img = pyautogui.screenshot()
   # res = img.getpixel((970,399))
   # print("文件名:auto.py, 行数:14, 参数是:res", res)
   item_box_bottom = 605 # 每个盒子的底部
   x_start = 970 # x轴最左侧
   x_end = 1284 # x轴最右边
   while True:
       img = pyautogui.screenshot() # 截图当前屏幕
       for i in range(x_start, x_end, 100):
           # 获取当前区域的颜色
           res = img.getpixel((i, item_box_bottom))
           print("文件名:auto.py, 行数:20, 参数是:res", res) # (2,2,2)
           if res[0] == 2:
               pyautogui.click(i, item_box_bottom)


if __name__ == '__main__':
   print(pyautogui.__version__)
   start()

打地鼠

https://www.4399.com/flash/178030_3.htm

复制代码
# 导入pyautogui库,用于自动化控制鼠标和键盘
import pyautogui

# 开启安全模式,使根据鼠标位置来中断程序的功能生效
pyautogui.FAILSAFE = True
# 设置每个自动化操作的延迟时间,以降低操作速度
pyautogui.PAUSE = 0.05

# 无限循环
while True:
   # 在屏幕上查找所有与给定图像匹配的位置
   # 注意:要使用confidence参数需要安装三方库:opencv-python
   tars = list(pyautogui.locateAllOnScreen('img.png', confidence=0.8))
   # 如果存在匹配的位置
   if tars:
       # 遍历所有匹配的位置
       for i in tars:
           # 点击该位置的中心点
           pyautogui.click(pyautogui.center(i))
           # 打印提示信息
           print('发现地鼠了~')
           # 将鼠标移动到指定位置
           # 为什么要移动到指定的位置?因为防止前后地鼠出现位置一样,这样锤子挡住了地鼠,就会识别不到这个地鼠
           pyautogui.moveTo(300, 300)
下一篇 / 尤雨溪推荐的全新Vue动画库

🎯 相关文章

💡 推荐文章

🕵️‍♂️ 评论 (0)