My attempt at writing screen recognition software. It actually worked pretty well, I didn’t have enough time to optimize the logic but the scanfunc itself worked like a charm and was able to map graphics to its internal memory to then calculate the next best move. I’ll probably break this code out again one day to make an Aimbot when I’m board but this was one of my more interesting projects.
The general idea of this code is in WritePixelColorCode function, I would place my mouse over a given pixel and it would Debug.Output the value of that pixel and the neighboring pixels in a 3×3 grid. I then hard code those values into my app then scan every pixel of the screen looking for that pattern, It should be unique to that specific GEM in this case for CandyCrushSaga, Once that is detected I note where it it on the grid in its internal memory. Once mapped its up to the logic to determine which move is best by bruteforcing each possibility then calculating the largest value out of all the moves. Code below no longer works due to an outdated agent in the WebBrowser object, but using Chromium in dotnet will most likely get the code to fly again if you get board.
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 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 |
Imports System.Runtime.InteropServices Public Class Form1 Dim leftmostBlock As Integer = 0 Dim TopMostBlock As Integer = 0 Dim Grid(8, 8) As GameBlock Dim GameBlocks(80) As GameBlock Structure GameBlock Implements IComparable(Of GameBlock) Dim Block As BlockColor Dim Location As Point Dim MyRow As Short Dim MyColumn As Short Public Function CompareTo(ByVal other As GameBlock) As Integer Implements System.IComparable(Of GameBlock).CompareTo Return (Me.Location.X + (Me.Location.Y * 10)).CompareTo(other.Location.X + (other.Location.Y * 10)) End Function End Structure Dim BlockColors(5) As BlockColor Structure BlockColor Dim Name As String Dim Color() As Color End Structure Dim redBlock() As Color = {Color.FromArgb(252, 0, 0), Color.FromArgb(255, 1, 0), Color.FromArgb(255, 0, 0), Color.FromArgb(254, 0, 1), Color.FromArgb(255, 0, 1), Color.FromArgb(255, 1, 0), Color.FromArgb(255, 0, 1), Color.FromArgb(255, 1, 1), Color.FromArgb(255, 1, 1)} Dim yellowBlock() As Color = {Color.FromArgb(252, 217, 1), Color.FromArgb(252, 219, 3), Color.FromArgb(252, 221, 4), Color.FromArgb(252, 221, 3), Color.FromArgb(252, 223, 4), Color.FromArgb(252, 225, 4), Color.FromArgb(252, 225, 2), Color.FromArgb(252, 227, 3), Color.FromArgb(253, 228, 3)} Dim greenBlock() As Color = {Color.FromArgb(49, 177, 1), Color.FromArgb(52, 179, 1), Color.FromArgb(49, 178, 1), Color.FromArgb(51, 179, 1), Color.FromArgb(52, 180, 1), Color.FromArgb(53, 181, 1), Color.FromArgb(51, 181, 1), Color.FromArgb(54, 183, 1), Color.FromArgb(55, 184, 1)} Dim blueBlock() As Color = {Color.FromArgb(65, 172, 255), Color.FromArgb(54, 168, 255), Color.FromArgb(34, 153, 255), Color.FromArgb(29, 140, 255), Color.FromArgb(36, 147, 255), Color.FromArgb(42, 154, 255), Color.FromArgb(39, 148, 255), Color.FromArgb(40, 150, 255), Color.FromArgb(42, 154, 255)} Dim purpleBlock() As Color = {Color.FromArgb(193, 28, 255), Color.FromArgb(190, 25, 255), Color.FromArgb(191, 25, 255), Color.FromArgb(202, 31, 255), Color.FromArgb(199, 28, 255), Color.FromArgb(200, 27, 255), Color.FromArgb(204, 31, 255), Color.FromArgb(208, 32, 255), Color.FromArgb(209, 31, 255)} Dim orangeBlock() As Color = {Color.FromArgb(255, 143, 19), Color.FromArgb(255, 137, 11), Color.FromArgb(255, 138, 9), Color.FromArgb(255, 142, 19), Color.FromArgb(255, 138, 13), Color.FromArgb(255, 138, 10), Color.FromArgb(255, 143, 18), Color.FromArgb(255, 141, 14), Color.FromArgb(255, 137, 9)} 'Dim BlockArray As New ArrayList({redBlock, yellowBlock, greenBlock, blueBlock, purpleBlock, orangeBlock}) Dim BlockArray As ArrayList <DllImport("user32.dll")> Private Shared Function GetDC(ByVal hwnd As IntPtr) As IntPtr End Function <DllImport("user32.dll")> Private Shared Function ReleaseDC(ByVal hwnd As IntPtr, ByVal hdc As IntPtr) As Int32 End Function <DllImport("gdi32.dll")> Private Shared Function GetPixel(ByVal hdc As IntPtr, ByVal nXPos As Integer, ByVal nYPos As Integer) As UInteger End Function '//methods Public Shared Function GetPixelColor(ByVal x As Integer, ByVal y As Integer) As System.Drawing.Color Dim hdc As IntPtr = GetDC(IntPtr.Zero) Dim pixel As UInteger = GetPixel(hdc, x, y) ReleaseDC(IntPtr.Zero, hdc) Return Color.FromArgb(CInt(pixel And &HFF), _ CInt(pixel And &HFF00) >> 8, _ CInt(pixel And &HFF0000) >> 16) End Function Public Shared Function GetPixelColorUI(ByVal x As Integer, ByVal y As Integer) As UInt32 Dim hdc As IntPtr = GetDC(IntPtr.Zero) Dim pixel As UInteger = GetPixel(hdc, x, y) ReleaseDC(IntPtr.Zero, hdc) Return pixel End Function Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load BlockColors(0).Color = redBlock BlockColors(0).Name = "Red" BlockColors(1).Color = greenBlock BlockColors(1).Name = "Green" BlockColors(2).Color = blueBlock BlockColors(2).Name = "Blue" BlockColors(3).Color = purpleBlock BlockColors(3).Name = "Purple" BlockColors(4).Color = orangeBlock BlockColors(4).Name = "Orange" BlockColors(5).Color = yellowBlock BlockColors(5).Name = "Yellow" BlockArray = New ArrayList({BlockColors(0), BlockColors(1), BlockColors(2), BlockColors(3), BlockColors(4), BlockColors(5)}) 'Draw() WebBrowser1.Navigate("https://apps.facebook.com/candycrush/?fb_source=search&ref=br_tf") 'cc1.midasplayer.com 'AxShockwaveFlash1.LoadMovie(0, "https://cc1.midasplayer.com/swf/CCPreloader.swf?_v=1f805vf") 'start() End Sub Private Sub ProdictBestMove() Debug.WriteLine("Checking left...") 'This checks up and down still.. Need different logic For x = 1 To 9 For y = 1 To 9 Try CheckUpDownDirection(Direction.up, y, x) Catch ex As Exception End Try 'CheckLeft(x, y) Next Next Exit Sub Debug.WriteLine("Checking Down...") For x = 1 To 9 For y = 1 To 9 Try CheckUpDownDirection(Direction.down, x, y) Catch ex As Exception End Try 'CheckLeft(x, y) Next Next Debug.WriteLine("Checking up...") For x = 1 To 9 For y = 1 To 9 Try CheckUpDownDirection(Direction.up, x, y) Catch ex As Exception End Try 'CheckLeft(x, y) Next Next End Sub Delegate Function DelPtr(ByVal A As Integer, ByVal B As Integer) Private Function Add(ByVal A As Integer, ByVal B As Integer) Return A + B End Function Private Function Subtract(ByVal A As Integer, ByVal B As Integer) Return A - B End Function Private Enum Direction up down left right End Enum Private Function CheckUpDownDirection(ByVal UpDown As Direction, ByVal x As Short, ByVal y As Short) As Boolean 'This function is base 0 Dim Logic1, Logic2 As DelPtr If UpDown = Direction.down Then Logic1 = AddressOf Add Logic2 = AddressOf Subtract End If If UpDown = Direction.up Then Logic1 = AddressOf Subtract Logic2 = AddressOf Add End If 'The upper left unit is 0,0 and the lower right is 8,8 If IsNothing(Grid(Logic1(x, 1), y).Block.Name) Then Return False If Grid(x, y).Block.Name = Grid(Logic1(x, 1), y).Block.Name Then 'check to the right Try If Logic1(x, 3) <= Grid.GetUpperBound(0) Then If (Grid(x, y).Block.Name = Grid(Logic1(x, 3), y).Block.Name) Then 'check to the right again then right again Debug.WriteLine(Grid(x, y).Block.Name & ": " & Grid(Logic1(x, 3), y).Block.Name & " " & Logic1(x, 3) & ", " & y & " => " & Grid(Logic1(x, 2), y).Block.Name & " " & Logic1(x, 2) & ", " & y) End If End If Catch ex As Exception Debug.WriteLine(ex.Message) End Try Try If Logic1(x, 2) <= Grid.GetUpperBound(0) And Logic1(y, 2) <= Grid.GetUpperBound(1) Then If (Grid(x, y).Block.Name = Grid(Logic1(x, 2), Logic1(y, 1)).Block.Name) Then 'check to the right again then up 1 Debug.WriteLine(Grid(x, y).Block.Name & ": " & Grid(Logic1(x, 2), Logic1(y, 1)).Block.Name & " " & Logic1(x, 2) & ", " & Logic1(y, 1) & " => " & Grid(Logic1(x, 2), y).Block.Name & " " & Logic1(x, 2) & ", " & y) End If End If Catch ex As Exception End Try Try If (Grid(x, y).Block.Name = Grid(Logic1(x, 2), Logic2(y, 1)).Block.Name) Then 'check to the right again then down 1 Debug.WriteLine(Grid(x, y).Block.Name & ": " & Grid(Logic1(x, 2), Logic2(y, 1)).Block.Name & " " & Logic1(x, 2) & ", " & Logic2(y, 1) & " => " & Grid(Logic1(x, 2), y).Block.Name & " " & Logic1(x, 2) & ", " & y) End If Catch ex As Exception End Try End If ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' If Grid(x, y).Block.Name = Grid(Logic1(x, 2), y).Block.Name Then 'check to the rightx2 Try If (Grid(x, y).Block.Name = Grid(Logic1(x, 1), Logic1(y, 1)).Block.Name) Then 'check to the right again then up 1 Debug.WriteLine(Grid(x, y).Block.Name & ": " & Grid(Logic1(x, 1), Logic1(y, 1)).Block.Name & " " & Logic1(x, 1) & ", " & Logic1(y, 1) & " => " & Grid(Logic1(x, 1), y).Block.Name & " " & Logic1(x, 1) & ", " & y) End If Catch ex As Exception End Try Try If (Grid(x, y).Block.Name = Grid(Logic1(x, 1), Logic2(y, 1)).Block.Name) Then 'check to the right again then down 1 Debug.WriteLine(Grid(x, y).Block.Name & ": " & Grid(Logic1(x, 1), Logic2(y, 1)).Block.Name & " " & Logic1(x, 1) & ", " & Logic2(y, 1) & " => " & Grid(Logic1(x, 1), y).Block.Name & " " & Logic1(x, 1) & ", " & y) End If Catch ex As Exception End Try End If End Function Private Sub ScanFunc() leftmostBlock = 0 TopMostBlock = 0 For i = 0 To 80 GameBlocks(i) = Nothing Dim A As Integer = Math.DivRem(i, 9, Nothing) Dim B As Integer = i Mod 9 'Debug.WriteLine(A & " - " & B) Grid(A, B) = Nothing Next Dim screensize As Size = New Size(My.Computer.Screen.Bounds.Width, My.Computer.Screen.Bounds.Height) Dim screenshot As New Bitmap(My.Computer.Screen.Bounds.Width, My.Computer.Screen.Bounds.Height) Dim g As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(screenshot) g.CopyFromScreen(New Point(0, 0), New Point(0, 0), screensize) Dim index As Integer = 0 Dim x As Integer = 1 Dim y As Integer = 1 Dim CurrentColumnPoint As Integer = 0 Dim CurrentColumn As Short = 0 Dim CurrentRowPoint As Integer = 0 Dim CurrentRow As Short = 0 Dim search As Boolean = True Try While search = True If x = screensize.Width Then Exit While For Each MyColorBlock As BlockColor In BlockArray If screenshot.GetPixel(x, y) = MyColorBlock.Color(4) Then If CheckWholeBlock(New Point(x, y), screenshot, MyColorBlock.Color) Then GameBlocks(index).Block.Name = MyColorBlock.Name GameBlocks(index).Block.Color = MyColorBlock.Color GameBlocks(index).Location = New Point(x, y) If leftmostBlock = 0 Then leftmostBlock = x CurrentColumnPoint = x End If If TopMostBlock > y Or (TopMostBlock = 0 And CurrentRowPoint = 0) Then TopMostBlock = y CurrentRowPoint = y End If If GameBlocks(index).Location.X < CurrentColumnPoint + 20 Then GameBlocks(index).MyColumn = CurrentColumn Else CurrentColumn += 1 GameBlocks(index).MyColumn = CurrentColumn CurrentColumnPoint = GameBlocks(index).Location.X End If index += 1 Cursor.Position = New Point(x, y) 'Draw() 'Threading.Thread.Sleep(200) End If End If Next y = y + 1 If y = My.Computer.Screen.Bounds.Height Then y = 0 x = x + 1 End If End While Array.Sort(GameBlocks) For index = 0 To 80 'For each cant write to memory? Something is weird here.. I had to use for i If GameBlocks(index).Location.Y = 0 Then Continue For If GameBlocks(index).Location.Y < CurrentRowPoint + 20 Then GameBlocks(index).MyRow = CurrentRow Else CurrentRow += 1 GameBlocks(index).MyRow = CurrentRow CurrentRowPoint = GameBlocks(index).Location.Y End If Next For i = 0 To 80 If GameBlocks(i).Location.Y = 0 Then Continue For Grid(GameBlocks(i).MyRow, GameBlocks(i).MyColumn) = GameBlocks(i) Next ProdictBestMove() Catch ex As Exception End Try End Sub Private Function CheckWholeBlock(ByVal atpoint As Point, ByVal screenshot As Bitmap, ByVal Blockcolor() As Color) If screenshot.GetPixel(atpoint.X - 1, atpoint.Y + 1) = Blockcolor(0) Then If screenshot.GetPixel(atpoint.X, atpoint.Y + 1) = Blockcolor(1) Then If screenshot.GetPixel(atpoint.X + 1, atpoint.Y + 1) = Blockcolor(2) Then If screenshot.GetPixel(atpoint.X - 1, atpoint.Y) = Blockcolor(3) Then If screenshot.GetPixel(atpoint.X + 1, atpoint.Y) = Blockcolor(5) Then If screenshot.GetPixel(atpoint.X - 1, atpoint.Y - 1) = Blockcolor(6) Then If screenshot.GetPixel(atpoint.X, atpoint.Y - 1) = Blockcolor(7) Then If screenshot.GetPixel(atpoint.X + 1, atpoint.Y - 1) = Blockcolor(8) Then Return True End If Else Return False End If Else Return False End If Else Return False End If Else Return False End If Else Return False End If Else Return False End If Else Return False End If Return True End Function Private Sub WritePixelColorCode() Try Debug.Write("{Color.FromArgb(" & GetPixelColor(Cursor.Position.X - 1, Cursor.Position.Y + 1).R & "," & GetPixelColor(Cursor.Position.X - 1, Cursor.Position.Y + 1).G & "," & GetPixelColor(Cursor.Position.X - 1, Cursor.Position.Y + 1).B & "), ") Debug.Write("Color.FromArgb(" & GetPixelColor(Cursor.Position.X, Cursor.Position.Y + 1).R & "," & GetPixelColor(Cursor.Position.X, Cursor.Position.Y + 1).G & "," & GetPixelColor(Cursor.Position.X, Cursor.Position.Y + 1).B & "), ") Debug.Write("Color.FromArgb(" & GetPixelColor(Cursor.Position.X + 1, Cursor.Position.Y + 1).R & "," & GetPixelColor(Cursor.Position.X + 1, Cursor.Position.Y + 1).G & "," & GetPixelColor(Cursor.Position.X + 1, Cursor.Position.Y + 1).B & "), ") Debug.Write("Color.FromArgb(" & GetPixelColor(Cursor.Position.X - 1, Cursor.Position.Y).R & "," & GetPixelColor(Cursor.Position.X - 1, Cursor.Position.Y).G & "," & GetPixelColor(Cursor.Position.X - 1, Cursor.Position.Y).B & "), ") Debug.Write("Color.FromArgb(" & GetPixelColor(Cursor.Position.X, Cursor.Position.Y).R & "," & GetPixelColor(Cursor.Position.X, Cursor.Position.Y).G & "," & GetPixelColor(Cursor.Position.X, Cursor.Position.Y).B & "), ") Debug.Write("Color.FromArgb(" & GetPixelColor(Cursor.Position.X + 1, Cursor.Position.Y).R & "," & GetPixelColor(Cursor.Position.X + 1, Cursor.Position.Y).G & "," & GetPixelColor(Cursor.Position.X + 1, Cursor.Position.Y).B & "), ") Debug.Write("Color.FromArgb(" & GetPixelColor(Cursor.Position.X - 1, Cursor.Position.Y - 1).R & "," & GetPixelColor(Cursor.Position.X - 1, Cursor.Position.Y - 1).G & "," & GetPixelColor(Cursor.Position.X - 1, Cursor.Position.Y - 1).B & "), ") Debug.Write("Color.FromArgb(" & GetPixelColor(Cursor.Position.X, Cursor.Position.Y - 1).R & "," & GetPixelColor(Cursor.Position.X, Cursor.Position.Y - 1).G & "," & GetPixelColor(Cursor.Position.X, Cursor.Position.Y - 1).B & "), ") Debug.Write("Color.FromArgb(" & GetPixelColor(Cursor.Position.X + 1, Cursor.Position.Y - 1).R & "," & GetPixelColor(Cursor.Position.X + 1, Cursor.Position.Y - 1).G & "," & GetPixelColor(Cursor.Position.X + 1, Cursor.Position.Y - 1).B & ")}") Debug.WriteLine("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=") Catch ex As Exception End Try End Sub Private Sub WritePixel() Try Debug.WriteLine(GetPixelColorUI(Cursor.Position.X - 1, Cursor.Position.Y + 1)) Debug.WriteLine(GetPixelColorUI(Cursor.Position.X, Cursor.Position.Y + 1)) Debug.WriteLine(GetPixelColorUI(Cursor.Position.X + 1, Cursor.Position.Y + 1)) Debug.WriteLine(GetPixelColorUI(Cursor.Position.X - 1, Cursor.Position.Y)) Debug.WriteLine(GetPixelColorUI(Cursor.Position.X, Cursor.Position.Y)) Debug.WriteLine(GetPixelColorUI(Cursor.Position.X + 1, Cursor.Position.Y)) Debug.WriteLine(GetPixelColorUI(Cursor.Position.X - 1, Cursor.Position.Y - 1)) Debug.WriteLine(GetPixelColorUI(Cursor.Position.X, Cursor.Position.Y - 1)) Debug.WriteLine(GetPixelColorUI(Cursor.Position.X + 1, Cursor.Position.Y - 1)) Debug.WriteLine("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=") Catch ex As Exception End Try End Sub Private Sub WebBrowser1_PreviewKeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.PreviewKeyDownEventArgs) Handles WebBrowser1.PreviewKeyDown If e.KeyCode = Keys.B Then Draw() End If If e.KeyCode = Keys.A Then 'WritePixel() WritePixelColorCode() End If If e.KeyCode = Keys.S Then Debug.WriteLine("Scanning") ScanFunc() Debug.WriteLine("Done") End If If e.KeyCode = Keys.D Then Debug.WriteLine("Dumping Scan") Debug.WriteLine("Highest Block Location" & " " & TopMostBlock) Debug.WriteLine("Leftmost Block Location" & " " & leftmostBlock) For Each MyBlock As GameBlock In GameBlocks If MyBlock.Block.Name IsNot Nothing Then Debug.WriteLine(MyBlock.Block.Name & " Column: " & MyBlock.MyColumn & " Row: " & MyBlock.MyRow & " " & MyBlock.Location.X & ", " & MyBlock.Location.Y) End If Next Debug.WriteLine("Done") Debug.Write(" | ") For i = 0 To 8 Debug.Write(i & " | ") Next Debug.WriteLine("") For i = 0 To 8 Debug.Write(i & " | ") For ii = 0 To 8 If Grid(i, ii).Block.Name Is Nothing Then Debug.Write(" " & " | ") Else Debug.Write(Grid(i, ii).Block.Name(0) & " | ") End If If ii = 8 Then Debug.WriteLine("") End If Next Next End If End Sub Private Sub Draw() Dim g As Graphics = Graphics.FromHwnd(IntPtr.Zero) Dim screenWidth As Integer = Screen.GetWorkingArea(Me).Width Dim screenHeight As Integer = Screen.GetWorkingArea(Me).Height Dim boxSize As Integer = 50 Dim brsh As Brush = New SolidBrush(Color.FromArgb(255, 255, 255, 255)) g.FillRectangle(brsh, Cursor.Position.X, Cursor.Position.Y, boxSize, boxSize) brsh.Dispose() g.Dispose() End Sub End Class |