最近练了一个品克缤,抗性很高,发现手动比较累,但是又不想买辅助。所以就写了一段代码来控制键盘完成自动攻击,加血加蓝。

由于不能适应血条到一定程度加血,所以以下代码只适合品克缤这一角色,只适用于怪物在一个平台的,比如莎翁小镇2 比如 哈闷。

大体思路就是,一个动作视为一个线程,往左移动和往右移动由于同时只做一件事,所以往左移动和往右移动写在一个线程里。

以下代码只能前台控制,即冒险岛为主要窗口的时候有效。

更加复杂的键盘鼠标操作可以使用,按键精灵,但是按键精灵只支持单线程,就是次序执行的,所以什么时候加血,什么时候加蓝,什么时候加BUFF会乱。

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.util.Date;
/**
 * 左右移动攻击,在指定时间内加血加蓝加buff
 * @author Administrator
 *
 */
public class KeyRunnable{
    public static void main(String[] args){
          Robot myRobot;
          try {
          myRobot = new Robot();
          //加载左右移动线程
          KeyLeftAndRight lr = new KeyLeftAndRight(myRobot);
          Thread thread1 = new Thread(lr);
          //加载攻击线程
          KeyA a = new KeyA(myRobot);
          Thread thread2 = new Thread(a);
          //加载加血线程
          KeyRed red = new KeyRed(myRobot);
          Thread thread3 = new Thread(red);
          //加载加蓝线程
          KeyBlue blue = new KeyBlue(myRobot);
          Thread thread4 = new Thread(blue);
          //加载加buff线程
          KeyBuff buff = new KeyBuff(myRobot);
          Thread thread5 = new Thread(buff);
          
          //启动所有线程
          thread1.start();
          thread2.start();
          thread3.start();
          thread4.start();
          thread5.start();
          
          } catch (AWTException e) {
                e.printStackTrace();
          } 
    }
}
/**
 * 向左移动7秒,向右移动7秒
 * @author Administrator
 *
 */
class KeyLeftAndRight implements Runnable{
    private Robot r;
    public KeyLeftAndRight(Robot robot){
        this.r = robot;
    }
    @Override
    public void run() {
        while(true){
            try {
                long begin = System.currentTimeMillis();
                long end = System.currentTimeMillis();
                while((end-begin)/1000<7){
                    pressLeft(r);
                    end = System.currentTimeMillis();
                    System.out.println("左边"+new Date());
                    Thread.sleep((int)(Math.random()*100+150));
                }
                while((end-begin)/1000<(7+7)){
                    pressRight(r);
                    end = System.currentTimeMillis();
                    System.out.println("右边"+new Date());
                    Thread.sleep((int)(Math.random()*100+150));
                }    
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    
    public static void pressLeft(Robot r){
        r.keyPress(KeyEvent.VK_LEFT);
        r.keyRelease(KeyEvent.VK_LEFT);    
    }
    public static void pressRight(Robot r){
        r.keyPress(KeyEvent.VK_RIGHT);
        r.keyRelease(KeyEvent.VK_RIGHT);    
    }
}

/**
 * 攻击按键 线程类 设置A为攻击键
 * @author Administrator
 *
 */
class KeyA implements Runnable{
    private Robot r;
    public KeyA(Robot robot){
        this.r = robot;
    }
    @Override
    public void run() {
        while(true){
            r.keyPress(KeyEvent.VK_A);
            r.keyRelease(KeyEvent.VK_A);
            try {
                Thread.sleep((int)(Math.random()*100+150));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
//加血线程类 N键  18秒加一次血 ,目前由于无法检测到血量 所以 不能做到精确加血
class KeyRed implements Runnable{
    private Robot r;
    public KeyRed(Robot robot){
        this.r = robot;
    }
    @Override
    public void run() {
        while(true){
            try {
                pressKey(r,KeyEvent.VK_N);
                Thread.sleep(18*1000);
                System.out.println("加了一下血"+new Date());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }        
    }
    public static void pressKey(Robot r,int key){
        r.keyPress(key);
        r.keyRelease(key);    
    }
}
//加蓝线程类 30秒加一次蓝
class KeyBlue implements Runnable{
    private Robot r;
    public KeyBlue(Robot robot){
        this.r = robot;
    }
    @Override
    public void run() {
        while(true){
            try {
                pressKey(r,KeyEvent.VK_J);
                System.out.println("加了一下蓝"+new Date());
                Thread.sleep(30*1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }        
    }
    public static void pressKey(Robot r,int key){
        r.keyPress(key);
        r.keyRelease(key);    
    }
}

//加Buff线程类 180秒加一次Buff 为了buff不中断 所以设置为10秒加一次 
class KeyBuff implements Runnable{
    private Robot r;
    public KeyBuff(Robot robot){
        this.r = robot;
    }
    @Override
    public void run() {
        while(true){
            try {
                System.out.println("加了一下Buff"+new Date());
                Thread.sleep(10*1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }        
    }
    public static void pressKey(Robot r,int key){
        r.keyPress(key);
        r.keyRelease(key);    
    }
}

 

版权声明:本文为CryOnMyShoulder原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/CryOnMyShoulder/p/8026661.html?ivk_sa=1024320u