forked from mehrandvd/LumberRacer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LumberRacerFormOld.cs
203 lines (163 loc) · 7.07 KB
/
LumberRacerFormOld.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using ScreenShotDemo;
using Timer = System.Windows.Forms.Timer;
namespace LumberRacer
{
public partial class LumberRacerFormOld : Form
{
public LumberRacerFormOld()
{
InitializeComponent();
}
private bool lookForGame = false;
private bool gameFound = false;
private bool playGame = false;
Timer timer;
private void AnalyzeParts(ImageAnalyzer analyzer)
{
analyzer.LocateParts();
}
public void Log(string text)
{
listLog.Items.Insert(0, text);
}
private void btnStartEye_Click(object sender, EventArgs e)
{
lookForGame = !lookForGame;
btnStartEye.Text = lookForGame ? "Stop looking" : "Look for game";
}
private void StartWatching()
{
timer = new Timer()
{
Interval = (int) numericUpDownRefreshRate.Value
};
timer.Tick += (o, args) =>
{
timer.Stop();
Image img;
using (ScreenCapture sc = new ScreenCapture())
{
img = sc.SeeHere(trackBarHorizontal.Value, trackBarVertical.Value);
var analyzer = new ImageAnalyzer()
{
Image = new Bitmap(img),
BeamSize = (int)numericUpDownBeamSize.Value,
ColorPrecision = (int)numericUpDownColorPrecision.Value,
GridSize = (int)numericUpDownGridSize.Value
};
var location = new Point(analyzer.Width/2, analyzer.Height/2);
sc.Graphics.DrawString("X", new Font(FontFamily.GenericMonospace, 10, FontStyle.Bold), new SolidBrush(Color.Black), location);
var centerColor = analyzer.PickBeamColor(location, analyzer.BeamSize);
panelSelectedColor.BackColor = centerColor;
textBoxR.Text = analyzer.GetColorDistance(ImageAnalyzer.ColorTreeDark, centerColor).ToString();
//textBoxG.Text = centerColor.G.ToString(); ;
//textBoxB.Text = centerColor.B.ToString(); ;
if (lookForGame)
{
AnalyzeParts(analyzer);
if (analyzer.Leafs.Any() && !gameFound)
{
gameFound = true;
Log("Game found.");
var commands = analyzer.GetKeyCommands();
Log($"Free Steps: {analyzer.FreeSteps}");
var str = string.Empty;
foreach (var command in commands)
{
str = $"{str}, {(command == KeyCommand.Left ? "L" : "R")}";
}
Log(str);
Application.DoEvents();
}
if (!analyzer.Leafs.Any() && gameFound)
{
gameFound = false;
Log("Game lost.");
}
var leafPen = new Pen(Color.Orange, 10);
var brush = new SolidBrush(Color.Black);
foreach (var p in analyzer.Leafs.Select(p => analyzer.NormalPoint(p)))
{
sc.Graphics.DrawString("Leaf", new Font(FontFamily.GenericMonospace, 10, FontStyle.Bold), brush, p);
}
var bread = analyzer.RealPoint(analyzer.Bread);
var tree = analyzer.RealPoint(analyzer.TreeRoot);
sc.Graphics.DrawString("Head", new Font(FontFamily.GenericMonospace, 10, FontStyle.Bold), brush, bread);
sc.Graphics.DrawString("Tree", new Font(FontFamily.GenericMonospace, 10, FontStyle.Bold), brush, tree);
if (gameFound && playGame)
{
var commands = analyzer.GetKeyCommands();
var str = string.Empty;
pictureBoxLost.Image = img;
foreach (var command in commands)
{
if (command == KeyCommand.Left)
SendKeys.Send("{LEFT}");
if (command == KeyCommand.Right)
SendKeys.Send("{RIGHT}");
Thread.Sleep(10);
}
Thread.Sleep(300);
}
}
pictureBoxEye.Image = img;
Application.DoEvents();
}
timer.Interval = (int) numericUpDownRefreshRate.Value;
timer.Start();
};
timer.Start();
}
private void LumberRacerForm_Load(object sender, EventArgs e)
{
StartWatching();
}
private void pictureBoxEye_MouseClick(object sender, MouseEventArgs e)
{
var position = new Point(e.X, e.Y);
var analyzer = new ImageAnalyzer()
{
Image = new Bitmap(pictureBoxEye.Image),
BeamSize = (int)numericUpDownBeamSize.Value,
ColorPrecision = (int)numericUpDownColorPrecision.Value,
GridSize = (int)numericUpDownGridSize.Value
};
//var position = pictureBoxEye.PointToClient(new Point(e.X, e.Y));
var point = analyzer.RealPoint(position);
//var color = analyzer.PickPixelColor(position);
var color = analyzer.PickBeamColor(position, (int)numericUpDownBeamSize.Value);
panelSelectedColor.BackColor = color;
textBoxR.Text = color.R.ToString();
textBoxG.Text = color.G.ToString();
textBoxB.Text = color.B.ToString();
if (analyzer.AreColorsNear(color, ImageAnalyzer.ColorTreeDark))
Log("Tree");
if (analyzer.AreColorsNear(color, ImageAnalyzer.ColorBlouse))
Log("Blouse");
if (analyzer.AreColorsNear(color, ImageAnalyzer.ColorBread))
Log("Bread");
if (analyzer.AreColorsNear(color, ImageAnalyzer.ColorLeaf))
Log("Leaf");
AnalyzeParts(analyzer);
}
private void LumberRacerForm_Deactivate(object sender, EventArgs e)
{
playGame = true;
}
private void LumberRacerForm_Activated(object sender, EventArgs e)
{
playGame = false;
}
}
}