**
用java的robot类以及Gui 制作一个游戏(阴阳师)脚本
**
由于阴阳师这个游戏肝度太大,就决定写一个脚本来模拟玩家的重复性操作.所以我就决定用java中的robot类中的一些方法来模拟鼠标点击,移动等功能,具体方法见下图.
因为一个好看的界面能让人看着更加的舒服,所以就决定用java中的GUI来设计一个操作界面,功能更能很好的展现方便使用.
具体思路
脚本的一个难题就是如何防止被封号,我的看法是使脚本的操作更加符合人的操作:禁止点击鼠标速度过快,过多,禁止点击同一位置,禁止每一次点击鼠标按下释放时间一致;
而另一个难题就是如何让鼠标点击该点的位置,判断什么时候结束是么时候点击开始,这里不能用记录每一个操作的坐标,这样不仅不方便还可能出错.所以我用了从一个大图片中找到小图片并且返回中心坐标的这个思路(借鉴大佬).
废话不多说,看图看代码
功能
魂土,日轮,逢魔,痴卷,地域鬼王,日常任务功能正在开发
脚本运行图
代码
地域鬼王部分代码:
就是用循环嵌套一直重复
其他功能也是类似
//地域鬼王
public static void dygw() {
String keyImagePath0 = "image\\dingwei.png";
test demo0 = new test(keyImagePath0);
demo0.printFindData();
if (a!=0||b!=0) {
width=960;
height=570;
Robot robot0;
try {
robot0 = new Robot();
robot0.delay(1000);
robot0.mouseMove(b, a);
robot0.delay(200);
robot0.mousePress(InputEvent.BUTTON1_MASK);
robot0.mouseMove(66, 10);
robot0.delay(200);
robot0.mouseRelease(InputEvent.BUTTON1_MASK);
robot0.delay(2000);
} catch (AWTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
txtrtn.setForeground(Color.BLACK );
txtrtn.append("\t\n");
txtrtn.append("定位成功,地域鬼王即将开始!!!");
txtrtn.setCaretPosition(txtrtn.getText().length());
while(exit_judge) {
try {
String keyImagePath = "image\\xianshiyaoyue.png";
test demo = new test(keyImagePath);
demo.printFindData();
if (a!=0||b!=0) {
Robot robot;
robot = new Robot();
robot.delay(200+(int)Math.random()*200);
robot.mouseMove(b,a);
robot.delay(200+(int)Math.random()*100);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.delay(200+(int)Math.random()*100);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
txtrtn.append("\t\n");
txtrtn.append("点击现世邀约");
txtrtn.setCaretPosition(txtrtn.getText().length());
}
String keyImagePath1 = "image\\fmfanhui1.png";
test demo1 = new test(keyImagePath1);
demo1.printFindData();
int dw_x=b+140;
int dw_y=a+461;
if (a!=0||b!=0) {
Robot robot2=new Robot();
robot2.delay(1000+(int)Math.random()*100);
robot2.mouseMove(dw_x, dw_y);
robot2.delay(200+(int)Math.random()*100);
robot2.mousePress(InputEvent.BUTTON1_MASK);
robot2.delay(200+(int)Math.random()*100);
robot2.mouseRelease(InputEvent.BUTTON1_MASK);
txtrtn.append("\t\n");
txtrtn.append("点击地狱鬼王");
txtrtn.setCaretPosition(txtrtn.getText().length());
}
String keyImagePath2 = "image\\dygw.png";
test demo2 = new test(keyImagePath2);
demo2.printFindData();
if (a!=0||b!=0) {
Robot robot3=new Robot();
robot3.delay(200+(int)Math.random()*100);
robot3.mouseMove(b, a-100);
robot3.delay(200+(int)Math.random()*100);
robot3.mousePress(InputEvent.BUTTON1_MASK);
robot3.delay(200+(int)Math.random()*100);
robot3.mouseRelease(InputEvent.BUTTON1_MASK);
txtrtn.append("\t\n");
txtrtn.append("点击丹霞山");
txtrtn.setCaretPosition(txtrtn.getText().length());
}
String keyImagePath3 = "image\\dygwdengji.png";
test demo3 = new test(keyImagePath3);
demo3.printFindData();
if (a!=0||b!=0) {
Robot robot4=new Robot();
robot4.delay(200+(int)Math.random()*100);
robot4.mouseMove(b, a);
robot4.delay(200+(int)Math.random()*100);
robot4.mousePress(InputEvent.BUTTON1_MASK);
robot4.delay(200+(int)Math.random()*100);
robot4.mouseMove(b-193, a);
robot4.delay(200+(int)Math.random()*100);
robot4.mouseRelease(InputEvent.BUTTON1_MASK);
txtrtn.append("\t\n");
txtrtn.append("调鬼王等级");
txtrtn.setCaretPosition(txtrtn.getText().length());
}
String keyImagePath4 = "image\\dygwtiaozhan.png";
test demo4 = new test(keyImagePath4);
demo4.printFindData();
if (a!=0||b!=0) {
Robot robot5=new Robot();
robot5.delay(200+(int)Math.random()*100);
robot5.mouseMove(b, a);
robot5.delay(200+(int)Math.random()*100);
robot5.mousePress(InputEvent.BUTTON1_MASK);
robot5.delay(200+(int)Math.random()*100);
robot5.mouseRelease(InputEvent.BUTTON1_MASK);
txtrtn.append("\t\n");
txtrtn.append("挑战");
txtrtn.setCaretPosition(txtrtn.getText().length());
}
String keyImagePath5 = "image\\dygwzhunbei.png";
test demo5 = new test(keyImagePath5);
demo5.printFindData();
if (a!=0||b!=0) {
Robot robot6=new Robot();
robot6.delay(200+(int)Math.random()*100);
robot6.mouseMove(b, a);
robot6.delay(200+(int)Math.random()*100);
robot6.mousePress(InputEvent.BUTTON1_MASK);
robot6.delay(200+(int)Math.random()*100);
robot6.mouseRelease(InputEvent.BUTTON1_MASK);
txtrtn.append("\t\n");
txtrtn.append("准备");
txtrtn.append("\t\n");
txtrtn.append("正在挑战");
txtrtn.setCaretPosition(txtrtn.getText().length());
while(exit_judge) {
txtrtn.setForeground(Color.green);
txtrtn.append("...");
txtrtn.setCaretPosition(txtrtn.getText().length());
String keyImagePath6 = "image\\dygwjieshu.png";
test demo6 = new test(keyImagePath6);
demo6.printFindData();
if (a!=0||b!=0) {
Robot robot7=new Robot();
robot7.mouseMove(b, a-100);
robot7.delay(500+(int)Math.random()*100);
robot7.mousePress(InputEvent.BUTTON1_MASK);
robot7.delay(200+(int)Math.random()*100);
robot7.mouseRelease(InputEvent.BUTTON1_MASK);
robot7.delay(500+(int)Math.random()*100);
robot7.mousePress(InputEvent.BUTTON1_MASK);
robot7.delay(200+(int)Math.random()*100);
robot7.mouseRelease(InputEvent.BUTTON1_MASK);
robot7.delay(1500+(int)Math.random()*100);
robot7.mousePress(InputEvent.BUTTON1_MASK);
robot7.delay(200+(int)Math.random()*100);
robot7.mouseRelease(InputEvent.BUTTON1_MASK);
robot7.delay(800+(int)Math.random()*100);
robot7.mousePress(InputEvent.BUTTON1_MASK);
robot7.delay(200+(int)Math.random()*100);
robot7.mouseRelease(InputEvent.BUTTON1_MASK);
txtrtn.append("\t\n");
txtrtn.append("结束");
txtrtn.setCaretPosition(txtrtn.getText().length());
}
String keyImagePath7 = "image\\dygwfanhui.png";
test demo7 = new test(keyImagePath7);
demo7.printFindData();
if (a!=0||b!=0) {
Robot robot8=new Robot();
robot8.delay(500+(int)Math.random()*100);
robot8.delay(500+(int)Math.random()*100);
robot8.keyPress(KeyEvent.VK_ESCAPE);
robot8.delay(200+(int)Math.random()*100);
robot8.keyRelease(KeyEvent.VK_ESCAPE);
txtrtn.append("\t\n");
txtrtn.append("返回");
txtrtn.setCaretPosition(txtrtn.getText().length());
Robot robot9=new Robot();
robot9.delay(2000+(int)Math.random()*100);
robot9.keyPress(KeyEvent.VK_ESCAPE);
robot9.delay(200+(int)Math.random()*100);
robot9.keyRelease(KeyEvent.VK_ESCAPE);
robot9.delay(2000+(int)Math.random()*100);
robot9.keyPress(KeyEvent.VK_ESCAPE);
robot9.delay(100);
robot9.keyRelease(KeyEvent.VK_ESCAPE);
txtrtn.append("\t\n");
txtrtn.append("返回");
txtrtn.setCaretPosition(txtrtn.getText().length());
exit_judge=false;
break;
}else {
String keyImagePath10 = "image\\dygw.png";
test demo10 = new test(keyImagePath10);
demo10.printFindData();
if (a!=0||b!=0) {
Robot robot10=new Robot();
robot10.delay(2000+(int)Math.random()*100);
robot10.keyPress(KeyEvent.VK_ESCAPE);
robot10.delay(200+(int)Math.random()*100);
robot10.keyRelease(KeyEvent.VK_ESCAPE);
robot10.delay(2000+(int)Math.random()*100);
robot10.keyPress(KeyEvent.VK_ESCAPE);
robot10.delay(100);
robot10.keyRelease(KeyEvent.VK_ESCAPE);
txtrtn.append("\t\n");
txtrtn.append("返回");
txtrtn.setCaretPosition(txtrtn.getText().length());
exit_judge=false;
}
}
try {
Thread.sleep(3000);
} catch (InterruptedException e)
{e.printStackTrace();}
}
}
} catch (Exception e) {
// TODO: handle exception
}
try {
Thread.sleep(1000);
} catch (InterruptedException e)
{e.printStackTrace();}
}
}else {
txtrtn.setForeground(Color.red );
txtrtn.append("\t\n");
txtrtn.append("无法定位阴阳师界面,请确保阴阳师界面在可视范围内");
txtrtn.append("\t\n");
txtrtn.append("请停止再次启动");
exit_judge=false;
}
}
Gui界面部分代码:
在设置按钮时间时一定要创建线程执行具体操作.因为这些方法都是循环,并不会立即停止,若不建立线程则按钮点击后,界面会卡着不动.按钮也无法点击
public test() {
//
setForeground(Color.BLACK);
setBackground(Color.WHITE);
setIconImage(Toolkit.getDefaultToolkit().getImage("C:\\Users\\Lenovo\\eclipse-workspace\\\u9634\u9633\u5E08\\D.png"));
setTitle("\u79C3\u5934\u4FDD\u62A4");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(1100, 0, 425, 385);
JMenuBar menuBar = new JMenuBar();
menuBar.setBackground(Color.WHITE);
setJMenuBar(menuBar);
JMenu mnNewMenu = new JMenu("");
mnNewMenu.setIcon(new ImageIcon("image\\set.jpg"));
mnNewMenu.setHorizontalAlignment(SwingConstants.CENTER);
menuBar.add(mnNewMenu);
JMenuItem ontopItem=new JMenuItem();
ontopItem.setBackground(Color.LIGHT_GRAY);
ontopItem.setIcon(null);
ontopItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (ONTOP_judge==true) {
ONTOP_judge=false;
setAlwaysOnTop(ONTOP_judge);
}else {
ONTOP_judge=true;
setAlwaysOnTop(ONTOP_judge);
}
}
});
ontopItem.setText("\u7F6E\u9876");
ontopItem.setSelected(true);
mnNewMenu.add(ontopItem);
contentPane = new JPanel();
contentPane.setBackground(Color.GRAY);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton btnNewButton = new JButton("\u6316\u571F(\u961F\u5458)");
btnNewButton.setForeground(Color.LIGHT_GRAY);
btnNewButton.setBackground(SystemColor.activeCaption);
btnNewButton.setFocusPainted(false);
btnNewButton.setContentAreaFilled(false);
btnNewButton.setToolTipText("自动点大舅妈,位置二号位");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new Thread() {
public void run() {
Zhuntu();
}
}.start();
}
});
btnNewButton.setBounds(21, 24, 94, 32);
contentPane.add(btnNewButton);
JButton btnNewButton_1 = new JButton("\u7ED3\u675F");
btnNewButton_1.setToolTipText("\u591A\u70B9\u51E0\u4E0B\u4E0D\u8981\u94B1,\u7701\u4E8B\u53C8\u5B89\u5168");
btnNewButton_1.setForeground(Color.LIGHT_GRAY);
btnNewButton_1.setContentAreaFilled(false);
btnNewButton_1.setFocusPainted(false);
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
exit_judge=false;
txtrtn.append("\t\n");
txtrtn.append("暂停");
txtrtn.setCaretPosition(txtrtn.getText().length());
}
});
btnNewButton_1.setBounds(158, 183, 94, 32);
contentPane.add(btnNewButton_1);
JButton btnNewButton_2 = new JButton("\u6316\u571F(\u961F\u957F)");
btnNewButton_2.setForeground(Color.LIGHT_GRAY);
btnNewButton_2.setToolTipText("\u65E0\u6807\u8BB0");
btnNewButton_2.setContentAreaFilled(false);
btnNewButton_2.setFocusPainted(false);
btnNewButton_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new Thread() {
public void run() {
exit_judge=true;
NOZDhuntu();
}
}.start();
}
});
btnNewButton_2.setBounds(158, 24, 94, 32);
contentPane.add(btnNewButton_2);
JButton btnNewButton_3 = new JButton("\u6316\u571F(\u961F\u957F)");
btnNewButton_3.setForeground(Color.LIGHT_GRAY);
btnNewButton_3.setToolTipText("\u81EA\u52A8\u6807\u8BB0\u5927\u8205\u5988,\u5927\u8205\u5988\u4E8C\u53F7\u4F4D");
btnNewButton_3.setContentAreaFilled(false);
btnNewButton_3.setFocusPainted(false);
//btnNewButton_3.setBorder(BorderFactory.createRaisedBevelBorder());
btnNewButton_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new Thread() {
public void run() {
DZhuntu();
}
}.start();
}
});
btnNewButton_3.setBounds(305, 24, 94, 32);
contentPane.add(btnNewButton_3);
JButton btnNewButton_4 = new JButton("\u65E5\u8F6E(\u961F\u5458)");
btnNewButton_4.setForeground(Color.LIGHT_GRAY);
btnNewButton_4.setToolTipText("\u81EA\u52A8\u6807\u8BB0\u5927\u8205\u5988,\u5927\u8205\u5988\u4E8C\u53F7\u4F4D");
btnNewButton_4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new Thread() {
public void run() {
ZZhun12();
}
}.start();
}
});
btnNewButton_4.setContentAreaFilled(false);
btnNewButton_4.setFocusPainted(false);
btnNewButton_4.setBounds(158, 104, 94, 32);
contentPane.add(btnNewButton_4);
JButton btnNewButton_5 = new JButton("\u5C01\u9B54");
btnNewButton_5.setForeground(Color.LIGHT_GRAY);
btnNewButton_5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new Thread() {
public void run() {
fm();
}
}.start();
}
});
//btnNewButton_5.setBorder(BorderFactory.createRaisedBevelBorder());
btnNewButton_5.setContentAreaFilled(false);
btnNewButton_5.setFocusPainted(false);
btnNewButton_5.setBounds(305, 104, 94, 32);
contentPane.add(btnNewButton_5);
JButton btnNewButton_7 = new JButton("\u5730\u57DF\u9B3C\u738B");
btnNewButton_7.setForeground(Color.LIGHT_GRAY);
btnNewButton_7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new Thread() {
public void run() {
exit_judge=true;
dygw();
}
}.start();
}
});
btnNewButton_7.setContentAreaFilled(false);
btnNewButton_7.setFocusPainted(false);
btnNewButton_7.setBounds(21, 183, 93, 32);
contentPane.add(btnNewButton_7);
JButton btnNewButton_6 = new JButton("\u75F4");
btnNewButton_6.setForeground(Color.LIGHT_GRAY);
btnNewButton_6.setContentAreaFilled(false);
btnNewButton_6.setFocusPainted(false);
btnNewButton_6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new Thread() {
public void run() {
exit_judge=true;
cj();
System.out.println("开始");
}
}.start();
}
});
btnNewButton_6.setBounds(21, 109, 93, 23);
contentPane.add(btnNewButton_6);
JButton btnNewButton_8 = new JButton("\u65E5\u5E38");
btnNewButton_8.setForeground(Color.LIGHT_GRAY);
btnNewButton_8.setContentAreaFilled(false);
btnNewButton_8.setFocusPainted(false);
btnNewButton_8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
btnNewButton_8.setBounds(305, 183, 94, 28);
contentPane.add(btnNewButton_8);
//
lblNewLabel_1 = new JLabel("New label");
lblNewLabel_1.setForeground(Color.CYAN);
lblNewLabel_1.setBounds(280, 246, 130, 15);
contentPane.add(lblNewLabel_1);
configTimeArea();
JLabel lblNewLabel = new JLabel("New label");
lblNewLabel.setIcon(new ImageIcon("image/preview.gif"));
lblNewLabel.setBounds(0, 0, 410, 261);
contentPane.add(lblNewLabel);
JPanel panel = new JPanel();
panel.setBounds(0, 260, 434, 86);
contentPane.add(panel);
panel.setLayout(null);
JScrollPane scrollPane = new JScrollPane();
txtrtn = new JTextArea();
txtrtn.setEditable(false);
txtrtn.setLineWrap(true);
txtrtn.setText("\u795D\u4F60\u597D\u8FD0!-------\u9F20\u6807\u60AC\u6D6E\u6709\u63D0\u793A");
txtrtn.setToolTipText("");
txtrtn.setBounds(0, 0, 434, 86);
panel.add(scrollPane);
scrollPane.setBounds(0, 0, 409, 107);
scrollPane.setViewportView(txtrtn);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
txtrtn.paintImmediately(txtrtn.getBounds());
}
代码太多,如有需要请联系作者
缺点
此脚本使用时必须保证游戏界面不被遮挡,并且会影响你的鼠标的其他操作.
此脚本使用时游戏分辨必须不能变,并且电脑不能设置125%所在缩放,只能100%
内存占用有点大,和运行一个阴阳师差不多了,我尝试用System.gc()来清理内存但效果不理想,
若大佬有方法或者新的思路或者意见可以告知一下小弟,在下感激不尽
本人联系方式[email protected]
本人第一次写,若有侵权请联系作者