50行Python代码玩转微信小游戏"颜色王者"
2018-03-16 16:35
971 查看
50行Python代码玩转微信小游戏”颜色王者”
游戏模式
在微信小程序里搜索“颜色王者”,即可找到该游戏。游戏的目标比拼色彩敏感度。点击图片中不一样的色块即可。
这游戏前面20多级还是比较简单的,到后面色块实在太小,颜色越来越接近以至于到下图的程度。
工具介绍
Python 3.5Android 手机
Adb 驱动
Python OpenCV库
原理说明
首先使用adb截图,并且推送到电脑调用OpenCV库,读入图片,把状态栏等区域截掉
findContours函数识别边框
minEnclosingCircle找出最小外接圆(目的是找出色块的中心)
利用像素值比较出不同的色块
程序源码
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个人是怎么识别后面的小方块的。
相关文章推荐
- 使用50行Python代码从零开始实现一个AI平衡小游戏
- 学习使用python玩转微信小游戏“跳一跳”
- 我在Python中用37行代码制霸微信小游戏跳一跳的朋友圈
- python代码玩微信小游戏跳一跳win10+android
- Python编写微信打飞机小游戏(三)
- 微信运动修改器python代码
- @微信官方给不了的圣诞帽,Python和OpenCV给你(附代码)
- python itchat实现微信好友头像拼接图的示例代码
- 50行python代码实现个代理server(你懂的)
- 用不到50行的Python代码构建最小的区块链
- 跟着鬼哥学爬虫-7-python微信-2-开始编写模版代码
- 跟风 玩转微信跳一跳小游戏 拿高分
- python代码:设计一个"石头,剪子,布"游戏
- 50行python代码实现的小区块链
- 50行Python代码实现人脸检测
- 基于Tkinter用50行Python代码实现简易计算器
- 解决"学习python-flask教程,下载源码运行的时候出现:TypeError: 'bool' object is not callable 错误代码:"
- Python编写微信打飞机小游戏(六)
- Python编写微信打飞机小游戏(十二)