title: colour detection bot
body: <p>I want to create a script to automatically click on a moving target on a game. </p>
  
  <p>To do this I want to check colours on the screen to determine where my target is and then click him, repeating this if he moves.</p>
  
  <p>I'm not skilled at programming and there might be an easier solution to what I have proposed below:</p>
  
  <p>1/Split the screen into equal tiles - size of tile should represent the in game object.</p>
  
  <p>2/Loop through each pixel of each tile and create a histogram of the pixel colours.</p>
  
  <p>3/If the most common recorded colour matches what we need, we MIGHT have the correct tile. Save the coords and click the object to complete task</p>
  
  <p>4/Every 0.5 seconds check colour to determine if the object has moved, if it hasnt, keep clicking, if it has repeat steps 1, 2 and 3.</p>
  
  <p>The step I am unsure of how to do technically is step 1. What data structure would I need for a tile? Would a 2D array suffice? Store the value of each colour in this array and then determine if it is the object. Also in pseudo how would I split the screen up into tiles to be searched? The tile issue is my main problem.</p>
  
  <p>EDIT for rayryeng 2:</p>
  
  <p>I will be using Python for this task. This is not my game, I just want to create a macro to automatically perform a task for me in the game. I have no code yet, I am looking more for the ideas behind making this work than actual code.</p>
  
  <p>3rd edit and final code:</p>
  
  <pre><code>#!/usr/bin/python
  
  import win32gui, win32api
  
  #function to take in coords and return colour
  
  def colour_return(x,y):
      colours = win32gui.GetPixel(win32gui.GetDC(win32gui.GetActiveWindow()), x,y)
      return colours
  
  def click(x,y):
      win32api.SetCursorPos((x,y))
      win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,x,y,0,0)
      win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,x,y,0,0)
  
  #variable declaration
  x = 1
  y = 1 
  pixel_value = []
  colour_found = 0
  
  while x &lt; 1600:
      pixel_value = colour_return(x,y)
      if pixel_value == 1844766:
          click(x,y)
      x=x+1
      #print x
      print y
      if x == 1600:
          y=y+1
          x=1
          #print tile
          pixel_value = 0
  </code></pre>
  
  <p>This is the final code that I have produced. It works but it is incredibly slow. It takes 30 seconds seconds to search all 1600 pixels of y=1. I guess it is my method that is not working. Instead of using histograms and tiles I am now just searching for a colour and clicking the coordinates when it matches. What is the fastest method to use when searching an entire screen for a certain colour? I've seen colour detection bots that manage to keep up every second with a moving character.</p>