50行Python代码玩转微信小游戏"颜色王者"

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_29300341/article/details/79583635

50行Python代码玩转微信小游戏”颜色王者”

游戏模式

在微信小程序里搜索“颜色王者”,即可找到该游戏。
游戏的目标比拼色彩敏感度。点击图片中不一样的色块即可。

这游戏前面20多级还是比较简单的,到后面色块实在太小,颜色越来越接近以至于到下图的程度。

工具介绍

  • Python 3.5
  • Android 手机
  • Adb 驱动
  • Python OpenCV库

原理说明

  1. 首先使用adb截图,并且推送到电脑
  2. 调用OpenCV库,读入图片,把状态栏等区域截掉
  3. findContours函数识别边框
  4. minEnclosingCircle找出最小外接圆(目的是找出色块的中心)
  5. 利用像素值比较出不同的色块

程序源码

import cv2
import os
import time

def compare(rgb1, rgb2):
    if (abs(rgb1[0]-rgb2[0]) < 3 and abs(rgb1[1]-rgb2[1]) < 3 and abs(rgb1[2]-rgb2[2]) < 3):
        return True
    else:
        return False
while True:
    os.system('adb shell screencap -p /sdcard/yanse.png')
    os.system('adb pull /sdcard/yanse.png G:/Python_code/wechat_jump_game-master/yansewangzhe/yanse.png')

    original_image = cv2.imread('G:/Python_code/wechat_jump_game-master/yansewangzhe/yanse.png')
    #选取出图片中准备识别的部分
    ROIimage = original_image[400:1500, 20:1200]
    res = cv2.resize(ROIimage, (900, 900), interpolation=cv2.INTER_CUBIC)

    gray_img = cv2.cvtColor(res, cv2.COLOR_RGB2GRAY)
    ret, binary_img = cv2.threshold(gray_img, 50, 200, cv2.THRESH_BINARY)
    im, contours, hierarchy = cv2.findContours(binary_img, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

    list1 = []
    list2 = []
    (x0, y0), radius = cv2.minEnclosingCircle(contours[0])
    for c in contours:
        (x, y), radius = cv2.minEnclosingCircle(c)
        if compareColor(res[int(x0), int(y0)], res[int(x), int(y)]):
            list1.append([int(x), int(y)])
        else:
            list2.append([int(x), int(y)])
    print(list1)
    print(list2)
    # 将不同颜色的色块中心标注黑色圆点
    if len(list1) == 1:
        cv2.circle(res, (list1[0][1], list1[0][0]), 2, (0, 0, 0), 10)
    else:
        cv2.circle(res, (list2[0][1], list2[0][0]), 2, (0, 0, 0), 10)


    cv2.imshow('Window1', res)
    cv2.waitKey()
    cv2.destroyAllWindows()
    time.sleep(1)

实际使用

程序按任意键后,会立即再截取新图片,运行效果如下。

最终当然是打满了52级。

Future

后面的小方块实在太多,对着电脑找点实在麻烦。当然也可以直接让adb点击,不过怼齐电脑和手机上的像素点有点麻烦。

PS:最强王者居然有6个人,真是难以想象之前5个人是怎么识别后面的小方块的。

猜你喜欢

转载自blog.csdn.net/qq_29300341/article/details/79583635
今日推荐