Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pixel color and action
#1
I am very new to Python and decided to build a game bot as a means of learning. Please bear with me..

GOAL:
Part of my program evaluates a specific pixel, checks for the R value for that pixel, moves the mouse cursor to that x,y location and performs a left click until that pixel no longer equals that R value.

In an effort to learn, I am trying to make this as polished as I can and get it to be as flawless in timing and function as possible.

PROBLEM:
While I have working code that performs the exact same function noted above in other areas of the program, in this specific area, it will not function.

When an enemy is killed, it can drop a loot container but this is not consistent. The code I wrote invokes the key press that would open that loot container if it existed and if the container doesn't exist, then nothing happens and the program repeats when the enemy spawns again and is killed.

If a loot container does exist, then the code will open it and a new, top layer window will appear. There is a button on this new window that allows you to take the items but it has two functions depending on what is in the loot container.

1. The container only has low value items in it
2. The container has both low and high value items in it

If 1 is true, then only one press of the button is required to take all of the items and the window closes
If 2 is true, then the first press of the button takes the low value items, and a second press is required to take the high value items.

The loot container button is pressed with a left click. However, left click is also used for attacking, so if scenario 1 is true, then an unnecessary attack occurs where I hard coded two left click functions in the event that scenario 2 was true. I rewrote the code to check for pixel RBG value because the button is highly distinguishable from the world environment.

I can get the container window to open but I cannot get the mouse to move to the button pixel coordinates based on the pixel [0] value.

QUESTIONS:

1. Can you code to look for more than one RGB value of a pixel?
(My thoughts here are that with more information, it maybe easier to find)

If so, can you provide an example of what that might look like compared to my code below?

2. Is this use of pixels and the use of their respective RGB values to perform and action, typically not the best way to do this?
If true, what is the better way?

CODE SECTION:
I know there are far better way to write this code but this is what I know as of today

Everything works perfectly up until the 4th IF statement

  def click(x,y):
    win32api.SetCursorPos((x,y))
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,0,0)
    time.sleep(0.1)
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,0,0)
    time.sleep(0.9) 

 #ATTACK SKILLS AND CONTAINER LOOTING
    if pyautogui.pixel (1096,127) [0] == 16:
            click(1096,127)
    if pyautogui.pixel(1096,127) [0] != 16:
                time.sleep(0.25)
                pyautogui.keyDown('shift') #opens loot container
                time.sleep (0.1)
                pyautogui.keyUp('shift')
                time.sleep(0.25)      
                if pyautogui.pixel (1021,724) [0] == 36:   #identifies the pixel located on the loot button
                        time.sleep(0.1)   
                        click(1021,724)    #performs click for small value items and continues clicking for high value items
                if pyautogui.pixel (1021,724) [0] != 36:
                        time.sleep(28)      
Reply
#2
Is this click()?
def click(x,y):
   win32api.SetCursorPos((x,y))
   win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,0,0)
   time.sleep(0.1)
   win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,0,0)
   time.sleep(0.9)
Or is this click()?
def click(x,y):
   win32api.SetCursorPos((x,y))
   win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,0,0)
   time.sleep(0.1)
   win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,0,0)
   time.sleep(0.9) 
 
   #ATTACK SKILLS AND CONTAINER LOOTING
   if pyautogui.pixel (1096,127) [0] == 16:
           click(1096,127)
   if pyautogui.pixel(1096,127) [0] != 16:
               time.sleep(0.25)
               pyautogui.keyDown('shift') #opens loot container
               time.sleep (0.1)
               pyautogui.keyUp('shift')
               time.sleep(0.25)      
               if pyautogui.pixel (1021,724) [0] == 36:   #identifies the pixel located on the loot button
                       time.sleep(0.1)   
                       click(1021,724)    #performs click for small value items and continues clicking for high value items
               if pyautogui.pixel (1021,724) [0] != 36:
What do you mean by this?
Quote:1. Can you code to look for more than one RGB value of a pixel?
Reply
#3
Thank you for the reply.

Maybe I misunderstood how the code structure works, but I thought you used "def" to name and then create lines of code that can be used multiple times in other places and call back to it without having to type it all over again.

This code is supposed to create the definition for "click" so that I can use it in multiple places

def click(x,y):
   win32api.SetCursorPos((x,y))
   win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,0,0)
   time.sleep(0.1)
   win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,0,0)
   time.sleep(0.9)
This code is where I used the callback to the click definition and the x,y coordinates. The 4th IF statement is where the mouse will not travel to the x,y and also wont click. So it fails in both movement and function. All other IF statements work as intended

#ATTACK SKILLS AND CONTAINER LOOTING
   if pyautogui.pixel (1096,127) [0] == 16:
           click(1096,127)
   if pyautogui.pixel(1096,127) [0] != 16:
               time.sleep(0.25)
               pyautogui.keyDown('shift') #opens loot container
               time.sleep (0.1)
               pyautogui.keyUp('shift')
               time.sleep(0.25)      
               if pyautogui.pixel (1021,724) [0] == 36:   #identifies the pixel located on the loot button
                       time.sleep(0.1)   
                       click(1021,724)    #performs click for small value items and continues clicking for high value items
               if pyautogui.pixel (1021,724) [0] != 36:
As to my question: 1. Can you code to look for more than one RGB value of a pixel?

This line of code is telling the software to look at the pixel located at x,y coordinates 1021,724 and check its R value (36). If 36 is true, then the actions below this line are executed. Since I seem to be having trouble getting the software to perform the action, I wondered if more color information would be useful getting the program to work. I want to know is there a line of code I can use to have the software check for all three color values of the pixel instead of just one, and what that code would look like.

It is my understanding that [0] = Red [1] = Blue and [2] = Green

if pyautogui.pixel (1021,724) [0] == 36:
Reply
#4
You can do this:
from PIL import Image

# Open an jpeg file to get a pixel.
img = Image.open("test.jpg")
pixel = img.getpixel((img.width // 2, img.height // 2))
print(pixel)
print(pixel == (114, 211, 254))
Output:
(114, 211, 254) True
But is that what you really want to do? Do you know for sure that some pixel will be a particular color? What happens if the window is a different size? What happens if the window is at a different location? Maybe that works. I don't know.

I would think that you would want to use other pyautogui functions like locateOnScreen(image, confidence).
Reply
#5
I think I understand what you coded. So I tell the software to reference a picture and match it to the on screen element?

The game allows you to play in windowed mode and all of the elements and pixels are at the top level and are in a static location. This includes the loot container window as it always shows up in the screen location and cannot be moved.

I had not thought of the use of screen/image matching and this would give me a chance to try something more advanced.

Ill give it a shot. Thank you !
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Voxel to pixel graphics voicemail 3 586 Nov-19-2023, 09:45 AM
Last Post: paul18fr
  Turtle Star Fill Color Yellow-White Interchanging Color Effect codelab 9 1,040 Oct-25-2023, 09:09 AM
Last Post: codelab
  Dynamic pixel display jerryf 2 735 Mar-17-2023, 07:26 PM
Last Post: jerryf
Question Running an action only between certain times alexbca 9 1,734 Mar-15-2023, 04:21 PM
Last Post: deanhystad
  Checkbox itens with a button to run action Woogmoog 3 958 Dec-19-2022, 11:54 AM
Last Post: Woogmoog
Question Running an action only if time condition is met alexbca 5 1,321 Oct-27-2022, 02:15 PM
Last Post: alexbca
  Plotting Pixel Intensity Doev 0 1,732 Oct-21-2020, 10:56 PM
Last Post: Doev
  What is the best way to search for a pixel in a screenshot? TheZadok42 1 2,616 May-15-2020, 12:37 PM
Last Post: scidam
  Fast get pixel and oparate. storzo 7 7,094 Aug-26-2019, 07:36 PM
Last Post: Larz60+
  Have an amount of time to perform and action CookieGamez2018 1 2,956 Dec-21-2018, 07:12 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020