用Kotlin破解Android版微信小遊戲-跳一跳

codeGoogle發表於2018-01-03

前言

微信又更新了,從更新日誌上來看,似乎只是一次不痛不癢的小更新. 不過,很快就有人發現,原來微信這次搞了個大動作——在小程式里加入了小遊戲。今天也是朋友圈被刷爆的緣故。

看到網上 有人弄了一個破解版的,於是自己也跟著網上的案例整了一下,感覺挺有意思的。

遊戲如下:

來玩遊戲

勞動成果

用Kotlin破解Android版微信小遊戲-跳一跳

跳一跳

微信小程式可以玩遊戲了,我們來破解一下《跳一跳》這個官方出品的小遊戲吧。

思路

用usb除錯安卓手機,用adb截圖並用滑鼠測量距離,然後計算按壓時間後模擬按壓。

$ adb shell input swipe <x1> <y1> <x2> <y2> [duration(ms)] (Default: touchscreen) # 模擬長按
$ adb shell screencap <filename> # 儲存截圖到手機
$ adb pull /sdcard/screen.png # 下載截圖檔案到本地
複製程式碼
主要核心
  • 得到手指按的時間 t
  • 時間 = 距離 / 速度(常量) t = L / k
  • L = p2 - p1
  • 獲取到起始點和結束點的座標

原始碼

開發環境: Kotlin, IetelliJ IDEA

github.com/iOSDevLog/J…

主要原始碼

fun main(args: Array<String>) {
    val jumpjump = JumpJump()
    var isFirst = true
    var firstPoint: Point? = null
    var secondPoint: Point?

    val jPanel = object : JPanel() {
        override fun paintComponent(g: Graphics) {
            super.paintComponent(g)
            try {
                var bufferedImage = ImageIO.read(File(SCREENSHOT_LOCATION))
                val newImage = BufferedImage(675, 1200, bufferedImage.getType())
                val gTemp = newImage.graphics
                gTemp.drawImage(bufferedImage, 0, 0, 675, 1200, null)
                gTemp.dispose()
                bufferedImage = newImage
                g.drawImage(bufferedImage, 0, 0, null)
            } catch (e: IOException) {
                e.printStackTrace()
            }
        }
    }

    jPanel.addMouseListener(object : MouseListener {
        override fun mouseReleased(e: MouseEvent?) {
        }

        override fun mouseEntered(e: MouseEvent?) {
        }

        override fun mouseClicked(e: MouseEvent?) {
        }

        override fun mouseExited(e: MouseEvent?) {
        }

        override fun mousePressed(e: MouseEvent?) {
            println("mousePressed")
            e.let {
                if (isFirst) {
                    println("first {pomt" + e!!.x + " " + e.y)
                    firstPoint = e.point
                    isFirst = false
                } else {
                    secondPoint = e!!.point
                    val distance = distance(firstPoint!!, secondPoint!!)
                    println("distance:" + distance)
                    isFirst = true
                    //magic number
                    call(distance * 2.2)
                    try {
                        // wait for screen cap
                        Thread.sleep(2500)
                    } catch (e1: InterruptedException) {
                        e1.printStackTrace()
                    }

                    printScreen()

                    jPanel.validate()
                    jPanel.repaint()
                }
            }
        }

    })

    jumpjump.isVisible = true
    jumpjump.contentPane.add(jPanel)

    printScreen()
    jumpjump.repaint()
    jumpjump.validate()
}

fun distance(a: Point, b: Point): Int {
    return Math.sqrt((a.x - b.getX()) * (a.x - b.getX()) + (a.y - b.getY()) * (a.y - b.getY())).toInt()
}
複製程式碼

在java中是如何操作的?

BackgroundImage4Panel.java
public class BackgroundImage4Panel extends javax.swing.JFrame {
    /**
     * serialVersionId
     */
    private static final long serialVersionUID = 1L;

    private static boolean isFirst = true;

    private static Point firstPoint;
    private static Point secondPoint;

    /**
     * Creates new form NewJFrame
     */
    public BackgroundImage4Panel() {
        setSize(675, 1200);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    /**
     * 測試入口
     *
     * @param args 引數列表
     */
    public static void main(String[] args) {
        AdbCaller.printScreen();
        final BackgroundImage4Panel backgroundImage4Panel = new BackgroundImage4Panel();
        backgroundImage4Panel.setVisible(true);

        JPanel jPanel = new JPanel() {
            /**
             * serialVersionId
             */
            private static final long serialVersionUID = -1183754274585001429L;

            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                try {
                    BufferedImage bufferedImage = ImageIO.read(new File(Constants.SCREENSHOT_LOCATION));
                    BufferedImage newImage = new BufferedImage(675, 1200, bufferedImage.getType());
                    /**
                     * try to resize
                     */
                    Graphics gTemp = newImage.getGraphics();
                    gTemp.drawImage(bufferedImage, 0, 0, 675, 1200, null);
                    gTemp.dispose();
                    bufferedImage = newImage;
                    g.drawImage(bufferedImage, 0, 0, null);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        };
        backgroundImage4Panel.getContentPane().add(jPanel);

        backgroundImage4Panel.getContentPane().getComponent(0).addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (isFirst) {
                    System.out.println("first " + e.getX() + " " + e.getY());
                    firstPoint = e.getPoint();
                    isFirst = false;
                } else {
                    secondPoint = e.getPoint();
                    int distance = distance(firstPoint, secondPoint);
                    System.out.println("distance:" + distance);
                    isFirst = true;
                    AdbCaller.call(distance * 2.19);//magic number
                    try {
                        Thread.sleep(2500);// wait for screencap
                    } catch (InterruptedException e1) {
                        e1.printStackTrace();
                    }
                    AdbCaller.printScreen();
                    JPanel jp = ((JPanel) backgroundImage4Panel.getContentPane().getComponent(0));
                    jp.validate();
                    jp.repaint();
                }

            }

            @Override
            public void mousePressed(MouseEvent e) {

            }

            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mouseEntered(MouseEvent e) {

            }

            @Override
            public void mouseExited(MouseEvent e) {

            }
        });
    }

    /**
     * 實現影象的等比縮放
     *
     * @param source  待處理的圖片流
     * @param targetW 寬度
     * @param targetH 高度
     * @return
     */
    public static BufferedImage resize(BufferedImage source, int targetW, int targetH) {
        int width = source.getWidth();// 圖片寬度
        int height = source.getHeight();// 圖片高度
        return zoomInImage(source, targetW, targetH);
    }

    /**
     * 對圖片進行強制放大或縮小
     *
     * @param originalImage 原始圖片
     * @return
     */
    public static BufferedImage zoomInImage(BufferedImage originalImage, int width, int height) {
        BufferedImage newImage = new BufferedImage(width, height, originalImage.getType());

        Graphics g = newImage.getGraphics();
        g.drawImage(originalImage, 0, 0, width, height, null);
        g.dispose();
        return newImage;
    }

    public static int distance(Point a, Point b) {//求兩點距離
        return (int) Math.sqrt((a.x - b.getX()) * (a.x - b.getX()) + (a.y - b.getY()) * (a.y - b.getY()));
    }
}

複製程式碼

AdbCaller.java

public class AdbCaller {

    public static void call(double timeMilli) {
        try {
            Runtime.getRuntime().exec(Constants.ADB_PATH + " shell input touchscreen swipe 170 187 170 187 " + (int) timeMilli);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void printScreen() {
        try {
            Process p1 = Runtime.getRuntime().exec(Constants.ADB_PATH + " shell screencap -p /sdcard/screenshot.png");
            p1.waitFor();
            Process p2 = Runtime.getRuntime().exec(Constants.ADB_PATH + " pull /sdcard/screenshot.png " + Constants.SCREENSHOT_LOCATION);
            p2.waitFor();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
複製程式碼
Constants.java
public class Constants {
    /**
     * adb所在位置
     */
    public static final String ADB_PATH = "E:\\android_install_64\\sdk\\platform-tools\\adb.exe";
    /**
     * 截圖檔案所在位置
     */
    public static final String SCREENSHOT_LOCATION = "E:\\android_install_64\\sdk\\platform-tools\\yyh.png";
}

複製程式碼

使用方法

  • 在電腦上下載好adb
  • 開啟安卓手機的usb除錯模式並授權連線的電腦
  • 開啟微信跳一跳,並點選開始
  • 在Constans.java中配置好adb路徑與截圖路徑,執行
  • 在彈出的視窗中先點選小人底部適當位置,然後再點想要跳的箱子的位置即可完成

參考

github.com/easyworld/P…

iosdevlog.com/2017/12/29/…

關於更多

分分鐘教你學會正規表示式

2017上半年技術文章集合—184篇文章分類彙總

高階UI特效仿直播點贊效果—一個優美炫酷的點贊動畫

一個實現錄音和播放的小案例

NDK專案實戰—高仿360手機助手之解除安裝監聽

相信自己,沒有做不到的,只有想不到的

微信公眾號:終端研發部

技術+職場

相關文章