我正在编写一个小程序来自动完成浏览器游戏的点击。使用下面的代码,鼠标总是移动到正确的位置,但是单击按钮的次数大约只有50%。为什么它每次都不起作用,我该如何解决?
编辑:这是更新的代码(由于代码块大小的限制,有关图像识别的2种方法留为空白):
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace CICautoplayer
{
public partial class CICplayer : Form
{
public CICplayer()
{
InitializeComponent();
}
[DllImport("user32.dll", EntryPoint = "SetCursorPos")]
private static extern bool SetCursorPos(int x, int y);
[DllImport("user32.dll")]
public static extern void mouse_event(int dwflags, int dx, int dy, int cButtons, int dwExtraInfo);
private const int MOUSEEVENTF_LEFTDOWN = 0x02;
private const int MOUSEEVENTF_LEFTUP = 0x04;
Random rnd = new Random();
bool run = false;
int[] corner = new int[] { 0, 0 };
int[] pos = new int[] { 0, 0 };
int level = 0;
string[] logbook = new string[9] { " ", " ", " ", " ", " ", " ", " ", " ", " " };
private void runner()
{
while (run)
{
// =================================================================== identify level
bool n1found = false;
bool n2found = false;
int n1 = 9;
int n2 = 9;
while (!n1found)
{
string name = "n" + n1.ToString();
pos = new int[] { 0, 0 };
pos = position_of_level(name, corner[0] + 0, corner[0] + 15, corner[1] + 10, corner[1] + 40);
if (pos[0] > 0 && pos[1] > 0) { n1found = true; }
else if (n1 > 0) { n1--; }
else { n1found = true;}
}
while (!n2found)
{
string name = "n" + n2.ToString();
pos = new int[] { 0, 0 };
pos = position_of_level(name, corner[0] + 15, corner[0] + 40, corner[1] + 10, corner[1] + 40);
if (pos[0] > 0 && pos[1] > 0){ n2found = true; }
else if (n2 > 0) { n2--; }
else { n2found = true; n2 = 1000; }
}
level = n1 * 10 + n2;
addentry("LVL " + level.ToString() + " => ");
// ================================================================ target rewards
if (level > 5)
{
click_on(new int[] { corner[0] + 20, corner[1] + 20 });
Thread.Sleep(1100 + rnd.Next(1, 100));
//string[] targetlist = new string[] { "saltpeter", "jewels", "tools", "magnificent", "steel", "bolts", "diamond", "lore"};
string[] targetlist = new string[] { "saltpeter", "jewels", "magnificent", "bolts", "diamond", "lore" };
int[] temp_pos = new int[] { 0, 0 };
temp_pos = position_of("corner_slider", corner[0] + 200, corner[0] + 250, corner[1] + 250, corner[1] + 550);
bool chosen = false;
int n = 0;
while (n < targetlist.Count() && !chosen)
{
pos = new int[] { 0, 0 };
pos = position_of(targetlist[n], temp_pos[0] + 200, temp_pos[0] + 550, temp_pos[1], temp_pos[1] + 50);
if (pos[0] != 0 && pos[1] != 0)
{
click_on(new int[] { pos[0] + 10, pos[1] + 10 });
chosen = true;
}
n++;
}
if (chosen)
{
addentry("Rwrd: " + targetlist[n - 1] + " => ");
}
else
{
addentry("Rwrd: NONE! => ");
}
// fecher o menu das rewards
Thread.Sleep(1000 + rnd.Next(1, 100));
click_on(new int[] { corner[0] + 50, corner[1] + 400 });
}
// =============================================================== wait time
int wtime = 0;
if (level < 30) { wtime = 4000 + level * 200;
} else if (level < 60)
{
wtime = 10000 + level * 250;
} else
{
wtime = 25000;
}
addentry("Wait: " + (wtime/1000).ToString() + " sec => ");
Thread.Sleep(wtime);
// ================================================================ reset
if (run) // condition in case the stop button is pressed meanwhile
{
addentry("reseting... \r\n");
click_on(new int[] { corner[0] + 20, corner[1] + 20 });
Thread.Sleep(2000 + rnd.Next(1, 500));
// select restart
int[] p_but = new int[] { 0, 0 };
p_but = position_of("button_restart", corner[0] + 450, corner[0] + 550, corner[1] + 200, corner[1] + 400);
p_but[0] += 10; p_but[1] += 10;
click_on(p_but);
Thread.Sleep(4000 + rnd.Next(1, 500));
click_on(new int[] { corner[0] + 700, corner[1] + (level < 6 ? 335 : 385)});
Thread.Sleep(5000);
}
}
}
public void click_on(int[] coor)
{
SetCursorPos(coor[0], coor[1]);
Application.DoEvents();
mouse_event(MOUSEEVENTF_LEFTDOWN, coor[0], coor[1], 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, coor[0], coor[1], 0, 0);
}
private void addentry(string text)
{
if (InvokeRequired)
{
this.Invoke(new Action<string>(addentry), new object[] { text });
return;
}
for (int i = 0; i < 8; i++) { logbook[i] = logbook[i + 1]; }
logbook[8] = text;
logtext.Text = "";
for (int i = 0; i < 9; i++)
{
logtext.Text += logbook[i];
}
}
private int[] position_of(string what, int xmin, int xmax, int ymin, int ymax)
{
// code to identify a picture in a specified area of the screen
// returns int[2] with the coordinates
}
private int[] position_of_level(string what, int xmin, int xmax, int ymin, int ymax)
{
// code to identify a number in a specified area of the screen
// returns int[2] with the coordinates
}
private void but_play_Click(object sender, EventArgs e)
{
Thread.Sleep(3000);
corner = position_of("corner", 0, 500, 0, 600);
addentry("Corner found at " + corner[0].ToString() + " , " + corner[1].ToString() + "\r\n");
run = true;
var thread = new Thread(new ThreadStart(runner));
thread.Start();
}
private void but_stop_Click(object sender, EventArgs e)
{
run = false;
}
}
}