最近QQ开启了个玩游戏送福气袋活动
游戏规则,游戏时间大于5s,积分不为0,则有几率获得红包。每个人可以领120。我在想一个人实现的话得玩多久,太浪费时间了,作为自动化的先驱(码农),就想到用代码来实现这一流程。
环境和工具:
termux(python3、Pillow)
windows macOS 实现看这里
git源码直通(两个版本都有)
部分代码
注释较全,有问题可以留言
#!/data/data/com.termux/files/usr/bin/python
# coding:utf-8
"""
2019年1月28日 刷QQ 福气袋活动脚本
LYM
程序思路:游戏规则,游戏时间大于5s,积分不为0,则有几率获得红包。
据此,天降福运活动。分别点击底部三个按钮,等等游戏结束即可。
其中,代码中出现的数据均以 屏幕为20*1440的手机为例
"""
from PIL import Image
import sys
import time
import subprocess
#封装subprocess.call, AOP思想,为满足不同使用者的需求
def shell(str):
subprocess.call(str ,shell=True)
#传入坐标x,y,在屏幕的位置,单位为百分比
def click(x, y):
x = round(x * width)
y = round(y * height)
shell("input tap " + str(x) +" " + str(y))
#获取手机屏幕分辨率
shell("/system/bin/screencap -p /sdcard/screen.png")
img = Image.open("/sdcard/screen.png")
width = img.width #宽
height = img.height #高
shell("rm -rf /sdcard/screen.png")
blessingCount = 0 #记录已经领了多少次福气袋,QQ这次活动120次封顶,到了120 就不用继续运行了
'''
手动打开活动界面,若想实现自动化,参考以下两行
(am start -n com.tencent.mobileqq/com.tencent.mobileqq.activity.SplashActivity
点击福字)
等待5s,点击天降福运
等待5s,点击开始游戏
等待1s,依次点击下面三个按钮
等待12s,游戏结束
if 出现福气袋
点击分享福袋
等待2s,选择分享给谁(点击最近的第一个联系人或群聊)
等待2s,点击发送福气袋
等待2s,点击开启福气袋
等待2s,点击去获取福袋 --> line 39
else
点击再来一局 --> line 41
'''
#打开活动页面
#手动打开,代码略...
#等待5s,点击天降福运
def click_tjfy():
time.sleep(5)
#坐标在(0.9,0.736)* 分辨率处
click(0.9, 0.736)
#等待5s,点击开始游戏
def click_start():
time.sleep(5)
click(0.5, 0.88)
#等待1s,依次点击下面三个按钮
def click_bottom3():
time.sleep(1)
click(0.16, 0.94)
time.sleep(1)
click(0.5, 0.94)
time.sleep(1)
click(0.83, 0.94)
#等待12s,游戏结束
def wait_to_end():
time.sleep(12)
#判断是否出现福气袋:经观察对比,如果出现福气袋,左上角像素只可能为(47,5,6)或(50,8,9)。
def has_blessing():
shell("/system/bin/screencap -p /sdcard/screen.png")
img = Image.open("/sdcard/screen.png")
for i in range(50,100):
for j in range(50,100):
totalColor = img.getpixel((i,j))[0] + img.getpixel((i,j))[1] + img.getpixel((i,j))[2] #如(251, 188, 86, 255),前三个分别为r,g,b
if totalColor > 80:
return False
return True
#点击分享福袋
def click_share():
click(0.5, 0.7)
#等待2s,点击最近的第一个联系人或群聊
def select_who_to_share():
time.sleep(2)
click(0.5, 0.43) #最近的第一个
#click(0.5, 0.5) #最近的第二个
#等待2s,点击发送福气袋
def send_blessing():
time.sleep(2)
click(0.7, 0.78)
#等待2s,点击开启福气袋
def open_blessing():
time.sleep(2)
click(0.55, 0.74)
#等待2s,点击去获取福袋
def go_to_get_blessing():
time.sleep(2)
click(0.7, 0.8)
#点击再来一局
def click_restart():
click(0.5, 0.72)
if __name__=="__main__":
#等待5s,点击天降福运
click_tjfy()
#等待5s,点击开始游戏
click_start()
while(True):
#等待1s,依次点击下面三个按钮
click_bottom3()
#等待12s,游戏结束
wait_to_end()
#判断是否出现福气袋:经观察对比,如果出现福气袋,左上角像素只可能为(47,5,6)或(50,8,9)。
if has_blessing():
#点击分享福袋
click_share()
#等待2s,点击最近的第一个联系人或群聊
select_who_to_share()
#等待2s,点击发送福气袋
send_blessing()
#等待2s,点击开启福气袋
open_blessing()
blessingCount = blessingCount + 1
if blessingCount > 120:
shell("rm -rf /sdcard/screen.png")#删掉截图,释放占用的空间
sys.exit()
#等待2s,点击去获取福袋
go_to_get_blessing()
#等待5s,点击天降福运
click_tjfy()
#等待5s,点击开始游戏
click_start()
else:
#点击再来一局
click_restart()