python编写coc部落冲突游戏辅助(1)

〇、准备工具

     Windows 10; 
     一个安卓真机 
    python 3 
    adb驱动

一、准备工作

1、 编程环境建立         

1.1   python库anaconda的安装           

软件下载地址: https://www.anaconda.com/distribution/      可能加载比较慢不要急,下载好后找安装教程,进行安装。安装好后,设置系统环境变量,安装教程会有。

 anaconda主要是包含一些python的库,基本库都有,不用再麻烦去安装一些基础库。有的人安装的anaconda有python库,也有一些编译环境这样就比较好,不用另外安装编译环境,而有的人安装的anaconda只有python的库。如 图(1)所示,为anaconda在开始菜单中的状态,此时为只有python的库的状态;如果在开始菜单中的状态中除了有anaconda  prompt外还有jupyter,spyder等等,那首先恭喜你了,下面的编译环境pycharm不用装了。只要你找到python的编译环境就可以了。

python编写coc部落冲突游戏辅助(1)_第1张图片 图(1)

1.2   python编译环境pycharm的安装

软件下载地址:   https://www.jetbrains.com/pycharm/download/#section=windows     加载比较慢不要急,下载好后找教程进行安装,并设置环境变量。pycharm的安装好后的状态如下图(2)所示。

python编写coc部落冲突游戏辅助(1)_第2张图片 图(2)

准备就绪后将pycharm与anaconda进行关联,步骤如下为:依次点击:    file>>settings...>>project>>project interpreter之后界面如下图。

python编写coc部落冲突游戏辅助(1)_第3张图片

之后点击上图中齿轮边上的下箭头,界面如下:

python编写coc部落冲突游戏辅助(1)_第4张图片

选择有anaconda的python。如果没有点击show All 进行加载。

2 、手机电脑联调工具adb安装

adb工具安装、下载、使用、测试请参考:https://blog.csdn.net/lwl223442/article/details/107900982

3、 手机与电脑相连

将安卓手机用USB与电脑相连接,在安卓手机上打开开发者模式,并开启USB调试模式,每种安卓手机开启的方法都不一样,这里需要小伙伴自己查询自己型号的手机如何开启开发者模式,以华为 荣耀8手机为例,在设置中找到手机版本号,连续点击5次即可开启开发者模式。如果以上操作没有任何错误,那么在CMD控制台,执行命令 adb devices可显示当前手机连接的端口号。

4、创建百度AI文字图像识别接口

百度的文字识别服务入门,根据教程注册并安装Python SDK即可,地址:https://cloud.baidu.com/doc/OCR/OCR-Python-SDK.html#.E9.85.8D.E7.BD.AEAipOcr

进去后用百度账号登录。

python编写coc部落冲突游戏辅助(1)_第5张图片

依次点击:提交工单>>产品服务>>人工智能>>文字识别>>创建工单。进去后完成注册,注册完成后开通相应的服务,拿到AppID、API Key和Secret Key就行。后续需要使用到百度的文字识别服务来识别资源。

python编写coc部落冲突游戏辅助(1)_第6张图片

注册完后会得到如下信息,这就是我们需要的。

python编写coc部落冲突游戏辅助(1)_第7张图片

如果不行就先使用本人的

coc

14339631

SeN0G3j9cKfiXWWvGmzXbbtl

RKi1Wgs9eRNEgBGXDvomQbXGOK8lABAT

二、基本功能的实现

1、长按、点击屏幕及滑屏功能实现

# !usr/bin/env python
# -*- coding:utf-8 -*-
__author__ = ""

import os
import sys
import re
from time import sleep

class sweep:

  def get_screen_size(*args):
    """Get size of window and return the size for swipe function
       args 作为可变参数 表示是否传入设备的ID
    """
    if not args:
        # 如果没有传入指定设备ID,执行以下ADB command,获取设备屏幕分辨率
        size_file = os.popen("adb shell dumpsys window displays").read()
    else:
        # 如果传入指定设备ID,执行以下ADB command,获取设备屏幕分辨率
        uid = args[0]
        size_file = os.popen("adb -s %s shell dumpsys window displays" % uid).read()
    if not size_file:
        # 获取设备分辨率失败
        print("Cannot get the resolution of screen, please check the ADB.")
        sys.exit()
    else:
        # 正则表达式匹配设备分辨率
        size_match = re.search(r"(\d+)x(\d+)", size_file)
        if not size_match:
            # 设备分辨率匹配失败
            print("Failed to match the screen size.")
            sys.exit()
        else:
            # 设备分辨率信息从字符串分割转换为二元元组
            size_screen = re.split(r"x", size_match.group())
            print(size_screen)
            # 字符串元素元组转换为整型元素列表
            size = [int(size_screen[0]), int(size_screen[1])]
            return size


  def swipe_up(*args, t=500, n=1):
    """Swipe device screen up in t milliseconds and repeat the operation n times
       t=100 作为命名关键字参数 表示默认的滑动时间为100ms 可自寻设计滑动时间
       n=1 作为命名关键字参数 表示默认的滑动次数为1次 可自寻设计滑动次数
    """
    size = sweep.get_screen_size(*args)
    x1 = size[0] * 0.5             #  注:这里的x,y和调试手机指针的x,y是反的
    y1 = size[1] * 0.2
    x2 = size[0] * 0.5
    y2 = size[1] * 0.3
    for i in range(n):
        if not args:
            os.system("adb shell input swipe %f %f %f %f %d" % (x1, y1, x2, y2, t))
        else:
            uid = args[0]
            os.system("adb -s %s shell input swipe %f %f %f %f %d" % (uid, x1, y1, x2, y2, t))
    print("up")


  def swipe_down(*args, t=500, n=1):
    """Swipe device screen down in t milliseconds and repeat the operation n times"""
    size = sweep.get_screen_size(*args)
    x1 = size[0] * 0.5
    y1 = size[1] * 0.3
    x2 = size[0] * 0.5
    y2 = size[1] * 0.2
    for i in range(n):
        if not args:
            os.system("adb shell input swipe %f %f %f %f %d" % (x1, y1, x2, y2, t))
        else:
            uid = args[0]
            os.system("adb -s %s shell input swipe %f %f %f %f %d" % (uid, x1, y1, x2, y2, t))
    print("down")


  def swipe_left(*args, t=500, n=1):
    """Swipe device screen left in t milliseconds and repeat the operation n times"""
    size = sweep.get_screen_size(*args)
    x1 = size[0] * 0.35
    y1 = size[1] * 0.3
    x2 = size[0] * 0.55
    y2 = size[1] * 0.3
    for i in range(n):
        if not args:
            os.system("adb shell input swipe %f %f %f %f %d" % (x1, y1, x2, y2, t))
        else:
            uid = args[0]
            os.system("adb -s %s shell input swipe %f %f %f %f %d" % (uid, x1, y1, x2, y2, t))
    print("left")


  def swipe_right(*args, t=500, n=1):
    """Swipe device screen right in t milliseconds and repeat the operation n times"""
    size = sweep.get_screen_size(*args)
    x1 = size[0] * 0.55
    y1 = size[1] * 0.5
    x2 = size[0] * 0.35
    y2 = size[1] * 0.5
    for i in range(n):
        if not args:
            os.system("adb shell input swipe %f %f %f %f %d" % (x1, y1, x2, y2, t))
        else:
            uid = args[0]
            os.system("adb -s %s shell input swipe %f %f %f %f %d" % (uid, x1, y1, x2, y2, t))
    print("right")


  def swipe_oblique(*args, t=500, n=1):
    """Swipe device screen oblique in t milliseconds and repeat the operation n times"""
    size = sweep.get_screen_size(*args)
    x1 = size[0] * 0.75
    y1 = size[1] * 0.05
    x2 = size[0] * 0.25
    y2 = size[1] * 0.95
    for i in range(n):
        if not args:
            os.system("adb shell input swipe %f %f %f %f %d" % (x1, y1, x2, y2, t))
        else:
            uid = args[0]
            os.system("adb -s %s shell input swipe %f %f %f %f %d" % (uid, x1, y1, x2, y2, t))




#i = 1
#while (i):
#    sweep.swipe_right()
#    sleep(3)
#    sweep.swipe_left()
#    sleep(3)
#    sweep.swipe_up()
#    sleep(3)
#    sweep.swipe_down()
#    sleep(3)


2、资源及奖杯的识别

from os import *
from time import sleep

# adb shell screencap -p /sdcard/1.png(保存到SDCard)
# adb pull /sdcard/screenshot.png d:/screenshot.png(保存到电脑)
class ScreenCapturer:

    # 获取的截图文件在手机中的保存位置(默认保存在内置存储根目录下)
    #ANDRIOD_PIC_PATH = "/storage/emulated/0/tmp/screenshot.png"
    ANDRIOD_PIC_PATH = "/storage/emulated/0/screenshot.png"#/storage/emulated/0/dctp

    # 文件保存在电脑中的目录
    TRANS_PIC_PATH = "F:/screenshot.png"
    # 获取截图的adb命令
    SCREEN_CAP_CMD = "adb shell screencap -p "
    # 传输文件至电脑adb命令
    TRANS_FILE_CMD = "adb pull "
    # 获取Android手机分辨率命令
    GET_RESOLUTION_CMD = "adb shell dumpsys window displays"
    # 模拟点击命令
    POINT_SCREEN_CMD = "adb shell input tap"
    # 手机分辨率
    phoneResolution = []

    def __init__(self,andriod="",trans=""):
        if andriod!="" and trans != "":
            # 判断是否为默认参数
            self.ANDRIOD_PIC_PATH = andriod
            self.TRANS_PIC_PATH = trans

    def getPhoneScreen(self):
        # 获取屏幕截图
        command = self.SCREEN_CAP_CMD + self.ANDRIOD_PIC_PATH
        system(command)

    def transPhoneScreen(self):
        # 将截图传输至电脑
        command = self.TRANS_FILE_CMD + self.ANDRIOD_PIC_PATH + " " +self.TRANS_PIC_PATH
        system(command)
        print("截图已获取")

    # 模拟点击某一位置
    def pointOnPhone(self,x=0.0,y=0.0):
        strX = str(x)
        strY = str(y)
        command = self.POINT_SCREEN_CMD + " " + strX + " " + strY
        system(command)
        pass

    # 获取屏幕分辨率
    def getPhoneResolution(self):
        # 获取命令行的打印值
        r = popen(self.GET_RESOLUTION_CMD)
        text = str(r.read())
        # 查找init=字符串,其后为手机分辨率情况
        beginC = text.find("init=")
        # 获取其后的10个字符
        line = text[beginC+5:beginC+15]
        resolution = line.split("x")
        self.phoneResolution.append(float(resolution[0]))
        self.phoneResolution.append(float(resolution[1]))
        print("weight =",self.phoneResolution[0],"\nheight =",self.phoneResolution[1])
        r.close()
        pass

    # 点击进攻按钮
    def pointAttackBtn(self):
        # 保留两位小数
        x = 0.07 * self.phoneResolution[1]
        y = 0.9 * self.phoneResolution[0]
        self.pointOnPhone(x,y)
        print("点击进攻按钮")

    # 点击搜索对手按钮
    def pointSearchAttacker(self):
        # 保留两位小数
        x = 0.77 * self.phoneResolution[1]
        y =  0.6* self.phoneResolution[0]
        self.pointOnPhone(x, y)
        print("点击搜索对手按钮")

    # 点击搜索下一个按钮
    def pointNextAttacker(self):
        # 保留两位小数
        x = 0.925 * self.phoneResolution[1]
        y = 0.732 * self.phoneResolution[0]
        self.pointOnPhone(x, y)
        print("点击搜索下一个按钮")

    # 点击结束战斗按钮
    def pointEndAttack(self):
        # 保留两位小数
        x = 0.075 * self.phoneResolution[1]
        y = 0.745 * self.phoneResolution[0]
        self.pointOnPhone(x, y)
        print("点击结束战斗按钮")


    def pointEndAttack(self):
        # 保留两位小数
        x = 0.075 * self.phoneResolution[1]
        y = 0.745 * self.phoneResolution[0]
        self.pointOnPhone(x, y)
        print("点击结束战斗按钮")

    def sure(self):
        # 保留两位小数
        x = 0.5953 * self.phoneResolution[1]
        y = 0.6611 * self.phoneResolution[0]
        self.pointOnPhone(x, y)
        print("点击确认")

    def backhome(self):
        # 保留两位小数
        x = 0.51 * self.phoneResolution[1]
        y = 0.90 * self.phoneResolution[0]
        self.pointOnPhone(x, y)
        print("点击回营")

    def huangmao_chose(self):
        # 保留两位小数
        x1 = 0.04895 * self.phoneResolution[1]
        y1 = 0.90 * self.phoneResolution[0]
        self.pointOnPhone(x1, y1)
        print("选择黄毛")

    def gongjianshou_chose(self):
        # 保留两位小数
        x1 = (0.04895 +0.071 ) * self.phoneResolution[1]
        y1 = 0.90 * self.phoneResolution[0]
        self.pointOnPhone(x1, y1)
        print("选择弓箭手")

    def gent_chose(self):
        # 保留两位小数
        x1 = (0.04895 +0.071*2 ) * self.phoneResolution[1]
        y1 = 0.90 * self.phoneResolution[0]
        self.pointOnPhone(x1, y1)
        print("选择巨人")


    def gebulin_chose(self):
        # 保留两位小数
        x1 = (0.04895 +0.071*3 ) * self.phoneResolution[1]
        y1 = 0.90 * self.phoneResolution[0]
        self.pointOnPhone(x1, y1)
        print("选择哥布林")

    def bumb_chose(self):
        # 保留两位小数
        x1 = (0.04895 +0.071*3 ) * self.phoneResolution[1]
        y1 = 0.90 * self.phoneResolution[0]
        self.pointOnPhone(x1, y1)
        print("选择选择炸弹人")

    def release_baby1(self):
        x1 = 0.1 * self.phoneResolution[1]
        y1 = 0.37 * self.phoneResolution[0]
        self.pointOnPhone(x1, y1)
        print("释放兵种1")
    def release_baby2(self):
        x1 = 0.1 * self.phoneResolution[1]
        y1 = 0.37 * self.phoneResolution[0]
        self.pointOnPhone(x1, y1)
        print("释放兵种2")
    def release_baby3(self):
        x1 = 0.787 * self.phoneResolution[1]
        y1 = 0.798 * self.phoneResolution[0]
        self.pointOnPhone(x1, y1)
        print("释放兵种3")

未完待续……

你可能感兴趣的:(python,coc,辅助,coc,游戏辅助,python)