当前位置: 移动技术网 > IT编程>开发语言>Java > java:神武防掉线挂机小程序

java:神武防掉线挂机小程序

2020年08月01日  | 移动技术网IT编程  | 我要评论
需求游戏中一段时间不操作,会自动掉线,本程序模拟鼠标点击操作,实现自动挂机、自动刷怪、副本挂机三个功能。实现定时点击屏幕固定位置,实现自动加血、自动打开副本宝箱、自动移动等功能。由于点击位置固定,所以需要将游戏窗口放在屏幕左上角。程序分为四个小模块:RobotUtils:鼠标点击工具类,模拟鼠标点击操作。Show:用于显示操作窗口。GuajiService:主要模块,根据不同模式使用不同策略定时点击不同位置。GuajiControler:程序启动入口。代码实现Robo

需求

游戏中一段时间不操作,会自动掉线,本程序模拟鼠标点击操作,实现自动挂机、自动刷怪、副本挂机三个功能。

实现

定时点击屏幕固定位置,实现自动加血、自动打开副本宝箱、自动移动等功能。由于点击位置固定,所以需要将游戏窗口放在屏幕左上角。

程序分为四个小模块:

RobotUtils:鼠标点击工具类,模拟鼠标点击操作。

Show:用于显示操作窗口。

GuajiService:主要模块,根据不同模式使用不同策略定时点击不同位置。

GuajiControler:程序启动入口。

代码实现

RobotUtils

package com.wzx.shenwu.utils;

import java.awt.*;
import java.awt.event.InputEvent;

import static java.lang.Thread.sleep;

public class RobotUtils {
    //点击左键
    static public void leftClick(Robot robot){
        // 模拟鼠标按下左键
        robot.mousePress(InputEvent.BUTTON1_MASK);
        try {
            sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        // 模拟鼠标松开左键
        robot.mouseRelease(InputEvent.BUTTON1_MASK);
    }

    //点击右键
    static public void rightClick(Robot robot){
        robot.mousePress(InputEvent.BUTTON3_MASK);
        robot.mouseRelease(InputEvent.BUTTON3_MASK);
    }
}

Show

package com.wzx.shenwu.ui;

import com.wzx.shenwu.service.GuajiService;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Show {
    static JButton randomBt;
    static JButton fixedBt;
    static JButton fubenBt;
    static JButton endBt;
    private int flag ;
    private ActionListener actionListener;
    JDialog dialog = null;

    //创建监听器
    private void initActionListener(){
        final GuajiService service = GuajiService.getService();
        actionListener = new ActionListener(){

            public void actionPerformed(ActionEvent e) {
                //点击固定选项
                if (e.getSource() == fixedBt) {
                    showMsg("即将开始挂机,请将自动战斗窗口放在左下角");
                    flag = 1;
                    service.setFlag(flag);
                    service.setIfRun(true);
                    service.getThread().start();
                }

                //点击随机选项
                if (e.getSource() == randomBt){
                    showMsg("即将开始挂机,请将自动战斗窗口放在左下角");
                    flag = 2;
                    service.setFlag(flag);
                    service.setIfRun(true);
                    service.getThread().start();
                }

                //点击副本
                if (e.getSource() == fubenBt){
                    showMsg("即将开始挂机,请将自动战斗窗口放在左下角");
                    flag = 3 ;
                    service.setFlag(flag);
                    service.setIfRun(true);
                    service.getThread().start();
                }

                if (e.getSource() == endBt) {
                    service.setIfRun(false);
                    service.getThread().stop();
                    showMsg("结束");
                    closeWindow();
                }


            }
        };
    }

    //显示界面
     public void showMain() {
        dialog = new JDialog();
        // 设置大小
        dialog.setSize(320, 100);
        // 设置标题
        dialog.setTitle("界面");

        randomBt = new JButton("防掉");
        fixedBt = new JButton("挂机");
        fubenBt = new JButton("副本");
        endBt = new JButton("结束");
        // 绑定监听
         initActionListener();
        randomBt.addActionListener(actionListener);
        fixedBt.addActionListener(actionListener);
        fubenBt.addActionListener(actionListener);
        endBt.addActionListener(actionListener);

        randomBt.setBounds(35, 10, 60, 40);
        fixedBt.setBounds(95, 10, 60, 40);
        fubenBt.setBounds(155,10,60,40);
        endBt.setBounds(215, 10, 60, 40);
        // 设置布局为空,使用坐标控制控件位置的时候,一定要设置布局为空
        dialog.setLayout(null);
        // 添加控件
        dialog.add(randomBt);
        dialog.add(fixedBt);
        dialog.add(fubenBt);
        dialog.add(endBt);
        // 设置dislog的相对位置,参数为null,即显示在屏幕中间
        dialog.setLocationRelativeTo(null);
        // 设置当用户在此对话框上启动 "close" 时默认执行的操作
        dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        // 设置是否显示
        dialog.setVisible(true);
    }
    //弹出提示框
    static public void showMsg(String msg) {
        JOptionPane.showMessageDialog(null, msg, "提示信息",
                JOptionPane.PLAIN_MESSAGE);
    }

    //关闭窗口
    public void closeWindow(){
        dialog.dispose();
    }
}

GuajiService

package com.wzx.shenwu.service;

import java.awt.*;

import static com.wzx.shenwu.utils.RobotUtils.leftClick;
import static com.wzx.shenwu.utils.RobotUtils.rightClick;

public class GuajiService {
    private GuajiService(){
    }

    private static final GuajiService service = new GuajiService();
    public static GuajiService getService(){
        return service;
    }


    //挂机类型标记
    int flag;
    public void setFlag(int flag){
        this.flag = flag;
    }
    //线程开关标记
    private boolean ifRun = false;
    public void setIfRun(boolean ifRun){
            this.ifRun = ifRun;
    }
    //提供线程访问接口
    public Thread getThread(){
        return thread;
    }
    private Thread thread = new Thread(new Runnable() {
        public void run() {
            Robot robot = null;

            try {
                robot = new Robot();
            } catch (AWTException e) {
                e.printStackTrace();
            }
            //固定
            if (flag==1){
                /*
                Point point = MouseInfo.getPointerInfo().getLocation();
                int x = (int) point.getX();
                int y = (int) point.getY();
                */
                while (ifRun) {
                    // 模拟鼠标按下左键
                    //robot.mousePress(InputEvent.BUTTON1_MASK);
                    // 模拟鼠标松开左键
                    //robot.mouseRelease(InputEvent.BUTTON1_MASK);
                    //leftClick(robot);
                    //sleep(100);
                    //自动回血回蓝
                    addHPMP(robot);
                    sleep(10000);

                }
            }
            // 随机
            if (flag == 2){
                while (ifRun) {
                    //随机获取鼠标位置,已废弃
                    //Random random = new Random();
                    //robot.mouseMove(random.nextInt(710)+30, random.nextInt(430)+150);

                    //回血回蓝
                    addHPMP(robot);
                    sleep(1000);

                    //四点循环点击
                    robot.mouseMove(200,160);
                    leftClick(robot);
                    sleep(5000);

                    robot.mouseMove(635,175);
                    leftClick(robot);
                    sleep(5000);

                    robot.mouseMove(200,475);
                    leftClick(robot);
                    sleep(5000);

                    robot.mouseMove(700,325);
                    leftClick(robot);
                    sleep(100);
                    robot.mouseMove(640,500);
                    leftClick(robot);

                    sleep(5000);
                }
            }

            //副本挂机
            if (flag == 3){
                while (ifRun) {
                    //自动回血回蓝
                    addHPMP(robot);
                    sleep(10000);
                    //点击第二个宝箱
                    robot.mouseMove(390, 330);
                    leftClick(robot);
                    sleep(1000);
                    //关闭宝箱界面
                    robot.mouseMove(600, 190);
                    leftClick(robot);
                    sleep(1000);
                }
            }

        }
    });

     private void sleep(int time){
         try {
             thread.sleep(time);
         } catch (InterruptedException e) {
             e.printStackTrace();
         }
     }

    //自动回血回蓝
      public  void addHPMP(Robot robot){
        //回血765 33
        robot.mouseMove(765,33);
        rightClick(robot);
        sleep(100);
        //回蓝765 45
        robot.mouseMove(765,45);
        rightClick(robot);
        sleep(100);
        //BB回血645 33
        robot.mouseMove(645,33);
        rightClick(robot);
        sleep(100);
        //BB回蓝
        robot.mouseMove(640,45);
        rightClick(robot);
        sleep(100);
        //自动战斗
        robot.mouseMove(155,580);
        leftClick(robot);
        sleep(100);
    }
}

GuajiControler

package com.wzx.shenwu.controler;

import com.wzx.shenwu.ui.Show;

public class GuajiControler {
    public static void main(String[] args) {
        //创建窗口
        Show show = new Show();
        show.showMain();

    }
}

实现效果

1.将游戏界面设置为最小,并放在屏幕左上角,如果是双屏幕,则放在主屏左上角。

2.启动程序,弹出选择窗口。

3.防掉:随机游走,并自动加血加蓝,点击到NPC对话时,会自动取消与NPC的对话窗口,适合在野外挂机。挂机:定时加血加蓝,将自动战斗窗口放在游戏左下角,会自动重置挂机回合。副本:自动加血加蓝,遇到宝箱自动选择第二个宝箱。

 

 

 

 

 

 

 

 

 

本文地址:https://blog.csdn.net/x950913/article/details/108187129

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网