吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 5809|回复: 11
收起左侧

[Python 原创] [原创]在雷电模拟器玩2048[源码]

[复制链接]
zerglurker 发表于 2018-12-20 18:13
首先参考我的上一个帖子 雷电模拟器说明附代码
里面的东西是这一篇的前提
有人问那一篇有什么用,这里主要是说一下有什么用
2048使用这个包名的版本:com.digiplex.game
广告最少,干扰最小
分辨率开1080*1920*480
目前用的贪婪算法,思考深度是1
基本可以玩到128,运气好到512
===
[Python] 纯文本查看 复制代码
import time

from Dnconsole import Dnconsole
import cv2 as cv


def compare_status(st0: list, st1: list) -> bool:  # True相同 False不同
    if len(st0) != len(st1):
        return False
    for i in range(len(st0)):
        if len(st0[i]) != len(st1[i]):
            return False
        for j in range(len(st0[i])):
            if st0[i][j] != st1[i][j]:
                return False
    return True


def copy_statu(status: list) -> list:
    result = list()
    for line in status:
        result.append([line[0], line[1], line[2], line[3]])
    return result


def read_num(area: tuple, screen: str, template: list) -> int:
    scr = cv.imread(screen)
    cur_area = scr[area[1]:area[3], area[0]:area[2]]  # 前面是y轴 后面是x轴
    for i, tmp in enumerate(template):
        tp = cv.imread(tmp)
        try:
            result = cv.matchTemplate(cur_area, tp, cv.TM_SQDIFF_NORMED)
            min_val, max_val, min_loc, max_loc = cv.minMaxLoc(result)
            if min_val < 0.001:
                return int(template[i].replace('2048/', '').replace('.png', ''))
        except cv.error:
            continue
    return -1


def get_cur_status() -> list:
    result = list()
    result.append([0, 0, 0, 0])
    result.append([0, 0, 0, 0])
    result.append([0, 0, 0, 0])
    result.append([0, 0, 0, 0])
    pos = [30, 260, 240, 470]  # , [300, 260, 510, 470], [570, 260, 780, 470], [840, 260, 1050, 470]]
    template = ['2048/0.png', '2048/2.png', '2048/4.png', '2048/8.png', '2048/16.png', '2048/32.png',
                '2048/64.png', '2048/128.png', '2048/256.png', '2048/512.png', '2048/1024.png']
    screen = 'E:/Project/PyCharmProj/Script/2048/share/2048.png'
    for i in range(4):
        for j in range(4):
            area = (pos[0] + 270 * j, pos[1] + 270 * i, pos[2] + 270 * j, pos[3] + 270 * i)
            result[i][j] = read_num(area, screen, template)
    return result


def swipe_up():
    Dnconsole.swipe(0, (540, 1280), (540, 260))
    print('上移')


def swipe_down():
    Dnconsole.swipe(0, (540, 260), (540, 1280))
    print('下移')


def swipe_left():
    Dnconsole.swipe(0, (1000, 770), (100, 770))
    print('左移')


def swipe_right():
    Dnconsole.swipe(0, (100, 770), (1000, 770))
    print('右移')


def get_null_count(status: list) -> int:
    if len(status) == 0:
        print('此方向移动无效')
        return -1
    result = 0
    for row in status:
        for node in row:
            if node == 0:
                result += 1
    return result


def get_sum(line: list) -> int:
    total = 0
    for i in range(len(line)):
        total += line[i]
    return total


def compress_line(line: list) -> list:
    # 压实一条数据
    length = len(line)
    result = line[:]
    if get_sum(line) == 0:  # 全零则直接返回
        return line
    for i in range(length):
        if get_sum(result[i:]) == 0:
            break
        while result[i] == 0:
            for j in range(i, length - 1):
                result[j] = result[j + 1]
                result[j + 1] = 0
    return result


def combine_line(line: list) -> list:
    # 合并一条数据
    length = len(line)
    result = line[:]
    for i in range(length - 1):
        if result[i] == result[i + 1]:
            result[i] *= 2
            result[i + 1] = 0
    return result


def deduction_up(status: list) -> list:
    result = copy_statu(status)
    # 先把数据压实,再合并
    for i in range(4):
        line = [result[0][i], result[1][i], result[2][i], result[3][i]]
        line = compress_line(line)  # 压实(去掉为0的格子)
        line = combine_line(line)  # 合并
        line = compress_line(line)  # 压实
        result[0][i], result[1][i], result[2][i], result[3][i] = line[0], line[1], line[2], line[3]
    if compare_status(result, status):
        return []
    return result


def deduction_down(status: list) -> list:
    result = copy_statu(status)
    # 先把数据压实,再合并
    for i in range(4):
        line = [result[3][i], result[2][i], result[1][i], result[0][i]]
        line = compress_line(line)  # 压实(去掉为0的格子)
        line = combine_line(line)  # 合并
        line = compress_line(line)  # 压实
        result[3][i], result[2][i], result[1][i], result[0][i] = line[0], line[1], line[2], line[3]
    if compare_status(result, status):
        return []
    return result


def deduction_left(status: list) -> list:
    result = copy_statu(status)
    # 先把数据压实,再合并
    for i in range(4):
        line = [result[i][0], result[i][1], result[i][2], result[i][3]]
        line = compress_line(line)  # 压实(去掉为0的格子)
        line = combine_line(line)  # 合并
        line = compress_line(line)  # 压实
        result[i][0], result[i][1], result[i][2], result[i][3] = line[0], line[1], line[2], line[3]
    if compare_status(result, status):
        return []
    return result


def deduction_right(status: list) -> list:
    result = copy_statu(status)
    # 先把数据压实,再合并
    for i in range(4):
        line = [result[i][3], result[i][2], result[i][1], result[i][0]]
        line = compress_line(line)  # 压实(去掉为0的格子)
        line = combine_line(line)  # 合并
        line = compress_line(line)  # 压实
        result[i][3], result[i][2], result[i][1], result[i][0] = line[0], line[1], line[2], line[3]
    if compare_status(result, status):
        return []
    return result


def run():
    if Dnconsole.is_running(0) is False:
        Dnconsole.launch(0)
    for i in range(120):
        if Dnconsole.is_running(0):
            time.sleep(10)
            break
        time.sleep(1)
    Dnconsole.invokeapp(0, 'com.digiplex.game')
    end_status = [[-1, -1, -1, -1], [-1, -1, -1, -1], [-1, -1, -1, -1], [-1, -1, -1, -1]]
    if Dnconsole.wait_activity(0, 'com.digiplex.game/.MainActivity', 20) is True:
        while True:
            Dnconsole.dnld(0, 'screencap -p /sdcard/Pictures/2048.png')
            time.sleep(1)
            status = get_cur_status()
            if compare_status(end_status, status):
                print('结束')
                break
            print('status=', status)
            func = [deduction_up, deduction_down, deduction_left, deduction_right]
            swipe = [swipe_up, swipe_down, swipe_left, swipe_right]
            max_count = 0
            direction = -1
            for i in range(4):
                c = get_null_count(func[i](status))
                if c > max_count:
                    direction = i
                    max_count = c
            if direction == -1:
                print('结束')
                return
            swipe[direction]()
            time.sleep(1.5)
            

===

64

64

128

128

256

256

32

32

16

16

512

512

8

8

4

4
0.png

2

2

免费评分

参与人数 2吾爱币 +6 热心值 +2 收起 理由
a5606495 + 1 + 1 谢谢@Thanks!
苏紫方璇 + 5 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

rewqfdsa036 发表于 2018-12-20 21:51
雷电广告虽少,但是win7安装雷电模拟,试了七、八个电视tv软件安装后,打不开视频!打游戏戏还可以https://www.52pojie.cn/thread-806714-1-1.html
 楼主| zerglurker 发表于 2018-12-20 18:38
补充一个1024的识别附件,上传的时候选择了,但是好像没有上传成功
这些都是用于做模板,进行比对的
通过比对来识别文字
1024.png
罗茂松 发表于 2018-12-20 18:27
wangqiustc 发表于 2018-12-20 18:41
Python已经无所不能了
gunxsword 发表于 2018-12-20 18:50
脚本收下了,学习一下,感谢分享!
19930111 发表于 2018-12-20 19:11
谢谢分享   谢谢论坛   
湖北吴彦祖 发表于 2018-12-20 20:40
虽然看不懂,但是很强。。。。
airwenlee 发表于 2018-12-20 20:52
前阵子雷电模拟器上被封了一个微信小号,也不靠谱啊
kof97 发表于 2018-12-20 21:24

虽然看不懂,但是很强。。。。
leonwqhb 发表于 2018-12-20 21:49
这个是游戏本身。还是辅助用的
不过这个游戏真的是有毒,一上手就停不下来
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-15 23:52

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表