Java Robot 编写脚本机器人(四)利用Java帮你打英雄联盟

很久以前写的一个自动操控英雄脚本(当时适用于扭曲丛林刷金币),原理是循环让鼠标不停的按键啊,只供参考

import java.awt.AWTException;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.Robot;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;

import org.junit.Test;

public class Main {
    static boolean ifRun = true;
    static JButton startBt;
    static JButton endBt;


    public static void main(String[] args) {
        // 显示主界面
        showMain();

    }

    static Thread thread = new Thread(new Runnable() {

        @Override
        public void run() {
            Robot robot = null;
            try {
                robot = new Robot();
            } catch (AWTException e) {
                e.printStackTrace();
            }
            // 模拟移动到当前鼠标位置
            while (ifRun) {
                Point point = MouseInfo.getPointerInfo().getLocation();
                System.out.println("x=" + point.getX() + ",y=" + point.getY());
                /* robot.mouseMove((int)1829.0, (int) 988.0);
                robot.mouseMove((int) point.getX(), (int) point.getY());/*
                // 模拟鼠标按下左键
                robot.mousePress(InputEvent.BUTTON1_MASK);
                // 模拟鼠标松开左键
                robot.mouseRelease(InputEvent.BUTTON1_MASK);/**/
                // 模拟鼠标按下右键
                /* robot.mousePress(InputEvent.BUTTON3_MASK);
                // 模拟鼠标松开右键
                robot.mouseRelease(InputEvent.BUTTON3_MASK); */

                try {
                    thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }

        }
    });

    static public void showMsg(String msg) {
        JOptionPane.showMessageDialog(null, msg, "提示信息",
                JOptionPane.PLAIN_MESSAGE);
    }

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

        startBt = new JButton("开始");
        endBt = new JButton("结束");
        // 绑定监听
        startBt.addActionListener(actionListener);
        endBt.addActionListener(actionListener);
        startBt.setBounds(35, 10, 60, 40);
        endBt.setBounds(90, 10, 60, 40);
        // 设置布局为空,使用坐标控制控件位置的时候,一定要设置布局为空
        dialog.setLayout(null);
        // 添加控件
        dialog.add(startBt);
        dialog.add(endBt);
        // 设置dislog的相对位置,参数为null,即显示在屏幕中间
        dialog.setLocationRelativeTo(null);
        // 设置当用户在此对话框上启动 "close" 时默认执行的操作
        dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        // 设置是否显示
        dialog.setVisible(true);
    }

    static ActionListener actionListener = new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {

            if (e.getSource() == startBt) {
                showMsg("已开始——————请在三秒内点击");
                new Timer().schedule(new TimerTask() {
                    @Override
                    public void run() {
                        showMsg("已开始获取鼠标位置并已启动线程");
                        thread1.start();
                    }
                }, 3000);

            }

            if (e.getSource() == endBt) {
                ifRun = false;
                showMsg("结束");
            }
        }
    };

    static public void sss() throws AWTException{
    	Robot robot = new Robot();
        Point point = MouseInfo.getPointerInfo().getLocation();
        
        
        robot.mousePress(InputEvent.BUTTON1_MASK);
        robot.mouseRelease(InputEvent.BUTTON1_MASK);
        //鼠标按下扭曲丛林坐标
        point.setLocation(740.0,408.0);
        //鼠标按下扭曲丛林确认坐标
        point.setLocation(893.0,872.0);
        //鼠标按下寻找对局
        point.setLocation(893.0,872.0);
    }
    static Thread thread1 = new Thread(new Runnable() {

        @Override
        public void run() {
            Robot robot = null;
            try {
                robot = new Robot();
            } catch (AWTException e) {
                e.printStackTrace();
            }
            int count = 0;
            boolean isFirend = false;
            boolean isFirend1 = false;
            boolean isFirend2 = false;
            // 模拟移动到当前鼠标位置
            while (ifRun) {
                Point point = MouseInfo.getPointerInfo().getLocation();
                robot.mouseMove((int) point.getX(), (int) point.getY());
                System.out.println("已经开始-"+count*3+"-秒");
                //在客户端15秒开始游戏
                if(count==5){
                	//鼠标15秒后按下play坐标
                	robot.mouseMove((int)458.0,(int)226.0);
                }else if(count == 6){
                    //鼠标按下扭曲丛林坐标
                    robot.mouseMove((int)740.0,(int)408.0);
                }else if(count == 7){
                    //鼠标按下扭曲丛林确认坐标
                    robot.mouseMove((int)893.0,(int)872.0);
                }else if(count == 8){
                    //鼠标按下寻找对局
                    robot.mouseMove((int)893.0,(int)872.0);//如果是低于优先级队列
                }else if(count > 9 && !isFirend){
                    //匹配到好友接受请求
                	isFirend = true;
                    robot.mouseMove((int)992.0,(int)731.0);
                }else if(isFirend&&!isFirend2){
                	//选择英雄
                	isFirend = false;
                    robot.mouseMove((int)730.0,(int)327.0);
                }if(count>150&&count<600&&!isFirend2){
                	//游戏开始,移动英雄
                	int x = count%2 == 0 ? (int) 1829.0 : (int) 988.0;
                	int y = count%2 == 0 ? (int) 988.0 : (int) 1829.0;
                	robot.mouseMove(x,y);
                    //模拟鼠标按下右键
                    robot.mousePress(InputEvent.BUTTON3_MASK);
                    // 模拟鼠标松开右键
                    robot.mouseRelease(InputEvent.BUTTON3_MASK);
                    try {
						thread1.sleep(3000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
                    count++;
                    continue;
                }else if(count>600&&!isFirend1){//游戏结束
                	isFirend2 = true;
                	isFirend1 = true;
                	//赞玩家
                    robot.mouseMove((int)992.0,(int)828.0); 
                }else if(isFirend1){
                	isFirend = false;
                	isFirend1 = false;
                	isFirend2 = false;
                	count = 0;
                	//
                    robot.mouseMove((int)898.0,(int)861.0); 
                }
                //赞玩家898.0,861.0
                //再玩一次898.0,861.0
                // 模拟鼠标按下左键
                robot.mousePress(InputEvent.BUTTON1_MASK);
                // 模拟鼠标松开左键
                robot.mouseRelease(InputEvent.BUTTON1_MASK);
                // 模拟鼠标按下右键
                /*robot.mousePress(InputEvent.BUTTON3_MASK);
                // 模拟鼠标松开右键
                robot.mouseRelease(InputEvent.BUTTON3_MASK);*/
                try {
                	count++;
                    thread1.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }

        }
    });
    
}
  • 3
    点赞
  • 43
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
编写游戏脚本需要以下步骤: 1. 确定需要自动化的操作或任务。 2. 在Java环境中安装相关的库,例如Selenium、Java Robot等。 3. 使用Selenium或Java Robot等库,获取游戏界面的信息和操作游戏。 4. 编写脚本,实现自动化的操作或任务,例如自动化采集、自动化战斗等。 5. 调试脚本,检查是否有错漏,优化脚本的性能和可靠性。 下面以使用Selenium编写游戏自动化脚本为例,给出详细的步骤: 1. 安装Java环境和Selenium库。 2. 打开Eclipse或其他Java IDE,新建Java项目。 3. 在项目中导入Selenium库。 4. 编写Java代码,使用Selenium打开游戏界面,定位游戏元素,实现自动化操作。 ```java import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; public class GameScript { public static void main(String[] args) throws InterruptedException { //设置Chrome驱动的路径 System.setProperty("webdriver.chrome.driver", "D:/chromedriver.exe"); //创建ChromeDriver对象 WebDriver driver = new ChromeDriver(); //打开游戏界面 driver.get("http://www.game.com"); //等待游戏加载完成 Thread.sleep(5000); //定位游戏元素 WebElement element = driver.findElement(By.id("game_element_id")); //实现自动化操作 element.click(); //关闭浏览器 driver.quit(); } } ``` 以上代码的作用是使用ChromeDriver打开游戏界面,等待5秒钟,定位游戏元素并进行点击操作,最后关闭浏览器。 需要注意的是,游戏自动化脚本编写需要遵守游戏的规则和法律法规,不得进行任何违规操作。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

terrybg

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值